Database Design I:: The Entity-Relationship Model
Database Design I:: The Entity-Relationship Model
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
Entities
Entity: an object that is involved in the enterprise
Ex: John, CSCI4333
Entity Type
Entity type described by set of attributes
Person: Id, Name, Address, Hobbies
Key: minimum set of attributes that uniquely identifies an entity (candidate key) Entity Schema: entity type name, attributes (and associated domain), key constraints
4
Set valued
5
Relationships
Relationship: relates two or more entities
John majors in Computer Science
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: Student, Department, Since Here we have used as the role name (Student) the name of the entity type (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
We do not have distinct names for the roles It is not clear who reports to whom
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
Graphical Representation
Roles are edges labeled with role names (omitted if role name = name of entity set). Most attributes have been omitted.
12
A relationship exists between a Freshman entity and the corresponding Student entity
e.g., Freshman John is related to Student John
IsA
Student
Represents 4 relationship types
IsA
Freshman
Sophmore
Junior
Senior
15
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
17
18
Disjointness constraint: Sets of subtype entities are disjoint from one another
Freshman, Sophomore, Junior, Senior are disjoint set
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 E-R
Professor
WorksIn
Department
20
Professor
WorksIn
Department
21
22
Cardinality Constraints
23
Cardinality Constraints
24
25
26
SectNo
S2000Courses
Teaching TAs
Professor Id
S2000Courses (CrsCode, SectNo, Enroll) Professor (Id, DeptId, Name) Teaching (CrsCode, SecNo, Id, RoomNo, TAs)
28
CSE305 CSE305
1 1
Joe Mary
29
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
30
Example 1
Since Professor WorksIn Status Department
CREATE TABLE WorksIn ( Since DATE, -- attribute Status CHAR (10), -- attribute ProfId INTEGER, -- role (key of Professor) DeptId CHAR (4), -- role (key of Department) PRIMARY KEY (ProfId), -- since a professor works in at most one department FOREIGN KEY (ProfId) REFERENCES Professor (Id), FOREIGN KEY (DeptId) REFERENCES Department )
31
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, FOREIGN KEY (SupplierId) REFERENCES Supplier (Id), FOREIGN KEY (PartNumber) REFERENCES Part (Number) )
32
Representation of Single Role Key Constraints in the Relational Model Relational model representation: key of the relation corresponding to the entity type is key of the relation corresponding to the relationship type
Id is primary key of Professor; ProfId is key of WorksIn. Professor 4100 does not participate in the relationship. Cannot use foreign key in Professor to refer to WorksIn since some professors may not work in any dept. (But ProfId is a foreign key in WorksIn that refers to Professor.) Professor
Id
WorksIn
Department
ProfId
Key
1123 3216
WorksIn
CSE AMS
33
34
Student
Id attribs1
Id
attribs2
Id
attribs3
Id
attribs4
Freshman
Sophmore
Junior
Senior
35
Employee
SSN Department 1234 Accounting Salary 35000
Student
SSN GPA StartDate 1234 3.5 1997
36
37
Inclusion 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 ) ) )
Professor
ProfId not a
WorksIn
candidate key
39
Professor
WorksIn
Department
40
WorksIn
41
Prof_WorksIn
43
Entity or Attribute?
Sometimes information can be represented as either an entity or an attribute.
Student Semester Grade Course
Semester Student Transcript Grade Course
44
Transcript
Entity or Relationship?
45
Part Sold
Project
Price
Supplier
46