Module 4 Overview of Database Design the ER

Module 4: Overview of Database Design -- the ER Model Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See www. db-book. com for conditions on re-use

Design Phases n The initial phase of database design is to characterize fully the data needs of the prospective database users. n Next, the designer chooses a data model and, by applying the concepts of the chosen data model, translates these requirements into a conceptual schema of the database. n A fully developed conceptual schema also indicates the functional requirements of the enterprise. In a “specification of functional requirements”, users describe the kinds of operations (or transactions) that will be performed on the data. Database System Concepts 4. 2 ©Silberschatz, Korth and Sudarshan

Design Phases (Cont. ) The process of moving from an abstract data model to the implementation of the database proceeds in two final design phases. n Logical Design – Deciding on the database schema. Database design requires that we find a “good” collection of relation schemas. l Business decision – What attributes should we record in the database? l Computer Science decision – What relation schemas should we have and how should the attributes be distributed among the various relation schemas? n Physical Design – Deciding on the physical layout of the database Database System Concepts 4. 3 ©Silberschatz, Korth and Sudarshan

Design Approaches n Entity Relationship Model (Chapter 7) l Models an enterprise as a collection of entities and relationships 4 Entity: a “thing” or “object” in the enterprise that is distinguishable from other objects – Described by a set of attributes 4 Relationship: l an association among several entities Represented diagrammatically by an entity-relationship diagram: n Normalization Theory (Chapter 8) l Formalize what designs are bad, and test for them Database System Concepts 4. 4 ©Silberschatz, Korth and Sudarshan

Entity Relationship Model Database System Concepts 4. 5 ©Silberschatz, Korth and Sudarshan

ER model -- Database Modeling n The ER data mode was developed to facilitate database design by allowing specification of an enterprise schema that represents the overall logical structure of a database. n The ER model is very useful in mapping the meanings and interactions of real-world enterprises onto a conceptual schema. Because of this usefulness, many database-design tools draw on concepts from the ER model. n The ER data model employs three basic concepts: l entity sets, l relationship sets, l attributes. n The ER model also has an associated diagrammatic representation, the ER diagram, which can express the overall logical structure of a database graphically. Database System Concepts 4. 6 ©Silberschatz, Korth and Sudarshan

Entity Sets n An entity is an object that exists and is distinguishable from other objects. l Example: specific person, company, event, plant n An entity set is a set of entities of the same type that share the same properties. l Example: set of all persons, companies, trees, holidays n An entity is represented by a set of attributes; i. e. , descriptive properties possessed by all members of an entity set. l Example: instructor = (ID, name, street, city, salary ) course= (course_id, title, credits) n A subset of the attributes form a primary key of the entity set; i. e. , uniquely identifiying each member of the set. Database System Concepts 4. 7 ©Silberschatz, Korth and Sudarshan

E-R Diagram of Entity Sets n Entities can be represented graphically as follows: • Rectangles represent entity sets. • Attributes listed inside entity rectangle • Underline indicates primary key attributes Database System Concepts 4. 8 ©Silberschatz, Korth and Sudarshan

Relationship Sets n A relationship is an association among several entities Example: 44553 (Peltier) student entity advisor relationship set 22222 (Einstein) instructor entity n A relationship set is a mathematical relation among n 2 entities, each taken from entity sets {(e 1, e 2, … en) | e 1 E 1, e 2 E 2, …, en En} where (e 1, e 2, …, en) is a relationship l Example: (44553, 22222) advisor Database System Concepts 4. 9 ©Silberschatz, Korth and Sudarshan

Relationship Set advisor Database System Concepts 4. 10 ©Silberschatz, Korth and Sudarshan

E-R Diagram corresponding to advisor n Diamonds represent relationship sets. Database System Concepts 4. 11 ©Silberschatz, Korth and Sudarshan

Roles n Entity sets of a relationship need not be distinct l Each occurrence of an entity set plays a “role” in the relationship n The labels “course_id” and “prereq_id” are called roles. Database System Concepts 4. 12 ©Silberschatz, Korth and Sudarshan

Relationship Sets with Attributes n An attribute can also be associated with a relationship set. n For instance, the advisor relationship set between entity sets instructor and student may have the attribute date which tracks when the student started being associated with the advisor Database System Concepts 4. 13 ©Silberschatz, Korth and Sudarshan

E-R diagram of Relationship Sets with Attributes Database System Concepts 4. 14 ©Silberschatz, Korth and Sudarshan

