Presentation is loading. Please wait.

Presentation is loading. Please wait.

©Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

Similar presentations


Presentation on theme: "©Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction."— Presentation transcript:

1 ©Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction of an E-R Diagram to Tables Chapter 7

2 ©Silberschatz, Korth and Sudarshan2Database System Concepts Entity Sets A database can be modeled as a collection of:  entities, and  relationships among those entities. An entity is an object that is distinguishable from other objects.  A specific person, company, automobile, etc. Entities have attributes:  People have names and addresses  An entity and its’ attributes are represented by a tuple (342-97-4873, Smith, Main, Orlando) An entity set is a set of entities of the same type, i.e., that share the same properties and attributes. An extension is an instance of it.  Set of all students, set of all companies, set of all automobiles

3 ©Silberschatz, Korth and Sudarshan3Database System Concepts Entity Sets customer and loan Entity sets:  The set of all customers of the Bank  The set of all loans at the bank Entities:  Customers of the Bank – Bob Jones, Sue Smith, Mark Hayes, etc.  Loans at the Bank – L17, L15, L23, etc. Attributes:  Bob Jones has an ID# (321-12-3231), a street address (475 Main Street), a city of residence (Orlando), and a last name (Jones).  Loan L17 has an amount ($4537), a date when the loan was taken out (12/15/2009), and a loan number (L17).

4 ©Silberschatz, Korth and Sudarshan4Database System Concepts Attributes The set of permitted values for an attribute is called the domain of that attribute. Attributes can be one of several types:  Simple (i.e., atomic) – height in inches, weight in ounces, last-name  Composite – name, address  Single-valued – date of birth, name  Multi-valued – phone-numbers, dependents, hobbies  Derived – Age is derived, or rather, computed from date-of-birth

5 ©Silberschatz, Korth and Sudarshan5Database System Concepts Attributes, Cont. Keep in mind that modeling is NOT design!  during modeling we are focused on what the relevant data is, and not whether or how it will be stored in the database.  age vs. date-of-birth This approach is:  consistent with most text-books  somewhat inconsistent with industry

6 ©Silberschatz, Korth and Sudarshan6Database System Concepts Relationship Sets A relationship is an association among two (or more) entities  Hayes is a depositor for account A-102  The relationship is denoted by a tuple (Hayes, A-102) More simply stated, a relationship set is a set of relationships, all of the same type.

7 ©Silberschatz, Korth and Sudarshan7Database System Concepts Attributes, Cont. Relationships can be visualized graphically: customer borrower Jonesloan SmithL-23 HayesL-15 JacksonL-14 CurryL-19 WilliamsL-17 Adams

8 ©Silberschatz, Korth and Sudarshan8Database System Concepts Relationship Sets (Cont.) An attribute can also be property of a relationship set.

9 ©Silberschatz, Korth and Sudarshan9Database System Concepts Relationship Sets with Attributes, Cont. Another example of a relationship set having attributes:  Entities: Student and Course  Relationship: Has-Taken Where does the attribute grade go?

10 ©Silberschatz, Korth and Sudarshan10Database System Concepts Degree of a Relationship Set The number of entity sets that participate in a relationship set is referred to as the degree of that relationship set. Relationship sets that involve two entity sets are called binary.  Most relationship sets are binary. Example of a ternary relationship set:  Employees of a bank have jobs at multiple branches, with different jobs at different branches.  This gives a ternary relationship set between employee, job and branch.

11 ©Silberschatz, Korth and Sudarshan11Database System Concepts Mapping Cardinalities The mapping cardinality of a relationship set expresses the number of entities to which one entity can be associated via the relationship set. Most useful in describing binary relationship sets. For a binary relationship there are four types:  One to one – legal US marriages  One to many – academic advisors (at most one major)  Many to one – same as one-to-many  Many to many – depositors

12 ©Silberschatz, Korth and Sudarshan12Database System Concepts Mapping Cardinalities One to oneOne to many Note: Some elements in A and B may not be mapped to any elements in the other set (i.e., total vs. partial relations)

