<<

• Goal: specification of Database Design I: • Methodology: – Use E-R model to get a high- graphical of The Entity-Relationship Model essential components of enterprise and how they are related Chapter 5 – Convert E-R diagram to DDL • E-R Model: enterprise viewed as set of – Entities – Relationships among entities

1 2

Entity Type Entities · Entity type described by set of attributes · Entity: an object that is involved in the ± Student: Id, Name, Address, Hobbies enterprise · Domain: possible values of an attribute ± Ex: John, CSE305 ± Value can be a set (in contrast to ) • (111111, John, 123 Main St, (stamps, coins)) · Entity Type: set of similar objects • Key: minimum set of attributes that uniquely ± Ex: students, courses identifies an entity () · Attribute: describes one aspect of an entity type • Entity Schema: entity type name, attributes (and ± Ex: name, maximum enrollment associated domain), key constraints

3 4

Representation in Relational Model • Entity type corresponds to a Entity Type (con' t) • Relation’s attributes = entity type’s attributes · Graphical Representation in E-R diagram: – Problem: entity type can have set valued attributes. – Solution: Use several rows to represent a single entity • (111111, John, 123 Main St, stamps) • (111111, John, 123 Main St, coins) – Problems with solution: • Redundancy • Key of entity type not key of relation • => resulting relation must be further transformed Set-valued attribute

5 6

•1 Relationship Attributes and Roles · Attribute of a relationship type describes the · Relationship: relates two or more entities · Attribute ± John majors in Computer Science relationship · Relationship Type: set of similar relationships ± e.g., John majors in CS since 2000 ± Student (entity type) related to Department (entity type) · John and CS are related by MajorsIn (relationship type). · 2000 describes relationship - value of SINCE attribute of MajorsIn relationship type · Distinction: MajorsIn ± relation (relational model) - set of tuples · Role of a relationship type names one of the ± relationship (E-R Model) ± describes relationship related entities between entities of an enterprise ± e.g., John is value of Student role, CS value of ± Both entity types and relationship types (E-R model) Department role of MajorsIn relationship type can be represented as relations (relational model) ± (John, CS, 2000) describes a relationship 7 8

Relationship Type Roles · Problem: relationship can relate elements of · Described by set of attributes and roles same entity type ± e.g., MajorsIn: Student, Department, Since ± e.g., ReportsTo relationship type relates two ± Here we have used as the role name (Student) elements of Employee entity type: the name of the entity type (Student) of the · Bob reports to Mary since 2000 participant in the relationship, but ... ± We do not have distinct names for the roles ± It is not clear who reports to whom

9 10

Schema of a Relationship Type

Roles (con’ t) • Role names, Ri, and their corresponding entity sets. Roles must be single valued (number of • Solution: role name of relationship type roles = degree) need not be same as name of entity type • Attribute names, Aj, and their corresponding from which participants are drawn domains. Attributes may be set valued – ReportsTo has roles Subordinate and • Key: Minimum set of roles and attributes that Supervisor and attribute Since uniquely identify a relationship

– Values of Subordinate and Supervisor both • Relationship: drawn from entity type Employee – ei is an entity, a value from Ri’ s entity set – aj is a set of attribute values with elements from domain of Aj 11 12

·2 Graphical Representation Representation of Relationship Type in Relational Model · Roles are edges labeled with role names (omitted if role name = name of entity set). Most attributes have been omitted. · Attributes of corresponding relation are ± Attributes of relationship type ± For each role, the primary key of the entity type associated with that role · Example:

RoomNo DeptId Name SectNo CrsCode Enroll

S2000Courses Teaching Professor

Id ± S2000Courses (CrsCode, SectNo, Enroll) ± Professor (Id, DeptId, Name) ± Teaching (CrsCode, SecNo, Id, RoomNo) 13 14

Representation in Relational Model Representation in SQL

· Candidate key of corresponding = candidate key of · Each role of relationship type produces a relation ± Except when there are set valued attributes in corresponding relation ± Example: Teaching (CrsCode, SectNo, Id, RoomNo, TA) ± Foreign key references table corresponding to · Key of relationship type = (CrsCode, SectNo) · Key of relation = (CrsCode, SectNo, TA) entity type from which role values are drawn

CrsCode SectNo Id RoomNo TA CSE305 1 22 1234 Hum Joe CSE305 1 22 1234 Hum Mary

15 16

Example 1 Example 2

Date Price Since Status Project Sold Part Professor WorksIn Department

Supplier CREATE TABLE WorksIn ( CREATE TABLE Sold ( Since DATE, -- attribute Price INTEGER, -- attribute Status CHAR (10), -- attribute Date DATE, -- attribute ProfId INTEGER, -- role (key of Professor) ProjId INTEGER, -- role DeptId CHAR (4), -- role (key of Department) SupplierId INTEGER, -- role PRIMARY KEY (ProfId), -- since a professor works in at most one department PartNumber INTEGER, -- role FOREIGN KEY (ProfId) REFERENCES Professor (Id), PRIMARY KEY (ProjId, SupplierId, PartNumber, Date), FOREIGN KEY (ProjId) REFERENCES Project, FOREIGN KEY (DeptId) REFERENCES Department ) FOREIGN KEY (SupplierId) REFERENCES Supplier (Id), FOREIGN KEY (PartNumber) REFERENCES Part (Number) ) 17 18

·3 Key Constraint (special case) Key Constraint (special case) · Relational model representation: key of relation corresponding to entity type is key of relation · If, for a particular participant entity type, corresponding to relationship type each entity participates in at most one ± Id is primary key of Professor; ProfId is key of relationship, corresponding role is a key of WorksIn. Professor 4100 does not participate. ± Cannot use foreign key in Professor since some relationship type professors do not participate ± E.g., Professor role is unique in WorksIn · Representation in E-R diagram: arrow Id ProfId 1123 4100 1123 CSE Professor WorksIn Department 3216 3216 AMS WorksIn 19 Professor 20

Entity Type Hierarchies IsA · One entity type might be subtype of another ± Freshman is a subtype of Student Student · A relationship exists between a Freshman entity Represents four and the corresponding Student entity IsA relationship types ± e.g., Freshman John is related to Student John · This relationship is called IsA ± Freshman IsA Student Freshman Sophmore Junior Senior ± The two entities related by IsA are always descriptions of the same real-world object

21 22

Properties of IsA IsA · Inheritance - Attributes of supertype apply to subtype. · Advantage: Used to create a more concise ± E.g., GPA attribute of Student applies to Freshman and readable E-R diagram ± Subtype inherits all attributes of supertype. ± Attributes common to different entity sets need ± Key of supertype is key of subtype not be repeated · Transitivity - Hierarchy of IsA ± They can be grouped in one place as attributes ± Student is subtype of Person, Freshman is of supertype subtype of Student, so Freshman is also a ± Attributes of (sibling) subtypes can be different subtype of Student

23 24

·4 IsA Hierarchy - Example Type Hierarchy

· Might have associated constraints: ± Covering constraint: Union of subtype entities is equal to set of supertype entities · Employee is either a secretary or a technician (or both) ± Disjointness constraint: Sets of subtype entities are disjoint from one another · Freshman, Sophomore, Junior, Senior are disjoint sets · Might be related to fragmentation of data

25 26

Type Hierarchies and Relational Model Type Hierarchies and Relational Model

· Supertypes and subtypes can be realized as Id attribs0 separate relations Student ± Need a way of identifying subtype entity with its (unique) related supertype entity · Choose a candidate key and make it an attribute of all entity types in hierarchy Id attribs1 Id attribs2 Id attribs3 Id attribs4

Freshman Sophmore Junior Senior 27 28

Participation Constraint Type Hierarchies and Relational Model · If every entity participates in at least one relationship, a participation constraint · Redundancy eliminated if IsA is not disjoint holds: ± For individuals who are both employees and ± A participation constraint of entity type E students, Name and DOB are stored once having role ρ in relationship type R states that for e in E there is an r in R such that ρ(r) = e. Person Employee Student ± e.g., every professor works in at least one department SSN Name DOB SSN Department Salary SSN GPA StartDate 1234 Mary 1950 1234 Accounting 35000 1234 3.5 1997 Reprsentation in E-R

Professor WorksIn Department 29 30

·5 Representing Participation Constraints Participation Constraint in Relational · Inclusion dependency: Every professor works in at least one dep' t. Model ± in relational model: (easy) · Professor (Id) references WorksIn (ProfId) ± in SQL: · Example (can' t use foreign key in Professor) · Special case: Every professor works in exactly one dep' t. (easy) ± FOREIGN KEY Id REFERENCES WorksIn (ProfId) ProfId Id · General case (not so easy): 1123 CSE 1123 1123 AMS 4100 CREATE ASSERTION ProfsInDepts 4100 ECO 3216 CHECK ( NOT EXISTS ( 3216 AMS SELECT * FROM Professor P Professor WHERE NOT EXISTS ( WorksIn SELECT * FROM WorksIn W WHERE P.Id = W.ProfId ) ) ) ProfId not a 31 candidate key 32

