Quick viewing(Text Mode)

Database Management Systems Chapter 3 Part 2 Logical DB Design

Database Management Systems Chapter 3 Part 2 Logical DB Design

Management Systems

Chapter 3 Part 2

The

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

Logical DB Design: ER to Relational

name ssn lot  Entity sets to tables:

Employees

CREATE Employees (ssn CHAR(11), name CHAR(20), lot INTEGER, CONSTRAINT EmKey (ssn));

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 2 Relationship Sets to Tables

 In translating a relationship set to a , attributes of the relation must include: . Primary keys for each participating entity set (as foreign keys). • This set of attributes forms a for the relation. • The primary key is decided by the key constraint of the relationship. . All descriptive attributes.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 3

Binary Relationship

since name dname ssn lot did budget

Employees Works_In Departments  Each dept has at least one employee, and each employee works for at least one department, according to the key constraint on Works_In. Translation to relational model?

Many-to-Many

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 4 since name dname ssn lot did budget

Employees Works_In Departments

CREATE TABLE Works_In( ssn CHAR(11), did INTEGER, since DATE, CONSTRAINT WIKey PRIMARY KEY (ssn, did), CONSTRAINT SSNFK FOREIGN KEY (ssn) REFERENCES Employees(ssn) ON DELETE CASCADE ON UPDATE NO ACTION, CONSTRAINT DIDFK FOREIGN KEY (did) REFERENCES Departments(did) ON DELETE CASCADE ON UPDATE NO ACTION);

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 5

Binary Relationship

since name dname ssn lot did budget

 Each dept has at most one manager, Employees Manages Departments according to the key constraint on Manages.

Translation to relational model?

1-to Many

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 6 Translating ER Diagrams with Key Constraints

since  Map relationship to a name dname table: ssn lot did budget . Since each department has most one manager, Employees Manages Departments there are no two tuples with the same did but differ on the ssn value, CREATE TABLE Manages( did is the key now! ssn CHAR(11), . Separate tables for did INTEGER, Employees and since DATE, Departments. PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees (ssn), FOREIGN KEY (did) REFERENCES Departments (did));

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 7

Translating ER Diagrams with Key Constraints

since  Since each name dname department has a ssn lot did budget unique manager, we could instead Employees Manages Departments combine Manages and Departments. . ssn can take CREATE TABLE Dept_Mgr( values since did INTEGER, several dname CHAR(20), departments have budget REAL, no managers. ssn CHAR(11), since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees);

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 8 Review: Participation Constraints

 Does every department have a manager? . If so, this is a participation constraint: the participation of Departments in Manages is said to be total (vs. partial). • Every did value in Departments table must appear in a of the Manages table (with a non-null ssn value!)

since name dname ssn lot did budget

Employees Manages Departments

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 9

Participation Constraints in SQL

 This approach is good for one-to-many relationships, when entity set with key constraint also has a total participation constraint. (Two tables are combined)

CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), REAL budget , An Employees tuple cannot ssn CHAR(11) NOT NULL, be deleted while it is pointed since DATE, to by a Dept_Mgr tuple PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, ON DELETE NO ACTION);

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 10 Translating ER Diagrams with Key Constraints  Every department since has at most one name dname manager, an ssn lot did budget employee could only manage at most one Employees Manages Departments department. CREATE TABLE Dept_Mgr( . ssn should be unique. did INTEGER, dname CHAR(20), budget REAL, chair_ssn CHAR(11), since DATE, PRIMARY KEY (did), UNIQUE (chair_ssn), FOREIGN KEY (chair_ssn) REFERENCES Employees(ssn));

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 11

Binary Relationship to RM

 Many-to-many: The primary key of R includes all the attributes in the primary keys of A and B.  One-to-many: The primary key of R is the same as B (i.e., the entity set on the “many” side, e.g. An employee could manage many dept., each dept. has at most one manager).  One-to-one: R has two candidate keys. The first (second) one is the same as A (B). One is primary key and the other is unique.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 12 Review: Weak Entities

 A weak entity can be identified uniquely only by considering the primary key of another (owner) entity. . Owner entity set and weak entity set must participate in a one-to-many relationship set (1 owner, many weak entities). . Weak entity set must have total participation in this identifying relationship set.

name cost ssn lot pname age

Employees Policy Dependents

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 13

Translating Weak Entity Sets  Weak entity set and identifying relationship set are translated into a single table. . When the owner entity is deleted, all owned weak entities must also be deleted. CREATE TABLE Dep_Policy ( pname CHAR(20) NOT NULL, age INTEGER, cost REAL, ssn CHAR(11) NOT NULL, PRIMARY KEY (pname, ssn), FOREIGN KEY (ssn) REFERENCES Employees (ssn) ON DELETE CASCADE);

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 14 Multiple-way Relationship

 Multi-way relationship set R: Create a table that includes the candidate keys of the participating entity sets the attributes of R (if any).  The primary key of the table includes all the attributes of the primary keys of the participating entity sets.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 15

since name dname ssn lot did budget

Employees Works_In2 Departments

address Locations capacity