13 ©Silberschatz, Korth and Sudarshan13Database System Concepts Mapping Cardinalities Many to oneMany to many Note: Some elements in A and B may not be mapped to any elements in the other set

14 ©Silberschatz, Korth and Sudarshan14Database System Concepts Mapping Cardinalities affect Attribute Placement access-date could be an attribute of account instead of a relationship attribute if each account can have only one customer, i.e., if the relationship is one-to-many.

15 ©Silberschatz, Korth and Sudarshan15Database System Concepts E-R Diagrams Rectangles - entity sets (new: double for weak entity set) (new version: divided in name/attributes) (new version: undivided for attribute of relation) Diamonds - relationship sets (new: double if linked to weak entity sets) Ellipses - attributes:  Double ellipses - multi-valued  Dashed ellipses – derived  Underlined - primary key attributes (will discuss later) Lines - connect attributes to entity sets, and entity sets to relationship sets. (new) Dashed Lines: link attributes of relationship to relationship set (new) Double Lines: Total participation of an entity in a relationship set

16 ©Silberschatz, Korth and Sudarshan16Database System Concepts E-R Diagram with Composite, Multivalued, and Derived Attributes Notes:  In many applications the terms are much more ambiguous (e.g., function designators)  An ER diagram is typically accompanied by a document that defines all the terms  Much harder to do than it appears

17 ©Silberschatz, Korth and Sudarshan17Database System Concepts Relationship Sets with Attributes

18 ©Silberschatz, Korth and Sudarshan18Database System Concepts Cardinality Constraints Cardinality constraints are indicated by drawing a directed line (  ), signifying “one,” or an undirected line (—), signifying “many,” between the relationship and the entity. If borrower were a one-to-one relationship:  A customer would be associated with at most one loan  A loan would be associated with at most one customer

19 ©Silberschatz, Korth and Sudarshan19Database System Concepts One-To-Many Relationship If borrower were a one-to-many relationship then a loan would be associated with at most one customer, and a customer would be associated with zero or more loans.

20 ©Silberschatz, Korth and Sudarshan20Database System Concepts Many-To-One Relationships If borrower were a many-to-one relationship then a loan would be associated with zero or more customers, and a customer would be associated with at most one loan.

21 ©Silberschatz, Korth and Sudarshan21Database System Concepts Many-To-Many Relationship If borrower were a many-to-many relationship then a loan would be associated with zero or more customers, and a customer would be associated with zero of more loans.

22 ©Silberschatz, Korth and Sudarshan22Database System Concepts Participation of an Entity Set in a Relationship Set If every entity in an entity set participates in a relationship set, then that entity set is said to have total participation in the relationship.  Indicated by a double-line If participation in a relationship is optional for some entities then that entity set is said to have partial participation in the relationship.

23 ©Silberschatz, Korth and Sudarshan23Database System Concepts Another Example of Total Participation Consider a relationship between entity sets Student and Faculty called Advises. Assumptions:  Each student has exactly one major, and hence exactly one advisor  Some faculty don’t advise students In this case the Advises relationship set is:  One-to-many from Faculty to Student  Student participation is total In most universities, the Advises relationship is more likely many- to-many, with the Student participation being total.

24 ©Silberschatz, Korth and Sudarshan24Database System Concepts Alternative Notation for Cardinality Limits Specific cardinality limits can also express participation constraints 0..*: a customer can have 0 or more loans. 1..1: a loan belongs to 1 and only 1 customer

25 ©Silberschatz, Korth and Sudarshan25Database System Concepts Roles Relationships can be labeled with additional descriptive information called roles.  Roles are optional, and used to clarify a relationship.  Particularly helpful when the entities in a relationship are not distinct e.g., prerequisite course for a give course The roles manager and worker specify how employee entities interact via the works-for relationship.

