Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Systems 236363 Entity Relationship Diagrams.

Similar presentations


Presentation on theme: "Database Systems 236363 Entity Relationship Diagrams."— Presentation transcript:

1 Database Systems 236363 Entity Relationship Diagrams

2 Entity Relationship (ER) Model Entity Relationship (ER) model enables to analyze the system’s requirements and represent the type of entities we would like to store data about as well as the relationships between these entities and various constraints on them Entity Relationship Diagram (ERD) provides a visualization through a diagram of the system being represented in the model. The diagram facilitates the process of defining the logical level of the data model, e.g., defining the schemas of the relational database. As such, it serves as a good middleman between the logical layer and the reality as we grasp it Being theoretical and general, it has some limitations. For example, it does not detail possible values for entities’ attributes

3 Notations There are several approaches to drawing the diagrams, yet the principles are the same We will use the one covered in the textbook Ullman and Widom, First Course in Database Systems

4 Attribute An attribute is the most basic unit of information representing a data item of a given type When representing the set of all entities of a given type as a table, each attribute corresponds to a column Notation: an ellipse with the attribute name inside it Name ID num

5 Entity An object of a given type that we would like to store in our system In a table or a file, an entity is usually represented as a record (or a row) For example, in a movies database, there will probably be an entity for the movie “Die Hard” Notice: ERDs do not include entities. Why?

6 Entity Set A set of entities of a given type – For example, movie players Each entity set is associated with attributes that define and describe specific entities of this type In a table representation, the entity set would be the table’s name. The attributes associated with these entities would be the columns’ names – An entity set cannot have two attributes with the same name

7 Entity Set - Notation Drawn as a rectangle – The corresponding attributes are drawn as ellipses connected to it by a solid line – Each attribute in the diagram should be connected either to a single entity set or to a single relationship set (defined shortly) The subset of underlined attributes makes up the primary key In an ER diagram, each rectangle represents a single entity set – each should have a unique name Movie actor Name Birth date Photo

8 Compound Attributes A compound attribute consists of several more basic attributes In the diagram, sub-attributes are connected to the compound one Date Day Month Year

9 Multiple Value Attributes A multiple value attribute may store multiple values for the same attribute Drawn as a double ellipse – E.g., we can store multiple photos for each actor Movie actor Name Birth date Photo

10 Relationship Represents a relationship between entities – For example, the relationship between actress Natalie Portman and the movie Léon represents the fact that she played in that movie A relationship can be – Binary – between two entities – Tertiary – between three entities –…–… – n-ary – between n entities Typically, a relationship is represented in a table storing the primary keys of the corresponding entities Do we also include relationships in the diagram?

11 Relationship Set Represents the relationship between two or more entity sets Drawn as a rhombus connected to the two entity sets; the name of the relationship set is inside the corresponding rhombus In this example, one or more actors can play in a given movie, and the same actor can play in multiple movies. Obviously, not every actor plays in every movie. Some movies (e.g., cartoons) may have no actors at all, and some actors might not play in any movie Movie actor Name Birth date Photo Movie Movie Name Year Genre Played in

12 Relationship Set’s Attributes A relationship set may have attributes – In this case, each relationship of this set must include the corresponding attribute In the following example, we will know the role played by each actor in each movie In the database schema, such a relationship is represented as a table consisting of the primary keys of the corresponding attribute sets plus the attributes of the relationship set Movie actor Name Birth date Photo Movie Movie Name Year Genre Played in Role

13 Primary Attributes of Relationship Sets In some situations, two (or more) entities might be related by multiple relationships of the same set, each identified by different attributes values – For example, the same actor might have multiple roles in the same movie Here, some of the attributes are defined as primary, and must be unique for each specific relationship Movie actor Name Birth date Photo Movie Movie Name Year Genre Played in Role

