Kifercomp 348761 ppt04
Kifercomp 348761 ppt04
Kifercomp 348761 ppt04
Database Design I:
The Entity-Relationship
Model
Database Design
Goal: specification of database schema
Methodology:
Use E-R model to get a high-level graphical view of
essential components of enterprise and how they are
related
Convert E-R diagram to DDL
E-R Model:
Model enterprise is viewed as a set of
Entities
Relationships among entities
2
Entities
Entity:
Entity an object that is involved in the
enterprise
Ex: John, CSE305
Entity Type:
Type set of similar objects
Ex: students,
students courses
Attribute:
Attribute describes one aspect of an entity type
Ex: name, maximum enrollment
3
Entity Type
Entity type described by set of attributes
Person:
Person Id, Name, Address, Hobbies
Domain:
Domain possible values of an attribute
Value can be a set (in contrast to relational model)
(111111, John, 123 Main St, {stamps, coins})
Key:
Key minimum set of attributes that uniquely
identifies an entity (candidate key)
Entity Schema:
Schema entity type name, attributes (and
associated domain), key constraints
4
Set valued
5
Relationships
Relationship:
Relationship relates two or more entities
John majors in Computer Science
Relationship Type:
Type set of similar relationships
Student (entity type) related to Department (entity type)
by MajorsIn (relationship type).
Distinction:
relation (relational model) - set of tuples
relationship (E-R Model) describes relationship
between entities of an enterprise
Both entity types and relationship types (E-R model)
may be represented as relations (in the relational model)
6
Relationship Type
Described by set of attributes and roles
e.g., MajorsIn:
MajorsIn Student, Department, Since
Here we have used as the role name (Student)
the name of the entity type (Student)
Student of the
participant in the relationship, but ...
Roles
Problem: relationship can relate elements of
same entity type
e.g., ReportsTo relationship type relates two
elements of Employee entity type:
Bob reports to Mary since 2000
Roles (cont)
Solution: role name of relationship type
need not be same as name of entity type
from which participants are drawn
ReportsTo has roles Subordinate and
Supervisor and attribute Since
Values of Subordinate and Supervisor both
drawn from entity type Employee
10
11
Graphical Representation
Roles are edges labeled with role names (omitted if role name
= name of entity set). Most attributes have been omitted.
12
IsA
Student
Represents 4
relationship types
IsA
Freshman
Sophmore
Junior
Senior
14
Properties of IsA
Inheritance - Attributes of supertype apply
to subtype.
E.g., GPA attribute of Student applies to
Freshman
Subtype inherits all attributes of supertype.
Key of supertype is key of subtype
Advantages of IsA
Can create a more concise and readable E-R
diagram
Attributes common to different entity sets need
not be repeated
They can be grouped in one place as attributes
of supertype
Attributes of (sibling) subtypes can be different
16
17
Disjointness constraint:
constraint Sets of subtype entities are
disjoint from one another
Freshman,
Freshman Sophomore,
Sophomore Junior,
Junior Senior are disjoint set
18
WorksIn
Department
19
Participation Constraint
If every entity participates in at least one
relationship, a participation constraint
holds:
A participation constraint of entity type E
having role in relationship type R states that
for e in E there is an r in R such that (r) = e.
e.g., every professor works in at least one
department
Reprsentation in ER
Professor
WorksIn
Department
20
Professor
WorksIn
Department
21
CrsCode
Enroll
S2000Courses
RoomNo
DeptId
Professor
Teaching
TAs
Name
Id
23
SectNo
CSE305
CSE305
1
1
Id
RoomNo
1234 Hum 22
1234 Hum 22
TAs
Set
valued
Joe
Mary
24
Representation in SQL
Each role of relationship type produces a
foreign key in corresponding relation
Foreign key references table corresponding to
entity type from which role values are drawn
25
Example 1
Since
Professor
Status
WorksIn
Department
Example 2
Date
Project
Price
Sold
Part
Supplier
CREATE TABLE Sold (
Price INTEGER,
-- attribute
Date DATE,
-- attribute
ProjId INTEGER,
-- role
SupplierId INTEGER,
-- role
PartNumber INTEGER,
-- role
PRIMARY KEY (ProjId, SupplierId, PartNumber, Date),
FOREIGN KEY (ProjId) REFERENCES Project,
Project
FOREIGN KEY (SupplierId) REFERENCES Supplier (Id),
FOREIGN KEY (PartNumber) REFERENCES Part (Number) )
27
Professor
WorksIn
Id
1123
4100
3216
Professor
Department
ProfId
1123
3216
WorksIn
Key
CSE
AMS
28
29
Student
Id attribs1
Id
attribs2
Freshman
Sophmore
Id
attribs3
Junior
Id
attribs4
Senior
30
Name DOB
Mary 1950
Employee
SSN Department
1234 Accounting
Student
Salary
35000
31
32
WorksIn
Department
Inclusion dependency:
dependency Every professor works in at least one dept.
in the relational model: (easy)
Professor (Id) references WorksIn (ProfId)
in SQL:
Simple case: If ProfId is a key in WorksIn (i.e., every professor works
in exactly one department) then it is easy:
FOREIGN KEY Id REFERENCES WorksIn (ProfId)
General case ProfId is not a key in WorksIn, so cant use foreign key
constraint (not so easy):
CREATE ASSERTION ProfsInDepts
CHECK ( NOT EXISTS (
SELECT * FROM Professor P
WHERE NOT EXISTS (
SELECT * FROM WorksIn W
WHERE P.Id = W.ProfId ) ) )
Id
1123
1123
4100
3216
1123
4100
3216
Professor
CSE
AMS
ECO
AMS
WorksIn
not a
candidate key
ProfId
34
Professor
WorksIn
Department
35
Id
xxxxxx 1123
yyyyyy 4100
zzzzzzz 3216
Professor
ProfId
1123
4100
3216
CSE
ECO
AMS
WorksIn
36
Id
xxxxxxx
yyyyyyy
zzzzzzzz
1123
4100
3216
DeptId
CSE
ECO
AMS
Prof_WorksIn
38
Entity or Attribute?
Sometimes information can be represented
as either an entity or an attribute.
Student
Semester
Transcript
Course
Grade
Semester
Student
Appropriate if Semester
has attributes
(next slide)
Transcript
Grade
Course
39
Entity or Relationship?
40
Date
Project
Sold
Price
Supplier
41
ER exercise 1
Consider the design of the following database system for
managing a conference X: a collection of papers are submitted to
X, each of which has a unique paper IDs, a list of authors (names,
affiliations, emails) in the order of contribution significance, title,
abstract, and a PDF file for its content. The conference has a list
of program committee (PC) members to review the papers. To
ensure review quality, each paper is assigned to 3 PC members for
review. To avoid overloading, each PC member is assigned with at
most 5 papers, assuming that there are enough PC members. Each
review report consists of a report ID, a description of review
comment, a final recommendation (accept, reject), and the date the
review report is submitted. A PC member can submit at most one
review report for the paper that is assigned to him/her.
42
ER exercise 1 (cont)
Draw an E-R diagram for the above system. Use underlines,
thick lines, and arrows to represent constraints. State your
assumptions if necessary.
Translate the previous E-R diagram for exercise1 into a
relational model, i.e., a set of CREAT TABLE statements
enforcing all stated constraints. In addition, write a CREATE
ASSERTION statement to enforce that no PC member will be
assigned to a paper of which she/he is a coauthor.
43
ER Diagram
44
SQL exercise
Create table paper (
paperid integer,
title VARCHAR(50),
abstract VARCHAR(250),
pdf VARCHAR(100),
primary key (paperid)
)
45
SQL exercise
Create table author(
email VARCHAR(100),
name VARCHAR(50),
affiliation VARCHAR(100),
primary key(email)
)
46
48
49
50
51
ER exercise 2
Suppose you are asked to design a club database system based on
the following information. Each student has a unique student id, a
name, and an email; each club has a unique club id, a name, a
contact telephone number, and has exactly one student as its
president. Each student can serve as a president in at most one of the
clubs, although he/she can be the members of several clubs. Clubs
organize activities and students can participate in any of them. Each
activity is described by a unique activity id, a place, a date, a time
and those clubs that organize it. If an activity is organized by more
than one club, different clubs might contribute different activity
fees.
52
Exercise 2 (cont)
Draw an E-R diagram for the system, in particular, use
arrows or thick lines to represent constraints appropriately.
Write down your assumptions if necessary.
Translate the above E-R diagram to a relational model, in
particular, specify your primary key and foreign key
constraints clearly.
53
Reference solution
54
55
57
58
59
Exercise 3
Consider the design of a database for the management of
grants. Each grant is identified by a unique grant ID, a title,
the funding source of the grant, the period (starting data and
ending date), and the amount of grant. Each grant might be
participated by several professors and each professor might
also participate in several grants. Each professor is identified
by a unique SSN, name, and email address. In addition,
several graduate students might be supported by a grant as
GRAs, although each student can be supported by at most one
grant. Each graduate student has exactly one professor as
his/her advisor.
61
Exercise 3 (cont)
Draw an E-R diagram for the system, in particular,
use arrows or thick lines to represent constraints
appropriately. Write down your assumptions and
justifications briefly and clearly.
Translate the above E-R diagram into a relational
model, i.e., write a set of CREATE TABLE
statements. In particular, specify primary key,
foreign key and other constraints whenever possible.
62
Reference solution
63
Create student
(studid integer,
name varchar(50),
status varchar(20),
advisor char(9) NOT NULL,
supportgrantid integer,
primary key(studid),
foreign key advisor references professor,
Foreign key supportgrantid references grant(grantid)
)
67
Exercise 4
Consider the design of the following database system: each
PhD student has exactly one a dissertation committee which
consists of 4-5 faculty, and each committee is for exactly one
student. Each student has an ordered list of advisors including
the primary advisor followed by 0 or more secondary
advisors. Each student has a unique studid, a name, and a
major. Each committee has a unique committee id, and the
date the committee is formed. Each faculty has a unique facid
and a name. Each faculty can participate in multiple
committees and be the advisors (either primary or secondary)
of several students.
68
Exercise 4 (cont)
Draw an E-R diagram for the above system. Use
underlines, thick lines, and arrows to represent constraints.
State your assumptions if necessary.
Translate your E-R diagram for problem 1 into a relational
model, i.e., a set of CREAT TABLE/ASSERTION
statements enforcing all stated constraints. In addition,
write a CREATE ASSERTION statement to enforce that
each committee consists of the primary advisor of the
student and all other members of the committee cannot be
the secondary advisors of the student.
69
Reference solution
70
Reference solution
CREATE TABLE Advise (
order VARCHAR(50),
studid VARCHAR(50),
facid VARCHAR(50),
PRIMARY KEY ( studid, facid ),
FOREIGN KEY ( studid ) REFERENCES Students
( studid ),
FOREIGN KEY (facid ) REFERENCES Faculty ( facid )
)
71
72
73
74
75