Mapping ER model to Relational Schema
Or
          Relational DB design using ER to Relational Mapping
Example: Company DB that we have considered for ER model
ER model for Company DB:
Dr. P. Gayathri, Professor, SCOPE                               Page 1
ER to Relational Mapping Algorithm:
       Step 1: Mapping of Regular (strong) Entity types
       Step 2: Mapping of Weak Entity types
       Step 3: Mapping of binary 1:1 Relationship 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
Step 1: Mapping of Regular (strong) Entity types:
For each strong entity type E in the ER schema,
   1. Create a relation R that includes all simple attributes of E
   2. If attribute is composite, then include only simple component
       attributes.
   3. Choose one of the key attributes of E as primary key for R
   4. If the chosen key of E is a composite, then set of simple attributes
       that forms it will together form the primary key of R
The result after this step is shown below:
Dr. P. Gayathri, Professor, SCOPE                                    Page 2
Step 2: Mapping of Weak Entity types:
For each weak entity type W in the ER schema with owner entity type E,
   1. Create a relation R that includes all simple attributes of W
   2. If attribute is composite, then include only simple component
       attributes.
   3. Include the primary key of owner entity type E as foreign key
       attribute of R
   4. The primary key of R is the combination of primary key(s) of
       owner(s) and partial keys of weak entity type W, if any.
The result after this step is shown below:
Note: we have renamed the primary key of owner entity type ssn into
Essn. But it is not necessary to rename.
Step 3: Mapping of binary 1:1 Relationship types:
For each binary 1:1 relationship type R in the ER schema, identify the
relations S and T
Foreign key approach – most useful and should be followed
           a. Choose one relation as S (better to choose entity type with
               total participation in R as S)
Dr. P. Gayathri, Professor, SCOPE                                    Page 3
           b. Include primary key of T as foreign key in S
           c. Include simple attributes (or simple components of composite
               attributes) of the 1:1 relationship type as attributes of S
In our example ER schema, 1:1 relationship is ‘manages’
       S – Department; T – Employee
The result after this step is shown below:
Step 4: Mapping of binary 1:N Relationship types:
For each binary 1:N relationship type R,
   1. Identify relation S (S relation will be the entity type at the N-side
       of the relationship type)
   2. Include primary key of T as foreign key in S
   3. Include simple attributes (or simple components of composite
       attributes) of the 1:N relationship type as attributes of S
In our example ER schema, 1:N relationships are ‘works_for’,
‘supervision’, ‘controls’
Works_for: S – Employee; T – Department
Supervision: S – Employee; T – Employee
Controls: S – Project; T – Department
The result after this step is shown below:
Dr. P. Gayathri, Professor, SCOPE                                            Page 4
Step 5: Mapping of binary M:N Relationship types:
For each binary relation of M:N relationship type,
   1. Create new relation S
   2. Include primary keys of participating relations as foreign key in S
   3. Primary key combination of participating relations form the
       primary key of S
   4. Include any simple attributes or simple components of composite
       attributes of M:N relationship type as attributes of S
In our example ER schema, M:N relationship is ‘works_on’ between
project and employee.
The result after this step is shown below:
Step 6: Mapping of multivalued attributes:
For each multivalued attribute A,
   1. Create new relation R
Dr. P. Gayathri, Professor, SCOPE                                    Page 5
   2. R contains an attribute corresponding to A and the primary key
       attribute K of the relation or relationship type that has A as
       attribute
   3. K becomes the foreign key
   4. Primary key of R is the combination of A and K
In our example ER schema, multivalued attribute is locations in
Department relation.
The result after this step is shown below:
Dr. P. Gayathri, Professor, SCOPE                               Page 6
Company relational DB schema obtained with step 1 through 6:
Note:
    There is no n-ary relationship type in our example.
    Refer the above figure for foreign key reference representation.
Step 7: Mapping of N-ary Relationship types:
For each n-ary relationship type R, where n>2,
   1. Create new relation S
Dr. P. Gayathri, Professor, SCOPE                                   Page 7
   2. Include the primary key of relations that represents the
       participating entity types as foreign key
   3. Include any simple attributes or simple components of composite
       attributes of N-ary relationship type as attributes of S
   4. Combination of all foreign keys forms the primary key of S
Example:
Note:
    If cardinality constraint of any of the entity type participating on R
       is 1, then primary key of S should not include the foreign key
       attribute that references the corresponding relation
Dr. P. Gayathri, Professor, SCOPE                                     Page 8