14 Full Participation When it is required that each entity of a given entity set would have at least one relationship with at least one entity of another entity set – This is represented by a double connecting line In this example, each stored actor must play in at least one movie – No more wannabes… Movie actor Name Birth date Photo Movie Movie Name Year Genre Played in Role

15 Relationship Multiplicity For a given relationship set, we can limit the number of entities of a given entity set to (at most) one – This is represented in the diagram as an arrow In this example, there can be at most one star for each movie Movie actor Name Birth date Photo Movie Movie Name Year Genre Star Role

16 Restricting Relationship Multiplicity Many to many One to many One to one ActorMovie R EmployeeBoss R PresidentCountry R

17 An Example: A Relationship Between Three Entities Enables storing only awards that were awarded at least once to a certain movie at a certain date An award cannot be given more than once to the same movie at two different dates, or at the same date to two different movies Movie Movie Name Year Genre Awarded Prize Award Name Date

18 Find the Differences… Student Lecturer Course Participated ID no Number Name Student Lecturer Course Participated ID no Number Name Participated

19 Roles Sometimes, a relationship set is used to connect the same entity set to itself We distinguish between the relation types by writing their name on the connecting edges In this example, each movie can have only a single origin, but multiple sequels Movie Movie Name Year Genre Sequel Origin Sequel

20 Aggregation Enables treating a relationship set as an entity set The attributes of an aggregation are the combination of all attributes of the corresponding entity sets and relationship sets An aggregation is not represented in a table – it is derived from the corresponding entities tables Movie actor Name Birth date Photo Movie Movie Name Year Genre Played in Role Awarded Award Award Name

21 Find the Differences… Movie actor Name Birth date Photo Movie Movie Name Year Genre Played in Role Awarded Award Award Name Movie actor Name Birth date Photo Movie Movie Name Year Genre Played in Role Awarded Award Award Name

22 A Weak Entity Set A weak entity set depends on another entity set(s) – Its key is composed of its own key combined with the key(s) of the entity set(s) it depends on – Both the weak entity set and it corresponding relationship sets are drawn in the diagram using double lines Chain Department Store C-S S-D Chain’s name Store name Department name What are the multiplicity constraints? What about full participation?

23 ISA Relationship Enables to represent entity sets that are subsets (sub-types) of other entity sets through inclusion Included entity sets (subclasses) inherit all attributes and relationships of the including set (superclass) Subclass entity sets inherit the key of the superclass entity set The subclass entity set may have its additional unique attributes and relationships For every entity of a subclass entity set there is a corresponding entity of the superclass entity set obtained by restricting its attributes to those of the superclass entity set

24 ISA Relationship in the ER Diagram Marked as a triangle pointing to the superclass entity set (in some books it is the opposite) Example Movie Movie Name Year Genre ISA Nature Film Cartoon Location Animator Voices Movie actor Actor Name Birth date Photo Role

25 Unique Inclusion If we would like each entity of the superclass entity set to appear in at most one subclass entity set, we use a single triangle Movie Movie Name Year Genre ISA Nature Film Cartoon Location Animator Voices Movie actor Actor Name Birth date Photo Role

26 Complete Inclusion If we would like each entity of the superclass set to have at least one entity of the subclass set, we use thick lines Movie Movie Name Year Genre ISA Nature Film Cartoon Location Animator Voices Movie actor Actor Name Birth date Photo Role

27 Representing ISA in Tables Option I: Having a table for the superclass entity set. In the table corresponding to each subclass entity set, the only attribute(s) of the superclass entity set is (are) the primary key Option II: Having a table for each subclass entity set. Joins of tables from different subclass entity sets will only consist of attributes of the superclass entity set When should we prefer one over the other?

28 Adding Constraints - Example Can we refine this to better capture reality? Person Name ID no. Parenthood Child Parent

29 Adding Constraints - Example Can we refine this to better capture reality? Person Name ID no. Motherhood Child Parent Fatherhood Child Parent