Degree of a Relationship Set n Binary relationship l involve two entity sets (or degree two). l most relationship sets in a database system are binary. n Relationships between more than two entity sets are rare. Most relationships are binary. (More on this later. ) 4 Example: students work on research projects under the guidance of an instructor. 4 relationship proj_guide is a ternary relationship between instructor, student, and project Database System Concepts 4. 15 ©Silberschatz, Korth and Sudarshan

Non-binary Relationship Sets n Most relationship sets are binary n There are occasions when it is more convenient to represent relationships as non-binary. n E-R Diagram with a Ternary Relationship Database System Concepts 4. 16 ©Silberschatz, Korth and Sudarshan

Complex Attributes n Attribute types: l Simple and composite attributes. l Single-valued and multivalued attributes 4 Example: l multivalued attribute: phone_numbers Derived attributes 4 Can be computed from other attributes 4 Example: age, given date_of_birth n Domain – the set of permitted values for each attribute Database System Concepts 4. 17 ©Silberschatz, Korth and Sudarshan

Composite Attributes Database System Concepts 4. 18 ©Silberschatz, Korth and Sudarshan

Notation to Express Entity with Complex Attributes Database System Concepts 4. 19 ©Silberschatz, Korth and Sudarshan

Mapping Cardinality Constraints n Express the number of entities to which another entity can be associated via a relationship set. n Most useful in describing binary relationship sets. n For a binary relationship set the mapping cardinality must be one of the following types: l One to one l One to many l Many to one l Many to many Database System Concepts 4. 20 ©Silberschatz, Korth and Sudarshan

Mapping Cardinalities One to many One to one Note: Some elements in A and B may not be mapped to any elements in the other set Database System Concepts 4. 21 ©Silberschatz, Korth and Sudarshan

Mapping Cardinalities Many to one Many to many Note: Some elements in A and B may not be mapped to any elements in the other set Database System Concepts 4. 22 ©Silberschatz, Korth and Sudarshan

E-R Diagrams and Cardinality Constraints n We express cardinality constraints by drawing either a directed line ( ), signifying “one, ” or an undirected line (—), signifying “many, ” between the relationship set and the entity set. n One-to-one relationship between an instructor and a student : A student is associated with at most one instructor via the relationship advisor l A student is associated with at most one department via stud_dept l Database System Concepts 4. 23 ©Silberschatz, Korth and Sudarshan

One-to-Many Relationship n one-to-many relationship between an instructor and a student l an instructor is associated with several (including 0) students via advisor l a student is associated with at most one instructor via advisor, Database System Concepts 4. 24 ©Silberschatz, Korth and Sudarshan

Many-to-One Relationships n In a many-to-one relationship between an instructor and a student, l an instructor is associated with at most one student via advisor, l and a student is associated with several (including 0) instructors via advisor Database System Concepts 4. 25 ©Silberschatz, Korth and Sudarshan

Many-to-Many Relationship n An instructor is associated with several (possibly 0) students via advisor n A student is associated with several (possibly 0) instructors via advisor Database System Concepts 4. 26 ©Silberschatz, Korth and Sudarshan

Total and Partial Participation n Total participation (indicated by double line): every entity in the entity set participates in at least one relationship in the relationship set participation of student in advisor relation is total 4 every student must have an associated instructor n Partial participation: some entities may not participate in any relationship in the relationship set l Example: participation of instructor in advisor is partial Database System Concepts 4. 27 ©Silberschatz, Korth and Sudarshan

Notation for Expressing More Complex Constraints n A line may have an associated minimum and maximum cardinality, shown in the form l. . h, where l is the minimum and h the maximum cardinality n A minimum value of 1 indicates total participation. n A maximum value of 1 indicates that the entity participates in at most one relationship n A maximum value of * indicates no limit. Instructor can advise 0 or more students. A student must have 1 advisor; cannot have multiple advisors Database System Concepts 4. 28 ©Silberschatz, Korth and Sudarshan

Primary Keys in the E-R Model n We must have a way to specify how entities within a given entity set and relationships within a given relationship set are distinguished. n For entity sets, the primary key is a set of attributes that uniquely identify each of the entities. . n For relationship sets, the combination of primary keys of the participating entity sets forms a super key of a relationship set. l If the relationship set has a set of attributes associated with it, then the set of attributes is also included to form a superkey. l If the attribute names of primary keys are not unique across entity sets, the attributes are renamed to distinguish them the name of the entity set combined with the name of the attribute would form a unique name. Database System Concepts 4. 29 ©Silberschatz, Korth and Sudarshan

