e-r model to r elational model

26
E-R Model to Relational Model

Upload: rhona

Post on 10-Jan-2016

40 views

Category:

Documents


0 download

DESCRIPTION

E-R Model to R elational Model. E-R Model to R elational 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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: E-R  Model to  R elational Model

E-R Model to Relational Model

Page 2: E-R  Model to  R elational 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

Page 3: E-R  Model to  R elational Model

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

Page 4: E-R  Model to  R elational Model

E-R Model to Relational Model

• example

Studentsmn name age email

Page 5: E-R  Model to  R elational Model

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.

Page 6: E-R  Model to  R elational Model

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

Page 7: E-R  Model to  R elational Model

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

Page 8: E-R  Model to  R elational Model

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.

Page 9: E-R  Model to  R elational Model

E-R Model to Relational Model

• Relationship Sets (without Constraints) to Tables

Studentsmn name age email

Coursescode name year

Takesmn code mark

Page 10: E-R  Model to  R elational Model

E-R Model to Relational Model

• Relationship Sets (without Constraints) to Tables

StudentsMn name age email

Coursescode name year

Takesmn code mark

Page 11: E-R  Model to  R elational Model

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.

Page 12: E-R  Model to  R elational Model

E-R Model to Relational Model

• Relationship Sets (with Constraints) to Tables

DoSStaff id

Directed Bymn Staff id

Students

mn name email

Page 13: E-R  Model to  R elational Model

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.

Page 14: E-R  Model to  R elational Model

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.

Page 15: E-R  Model to  R elational Model

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),• staff id char(8),• primary key (mn),• foreign key (staff id) references DoS )• Note that this has still not included the participation constraint

on

Directed Bymn Staff id name email

Page 16: E-R  Model to  R elational Model

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.

Page 17: E-R  Model to  R elational Model

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.

Page 18: E-R  Model to  R elational Model

E-R Model to Relational Model

• create table Students (• mn char(8),• name char(20),• age integer,• email char(15),• staff id char(8) not null,• primary key (mn),• foreign key (staff id) references DoS )

Directed Bymn Staff id name age email Staff id

Page 19: E-R  Model to  R elational Model

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

Page 20: E-R  Model to  R elational Model

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 )

Page 21: E-R  Model to  R elational Model

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.

Page 22: E-R  Model to  R elational Model

E-R Model to Relational Model

• create table PT Students (• mn char(8),• pt frac integer,• primary key (mn),• foreign key (mn) references Students )

Page 23: E-R  Model to  R elational Model

E-R Model to Relational Model

Page 24: E-R  Model to  R elational 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)

Page 25: E-R  Model to  R elational Model

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.

Page 26: E-R  Model to  R elational Model

E-R Model to Relational Model