CS340 · Annotated SlidesChapter 5 — ER-to-Relational Mapping and DDL
Chapter 5 · Annotated slides

ER-to-Relational Mapping and DDL

The 7-step ER-to-relational mapping algorithm (strong and weak entities, 1:1, 1:N and M:N relationships, multivalued attributes, n-ary relationships), basic DDL (CREATE TABLE, keys, foreign keys, data types, constraints), and step 8 options for mapping specialization.

Slide text Study notes Highlight: key words Tips, tricks & traps Seen again elsewhere How it can come up

ER-to-Relational Mapping and DDL

CS340 · Chapter 5
1

LEARNING GOALS

CLO1: Define the "database”, its architecture, the main concepts and characteristics of databases, as well as concepts related to the design, and implementation of a relational databases. (Knowledge & Understanding) CLO3: Create a logical database design based on the conceptual ER model.

(Skills) Chapter objectives:

Learn ER/EER mapping to relational model. This includes mapping all the following:

 Regular/weak Entity Types  Binary 1:1, 1:N, M:N relationship types  Multivalued attributes.

 N-ary relationship types.

 Specialization/Generalization and union types.

2
Exam weight Low

    GOALS DURING MAPPING

     Preserve all information (that includes entities, relationships and all

    attributes)

     Maintain the constraints as much as possible (Relational Model

    cannot preserve all constraints- e.g., max cardinality ratio such as 1:10 in ER; exhaustive classification into subtypes, e.g., STUDENTS are specialized into Domestic and Foreign).

     Minimize null values.

    3
    Exam weight Low
      Slide 4: Slide 4
      Exam weight Low
        Slide 5: Slide 5
        Exam weight Low

          ER-TO-RELATIONAL MAPPING ALGORITHM

          ER-to-Relational Mapping Algorithm 

          Step 1: Mapping of Regular Entity Types

          Step 2: Mapping of Weak Entity Types

          Step 3: Mapping of Binary 1:1 Relation Types

          Step 4: Mapping of Binary 1:N Relationship Types.

          Step 5: Mapping of Binary M:N Relationship Types.

          Step 6: Mapping of Multivalued attributes.

          Step 7: Mapping of N-ary Relationship Types.

          6
          Exam weight Low
            Slide 7: ER-TO-RELATIONAL MAPPING ALGORITHM
            Exam weight Low
            1. Step 1: each strong entity → a relation with its simple attributes; composite attributes → their components; pick a primary key.
            Slide 8: strong
            Exam weight Low
              Slide 9: REGULAR ENTITY TYPES WITH COMPOSITE ATTRIBUTES
              Exam weight Low
                Slide 10: REGULAR ENTITY TYPES (EMPLOYEE)
                Exam weight Low
                  Slide 11: REGULAR ENTITY TYPES (DEPARTMENT)
                  Exam weight Low
                    Slide 12: REGULAR ENTITY TYPES (PROJECT)
                    Exam weight Low
                      Slide 13: ER-TO-RELATIONAL MAPPING ALGORITHM 595
                      Exam weight Low
                        Slide 14: ER-TO-RELATIONAL MAPPING ALGORITHM (CONTD.)
                        Exam weight Low
                          ★ Past exam
                          Slide 15: WEAK ENTITY TYPES
                          Exam weight High
                          1. Step 2 weak entity: relation with its attributes + the owner’s primary key as a foreign key; PK = owner PK + partial key.
                          🎯 How this can come up · 1 question
                          MCQ

                          The primary key of a relation mapped from a weak entity is:

                          Slide 16: DDL BASICS
                          Exam weight Low
                          1. CREATE TABLE name (col type constraints, …, PRIMARY KEY (…), FOREIGN KEY (…) REFERENCES other(…));

                          DDL BASICS (PRIMARY KEY)

                          Example:

                          CREATE TABLE CUSTOMER(

                          Customer_id number(4) NOT NULL, CustomerName varchar(30) NOT NULL, CustomerAddress varchar(50), PRIMARY KEY (Customer_id)

                          );

                          17
                          Exam weight Low

                            DDL BASICS (COMPOSITE KEYS AND NAMING CONSTRAINTS)

                            CREATE TABLE Persons ( ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int, CONSTRAINT PK_Person PRIMARY KEY (ID,LastName) );

                            18
                            Exam weight Low

                              DDL BASICS (FOREIGN KEYS)

                              CREATE TABLE Orders ( OrderID int NOT NULL, OrderNumber int NOT NULL, PersonID int,

                              PRIMARY KEY (OrderID), FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)

                              ); OR you can name the constraint:

                              CREATE TABLE Orders ( OrderID int NOT NULL, OrderNumber int NOT NULL, PersonID int,

                              PRIMARY KEY (OrderID), CONSTRAINT FK_PersonOrder FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)

                              );

                              19
                              Exam weight High
                              1. FOREIGN KEY (Dno) REFERENCES DEPARTMENT(Dnumber) ON DELETE CASCADE / SET NULL.
                              🎯 How this can come up · 1 question
                              MCQ

                              Which defines a foreign key?

                              Slide 20: ACTIVITY
                              Exam weight Low

                                ACTIVITY (SOLUTION)

                                CREATE TABLE Employee ( ID int NOT NULL, EmployeeName varchar(255) NOT NULL, PRIMARY KEY (ID) ); CREATE TABLE DEPENDENT ( Name NOT NULL, EmployeeID int NOT NULL, DateOfBirth date, Gender varchar(1) DEFAULT ‘f’,

                                FOREIGN KEY (EmployeeID) REFERENCES Employee(ID) CONSTRAINT PK_Dependent PRIMARY KEY (EmployeeID,Name)

                                );

                                21
                                Exam weight Low

                                  A BIT MORE DDL

                                  ALTER command:

                                  ALTER TABLE name_of_table ADD column_name column_definition; Add column, change column, etc.

                                  DROP command:

                                  DROP TABLE EMPLOYEE; Drop table or drop database Other commands:

                                  TRUNCATE TABLE Table_Name; //clears table of all tuples RENAME TABLE Old_Table_Name TO New_Table_Name; //renames table

                                  22
                                  Exam weight Low
                                    Slide 23: SOME SQL DATA TYPES (MYSQL)
                                    Exam weight Low

                                      DDL CONSTRAINTS

                                      Attribute constraints:

                                      

                                      • NOT NULL the column must have a value 
                                      • UNIQUE the column value must be unique in the table 
                                      • CHECK the column value conforms to an arbitrary condition

                                      Foreign key conditions:

                                      • FOREIGN KEY(fk) REFERENCES refrenced_table(pk) ON DELETE CASCADE / ON UPDATE CASCADE
                                      • FOREIGN KEY(fk) REFERENCES refrenced_table(pk) ON DELETE SET NULL
                                      24
                                      Exam weight Low
                                      1. Constraints: NOT NULL, UNIQUE, DEFAULT, CHECK (e.g. CHECK (Salary > 0)).

                                      ER-TO-RELATIONAL MAPPING ALGORITHM (CONTD.)

                                      Step 3: Mapping of Binary 1:1 Relation Types

                                       For each binary 1:1 relationship type R in the ER

                                      schema, identify the relations S and T that correspond to the entity types participating in R.

                                      There are three possible approaches:

                                      1. Foreign Key (2 relations) approach
                                      2. Merged relation (1 relation) option
                                      3. Cross-reference or relationship relation (3 relations) option
                                      25
                                      Exam weight Low
                                        Slide 26: MAPPING OF BINARY 1:1 RELATIONSHIP TYPES
                                        Exam weight High
                                        1. 1:1: three choices. Foreign-key approach (best): put the PK of one side as FK in the side with TOTAL participation. Merged relation (both total). Relationship relation.
                                        🎯 How this can come up · 1 question
                                        MCQ

                                        For 1:1 MANAGES where DEPARTMENT participates totally, the FK goes in:

                                        Slide 27: 1:1 FOREIGN KEY APPROACH
                                        Exam weight Low
                                          Slide 28: 1:1 FOREIGN KEY APPROACH
                                          Exam weight Low

                                            1:1 FOREIGN KEY APPROACH

                                            What will happen if the primary key of S is included as a foreign key in T?

                                            In the previous example, the S relation is DEPARTMENT and T relation is EMPLOYEE. Thus, the primary key of S is Dept_managed will be the foreign key for the EMPLOYEE table. But it will have NULL values for employee tuples who do not manage a department. If only 10% of employee manage a department, then 90% of the foreign keys would be NULL in this case.

                                            Another possibility is to have foreign keys in both relations S and T redundantly, but this incurs a penalty for consistency

                                            29
                                            Exam weight Low

                                              MAPPING OF BINARY 1:1 RELATIONSHIP TYPES

                                              Merged relation option:

                                              Merging the two entity types and the relationship into a single relation. (Used when both participations are total)

                                              30
                                              Exam weight Low
                                                Slide 31: 1:1 MERGED RELATION APPROACH
                                                Exam weight Low

                                                  MAPPING OF BINARY 1:1 RELATIONSHIP TYPES

                                                  Cross-reference or relationship relation option: (Both participations are partial) Setting up a third relation R for the purpose of cross-referencing the primary keys of the two relations S and T representing the entity types.

                                                  The relationship R is called a relationship relation (or sometimes a lookup table), because each tuple in R represents a relationship instance that relates one tuple from S with one tuple of T.

                                                  The primary key of the relationship relation will be either one of the foreign keys that reference the participating entity relations.

                                                  This alternative is useful when few relationship instances (i.e. partial participation) exist to avoid NULL values in foreign keys.

                                                  32
                                                  Exam weight Low
                                                    Slide 33: 1:1 RELATIONSHIP RELATION APPROACH
                                                    Exam weight Low

                                                      ER-TO-RELATIONAL MAPPING ALGORITHM (CONTD.)

                                                      Step 4: Mapping of Binary 1:N Relationship Types.

                                                       For each regular binary 1:N relationship type R, identify the relation S that

                                                      represent the participating entity type at the N-side of the relationship type.

                                                       Include as foreign key in S the primary key of the relation T that

                                                      represents the other entity type participating in R.

                                                       Include any simple attributes of the 1:N relationship type as attributes of

                                                      S.

                                                      Example: 1:N relationship type: WORKS_FOR For WORKS_FOR we include the primary key DNUMBER of the DEPARTMENT relation as foreign key in the EMPLOYEE relation and call it DNO.

                                                      An alternative approach is to use a Relationship relation (cross referencing

                                                      34
                                                      Exam weight High
                                                      1. 1:N: put the PK of the "1" side as an FK in the "N" side (plus any relationship attributes).
                                                      Memory trick1:N → the FK goes on the MANY side.
                                                      🎯 How this can come up · 1 question
                                                      MCQ

                                                      EMPLOYEE (N) works for DEPARTMENT (1). Map it by:

                                                      Slide 35: 1:N FOREIGN KEY APPROACH
                                                      Exam weight Low
                                                        Slide 36: 1:N FOREIGN KEY APPROACH
                                                        Exam weight Low
                                                          Slide 37: ER-TO-RELATIONAL MAPPING ALGORITHM (CONTD.)
                                                          Exam weight High
                                                          1. M:N: a NEW relation with both PKs as FKs; its PK is the combination; relationship attributes go here.
                                                          🎯 How this can come up · 1 question
                                                          MCQ

                                                          WORKS_ON (M:N, attribute Hours) becomes:

                                                          Slide 38: N:M MAPPING TO RELATIONAL
                                                          Exam weight Low
                                                            Slide 39: ACTIVITY
                                                            Exam weight Low
                                                              Slide 40: ER-TO-RELATIONAL MAPPING ALGORITHM (CONTD.)
                                                              Exam weight Low
                                                              1. Multivalued attribute: new relation (attribute + owner PK), PK = both.
                                                              Slide 41: MAPPING MULTI-VALUED ATTRIBUTES
                                                              Exam weight Low

                                                                ACTIVITY

                                                                Map the locations attribute into a relation DEPT_LOCATIONS.

                                                                Include the primary key and foreign keys.

                                                                Write the DDL for this specific relation.

                                                                42
                                                                Exam weight Low

                                                                  ER-TO-RELATIONAL MAPPING ALGORITHM (CONTD.)

                                                                  Step 7: Mapping of N-ary Relationship Types.

                                                                  For each n-ary relationship type R, where n>2, create a new relationship S to represent R.

                                                                  Include as foreign key attributes in S the primary keys of the relations that represent the participating entity types.

                                                                  Also include any simple attributes of the n-ary relationship type (or simple components of composite attributes) as attributes of S.

                                                                  Example: The relationship type SUPPLY in the ER on the next slide.

                                                                  This can be mapped to the relation SUPPLY

                                                                  shown in the relational

                                                                  schema, whose primary key is the combination of the three foreign keys {SNAME, PARTNO, PROJNAME}

                                                                  43
                                                                  Exam weight Low
                                                                  1. n-ary relationship: new relation with all participating PKs as FKs.

                                                                  MAPPING OF N-ARY RELATIONSHIPS

                                                                  The primary key of S is usually a combination of all the foreign keys that reference the relations representing the participating entity types.

                                                                   the cardinality constraints on any of the entity types E

                                                                  If participating in R is 1, then the primary key of S should not include the foreign key attribute that references the relation E’ corresponding to E.

                                                                  44
                                                                  Exam weight Low
                                                                    Slide 45: MAPPING OF N-ARY RELATIONSHIPS
                                                                    Exam weight Low

                                                                      UNARY RELATIONSHIPS

                                                                      1:N unary relationships:

                                                                      1. With this type of relationship, ONE relation is created: one entity represents the entity type in the relationship consisting of PK and FK in the same relation. The FK is referenced to the PK of the same relation
                                                                      2. Any non key attribute of the relationship is included in the relation.

                                                                      N:M unary relationships:

                                                                      1. With this type of relationship, two relations are created: one to represent the entity type in the relationship and the other an associative relation to represent the M:N relationship.
                                                                      2. The primary key of the associative relation consists of two attributes. These attributes (which need not to have the same name) both take their values from the primary key of the other relation. Any non key attribute of the relationship is included in the associative relation
                                                                      46
                                                                      Exam weight Low
                                                                      1. Unary 1:N: FK to the same table (Super_ssn → EMPLOYEE.Ssn). Unary M:N: new relation with two FKs to the same table.
                                                                      Slide 47: 1:N UNARY RELATIONSHIPS
                                                                      Exam weight Low
                                                                        Slide 48: 1:N UNARY RELATIONSHIPS (PARTIAL BOTH SIDES)
                                                                        Exam weight Low
                                                                          Slide 49: M:N UNARY RELATIONSHIPS
                                                                          Exam weight Low
                                                                            Slide 50: 1:N UNARY RELATIONSHIPS (PARTIAL ONE SIDE)
                                                                            Exam weight Low
                                                                              Slide 51: SUMMARY OF MAPPING CONSTRUCTS AND CONSTRAINTS
                                                                              Exam weight Low

                                                                                ER-TO-RELATIONAL MAPPING ALGORITHM (CONTD.)

                                                                                Step8: Options for Mapping Specialization or Generalization.

                                                                                 Convert each specialization with m subclasses {S1, S2,....,Sm} and

                                                                                generalized superclass C, where the attributes of C are {k,a1,...an} and k is the (primary) key, into relational schemas using one of the four following options:

                                                                                 Option 8A: Multiple relations-Superclass and subclasses  Option 8B: Multiple relations-Subclass relations only  Option 8C: Single relation with one type attribute  Option 8D: Single relation with multiple type attributes

                                                                                52
                                                                                Exam weight High
                                                                                1. Step 8 specialization options: 8A superclass + subclass relations (any case); 8B subclass relations only (total, disjoint); 8C one relation + type attribute (disjoint); 8D one relation + Boolean flags (overlapping).
                                                                                🎯 How this can come up · 1 question
                                                                                MCQ

                                                                                Mapping option that uses ONE relation with a Boolean flag per subclass suits:

                                                                                MAPPING EER MODEL CONSTRUCTS TO RELATIONS

                                                                                Option 8A: Multiple relations-Superclass and subclasses Create a relation L for C with attributes Attrs(L) = {k,a1,...an} and PK(L) = k. Create a relation Li for each subclass Si, 1 < i < m, with the attributesAttrs(Li) = {k} U {attributes of Si} and PK(Li)=k.

                                                                                This option works for any specialization (total or partial, disjoint or over -lapping).

                                                                                Option 8B: Multiple relations-Subclass relations only Create a relation Li for each subclass Si, 1 < i < m, with the attributes Attr(Li) = {attributes of Si} U {k,a1...,an} and PK(Li) = k.

                                                                                This option only works for a specialization whose subclasses are total (every entity in the superclass must belong to (at least) one of the subclasses).

                                                                                53
                                                                                Exam weight Low
                                                                                  Slide 54: MAPPING EER MODEL CONSTRUCTS TO RELATIONS
                                                                                  Exam weight Low
                                                                                    Slide 55: EXAMPLE: EMPLOYEE AND JOB TYPE
                                                                                    Exam weight Low
                                                                                      Slide 56: OPTION 8A: MULTIPLE RELATIONS-SUPERCLASS AND SUBCLASSES
                                                                                      Exam weight Low

                                                                                        OPTION 8C: EXTRA ATTRIBUTES

                                                                                        Extra attributes

                                                                                        EMPLOYEE is a single relation, with a type attribute JobType that determines the type of the employee. The other attributes (TypingSpeed, Tgrade, and EngType) belong to the different employee types (SECRETARY, TECHNICIAN, and ENGINEER respectively).

                                                                                        OPTION 8C: EXTRA ATTRIBUTES
                                                                                        57
                                                                                        Exam weight Low
                                                                                          Slide 58: EXAMPLE: VEHICLE, CAR OR TRUCK
                                                                                          Exam weight Low
                                                                                            Slide 59: OPTION 8B: SUB-CLASSES ONLY
                                                                                            Exam weight Low
                                                                                              Slide 60: EXAMPLE: PART ENTITY
                                                                                              Exam weight Low

                                                                                                OPTION 8D: BOOLEAN FLAGS

                                                                                                The Boolean flags determine the type of PART. If Mflag is 1 and Pflag is 0, it is a MANUFACTURED_PART. If Mflag is 0 and Pflag is 1, it is a PURCHASED_PART. If both are 1, the part is both manufactured and purchased.

                                                                                                Boolean flags

                                                                                                OPTION 8D: BOOLEAN FLAGS
                                                                                                61
                                                                                                Exam weight Low

                                                                                                  DIFFERENT OPTIONS FOR MAPPING

                                                                                                  Next Slide: Options for mapping specialization or generalization.

                                                                                                  (a) Mapping the EER schema using option 8A.

                                                                                                  (b) Mapping the EER schema using option 8B.

                                                                                                  (c) Mapping the EER schema using option 8C.

                                                                                                  (d) Mapping EER schema using option 8D with Boolean type fields Mflag and Pflag.

                                                                                                  62
                                                                                                  Exam weight Low
                                                                                                    Slide 63: Slide 63
                                                                                                    Exam weight Low
                                                                                                      Slide 64: MAPPING OTHER EER MODEL CONSTRUCTS TO RELATIONS
                                                                                                      Exam weight Low
                                                                                                        Slide 65: Slide 65
                                                                                                        Exam weight Low
                                                                                                          Slide 66: RELATIONAL MAPPING
                                                                                                          Exam weight Low

                                                                                                            ER-TO-RELATIONAL MAPPING ALGORITHM (CONTD.)

                                                                                                            Step 9: Mapping of Union Types (Categories).

                                                                                                             For mapping a category whose defining superclass have

                                                                                                            different keys, it is customary to specify a new key attribute, called a surrogate key, when creating a relation to correspond to the category.

                                                                                                             In the example below we can create a relation OWNER to

                                                                                                            correspond to the OWNER category and include any attributes of the category in this relation. The primary key of the OWNER relation is the surrogate key, which we called OwnerID.

                                                                                                            67
                                                                                                            Exam weight Low
                                                                                                              Slide 68: Slide 68
                                                                                                              Exam weight Low
                                                                                                                Slide 69: Slide 69
                                                                                                                Exam weight Low
                                                                                                                  Slide 70: Slide 70
                                                                                                                  Exam weight Low
                                                                                                                    Slide 71: ACTIVIT Y
                                                                                                                    Exam weight Low

                                                                                                                      SOLUTION

                                                                                                                      Step 1: strong entities SHIP (Sname, Owner) SHIP_TYPE (Type, Tonnage, Hull) STATE/COUNTRY (Name, Continent) SEA/OCEAN/LAKE (Name) Step 2: weak entities SHIP_MOVEMENT (ShipName (FK), Date, time, latitude, longitude) PORT (SCName (FK), SOLName (FK), Pname) VISIT (Pname, SCName (FK) , Sname (FK) , StartDate, EndDate)

                                                                                                                      72
                                                                                                                      Exam weight Low
                                                                                                                        Slide 73: SOLUTION
                                                                                                                        Exam weight Low
                                                                                                                          Slide 74: FINAL SOLUTION
                                                                                                                          Exam weight Low
                                                                                                                            Slide 75: ACTIVIT Y
                                                                                                                            Exam weight Low

                                                                                                                              CHAPTER SUMMARY

                                                                                                                              ER-to-Relational Mapping Algorithm 

                                                                                                                              Step 1: Mapping of Regular Entity Types

                                                                                                                              Step 2: Mapping of Weak Entity Types

                                                                                                                              Step 3: Mapping of Binary 1:1 Relation Types

                                                                                                                              Step 4: Mapping of Binary 1:N Relationship Types.

                                                                                                                              Step 5: Mapping of Binary M:N Relationship Types.

                                                                                                                              Step 6: Mapping of Multivalued attributes.

                                                                                                                              Step 7: Mapping of N-ary Relationship Types.

                                                                                                                              Mapping EER Model Constructs to Relations 

                                                                                                                              Step 8: Options for Mapping Specialization or Generalization.

                                                                                                                              Step 9: Mapping of Union Types (Categories).

                                                                                                                              Some DDL basics sprinkled in. Next: Full fledged SQL!

                                                                                                                              76
                                                                                                                              Exam weight Low