Keys for Relationship Sets n Consider the advisor relationship set. l (s_id, i_id) is the super key of advisor l Note this means a pair of entity sets can have at most one relationship in a particular relationship set. 4 Example: if we wish to track multiple meeting dates between a student and her advisor, we cannot assume a relationship for each meeting. We can use a multivalued attribute though n Need to consider semantics of relationship set in selecting the primary key in case of more than one candidate key. Database System Concepts 4. 30 ©Silberschatz, Korth and Sudarshan

Keys for Relationship Sets (Cont. ) n We must consider the mapping cardinality of the relationship set when deciding what are the candidate keys. For example, consider the entity sets instructor and student, and the relationship set advisor, with attribute date. l If the relationship set is many-to-many, then the primary key of advisor consists of the union of the primary keys of instructor and student. l If the relationship is many-to-one from student to instructor, then the primary key of advisor is the primary key of student. l if the advisor relationship is many-to-one from instructor to student then the primary key of advisor is the primary key of instructor. l For one-to-one relationships either candidate key can be used as the primary key. Database System Concepts 4. 31 ©Silberschatz, Korth and Sudarshan

Non-binary Relationship Sets n Most relationship sets are binary n There are occasions when it is more convenient to represent relationships as non-binary. n E-R Diagram with a Ternary Relationship Database System Concepts 4. 32 ©Silberschatz, Korth and Sudarshan

Keys for non-binary Relationship Sets n If no cardinality constraints are present then the superkey formed as described earlier. n The choice of the primary key is more complicated if cardinality constraints are present. Database System Concepts 4. 33 ©Silberschatz, Korth and Sudarshan

Keys for non-binary Relationship Sets n We allow at most one arrow out of a ternary (or greater degree) relationship to indicate a cardinality constraint n An arrow from proj_guide to project indicates each student works on at most one project with a given instructor. n If there is more than one arrow, there are two ways of defining the meaning. l 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 l Each alternative has been used in different formalisms l To avoid confusion we outlaw more than one arrow Database System Concepts 4. 34 ©Silberschatz, Korth and Sudarshan

Entity Sets with no Primary Key n Consider an employee entity, which is uniquely identified by a SS# n Consider a dependent entity which identifies a spouse/child that is a dependent of an employee. n Clearly, dependent entities are related to employee entities. This relationship is denoted via a relationship set employee_ dependent. n The attributes of dependent are {name, age}. n The dependent entity set does not have a primary key - why? n We can add SS# of a dependent to the attributes of dependent but this is really not necessary. n This leads to a notion of a “weak” entity set. Database System Concepts 4. 35 ©Silberschatz, Korth and Sudarshan

Weak Entity Sets n An entity set that does not have a primary key is referred to as a weak entity set. n The existence of a weak entity set depends on the existence of a identifying entity set l It must relate to the identifying entity set via a total, one-to-many relationship set from the identifying to the weak entity set l Identifying relationship depicted using a double diamond n The discriminator (or partial key) of a weak entity set is the set of attributes that distinguishes among all the entities of a weak entity set. n The primary key of a weak entity set is formed by the primary key of the strong entity set on which the weak entity set is existence dependent, plus the weak entity set’s discriminator. Database System Concepts 4. 36 ©Silberschatz, Korth and Sudarshan

Weak Entity Set Example in University DB n In the university database, course is a strong entity and section is modeled as a weak entity n The discriminator of section is {sec_id, semester, year } n If we wanted to model section as a strong entity we would add course_id as an attribute. l Database System Concepts Then the relationship with course would be implicit in the course_id attribute 4. 37 ©Silberschatz, Korth and Sudarshan

Expressing Weak Entity Sets n In E-R diagrams, a weak entity set is depicted via a double rectangle. n We underline the discriminator of a weak entity set with a dashed line. n The relationship set connecting the weak entity set to the identifying strong entity set is depicted by a double diamond. n Primary key for section – (course_id, sec_id, semester, year) Database System Concepts 4. 38 ©Silberschatz, Korth and Sudarshan

