0% found this document useful (0 votes)
34 views26 pages

2) E-R Model To Relational Model

Uploaded by

Vishnupriya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views26 pages

2) E-R Model To Relational Model

Uploaded by

Vishnupriya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

E-R Model to Relational Model

E-R Model to Relational Model


• Entity Sets to Tables
• Main Idea
• Each entity set maps to a new table
• Each attribute maps to a new table column
• Each relationship set maps to either new table
columns or to a new table
E-R Model to Relational Model
• Entity Sets to Tables
• (Representing Strong Entity Sets)
• Entity set E with attributes a1,..,an translates to
table E with attributes a1,a2..an
• Primary key of entity set
• 
• primary key of table
E-R Model to Relational Model
• example

Students
mn name age email
       
       
E-R Model to Relational Model
• Weak entity set E translates to table E
• A weak entity can be identified uniquely only by
considering the primary key of another (owner)
entity.
• – Owner entity set and weak entity set must
participate in a one-to-many relationship set (1
owner, many weak entities).
• – When the owner entity is deleted, all owned
weak entities must also be deleted.
E-R Model to Relational Model
• Weak entity set E translates to table E
• CREATE TABLE Policy (
• pname CHAR(20),
• age INTEGER,
• cost NUMBER(10,2),
• ssn CHAR(11),
• PRIMARY KEY (pname, ssn),
• FOREIGN KEY (ssn) REFERENCES Employees,
• ON DELETE CASCADE)

Policy
pname age cost ssn
E-R Model to Relational Model
• Relationship Sets to Tables
• Columns of table R should include
• Attributes of the relationship set
• Primary key attributes of each component entity
set
E-R Model to Relational Model
• Relationship Sets (without Constraints) to Tables
• Create a table for the relationship set.
• Add all primary keys of the participating entity
sets as fields of the table.
• Add fields of attribute in the relationship.
• Declare a primary key using all key fields from
the entity sets.
• Declare foreign key constraints for all these
fields from the entity sets.
E-R Model to Relational Model
• Relationship Sets (without Constraints) to Tables

Students Courses
mn name age email code name year
             

Takes
mn code mark
     
E-R Model to Relational Model
• Relationship Sets (without Constraints) to Tables
Students
Mn name age email
       

Courses
code name year
     

Takes
mn code mark
     
E-R Model to Relational Model
• Relationship Sets (with Constraints) to Tables
• Create a table for the relationship set.
• Add all primary keys of the participating entity sets as
fields of the table.
• Add a field for each attribute of the relationship.
• Declare a primary key using the key fields from the
source entity set only.
• Declare foreign key constraints for all the fields from
the source and target entity sets.
E-R Model to Relational Model
• Relationship Sets (with Constraints) to Tables

Students
DoS
mn name email Staff id

Directed By
mn Staff id
E-R Model to Relational Model
• Relationship Sets (with Constraints) to Tables

• create table Directed By (


• mn char(8),
• staff id char(8),
• primary key (mn),
• foreign key (mn) references Students,
• foreign key (staff id) references DoS );
• Note that this has captured the key constraint,
but not the participation constraint.
E-R Model to Relational Model
• Mapping relationship sets (with key constraints,
2nd method)
• Create a table for the source and target entity
sets as usual.
• • Add every primary key field of the target as a
field in the source.
• • Declare these fields as foreign keys.
E-R Model to Relational Model
• Mapping relationship sets (with key constraints, 2nd
method)

• create table Students (


• mn char(8),
• name char(20),
• age integer,
• email char(15), Directed
mn
By
Staff id name email
• staff id char(8),
• primary key (mn),
• foreign key (staff id) references DoS )
• Note that this has still not included the participation
constraint on
E-R Model to Relational Model
• Null values
• In SQL, a field can have the special value null
• A null value means that a field is either
unknown/missing/unavailable, or
undefined/makes no sense here.
• Some implementations do not allow the null
value to appear in primary key fields.
E-R Model to Relational Model
• Mapping relationship sets with
key+participation constraints
• Create a table for the source and target entity
sets as usual.
• Add every primary key field of the target as a
field in the source.
• Declare these fields as not null.
• Declare these fields as foreign keys.
E-R Model to Relational Model

• create table Students (


• mn char(8),
• name char(20),
Directed By
• age integer, mn Staff id name age email Staff id
• email char(15),
• staff id char(8) not null,
• primary key (mn),
• foreign key (staff id) references DoS )
E-R Model to Relational Model
• Mapping weak entity sets and identifying
relationships
• Create a table for the weak entity set.
• Make each attribute of the weak entity set a field
of the table.
• Add fields for the primary key attributes of the
identifying owner.
• Declare a foreign key constraint on these
identifying owner fields.
• Instruct the system to automatically delete any
tuples in the table for which there are no owners
E-R Model to Relational Model

• create table is_Located_In (


• number char(8),
• capacity integer,
• name char(20),
• primary key (number, name),
• foreign key (name) references Buildings
• on delete cascade )
E-R Model to Relational Model
• Translating class hierarchies
• Declare a table as usual for the superclass of the
hierarchy.
• For each subclass declare another table using
superclass’s primary key and the subclass’s extra
attributes.
• Declare the primary key from the superclass as
the primary key of the subclass, and with a
foreign key constraint.
E-R Model to Relational Model

• create table PT Students (


• mn char(8),
• pt frac integer,
• primary key (mn),
• foreign key (mn) references Students )
E-R Model to Relational Model
E-R Model to Relational Model
• Translating class hierarchies
• As in C++, or other PLs, attributes are inherited.
• When declare A ISA B, every A entity is also considered to be a B entity.
• Overlap constraints: Can Joe be an Hourly_Emps as well as a
Contract_Emps entity? (Allowed/disallowed)
• Covering constraints: Does every Employees entity also have to be
an Hourly_Emps or a Contract_Emps entity? (Yes/no)
E-R Model to Relational Model
• Translating class hierarchies
• General approach:
• – 3 relations: Employees, Hourly_Emps and Contract_Emps.
• • Hourly_Emps: Every employee is recorded in Employees.
• For hourly emps, extra info recorded in Hourly_Emps
(hourly_wages, hours_worked, ssn); must delete Hourly_Emps
tuple if referenced Employees tuple is deleted).
• • Queries involving all employees easy, those involving just
Hourly_Emps require a join to get some attributes.
• • Alternative: Just Hourly_Emps and Contract_Emps.
• – Hourly_Emps: ssn, name, lot, hourly_wages, hours_worked.
• – Each employee must be in one of these two subclasses.
E-R Model to Relational Model

You might also like