26 ©Silberschatz, Korth and Sudarshan26Database System Concepts Keys A super key of an entity set is a set of one or more attributes that uniquely identify each entity in the entity set. A candidate key of an entity set is a minimal super key  customer-id is candidate key of customer  account-number is candidate key of account Although several candidate keys may exist, one of the candidate keys is selected to be the primary key.  All others are referred to as secondary keys Later we will also discuss foreign keys and search keys.

27 ©Silberschatz, Korth and Sudarshan27Database System Concepts Other Examples of Keys Student = (SS#, Name, Date-of-Birth, Status) SS#- Super key and candidate key SS#, Name, DOB- Super key, but not candidate key SS#, Status- Super key, but not candidate key Name- Neither Payment = (Payment#, Loan#, Date-Made, ID#) ID#- Super key, and candidate key Payment#, Loan#- Super key, and candidate key ID#, Date-Made- Super key, but not candidate key Date-Made- Neither ID# is frequently referred to ask a pseudo-key.

28 ©Silberschatz, Korth and Sudarshan28Database System Concepts Keys in an ER Diagram A primary key of an entity set is specified in an ER diagram by underlining the key attributes.

29 ©Silberschatz, Korth and Sudarshan29Database System Concepts Keys for Relationship Sets The combination of primary keys of the participating entity sets forms a super key of a relationship set.  (customer-id, account-number) is the super key of depositor  Question: why primary and super in the above? The mapping cardinality of a relationship set will determine, in part, what the candidate keys of the relationship set are.

30 ©Silberschatz, Korth and Sudarshan30Database System Concepts Design Options Frequently there are many ways to model a given situation. Use of an entity vs. an attribute:  Is Telephone-Number an attribute or an entity?  Choice mainly depends on the structure of the enterprise being modeled, and on the semantics/meaning associated with the attribute in question. Use of an entity vs. a relationship:  Is Loan an entity or a relationship?  Possible guideline - use a relationship to describe an action between entities Binary versus n-ary relationships:  e.g., Family, as opposed to mother, father, brother, sister Placement of relationship attributes

31 Lets Try an Example... 7.1 Construct an ER diagram for a car insurance company whose customers own one or more cars each. Each car has associated with it zero to any number of recorded accidents.

32 ©Silberschatz, Korth and Sudarshan32Database System Concepts Weak Entity Sets An entity set that has a primary key is referred to as a strong entity set. An entity set that does not have a primary key is referred to as a weak entity set. A weak entity set is typically associated with an identifying entity set (which is usually strong) via a total, one-to-many relationship.  e.g., course  section (sec_id,semester, year)  car  accident (date, cost)

33 ©Silberschatz, Korth and Sudarshan33Database System Concepts Weak Entity Sets, Cont. The discriminator (or partial key) of a weak entity set is the set of attributes that distinguishes among all entities of the weak entity set associated with one identifying entity. The primary key of a weak entity set has two parts:  The primary key of the associated identifying entity set  The weak entity set’s discriminator

34 ©Silberschatz, Korth and Sudarshan34Database System Concepts Weak Entity Sets (Cont.) A weak entity set is represented by double rectangles. The discriminator is underlined with a dashed line. Primary key for payment is (loan-number, payment-number)

35 ©Silberschatz, Korth and Sudarshan35Database System Concepts Weak Entity Sets (Cont.) The primary key of the strong entity set is not explicitly specified in the weak entity set, since it is implicit in the identifying relationship. If loan-number were explicitly specified:  Payment would be a strong entity set  The relationship between payment and loan would not be as clear

36 ©Silberschatz, Korth and Sudarshan36Database System Concepts More Weak Entity Set Examples In a university, a course is a strong entity and a course-offering can be modeled as a weak entity. Course = (course-number, name, description) Course-offering = (semester, section-number, instructor) The discriminator of course-offering would be semester (including year) and section-number (if there is more than one section). If course-offering were modeled as a strong entity then it would have course-number as an attribute.  The relationship with course would be implicit in the course-number attribute.

37 ©Silberschatz, Korth and Sudarshan37Database System Concepts Specialization and Generalization Example

38 ©Silberschatz, Korth and Sudarshan38Database System Concepts Specialization Top-down design process; we designate sub-groupings within an entity set that are distinctive from other entities in the set. The lower-level entity sets may:  Have attributes that do not apply to the higher-level entity set.  Participate in relationships that do not apply to the higher-level entity set. A lower-level entity set is said to inherit all the attributes and relationships from the higher-level entity set to which it is linked. Depicted by a triangle component labeled ISA, e.g. customer “is a” person.

39 ©Silberschatz, Korth and Sudarshan39Database System Concepts Generalization Bottom-up design process: combine a number of entity sets that share the same features into a higher-level entity set. The terms specialization and generalization are used interchangeably, for the obvious reasons. The ISA relationship also referred to as a superclass-subclass relationship.

40 ©Silberschatz, Korth and Sudarshan40Database System Concepts Specialization and Generalization, Cont. A specialization/generalization relationship can be:  disjoint  overlapping  total  Partial The above could be noted in any number of ways on the diagram, e.g., by writing “disjoint” next to the relationship. Multiple specializations of an entity set are possible:  permanent-employee vs. temporary-employee  In addition to officer vs. secretary vs. teller Each particular employee would be a member of:  one of permanent-employee or temporary-employee, and  one of officer, secretary, or teller

41 ©Silberschatz, Korth and Sudarshan41Database System Concepts E-R Diagram for a Banking Enterprise

42 ©Silberschatz, Korth and Sudarshan42Database System Concepts Alternative E-R Notations

43 ©Silberschatz, Korth and Sudarshan43Database System Concepts UML UML: Unified Modeling Language UML has many components to graphically model different aspects of an entire software system UML Class Diagrams correspond to E-R Diagram, but several differences.  Focus is on software, e.g., UML includes methods  Keys are not included

44 ©Silberschatz, Korth and Sudarshan44Database System Concepts Reduction of an E-R Schema to Tables An E-R diagram can be converted to a collection of tables in a relational database.  Each entity set is converted to its’ own table.  Each relationship can be (but does not have to be) converted to its’ own table. Each table has a number of columns, which generally corresponds to the attributes in the corresponding entity or relationship set.

45 ©Silberschatz, Korth and Sudarshan45Database System Concepts Representing Entity Sets as Tables A strong entity set reduces to a table with the same attributes.

46 ©Silberschatz, Korth and Sudarshan46Database System Concepts Composite and Multi-valued Attributes Composite attributes are broken up:  Example: attribute name with components first-name and last-name becomes two attributes in the corresponding table: name.first-name and name.last-name. A multi-valued attribute M of entity E is represented by a new table with the following attributes:  The primary key of E  An attribute corresponding to multi-valued attribute M Example: Entity Set: employee with attributes id#, name, phone#, dependents Tables: employee (id#, name, phone#) dependent (id#, dname)

47 ©Silberschatz, Korth and Sudarshan47Database System Concepts Representing Weak Entity Sets A weak entity set becomes a table that includes a column for the primary key of the identifying strong entity set.

48 ©Silberschatz, Korth and Sudarshan48Database System Concepts Representing Relationship Sets as Tables A many-to-many relationship set is represented as a table with columns for the primary keys of the two participating entity sets, and any descriptive attributes of the relationship set. Example: the borrower relationship set:

49 ©Silberschatz, Korth and Sudarshan49Database System Concepts Representing Relationship Sets as Tables, cont. A many-to-one relationship set can be represented just like a many-to- many relationship. Technically this is not necessary, and in some cases it does not result in a good design. Example: Entity Sets: account with attributes account-number, balance branch with attributes branch-name, branch-city, assets Relationship Set (total, many-to-one from account to branch): account-branch with attributes account-number, branch-name

50 ©Silberschatz, Korth and Sudarshan50Database System Concepts Representing Relationship Sets as Tables, cont. The preceding could be converted to tables directly, or as follows: account (account-number, balance, branch-name) branch (branch-name, branch-city, assets) Since the above relationship is total, a distinct table for the relationship set is probably not necessary.  Eliminating an unnecessary table is frequently considered…cool On the other hand, suppose:  The relationship is partial, and most accounts don’t have branches  The account table is sorted by balance  Queries that look up the branch-name for a given account # are common.

51 ©Silberschatz, Korth and Sudarshan51Database System Concepts Representing Relationship Sets as Tables, cont. For one-to-one relationship sets, the extra attribute can be added to either of the tables corresponding to the two entity sets. Note that either of the above could introduce null values if the relationship is not total.

52 ©Silberschatz, Korth and Sudarshan52Database System Concepts Representing Specialization as Tables Note: This discussion assumes a 2-level inheritance hierarchy.  Exercise: Generalize it to an arbitrarily deep hierarchy. Method 1:  Form a table for the higher level entity set.  Form a table for each lower level entity set, including the primary key of the higher level entity set and local attributes. table attributes person name, street, city customer name, credit-rating employee name, salary  One Drawback: getting information about specific entities requires accessing two tables

53 ©Silberschatz, Korth and Sudarshan53Database System Concepts Representing Specialization as Tables cont. Method 2:  Form a table for each entity set with all local and inherited attributes table table attributes personname, street, city customername, street, city, credit-rating employee name, street, city, salary This method can be redundant:  Some duplication for persons who are both customers and employees.  If specialization is total, the table for the generalized entity is redundant. May still be needed for foreign key constraints. For convenience, the generalized entity can an be defined as a “view” relation containing union of specialization tables.

54 ©Silberschatz, Korth and Sudarshan54Database System Concepts Representing Specialization as Tables Method 3: (not presented in the book)  Form one table for the higher level entity set  This table has one column for every attribute in every subclass. table attributes personname, street, city, credit-rating, salary  Optionally, include a type attribute that indicates which subclass the stored entity belongs to. One Drawback: Contains multiple nullable attributes. Sometimes referred to as a “junk drawer”

55 ©Silberschatz, Korth and Sudarshan55Database System Concepts Constraints on Generalization Condition-defined: student_type=graduate User-defined: manual placement Disjointness constraint Overlapping generalizations Completeness constraint (does each high level entity belong to some low level one?):  total generalization/specialization  Partial generalization/specialization

56 End of Chapter 7

57 ©Silberschatz, Korth and Sudarshan57Database System Concepts E-R Diagram with a Ternary Relationship

58 ©Silberschatz, Korth and Sudarshan58Database System Concepts Cardinality Constraints on Ternary Relationship Sets We allow at most one arrow out of a ternary (or greater degree) relationship to indicate a cardinality constraint  An arrow from works-on to job indicates each employee works on at most one job at any branch. If there is more than one arrow, there are two ways of defining the meaning.  E.g a ternary relationship R between A, B and C with arrows to B and C could mean  1. each A entity is associated with a unique entity from B and C or  2. each pair of entities from (A, B) is associated with a unique C entity, and each pair (A, C) is associated with a unique B  Each alternative has been used in different formalisms  To avoid confusion we outlaw more than one arrow

59 ©Silberschatz, Korth and Sudarshan59Database System Concepts Binary Vs. Non-Binary Relationships Some relationships that appear to be non-binary may be better represented using binary relationships  E.g. A ternary relationship parents, relating a child to his/her father and mother, is best replaced by two binary relationships, father and mother Using two binary relationships allows partial information (e.g. only mother being know)  But there are some relationships that are naturally non-binary E.g. works-on

60 ©Silberschatz, Korth and Sudarshan60Database System Concepts Converting Non-Binary Relationships to Binary Form (Hidden Variable Encoding) In general, any non-binary relationship can be represented using binary relationships by creating an artificial entity set.  Replace R between entity sets A, B and C by an entity set E, and three relationship sets: 1. R A, relating E and A 2.R B, relating E and B 3. R C, relating E and C  Create a special identifying attribute for E  Add any attributes of R to E  For each relationship (a i, b i, c i ) in R, create 1. a new entity e i in the entity set E 2. add (e i, a i ) to R A 3. add (e i, b i ) to R B 4. add (e i, c i ) to R C

61 ©Silberschatz, Korth and Sudarshan61Database System Concepts Converting Non-Binary Relationships, Cont. Also need to translate constraints  Translating all constraints may not be possible  There may be instances in the translated schema that cannot correspond to any instance of R Exercise: add constraints to the relationships R A, R B and R C to ensure that a newly created entity corresponds to exactly one entity in each of entity sets A, B and C  We can avoid creating an identifying attribute by making E a weak entity set (described shortly) identified by the three relationship sets

62 ©Silberschatz, Korth and Sudarshan62Database System Concepts E-R Diagram for Exercise 7.20 a)List entity sets and primary keys b)model addition of Blue-Ray discs and downloadable video. Same item may be available in different formats with different prices. c)Use generalization to model the case where a shopping basket may contain any combination of books, blue-ray discs and downloadable video