Redundant Attributes n Suppose we have entity sets: l instructor, with attributes: ID, name, dept_name, salary l department, with attributes: dept_name, building, budget n We model the fact that each instructor has an associated department using a relationship set inst_dept n The attribute dept_name appears in both entity sets. Since it is the primary key for the entity set department, it replicates information present in the relationship and is therefore redundant in the entity set instructor and needs to be removed. n BUT: when converting back to tables, in some cases the attribute gets reintroduced, as we will see later. Database System Concepts 4. 39 ©Silberschatz, Korth and Sudarshan

E-R Diagram for a University Enterprise Database System Concepts 4. 40 ©Silberschatz, Korth and Sudarshan

Reduction to Relation Schemas Database System Concepts 4. 41 ©Silberschatz, Korth and Sudarshan

Reduction to Relation Schemas n Entity sets and relationship sets can be expressed uniformly as relation schemas that represent the contents of the database. n A database which conforms to an E-R diagram can be represented by a collection of schemas. n For each entity set and relationship set there is a unique schema that is assigned the name of the corresponding entity set or relationship set. n Each schema has a number of columns (generally corresponding to attributes), which have unique names. Database System Concepts 4. 42 ©Silberschatz, Korth and Sudarshan

Representing Strong Entity Sets n A strong entity set reduces to a schema with the same attributes l instructor (ID, name, salary ) l student(ID, name, tot_cred) Database System Concepts 4. 43 ©Silberschatz, Korth and Sudarshan

Representing Weak Entity Sets n A weak entity set becomes a schema that includes all the attributes of the weak entity set plus the primary key of the identifying strong entity set section ( course_id, sec_id, sem, year ) Database System Concepts 4. 44 ©Silberschatz, Korth and Sudarshan

Representing Relationship Sets n A many-to-many relationship set is represented as a schema with attributes for the primary keys of the two participating entity sets, and any descriptive attributes of the relationship set. n Example: schema for relationship set advisor = (s_id, i_id) Database System Concepts 4. 45 ©Silberschatz, Korth and Sudarshan

Representing Entity Sets with Composite Attributes n Composite attributes are flattened out by creating a separate attribute for each component attribute l Example: given entity set instructor with composite attribute name with component attributes first_name and last_name the schema corresponding to the entity set has two attributes name_first_name and name_last_name 4 Prefix omitted if there is no ambiguity (name_first_name could be first_name) n Ignoring multivalued attributes, extended instructor schema is l Database System Concepts instructor(ID, first_name, middle_initial, last_name, street_number, street_name, apt_number, city, state, zip, date_of_birth) 4. 46 ©Silberschatz, Korth and Sudarshan

Representing Entity Sets with Multivalued Attributes n A multivalued attribute M of an entity E is represented by a separate schema EM n Schema EM has attributes corresponding to the primary key of E and an attribute corresponding to multivalued attribute M n Example: Multivalued attribute phone_number of instructor is represented by a schema: inst_phone= ( ID, phone_number) n Each value of the multivalued attribute maps to a separate tuple of the relation on schema EM l For example, an instructor entity with primary key 22222 and phone numbers 456 -7890 and 123 -4567 maps to two tuples: (22222, 456 -7890) (22222, 123 -4567) Database System Concepts 4. 47 ©Silberschatz, Korth and Sudarshan

Redundancy of Schemas n Many-to-one and one-to-many relationship sets that are total on the many-side can be represented by adding an extra attribute to the “many” side, containing the primary key of the “one” side n Example: Instead of creating a schema for relationship set inst_dept, add an attribute dept_name to the schema arising from entity set instructor n Thus, we can eliminate inst_dept and stud-dept Database System Concepts 4. 48 ©Silberschatz, Korth and Sudarshan

Redundancy of Schemas (Cont. ) n For one-to-one relationship sets, either side can be chosen to act as the “many” side l That is, an extra attribute can be added to either of the tables corresponding to the two entity sets n If participation is partial on the “many” side, replacing a schema by an extra attribute in the schema corresponding to the “many” side could result in null values Database System Concepts 4. 49 ©Silberschatz, Korth and Sudarshan

Redundancy of Schemas (Cont. ) n The schema corresponding to a relationship set linking a weak entity set to its identifying strong entity set is redundant. n Example: The section schema already contains the attributes that would appear in the sec_course schema l Thus, we can eliminate sec_course Database System Concepts 4. 50 ©Silberschatz, Korth and Sudarshan

End of Module 4 Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See www. db-book. com for conditions on re-use
- Slides: 51