CREATE TABLE Works_In2( ssn CHAR(11), did INTEGER, address CHAR(20), since DATE, PRIMARY KEY (ssn, did, address), FOREIGN KEY (ssn) REFERENCES Employees (ssn), FOREIGN KEY (did) REFERENCES Departments (did), FOREIGN KEY (address) REFERENCES Locations (address));

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 16 name

ssn lot

Employees

supervisor subordinate

Reports_To

CREATE TABLE Reports_To( supervisor_ssn CHAR(11), subordinate_ssn CHAR(11), PRIMARY KEY (supervisor_ssn, subordinate_ssn), FOREIGN KEY (supervisor_ssn) REFERENCES Employees (ssn), FOREIGN KEY (subordinate_ssn) REFERENCES Employees(ssn));

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 17

Review: ISA Hierarchies name ssn lot

Employees  As in C++, or other PLs, attributes are inherited. hourly_wages hours_worked ISA  If we declare A ISA B, every A contractid entity is also considered to be a B Contract_Emps entity. Hourly_Emps

 Overlap constraints: Can Joe be an Hourly_Emps as well as a Contract_Emps entity? (Allowed/disallowed)  Covering constraints: Does every Employees entity also have to be an Hourly_Emps or a Contract_Emps entity? (Yes/no)

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 18 Translating ISA Hierarchies to Relations

 General approach: . 3 relations: Employees, Hourly_Emps and Contract_Emps. • Hourly_Emps: Every employee is recorded in Employees. For hourly emps, extra info recorded in Hourly_Emps (hourly_wages, hours_worked, ssn); must Hourly_Emps tuple if referenced Employees tuple is deleted). • Queries involving all employees easy, those involving just Hourly_Emps require a to get some attributes.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 19

name ssn lot

Employees

hourly_wages hours_worked ISA contractid

Hourly_Emps Contract_Emps

CREAT TABLE Employees (ssn CHAR(10), name CHAR(20), lot INTEGER, CONSTRAINT EmployeesKey PRIMARY KEY (ssn));

CREATE TABLE Hourly_Emps (ssn CHAR(10), hourly_wages REAL, hours_worked INTEGER, CONSTRAINT HourlyEmplsKey PRIMARY KEY (ssn), FOREIGN KEY (ssn) REFERENCES Employees ON DELETE CASCADE); Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 20 Translating ISA Hierarchies to Relations

 Alternative: Just Hourly_Emps and Contract_Emps. . Hourly_Emps: ssn, name, lot, hourly_wages, hours_worked. . Contract_Emps: ssn, name, lot, contractId. . Each employee must be in one of these two subclasses. . If an employee is both an Hourly_Emps and a Contract_Emps entity, then the same name and lot values are stored in two tables.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 21

Translating ER Diagram with Aggregation name ssn lot

Employees

Monitors until

started_on since dname pid pbudget did budget

Projects Sponsors Departments

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 22 CREATE TABLE Employees (ssn CHAR(11), name CHAR(20), lot INTEGER, PRIMARY KEY (ssn)); CREATE TABLE Departments (did INTEGER, dname CHAR(20), budget REAL, PRIMARY KEY (did)); CREATE TABLE Projects (pid INTEGER, started_on DATE, pbudget REAL, PRIMARY KEY (pid)); CREATE TABLE Sponsors (did INTEGER, pid INTEGER, since DATE, PRIMARY KEY (did, pid), FOREIGN KEY (did) REFERENCES Departments, FOREIGN KEY (pid) REFERENCES Projects); CREATE TABLE Monitors (ssn CHAR(11), did INTEGER, pid INTEGER, until DATE, PRIMARY KEY (ssn, did, pid), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments, FOREIGN KEY (pid) REFERENCES Projects);

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 23

Review: Binary vs. Ternary

Relationships name ssn lot pname age

Employees Covers Dependents  What are the Bad design additional Policies constraints in policyid cost name pname age the 2nd ssn lot diagram? Dependents Employees

Purchaser Beneficiary

Better design Policies

policyid cost Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 24 Binary vs. Ternary Relationships (Contd.)

 The key CREATE TABLE Policies ( constraints allow policyid INTEGER, us to combine cost REAL, Purchaser with ssn CHAR(11) NOT NULL, PRIMARY KEY (policyid). Policies and FOREIGN KEY (ssn) REFERENCES Employees, Beneficiary with ON DELETE CASCADE); Dependents.  Participation CREATE TABLE Dependents ( constraints lead to pname CHAR(20), NOT NULL age INTEGER, constraints. policyid INTEGER,  What if Policies is PRIMARY KEY (pname, policyid). a weak entity set? FOREIGN KEY (policyid) REFERENCES Policies, ON DELETE CASCADE);

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 25

Policies is a weak entity set

CREATE TABLE Dependents ( pname CHAR(20), ssn CHAR(11), age INTEGER, policyid INTEGER NOT NULL, PRIMARY KEY (pname, policyid, ssn), FOREIGN KEY (policyid, ssn) REFERENCES Policies, ON DELETE CASCADE);

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 26 Relational Model: Summary

 A tabular representation of .  Simple and intuitive, currently the most widely used.  Integrity constraints can be specified by the DBA, based on application semantics. DBMS checks for violations. . Two important ICs: primary and foreign keys . In addition, we always have domain constraints.  Powerful and natural query languages exist.  Rules to translate ER to relational model

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 27