2
Keys are special fields that serve two main purposes:◦ Primary keys are unique identifiers of the relation in
question. Examples include employee numbers, social security numbers, etc. This is how we can guarantee that all rows are unique
◦ Foreign keys are identifiers that enable a dependent relation (on the many side of a relationship) to refer to its parent relation (on the one side of the relationship)
Keys can be simple (a single field) or composite (more than one field)
Keys usually are used as indexes to speed up the response to user queries
Key Fields
3
Primary Key
Foreign Key (implements 1:N relationship between customer and order)
Combined, these are a composite primary key (uniquely identifies the order line)…individually they are foreign keys (implement M:N relationship between order and product)
4
Domain Constraints◦ Allowable values for an attribute.
Entity Integrity◦ No primary key attribute may be null. All primary
key fields MUST have data Action Assertions
◦ Business rules.
Integrity Constraints
5
Mapping Regular Entities to Relations 1. Simple attributes: E-R attributes map directly
onto the relation2. Composite attributes: Use only their simple,
component attributes 3. Multivalued Attribute - Becomes a separate
relation with a foreign key taken from the superior entity
Transforming EER Diagrams into Relations
6
(a) CUSTOMER entity type with simple attributes
Figure 5-8: Mapping a regular entity
(b) CUSTOMER relation
7
(a) CUSTOMER entity type with composite attribute
Mapping a composite attribute
(b) CUSTOMER relation with address detail
8
Figure 5-10: Mapping a multivalued attribute
1–to–many relationship between original entity and new relation
(a)
Multivalued attribute becomes a separate relation with foreign key
(b)
9
Mapping Weak Entities◦Becomes a separate relation with a foreign key taken from the superior entity
◦Primary key composed of: Partial identifier of weak entity Primary key of identifying relation
(strong entity)
Transforming EER Diagrams into Relations (cont.)
11
NOTE: the domain constraint for the foreign key should NOT allow null value if DEPENDENT is a weak entity
Foreign key
Composite primary key
12
Mapping Unary Relationships◦ One-to-Many - Recursive foreign key in the same
relation◦ Many-to-Many - Two relations: One for the entity type One for an associative relation in which
the primary key has two attributes, both taken from the primary key of the entity
Transforming EER Diagrams into Relations (cont.)
13
Mapping a unary 1:N relationship
(a) EMPLOYEE entity with Manages relationship
(b) EMPLOYEE relation with recursive foreign key
14
Figure 5-18: Mapping a unary M:N relationship
(a) Bill-of-materials relationships (M:N)
(b) ITEM and COMPONENT relations
Supertype:A sub grouping of the entities in an entity type which has attributes that are distinct from those in other sub groupings
Supertype: An generic entity type that has a relationship with one or more subtypes
Supertypes and Subtypes
Relationships at the supertype level indicate that all subtypes will participate in the relationship
The instances of a subtype may participate in a relationship unique to that subtype. In this situation, the relationship is shown at the subtype level
Relationships and Subtypes
Supertype/subtype relationships in a hospital
Both outpatients and resident patients are cared for by a responsible physician
Only resident patients are assigned to a bed
23
Mapping Supertype/subtype relationships to relations
These are implemented as one-to-one relationships
Generalization: The process of defining a more general entity type from a set of more specialized entity types. BOTTOM-UP
Specialization: The process of defining one or more subtypes of the supertype, and forming supertype/subtype relationships. TOP-DOWN
Generalization and Specialization
Example of generalizationThree entity types: CAR, TRUCK, and MOTORCYCLE
All these types of vehicles have common attributes
Completeness Constraints: Whether an instance of a supertype must also be a member of at least one subtype◦Total Specialization Rule: Yes (double line)
◦Partial Specialization Rule: No (single line)
Constraints in Supertype/ Completeness Constraint
Examples of completeness constraints
Total specialization rule
A patient must be either an outpatient or a resident patient
Disjointness Constraints: Whether an instance of a supertype may simultaneously be a member of two (or more) subtypes◦ Disjoint Rule: An instance of the supertype can be
only ONE of the subtypes◦ Overlap Rule: An instance of the supertype could
be more than one of the subtypes
Constraints in Supertype/ Disjointness constraint
Introducing a subtype discriminator (disjoint rule)
A simple attribute with different possible values indicating the subtype