Participation and Key Constraint Participation and Key Constraint in SQL

· If every entity participates in exactly one · If both participation and key constraints apply, use relationship, both a participation and a key foreign key constraint in entity table (but beware: if constraint hold: candidate key in entity table is not primary, presence ± e.g., every professor works in exactly one of nulls violates participation constraint). department CREATE TABLE Professor ( E-R representation Id INTEGER, ¼ ¼ PRIMARY KEY (Id), -- Id can’t be FOREIGN KEY (Id) REFERENCES WorksIn (ProfId) --all professors participate ) Professor WorksIn Department

Professor WorksIn Department 33 34

Participation and Key Constraint in Participation and Key Constraint in Relational Model Relational Model (again) · Example: · Alternate solution if both key and ProfId Id participation constraints apply: merge the xxxxxx 1123 1123 CSE yyyyyy 4100 4100 ECO tables representing the entity and zzzzzzz 3216 3216 AMS relationship sets Professor WorksIn ± Since there is a 1-1 and onto relationship between the rows of the entity set and the relationship sets, might as well put all the attributes in one table

35 36

·6 Participation and Key Constraint in Entity or Attribute? Relational Model · Sometimes information can be represented · Example as either an entity or an attribute.

xxxxxxx 1123 CSE yyyyyyy 4100 ECO Student Transcript Semester zzzzzzzz 3216 AMS Grade Prof_WorksIn Course

Appropriate if Semester Semester has attributes Student Transcript (next slide) Grade Course 37 38

Entity or Relationship? (Non-) Equivalence of Diagrams

· Transformations between binary and ternary relationships.

Date Part Project Sold

Price Supplier

39 40

E-R and Object

· Object databases allow set-valued: translation from E-R diagram into ODB is easier. · IsA relationship can be directly represented in ODB.

41

·7