CSE 530A. ER Model to Relational Schema. Washington University Fall 2013

Similar documents
CSE 530A. ER Model. Washington University Fall 2013

Data Modeling. Yanlei Diao UMass Amherst. Slides Courtesy of R. Ramakrishnan and J. Gehrke

The Relational Model 2. Week 3

Database Management Systems. Chapter 3 Part 2

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

Relational Model. Topics. Relational Model. Why Study the Relational Model? Linda Wu (CMPT )

Introduction to Database Design

Introduction to Databases

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

Database Systems ( 資料庫系統 )

Introduction to Database Design

From ER to Relational Model. Book Chapter 3 (part 2 )

High-Level Database Models (ii)

Database Applications (15-415)

The Entity-Relationship Model

The Relational Model. Chapter 3

The Entity-Relationship Model. Overview of Database Design

CSE 530A SQL. Washington University Fall 2013

The Relational Model. Chapter 3. Database Management Systems, R. Ramakrishnan and J. Gehrke 1

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

The Relational Model. Outline. Why Study the Relational Model? Faloutsos SCS object-relational model

High Level Database Models

The Relational Model

The Relational Model (ii)

The Relational Model

Database Systems. Course Administration

Database Design. ER Model. Overview. Introduction to Database Design. UVic C SC 370. Database design can be divided in six major steps:

LAB 3 Notes. Codd proposed the relational model in 70 Main advantage of Relational Model : Simple representation (relationstables(row,

Overview. Introduction to Database Design. ER Model. Database Design

Relational Databases BORROWED WITH MINOR ADAPTATION FROM PROF. CHRISTOS FALOUTSOS, CMU /615

The Relational Model. Roadmap. Relational Database: Definitions. Why Study the Relational Model? Relational database: a set of relations

Introduction to Data Management. Lecture #4 (E-R à Relational Design)

Conceptual Design. The Entity-Relationship (ER) Model

CIS 330: Applied Database Systems. ER to Relational Relational Algebra

CIS 330: Applied Database Systems

The Relational Model

CONCEPTUAL DESIGN: ER TO RELATIONAL TO SQL

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

ER to Relational Mapping. ER Model: Overview

Database Applications (15-415)

Review The Big Picture

The Entity-Relationship (ER) Model

CS W Introduction to Databases Spring Computer Science Department Columbia University

Introduction to Data Management. Lecture #4 (E-R Relational Translation)

MIS Database Systems Entity-Relationship Model.

The Entity-Relationship Model

OVERVIEW OF DATABASE DEVELOPMENT

Introduction to Database Design. Dr. Kanda Runapongsa Dept of Computer Engineering Khon Kaen University

Why Study the Relational Model? The Relational Model. Relational Database: Definitions. The SQL Query Language. Relational Query Languages

CIS 330: Web-driven Web Applications. Lecture 2: Introduction to ER Modeling

CSC 261/461 Database Systems Lecture 10

Comp 5311 Database Management Systems. 4b. Structured Query Language 3

The Entity-Relationship Model. Overview of Database Design. ER Model Basics. (Ramakrishnan&Gehrke, Chapter 2)

Where Are We? Next Few Lectures. Integrity Constraints Motivation. Constraints in E/R Diagrams. Keys in E/R Diagrams

Contents. Database. Information Policy. C03. Entity Relationship Model WKU-IP-C03 Database / Entity Relationship Model

Introduction to Data Management. Lecture #5 (E-R Relational, Cont.)

Database Applications (15-415)

SQL DDL. CS3 Database Systems Weeks 4-5 SQL DDL Database design. Key Constraints. Inclusion Constraints

ER Model. Objectives (2/2) Electricite Du Laos (EDL) Dr. Kanda Runapongsa Saikaew, Computer Engineering, KKU 1

Introduction to Data Management. Lecture #5 Relational Model (Cont.) & E-Rà Relational Mapping

The Entity-Relationship Model

COMP Instructor: Dimitris Papadias WWW page:

The Entity-Relationship (ER) Model 2

The Relational Model. Week 2

ER Model Overview. The Entity-Relationship Model. Database Design Process. ER Model Basics

CS/INFO 330 Entity-Relationship Modeling. Announcements. Goals of This Lecture. Mirek Riedewald

CSC 261/461 Database Systems Lecture 8. Fall 2017

Translating an ER Diagram to a Relational Schema

Database Management System (15ECSC208) UNIT I: Chapter 2: Relational Data Model and Relational Algebra

The Entity-Relationship Model. Steps in Database Design

Introduction to Database Design

Translation of ER-diagram into Relational Schema. Dr. Sunnie S. Chung CIS430/530

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Relational Databases Fall 2017 Lecture 7

CSE 344 MAY 14 TH ENTITIES

CSC 261/461 Database Systems Lecture 8. Spring 2018

Introduction to Data Management. Lecture #6 E-Rà Relational Mapping (Cont.)

The DBMS accepts requests for data from the application program and instructs the operating system to transfer the appropriate data.

Introduction to Data Management CSE 344

3.1. Keys: Super Key, Candidate Key, Primary Key, Alternate Key, Foreign Key

Database Design CENG 351

Database Management System Dr. S. Srinath Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

Entity-Relationship Diagrams

The Relational Model. Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC)

Part I: Structured Data

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

Overview of db design Requirement analysis Data to be stored Applications to be built Operations (most frequent) subject to performance requirement

Database Design & Deployment

Database Applications (15-415)

The Relational Model of Data (ii)

Relational model continued. Understanding how to use the relational model. Summary of board example: with Copies as weak entity

Introduction to Database Systems CSE 414

Announcements. Database Design. Database Design. Database Design Process. Entity / Relationship Diagrams. Introduction to Data Management CSE 344

Integrity constraints, relationships. CS634 Lecture 2

Introduction to Data Management. Lecture #3 (E-R Design, Cont d.)

Introduction to Data Management CSE 344

Handout 6 Logical design: Translating ER diagrams into SQL CREATE statements

Database Management Systems. Chapter 2 Part 2

Introduction to Data Management CSE 344

Part 5: Introduction to Logical Design

Database Systems. Lecture2:E-R model. Juan Huo( 霍娟 )

Transcription:

CSE 530A ER Model to Relational Schema Washington University Fall 2013

Relational Model A relational database consists of a group of relations (a.k.a., tables) A relation (table) is a set of tuples (rows) The tuples (rows) have a fixed set of fields (columns)

ER Entity Sets An entity set in the ER model maps to a table in the relational model The entity set's attributes map to columns first_name ssn employees last_name employees ssn last_name first_name 111-11-1111 Bunny Bugs 333-33-3333 Duck Daffy 222-22-2222 Pig Porky 444-44-4444 Pig Petunia

Tables CREATE TABLE employees ( ssn CHAR(11), last_name TEXT, first_name TEXT employees ssn last_name first_name 111-11-1111 Bunny Bugs 333-33-3333 Duck Daffy 222-22-2222 Pig Porky 444-44-4444 Pig Petunia

Keys A key for an entity set is a set of attributes that uniquely identifies each entity Two entities cannot have identical values for all the attributes in a key A key should be a minimal subset of attributes that satisfies the above No subset of the attributes in a key is a unique identifier for an entity

UNIQUE We can specify that a group of columns must be unique for each row by adding a UNIQUE constraint to the table This will cause the DBMS to enforce the constraint that no two rows have the same values for both last_name and first_name (Note this would mean that we couldn't have two employees with the same name, which is probably not what we really want this is for illustration only.) CREATE TABLE employees ( ssn CHAR(11), last_name TEXT, first_name TEXT, UNIQUE (last_name, first_name)

UNIQUE The DBMS will enforce the constraint and not allow us to insert or update a row so that it has the same last_name and first_name as another row But it doesn't keep up from entering rows with NULL values for the last_name and first_name Remember NULL NULL, so multiple rows could have NULL last_name or first_name

NOT NULL We need to include a NOT NULL constraint on the columns if we want to exclude NULLs CREATE TABLE employees ( ssn CHAR (11), last_name TEXT NOT NULL, first_name TEXT NOT NULL, UNIQUE (last_name, first_name)

Primary Keys One candidate key should be chosen as the primary key PRIMARY KEY is basically the same as specifying UNIQUE and NOT NULL, except There can be only one primary key per table The DBMS optimizes the table for queries that use the primary key CREATE TABLE employees ( ssn CHAR(11), last_name TEXT, first_name TEXT, CONSTRAINT employees_key PRIMARY KEY (last_name, first_name)

Primary Keys If the primary key is a single column then a shorter version can be used CREATE TABLE employees ( ssn CHAR(11) PRIMARY KEY, last_name TEXT, first_name TEXT Most DBMSs allow duplicate rows unless constraints are specified, but this is rarely useful Generally, every table should have a primary key

Foreign Keys Often, information in one table is linked to another table The foreign key in the second table must match the columns of the primary key of the first table employees ssn last_name first_name 111-11-1111 Bunny Bugs 333-33-3333 Duck Daffy 222-22-2222 Pig Porky 444-44-4444 Pig Petunia addresses ssn address 111-11-1111 1 Acme Lane 333-33-3333 5 Pond Ave 222-22-2222 3 Farm St 444-44-4444 3 Farm St

Foreign Keys A foreign key constraint can enforce the relationship CREATE TABLE addresses ( ssn CHAR(11), address TEXT, FOREIGN KEY (ssn) REFERENCES employees(ssn) The DBMS will not allow an insert or update on the addresses table if the value for ssn does not exist in the employees table

Foreign Keys If the foreign key is a single column then a shorthand version can be used CREATE TABLE addresses( ssn CHAR(11) REFERENCES employees(ssn), address TEXT

Foreign Keys A foreign key constraint does not keep NULL values from being entered Must either use NOT NULL constraints or PRIMARY KEY constraint CREATE TABLE addresses( ssn CHAR(11) PRIMARY KEY REFERENCES employees(ssn), address TEXT In this example, the ssn in the addresses table is both the primary key and a foreign key

Referential Integrity What happens if we attempt to delete a row from the employees table when there exists a row in the addresses table with that ssn? Or change the ssn of an employees row? Four options: 1. Don't allow the deletion of the row from employees 2. Delete all rows from addresses that refer to the deleted row from employees 3. Set the ssn of the addresses rows to some default value (which must exist in employees) 4. Set the ssn of the addresses rows to NULL

Referential Integrity Default action is choice 1: do not allow the deletion DBMS will give an error if a delete attempt is made Can change the action with ON DELETE CREATE TABLE addresses ( ssn CHAR(11) PRIMARY KEY, address TEXT, FOREIGN KEY (ssn) REFERENCES employees(ssn) ON DELETE CASCADE CREATE TABLE addresses( ssn CHAR(11) PRIMARY KEY REFERENCES employees(ssn) ON DELETE CASCADE, address TEXT ON DELETE CASCADE will cause all referencing rows in addresses to be deleted when the referenced row in employees is deleted Useful, but be careful!

Referential Integrity Can use ON DELETE SET DEFAULT to set the ssn to a default value when the referenced employee is deleted Must also declare a default value for the ssn in addresses CREATE TABLE addresses( ssn CHAR(11) PRIMARY KEY DEFAULT '000-00-0000' REFERENCES employees(ssn) ON DELETE SET DEFAULT, address TEXT Note that because ssn has been declared a primary key, only one row can actually have the default value

Referential Integrity Can use ON DELETE SET NULL to set the ssn to NULL when the reference employeed is deleted CREATE TABLE addresses( ssn CHAR(11) PRIMARY KEY REFERENCES employees(ssn) ON DELETE SET NULL, address TEXT Note that because ssn has been declared a primary key, the ssn cannot actually be set to NULL, so the delete from employees will fail with an error

Referential Integrity The same issues when deleting occur when updating Can use ON UPDATE to specify alternate actions The default action (for both update and delete) is NO ACTION, which is to not allow the update or delete CREATE TABLE addresses( ssn CHAR(11) PRIMARY KEY REFERENCES employees(ssn) ON DELETE CASCADE ON UPDATE NO ACTION, address TEXT

ER Relationship Sets since ssn name id name employees works_in departments A many-to-many relationship set is implemented using a mapping table with foreign keys to the primary keys of the entity tables

ER Relationship Sets employees ssn name 111-11-1111 Bunny, Bugs 333-33-3333 Duck, Daffy 222-22-2222 Pig, Porky 444-44-4444 Pig, Petunia works_in ssn did since 111-11-1111 5 2001 111-11-1111 7 2003 333-33-3333 4 2001 222-22-2222 4 2002 444-44-4444 7 2002 departments did name 5 HR 4 IT 7 Sales A many-to-many relationship set is implemented using a mapping table with foreign keys to the primary keys of the entity tables

Many-to-Many CREATE TABLE employees ( ssn CHAR(11) PRIMARY KEY, name TEXT NOT NULL CREATE TABLE departments ( did INTEGER PRIMARY KEY, name TEXT NOT NULL CREATE TABLE works_in ( ssn CHAR(11) REFERENCES employees(ssn), did INTEGER REFERENCES departments(did), since INTEGER NOT NULL, PRIMARY KEY (ssn, did) Note this can be expanded to relationships between more than two tables

One-to-Many since ssn name id name employees N 1 works_in departments A one-to-many relationship set can also be implemented using a mapping table with foreign keys to the primary keys of the entity tables With the additional constraint that the foreign key to the "many" entity set be unique in the mapping table

One-to-Many employees ssn name 111-11-1111 Bunny, Bugs 333-33-3333 Duck, Daffy 222-22-2222 Pig, Porky 444-44-4444 Pig, Petunia works_in ssn did since 111-11-1111 5 2001 333-33-3333 4 2001 222-22-2222 4 2002 444-44-4444 7 2002 departments did name 5 HR 4 IT 7 Sales In this example, assume the relationship from departments to employees is one-to-many So departments can have multiple employees but employees can only work in one department So the unique constraint needs to be on the ssn column of works_in (not the did column)

One-to-Many CREATE TABLE employees ( ssn CHAR(11) PRIMARY KEY, name TEXT NOT NULL CREATE TABLE departments ( did INTEGER PRIMARY KEY, name TEXT NOT NULL CREATE TABLE works_in ( ssn CHAR(11) REFERENCES employees(ssn) UNIQUE, did INTEGER REFERENCES departments(did), since INTEGER NOT NULL, PRIMARY KEY (ssn, did) In this example, assume the relationship from departments to employees is one-to-many So departments can have multiple employees but employees can only work in one department So the unique constraint needs to be on the ssn column of works_in (not the did column)

One-to-One since ssn name id name employees 1 1 manages departments A one-to-one mapping table would have unique constraints on both foreign keys

One-to-One employees ssn name 111-11-1111 Bunny, Bugs 333-33-3333 Duck, Daffy 222-22-2222 Pig, Porky 444-44-4444 Pig, Petunia manages ssn did since 111-11-1111 5 2001 333-33-3333 4 2001 444-44-4444 7 2002 departments did name 5 HR 4 IT 7 Sales A one-to-one mapping table would have unique constraints on both foreign keys

One-to-One CREATE TABLE employees ( ssn CHAR(11) PRIMARY KEY, name TEXT NOT NULL CREATE TABLE departments ( did INTEGER PRIMARY KEY, name TEXT NOT NULL CREATE TABLE manages ( ssn CHAR(11) REFERENCES employees(ssn) UNIQUE, did INTEGER REFERENCES departments(did) UNIQUE, since INTEGER NOT NULL, PRIMARY KEY (ssn, did) Note that having UNIQUE constraints on the individual columns is different than having a UNIQUE constraint on the combination

Roles ssn name supervisor employees underling We can also have a reference from a table to itself reports_to

Self-Reference employees ssn name 111-11-1111 Bunny, Bugs 333-33-3333 Duck, Daffy 222-22-2222 Pig, Porky 444-44-4444 Pig, Petunia reports_to supervisor underling 111-11-1111 333-33-3333 333-33-3333 222-22-2222 444-44-4444 111-11-1111 We can again use a mapping table Potentially with UNIQUE constraints if one-tomany or one-to-one is desired

Self-Reference CREATE TABLE employees ( ssn CHAR(11) PRIMARY KEY, name TEXT NOT NULL CREATE TABLE reports_to ( supervisor CHAR(11) REFERENCES employees(ssn), underling CHAR(11) REFERENCES employees(ssn) UNIQUE, PRIMARY KEY (supervisor, underling) We can again use a mapping table Potentially with UNIQUE constraints if one-tomany or one-to-one is desired

One-to-Many employees ssn name did since 111-11-1111 Bunny, Bugs 5 2001 333-33-3333 Duck, Daffy 4 2001 222-22-2222 Pig, Porky 4 2002 444-44-4444 Pig, Petunia 7 2002 departments did name 5 HR 4 IT 7 Sales For one-to-many, we can use a foreign key in the "many" table instead of a separate mapping table Often preferred as it eliminates a table and a JOIN in queries

One-to-One employees ssn name did since 111-11-1111 Bunny, Bugs 5 2001 333-33-3333 Duck, Daffy 4 2001 444-44-4444 Pig, Petunia 7 2002 departments did name 5 HR 4 IT 7 Sales One-to-one is a special case of one-to-many with an extra UNIQUE constraint on the foreign key In this example, that means the did column in employees should be unique Note that many-to-many requires a separate mapping table

Self-Reference employees ssn name supervisor 111-11-1111 Bunny, Bugs 444-44-4444 333-33-3333 Duck, Daffy 111-11-1111 222-22-2222 Pig, Porky 333-33-3333 444-44-4444 Pig, Petunia NULL If the self-reference is one-to-many or one-toone then a foreign key in the table to itself can be used

Total Participation NOT NULL constraints can sometimes be used to require total participation of an entity set in a relationship set For example, if every department must have a manager then a NOT NULL constraint could be added to the department's manger column CREATE TABLE departments ( did INTEGER PRIMARY KEY, name TEXT NOT NULL, manager CHAR(11) REFERENCES employees(ssn) NOT NULL In general, however, participation constraints often require more expensive table constraints or assertions to enforce We'll talk about these later in the semester, but they're often a bad idea as they can severely impact performance

Weak Entity Sets Weak entity sets can be viewed as a special case of a one-to-many relationship ssn name dname age employees insurance dependents

Weak Entity Sets CREATE TABLE employees ( ssn CHAR(11) PRIMARY KEY, name TEXT NOT NULL CREATE TABLE dependents ( ssn CHAR(11) REFERENCES employees(ssn) NOT NULL, dname TEXT NOT NULL, age INTEGER NOT NULL, PRIMARY KEY (ssn, dname) The foreign key reference is in the "many" side (dependents) to the "one" side (employees) The foreign key and key attributes have NOT NULL constraints The combination of the foreign key and the key attribute are declared as a PRIMARY KEY

Class Hierarchies ssn name employees hourly_wage ISA salary hourly_emps salaried_emps

Class Hierarchies Two options: 1. A parent table with child tables for the subclasses Each child table has a foreign key reference to the parent table, typically with a one-to-one relationship 2. Child tables with no parent table Option 1 is generally preferred Option 2 disallows having a record which doesn't fit into any of the child subclasses Option 2 also make operations like selecting all employees more difficult

Aggregation ssn name employees monitors until pid budget since did name projects sponsors departments

Aggregation First, create tables for the aggregate part CREATE TABLE projects ( pid INTEGER PRIMARY KEY, budget INTEGER NOT NULL CREATE TABLE departments ( did INTEGER PRIMARY KEY, name TEXT NOT NULL CREATE TABLE sponsors ( pid INTEGER REFERENCES projects(pid), did INTEGER REFERENCES departments(did), since INTEGER NOT NULL, PRIMARY KEY (pid, did)

Aggregation Then, create the table for the relationship to the aggregate, using the primary key of the aggregate's relation as a foreign key CREATE TABLE employees ( ssn CHAR(11) PRIMARY KEY, name TEXT NOT NULL CREATE TABLE monitors ( ssn CHAR(11) REFERENCES employees(ssn) NOT NULL, pid INTEGER NOT NULL, did INTEGER NOT NULL, until INTEGER NOT NULL, PRIMARY KEY (ssn, pid, did), FOREIGN KEY (pid, did) REFERENCES sponsors(pid, did)

ALTER TABLE Constraints can be added or removed after table creation by using ALTER TABLE CREATE TABLE employees ( ssn CHAR(11) PRIMARY KEY, name TEXT ALTER TABLE employees ALTER COLUMN name SET NOT NULL; However, adding a constraint after data has been inserted will fail if the existing data violates the constraint

Example cid name nbr customers 1 N placed orders quantity includes isbn products

Example cid name customers CREATE TABLE customers ( cid INTEGER PRIMARY KEY, name TEXT NOT NULL

Example nbr orders CREATE TABLE orders ( nbr INTEGER PRIMARY KEY

Example cid name nbr customers 1 N placed orders CREATE TABLE orders ( nbr INTEGER PRIMARY KEY, cid INTEGER REFERENCES customers(cid) NOT NULL

Example CREATE TABLE products ( isbn TEXT PRIMARY KEY isbn products

Example CREATE TABLE includes ( nbr INTEGER REFERENCES orders(nbr), isbn TEXT REFERENCES products(isbn), quantity INTEGER NOT NULL, PRIMARY KEY (nbr, isbn) orders nbr quantity includes isbn products