63 ©Silberschatz, Korth and Sudarshan63Database System Concepts E-R Diagram for Exercise 7.6 a)Show a simple instance of E, A, B, C, Ra, Rb, Rc that cannot correspond to any instance of A, B, C, and R. b)Modify the E-R diagram (b) to introduce constraints that will guarantee that any instance of E, A, B, C, Ra, Rb, Rc that satisfies the constraints will correspond to an instance of A, B, C, and R c)Modify the translation above to handle total participation constraints on the ternary relationship d)Show how to treat E as a weak entity (without a primary key)

64 ©Silberschatz, Korth and Sudarshan64Database System Concepts E-R Diagram for Exercise 2.18 The figure shows a lattice structure of generalization and specialization. For entity sets A, B, C, explain how attributes are inherited from higher-level entity sets X and Y. Discuss how to handle a case where an attribute X has the same name as some attribute of Y.

65 ©Silberschatz, Korth and Sudarshan65Database System Concepts E-R Diagram for Exercise 7.24 Discuss the merits of representations (b) and (c) to represent (a)

66 ©Silberschatz, Korth and Sudarshan66Database System Concepts Existence Dependencies If the existence of entity x depends on the existence of entity y, then x is said to be existence dependent on y.  y is a dominant entity (in example below, loan)  x is a subordinate entity (in example below, payment) loan-payment payment loan If a loan entity is deleted, then all its associated payment entities must be deleted also.