30 Example Can we further refine this to better capture reality? Person Name ID no. Motherhood Child Parent Fatherhood Child Parent FatherMother ISA

31 Summarizing Example Consider this diagram for trains operation Station Height S_Name S_Type Line Length L_Num Direction Serves Km Train T_Num Days Service T_Category Class Food Gives Arrives Platform D_TimeA_Time

32 What Tables do we Extract? What columns should exist for the relationship set “Serves”? The key S_Name (of the “Station” entity set) The key’s attributes L_Num and Direction (of “Line”) – These triplet would serve as the key for “Serves” – In addition, a column for the relation attribute Km What columns should exist for the relationship set “Arrives”? – The key T_Num of the entity set “Train” – The key’s attributes for the aggregated relationship set “Serves”, i.e., S_Name, L_num, and Direction – The three attributes of the relationship set “Arrives” itself Platform, A_time, D_time

33 Extracting Tables from ERD

34 A Table Example t1 = (foo,bar,baz,{x,y}) t2 = (quz,bar,foo,{y,z}) t1[a1] = (baz) t2[a2] = ({y,z}) t1[a̅] = (baz,{x,y}) t2[k̅] = (quz,bar) a2a1k2k1 {x,y}bazbarfoo {y,z}foobarquz t1 t2 k̅ = (k1,k2) a̅ = (a1,a2) Vector of all key attributes Vector of all non-key attributes

35 Entities A short representation: E k1kna1am k̅ = (k1,…,kn)a̅ = (a1,…,am) E k̅a̅

36 Entities Translation to a table: a̅k̅ am…a1kn…k1 E k̅a̅ Constraints: t 1 [k̅] = t 2 [k̅] ⇒ t 1 [a̅] = t 2 [a̅]

37 Entities k̅, a̅ may be empty “No Key” – empty a̅ ! – Every attribute is a part of the key (underlined) What is the meaning of empty k̅ ? E k̅a̅

38 Entities Any a i (or k i ) may be multi-valued In domain D, for a table row t: – for an attribute a i : t[a i ] ∈ D – for a multi-valued attribute a j : t[a j ] ∈ P(D) a powerset. E k1kna1am aiai

39 Another Representation of a Multi-Valued Attribute A table without multi-valued attributes: Tables - one for each multi-valued attribute: Multi-valued attributes cannot be a part of the key E k1kn a1 am a̅k̅ am… a2 kn…k1 a1 k̅ kn…k1

40 Relationships Each k̅, a̅ may be empty Translation to a table: E1E1 k̅ 1 a̅ 1 k̅ 2 a̅ R k̅ R k̅ 1 R E2E2 k̅ R a̅ R k̅ 2 a̅ 2

41 Relationships Constraints: – K 2 ∧ K R →K 1 ∧ A R (1) – K 1 ∧ K R →K 2 ∧ A R (2) – (K 2 ∧ K R →K 1 ∧ A R ) ∧ (K 1 ∧ K R →K 2 ∧ A R ) (1+2) E1E1 k̅ 1 a̅ 1 R E2E2 k̅ R a̅ R k̅ 2 a̅ 2 12 K 1 ≔ t 1 [k̅ 1 ]=t 2 [k̅ 1 ] K 2 ≔ t 1 [k̅ 2 ]=t 2 [k̅ 2 ] K R ≔ t 1 [k̅ R ]=t 2 [k̅ R ] A R ≔ t 1 [a̅ R ]=t 2 [a̅ R ]

42 Relationships Constraints: – π k2 (E 2 ) ⊆ π k2 (R) – π k2 (R) ⊆ π k2 (E 2 ) (true for any relationship!) E1E1 k̅ 1 a̅ 1 R E2E2 k̅ R a̅ R k̅ 2 a̅ 2

