3 RelationalERmapping
3 RelationalERmapping
ER-Relational Mapping
COMP 353/453
Database Programming
Prof. Silva
These slides were prepared by Prof. Silva adapting the slides from Fundamentals of Database Systems (Elmasri and Navathe) and Understanding
Relational Database Query Languages (Dietrich)
Relational Data Model: Informally
id name major
1111 Student1 CSE
2222 Student2 EEE
3333 Student3 CSE
4444 Student4 EEE
5555 Student5 CSE
Then the cartesian product specifies all possible combinations of values from
the underlying domains and has total number of tuples:
|D1| * |D2| * ... * |Dn|
Relational Database
cse_majors eee_majors
id name class
id name class
1111 Student1 FR
2222 Student2 SO
2222 Student2 SO
3333 Student3 JR 4444 Student4 SR
4444 Student4 SR 6666 Student6 SR
5555 Student5 GR
cse_courses
cse_profs crsid crstitle
name office
CSE412 Database Management
Prof1 Office1
CSE414 Advanced Database Concepts
Prof2 Office2
CSE514 Object Oriented Databases
University Database: teaches
We underline the attributes that form the candidate key of the table
(similar definition to a candidate key in the ER model) :
In the University schema, each table has only one candidate key,
which is therefore the primary key.
If a table has multiple candidate keys, a uniqueness constraint can
be used on the candidate keys that do not comprise the primary
key. More later…
Referential Integrity Constraint: Foreign Key
1. the attrs of Fk have the same domain as the primary key attrs Pk of
the relation scheme R2 that Fk references, and
or
t1[Fk]=t2[Pk] for some t2 of r2
Foreign Key Example
teaches(name, crsid)
cse_profs(name, office) PK
cse_courses(crsid, crstitle) FK
Example: FK
• The name attribute of teaches has the same domain as the name attribute of
cse_profs, and
• the non-null value of the name attribute of teaches (teaches[name]) must be equal
to some name value in cse_profs (cse_profs[name]).
A similar constraint holds for the crsid attribute between the tables teaches and
cse_courses.
How does one take the ER conceptual design and map it to the relational
data model for implementation?
Candidate Key: each primary key of the entity types involved in the relationship
represents a candidate key; choose one as a primary key
mgr(id,dnum)
dept(dnum,dname)
emp(id,name)
Alternative Design:
• No relation for the relationship type itself
• Add to one of the relations:
- attributes corresponding to the primary key of the other entity
involved in the relationship type
- attributes of the relationship type itself (if any);
- If possible, add the attributes to the relation for the entity that has total
participation in the relationship type.
dept(dnum,dname,mgrid)
emp(id,name)
1:N Cardinality Ratio
Candidate Key: the attributes that form the primary key of the many entity in the
relationship
teaches(crsid,tname)
course(crsid,title)
teacher(tname,dept)
Alternative Design:
• No relation for the relationship type itself
• Add to the relation for the entity on the many side of the relationship
type:
- attributes corresponding to the primary key of the entity on the one side of
the relationship type
- attributes of the relationship type itself (if any)
course(crsid,title,tname)
teacher(tname,dept)
M:N Cardinality Ratio
export(cname,pnum,qty)
NOTE:
The candidate key must be based on the semantics of the enterprise.
The meaning of the descriptive attributes of the relationship must be
considered when determining the candidate key of the relation.
Mapping: Multivalued Attributes and Identifying
Relationships
o(oKey, oSingleValuedAttr)
Mapping: Multivalued Attributes
oMulti(oKey, oMultiValuedAttr)
Mapping: Identifying Relationships
Map the ER diagram for employee training courses into the relational data
model:
1
MGR 1
id dnum
name EMP DEPT
N 1 dname
sal M
WORKS
date TAKES
N
crsid inst
COURSE
cname length
Creation of Tables (DDL, DML)
• Data Definition Language (DDL)
• Create tables, constraints, etc.
• Data Manipulation Language (DML)
• Insert data, delete, update, etc.
• Example: PostgreSQL
• Emp (id, name, sal)
• Course (crsid, cname, inst, length)
• Takes (eid, crsid, cdate);
CREATE TABLE Course (crsid integer, INSERT INTO Course VALUES (001, 'MS Access', 'Y. Silva', 4);
cname varchar(30), INSERT INTO Course VALUES (002, 'SQL Server', 'G. Smith', 4);
inst varchar(40),
length integer, INSERT INTO Takes
primary key (crsid));
VALUES (0001, 001, to_date('2012/01/05', 'YYYY/MM/DD'));
INSERT INTO Takes
CREATE TABLE Takes (eid integer,
crsid integer, VALUES (0001, 002, to_date('2012/01/05', 'YYYY/MM/DD'));
cdate date, INSERT INTO Takes
primary key (eid, crsid), VALUES (0002, 002, to_date('2012/01/05', 'YYYY/MM/DD'));
foreign key (eid) references Emp(id),
foreign key (crsid) references Course(crsid));
Summary
What is next?
• The retrieval of information from the relational database using a
query language.
Video: Introduction to Databases
http://www.gcflearnfree.org/access2010
pgModeler: PostgreSQL Database Modeler
• Open-source tool
• Supports the creation of
logical data models
(similar to E-R models)
• Supports the generation
of the corresponding
relational models
• Supports DDL
generation
https://pgmodeler.io/