67 ©Silberschatz, Korth and Sudarshan67Database System Concepts Aggregation Consider the ternary relationship works-on, which we saw earlier Suppose we want to record managers for tasks performed by an employee at a branch

68 ©Silberschatz, Korth and Sudarshan68Database System Concepts Aggregation (Cont.) Relationship sets works-on and manages represent overlapping information  Every manages relationship corresponds to a works-on relationship  However, some works-on relationships may not correspond to any manages relationships So we can’t discard the works-on relationship Eliminate this redundancy via aggregation  Treat relationship as an abstract entity  Allows relationships between relationships  Abstraction of relationship into new entity

69 ©Silberschatz, Korth and Sudarshan69Database System Concepts Aggregation (Cont.) Without introducing redundancy, the following diagram represents:  An employee works on a particular job at a particular branch  An employee, branch, job combination may (or may not) have an associated manager

70 ©Silberschatz, Korth and Sudarshan70Database System Concepts E-R Diagram With Aggregation

71 ©Silberschatz, Korth and Sudarshan71Database System Concepts Relations Corresponding to Aggregation To represent aggregation, create a table containing  The primary key of the aggregated relationship,  The primary key of the associated entity set  Any descriptive attributes  Solution II: create a single table and use null values for manager if there is none

72 ©Silberschatz, Korth and Sudarshan72Database System Concepts Relations Corresponding to Aggregation (Cont.) Example: to represent aggregation manages between relationship works- on and entity set manager, create a table: manages(employee-id, branch-name, job, manager-name)  Note: manager-name may be null.  If this is acceptable, then table works-on is not required.


Download ppt "©Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction."

Similar presentations


Ads by Google