ER to Relational Mapping - University of Albertadrafiei/291/notes/4.ER2...14 Review: Weak Entities A...
Transcript of ER to Relational Mapping - University of Albertadrafiei/291/notes/4.ER2...14 Review: Weak Entities A...
![Page 1: ER to Relational Mapping - University of Albertadrafiei/291/notes/4.ER2...14 Review: Weak Entities A weak entity can be identified uniquely only by considering the primary key of another](https://reader033.fdocuments.us/reader033/viewer/2022041520/5e2e3a6bd6908044ba430bc5/html5/thumbnails/1.jpg)
1
ER to Relational Mapping
Davood Rafiei
![Page 2: ER to Relational Mapping - University of Albertadrafiei/291/notes/4.ER2...14 Review: Weak Entities A weak entity can be identified uniquely only by considering the primary key of another](https://reader033.fdocuments.us/reader033/viewer/2022041520/5e2e3a6bd6908044ba430bc5/html5/thumbnails/2.jpg)
2
ER Model: Overview
� The “world” is described in terms of• Entities• Relationships• Attributes
� Constraints and Complications• Key constraints• Participation constraints• Set-valued attributes• Weak entities• ISA hierarchies
![Page 3: ER to Relational Mapping - University of Albertadrafiei/291/notes/4.ER2...14 Review: Weak Entities A weak entity can be identified uniquely only by considering the primary key of another](https://reader033.fdocuments.us/reader033/viewer/2022041520/5e2e3a6bd6908044ba430bc5/html5/thumbnails/3.jpg)
3
Relational Model: Overview
� Database: a set of relations (tables)
� For each table, we specify• Columns and the domain of each column,• Often the primary key and the foreign keys,• Other constraints (if any),• The way referential integrities must be enforced.
![Page 4: ER to Relational Mapping - University of Albertadrafiei/291/notes/4.ER2...14 Review: Weak Entities A weak entity can be identified uniquely only by considering the primary key of another](https://reader033.fdocuments.us/reader033/viewer/2022041520/5e2e3a6bd6908044ba430bc5/html5/thumbnails/4.jpg)
4
Entity Sets to Tables
� Entity sets to tables.
CREATE TABLE Employees (sin CHAR(11),name CHAR(20),PRIMARY KEY (sin))
sin
Employees
name
![Page 5: ER to Relational Mapping - University of Albertadrafiei/291/notes/4.ER2...14 Review: Weak Entities A weak entity can be identified uniquely only by considering the primary key of another](https://reader033.fdocuments.us/reader033/viewer/2022041520/5e2e3a6bd6908044ba430bc5/html5/thumbnails/5.jpg)
5
Relationship Sets to Tables
� Constraint: none.� Attributes of the relation
(table):• Key of every participating
entity set (as foreign keys).
• All descriptive attributes
CREATE TABLE Works_On(sin CHAR(11),pid INTEGER,since DATE,PRIMARY KEY (sin, pid),FOREIGN KEY (sin)
REFERENCES Employees,FOREIGN KEY (pid)
REFERENCES Projects)
Employees
sin
Works_On Projects
pidsince
![Page 6: ER to Relational Mapping - University of Albertadrafiei/291/notes/4.ER2...14 Review: Weak Entities A weak entity can be identified uniquely only by considering the primary key of another](https://reader033.fdocuments.us/reader033/viewer/2022041520/5e2e3a6bd6908044ba430bc5/html5/thumbnails/6.jpg)
6
Relationships with Key Constraints
� Constraint: each employee works in at most one department.
� Map the relationship to a table:• What is the key
now?
budget
dname
did
sincename
sin
Works_InEmployees Departments
CREATE TABLE Works_In (sin CHAR(11),did CHAR(3),since DATE,PRIMARY KEY (sin),FOREIGN KEY (sin) REFERENCES Employees,FOREIGN KEY (did) REFERENCES Departments)
![Page 7: ER to Relational Mapping - University of Albertadrafiei/291/notes/4.ER2...14 Review: Weak Entities A weak entity can be identified uniquely only by considering the primary key of another](https://reader033.fdocuments.us/reader033/viewer/2022041520/5e2e3a6bd6908044ba430bc5/html5/thumbnails/7.jpg)
7
Relationships with Key Constraints (Cont.)
� Better mapping:
• Since each employee can work in at most one department, we could instead combine Works_In and Employees.
• Has one less table�
CREATE TABLE Emp_Works(sin CHAR(11),name CHAR(20),did CHAR(3),since DATE,PRIMARY KEY (sin),FOREIGN KEY (did) REFERENCES Departments)
![Page 8: ER to Relational Mapping - University of Albertadrafiei/291/notes/4.ER2...14 Review: Weak Entities A weak entity can be identified uniquely only by considering the primary key of another](https://reader033.fdocuments.us/reader033/viewer/2022041520/5e2e3a6bd6908044ba430bc5/html5/thumbnails/8.jpg)
8
Relationships with Key Constraints (Cont.)
� Constraint: each employee manages at most one department and each department is managed by at most one employee.
budget
dname
did
sincename
sin
ManagesEmployees Departments
CREATE TABLE Dept(did CHAR(3),dname CHAR(20),budget INTEGER,mgr CHAR(11),since DATE,PRIMARY KEY (did),FOREIGN KEY (mgr) REFERENCES Employees)
� We can combine Manages with Departments.
� We can also combine Manages with Employees.
![Page 9: ER to Relational Mapping - University of Albertadrafiei/291/notes/4.ER2...14 Review: Weak Entities A weak entity can be identified uniquely only by considering the primary key of another](https://reader033.fdocuments.us/reader033/viewer/2022041520/5e2e3a6bd6908044ba430bc5/html5/thumbnails/9.jpg)
9
Review: Participation Constraints
� Does every project have a supervisor?• If so, this is a participation constraint: the participation of
Projects in Supervises is said to be total (vs. partial).�Every pid value in Projects table must appear in a row of
the Supervises table (with a non-null sin value!)
namebudget
sincename pname
budgetpid
since
Supervises ProjectsEmployees
sin
![Page 10: ER to Relational Mapping - University of Albertadrafiei/291/notes/4.ER2...14 Review: Weak Entities A weak entity can be identified uniquely only by considering the primary key of another](https://reader033.fdocuments.us/reader033/viewer/2022041520/5e2e3a6bd6908044ba430bc5/html5/thumbnails/10.jpg)
10
Participation Constraints
CREATE TABLE Proj_Supervises ( pid INTEGER,pname CHAR(20),budget REAL,sin CHAR(11) since DATE,PRIMARY KEY (pid),
NOT NULL,
FOREIGN KEY (sin) REFERENCES EmployeesON DELETE NO ACTION)
namebudget
sincename pname
budgetpid
since
Supervises ProjectsEmployees
sin
![Page 11: ER to Relational Mapping - University of Albertadrafiei/291/notes/4.ER2...14 Review: Weak Entities A weak entity can be identified uniquely only by considering the primary key of another](https://reader033.fdocuments.us/reader033/viewer/2022041520/5e2e3a6bd6908044ba430bc5/html5/thumbnails/11.jpg)
11
Participation Constraints (Cont.)
� How can we map Works_On relationship to a table and still keep the participation constraints?
namebudget
sincename pname
budgetpid
since
Works_On ProjectsEmployees
sin
� Can’t without resorting to CHECK constraints (will be discussed later).
![Page 12: ER to Relational Mapping - University of Albertadrafiei/291/notes/4.ER2...14 Review: Weak Entities A weak entity can be identified uniquely only by considering the primary key of another](https://reader033.fdocuments.us/reader033/viewer/2022041520/5e2e3a6bd6908044ba430bc5/html5/thumbnails/12.jpg)
12
Set-Valued Attributes
� Cannot store more than one value in a field!� What is the key of the relation?
• sin cannot be a key!
� The same problem arises in mapping a relationship with a set-valued attribute.
hobbyname
Employees
sinCREATE TABLE Employees (
sin CHAR(11),name CHAR(20),hobby char(15),PRIMARY KEY )(sin, hobby)
![Page 13: ER to Relational Mapping - University of Albertadrafiei/291/notes/4.ER2...14 Review: Weak Entities A weak entity can be identified uniquely only by considering the primary key of another](https://reader033.fdocuments.us/reader033/viewer/2022041520/5e2e3a6bd6908044ba430bc5/html5/thumbnails/13.jpg)
13
Set-Valued Attributes (Cont)
� Can sin reference employees any more?
CREATE TABLE Works_On(sin CHAR(11),pid INTEGER,since DATE,PRIMARY KEY (sin, pid),FOREIGN KEY (pid)
REFERENCES Projects,FOREIGN KEY (sin)
REFERENCES Employees)
Works_On Projects
pidsincehobbyname
Employees
sin
� No. We cannot define sin as a foreign key any longer.
![Page 14: ER to Relational Mapping - University of Albertadrafiei/291/notes/4.ER2...14 Review: Weak Entities A weak entity can be identified uniquely only by considering the primary key of another](https://reader033.fdocuments.us/reader033/viewer/2022041520/5e2e3a6bd6908044ba430bc5/html5/thumbnails/14.jpg)
14
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
agedname
DependentsEmployees
sin
Policy
cost
![Page 15: ER to Relational Mapping - University of Albertadrafiei/291/notes/4.ER2...14 Review: Weak Entities A weak entity can be identified uniquely only by considering the primary key of another](https://reader033.fdocuments.us/reader033/viewer/2022041520/5e2e3a6bd6908044ba430bc5/html5/thumbnails/15.jpg)
15
Translating Weak Entity Sets
� Weak entity set and identifying relationship set are translated into a single table.
NOT NULL,(dname, sin),
FOREIGN KEY (sin) REFERENCES EmployeesON DELETE CASCADE)
CREATE TABLE Dep_Policy (dname CHAR(20),age INTEGER,cost REAL,sin CHAR(11)PRIMARY KEY
• When the owner entity is deleted, all owned weak entities must also be deleted.
![Page 16: ER to Relational Mapping - University of Albertadrafiei/291/notes/4.ER2...14 Review: Weak Entities A weak entity can be identified uniquely only by considering the primary key of another](https://reader033.fdocuments.us/reader033/viewer/2022041520/5e2e3a6bd6908044ba430bc5/html5/thumbnails/16.jpg)
16
Review: ISA Hierarchies
Contract_Emps
namesin
Employees
hourly_wagesISA
Hourly_Emps
contractid
hours_worked
![Page 17: ER to Relational Mapping - University of Albertadrafiei/291/notes/4.ER2...14 Review: Weak Entities A weak entity can be identified uniquely only by considering the primary key of another](https://reader033.fdocuments.us/reader033/viewer/2022041520/5e2e3a6bd6908044ba430bc5/html5/thumbnails/17.jpg)
17
Translating ISA Hierarchies to Relations
� General approach: 3 relations�Employees(sin, name)�Hourly_Emps(sin, hourly_wages, hours_worked)�Contract_Emps(sin, contract_id)
CREATE TABLE Contract_Emps (sin CHAR(11) NOT NULL,contract_id CHAR(4),PRIMARY KEY (sin),FOREIGN KEY (sin) REFERENCES Employees
ON DELETE )
� Alternative: Just Hourly_Emps and Contract_Emps• if Hourly_Emps and Contract_Emps Cover Employees.
�Hourly_Emps(sin, name, hourly_wages, hours_worked).�Contract_Emps(sin, name, contract_id)
CASCADE
![Page 18: ER to Relational Mapping - University of Albertadrafiei/291/notes/4.ER2...14 Review: Weak Entities A weak entity can be identified uniquely only by considering the primary key of another](https://reader033.fdocuments.us/reader033/viewer/2022041520/5e2e3a6bd6908044ba430bc5/html5/thumbnails/18.jpg)
18
Exercise: Map to Relations
Beneficiary
agedname
Dependents
policyid cost
Policies
Purchaser
name
Employees
sin
![Page 19: ER to Relational Mapping - University of Albertadrafiei/291/notes/4.ER2...14 Review: Weak Entities A weak entity can be identified uniquely only by considering the primary key of another](https://reader033.fdocuments.us/reader033/viewer/2022041520/5e2e3a6bd6908044ba430bc5/html5/thumbnails/19.jpg)
19
Exercise: Answer
� The key constraints allow us to combine Purchaser with Policies and Beneficiary with Dependents.
� Participation constraints lead to NOT NULL constraints.
CREATE TABLE Policies (policyid INTEGER,cost REAL,sin CHAR(11) NOT NULL,PRIMARY KEY (policyid),FOREIGN KEY (sin) REFERENCES Employees
ON DELETE CASCADE)
CREATE TABLE Dependents (dname CHAR(20),age INTEGER,policyid INTEGER,PRIMARY KEY (dname, policyid),FOREIGN KEY (policyid) REFERENCES Policies
ON DELETE CASCADE)
![Page 20: ER to Relational Mapping - University of Albertadrafiei/291/notes/4.ER2...14 Review: Weak Entities A weak entity can be identified uniquely only by considering the primary key of another](https://reader033.fdocuments.us/reader033/viewer/2022041520/5e2e3a6bd6908044ba430bc5/html5/thumbnails/20.jpg)
20
Relational Model: Summary
� A tabular representation of data.� 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
![Page 21: ER to Relational Mapping - University of Albertadrafiei/291/notes/4.ER2...14 Review: Weak Entities A weak entity can be identified uniquely only by considering the primary key of another](https://reader033.fdocuments.us/reader033/viewer/2022041520/5e2e3a6bd6908044ba430bc5/html5/thumbnails/21.jpg)
21
Works_pd
name
Professor
sin
project
Runs
age
office
dname
dno
Manages
Works_pp
Graduate
Works_gp
pid
rank budget
end_datest_date
sponsor
sin
age
degreename
Department
pc_time
The ER diagram discussed in class
specialty
Map it into relations…
![Page 22: ER to Relational Mapping - University of Albertadrafiei/291/notes/4.ER2...14 Review: Weak Entities A weak entity can be identified uniquely only by considering the primary key of another](https://reader033.fdocuments.us/reader033/viewer/2022041520/5e2e3a6bd6908044ba430bc5/html5/thumbnails/22.jpg)
22
An ER diagram for the exercise presented earlier.
Works_pd
name
Professor
sin
Project
Runs
age
office
dname
dno
Manages
Works_pp
Graduate
cont_grad
pid
rank budget
end_datest_date
sponsor
sin
age
degreename
Department
pc_time
Contract
cont_prj
supervisescid