43 Ternary Relationships Constraints: – K 2 ∧ K 3 ∧ K R →K 1 ∧ A R (1) – K 1 ∧ K 3 ∧ K R →K 2 ∧ A R (2) – (K 2 ∧ K 3 ∧ K R →K 1 ∧ A R ) ∧ (K 1 ∧ K 3 ∧ K R →K 2 ∧ A R ) (1+2) – Each arrow is a layer and several arrows are a conjunction ( ∧ ) of layers E1E1 k̅ 1 a̅ 1 R E2E2 k̅ R a̅ R k̅ 2 a̅ 2 12 K 1 ≔ t 1 [k̅ 1 ]=t 2 [k̅ 1 ] K 2 ≔ t 1 [k̅ 2 ]=t 2 [k̅ 2 ] K 3 ≔ t 1 [k̅ 3 ]=t 2 [k̅ 3 ] K R ≔ t 1 [k̅ R ]=t 2 [k̅ R ] A R ≔ t 1 [a̅ R ]=t 2 [a̅ R ] E3E3 k̅ 1 a̅ 1 3

44 Roles Translation to a table: E1E1 k̅a̅ k̅ role2 a̅ R k̅ R k̅ role1 R k̅ R a̅ R role1 role2 Constraints are the same

45 Aggregations Turns the relationship into an entity with attributes of the relationship E1E1 k̅ 1 a̅ 1 R E2E2 k̅ R a̅ R k̅ 2 a̅ 2 ERER E3E3 k̅ 3 a̅ 3 S k̅ S a̅ S

46 Aggregations E1: E2: E3: a̅ 1 k̅ 1 a̅ 2 k̅ 2 a̅ 3 k̅ 3 a̅ R k̅ R k̅ 2 k̅ 1 a̅ S k̅ S k̅ R k̅ 3 k̅ 2 k̅ 1 R: S:

47 Weak Entities Translation to tables: E1E1 k̅ 1 a̅ 1 k̅ 1 E2E2 k̅ 2 a̅ 2 E3E3 k̅ 3 a̅ 3 a̅ 2 k̅ 2 k̅ 1 a̅ 3 k̅ 3 k̅ 2 k̅ 1

48 Weak Entities Constraints: π k1 (E 2 ) ⊆ π k1 (E 1 ) π k1,k2 (E 3 ) ⊆ π k1,k2 (E 2 ) E1E1 k̅ 1 a̅ 1 E2E2 k̅ 2 a̅ 2 E3E3 k̅ 3 a̅ 3

49 ISA ISA – a branching weak entity without key components in the subclass E1E1 k̅ 1 a̅ 1 E2E2 a̅ 2 E3E3 a̅ 3 ISA superclass subclass

50 ISA – Translations and Constraints E1: E2: E3: Constraints: π k1 (E 3 ) ⊆ π k1 (E 1 )π k1 (E 2 ) ⊆ π k1 (E 1 ) a̅ 1 k̅ 1 a̅ 2 k̅ 1 a̅ 3 k̅ 1 E1E1 a̅ 1 E2E2 a̅ 2 E3E3 a̅ 3 ISA

51 Exclusive ISA E1:E2:E3:E1:E2:E3: Constraints: π k1 (E 3 ) ⊆ π k1 (E 1 )π k1 (E 2 ) ⊆ π k1 (E 1 ) π k1 (E 2 )∩π k1 (E 3 )= ∅ a̅ 1 k̅ 1 a̅ 2 k̅ 1 a̅ 3 k̅ 1 E1E1 a̅ 1 E2E2 a̅ 2 E3E3 a̅ 3 ISA

52 Covering All ISA E1: E2: E3: Constraints: π k1 (E 2 )∩π k1 (E 3 )= ∅ a̅ 2 a̅ 1 k̅ 1 E1E1 a̅ 1 E2E2 a̅ 2 E3E3 a̅ 3 ISA Thick lines NO Table! a̅ 3 a̅ 1 k̅ 1


Download ppt "Database Systems 236363 Entity Relationship Diagrams."

Similar presentations


Ads by Google