Sie sind auf Seite 1von 30

ER to Relational Mapping

2002, National Centre for Software Technology, Bangalore.


ER to Relational 1999-2002, NCST 1

Translating the E-R Model

E-R model captures the database at the conceptual level - translating the model into the relational model (or any other model) yields the actual database.
a Data-Definition Language (DDL) is used to set up the actual database schema.

ER to Relational

1999-2002, NCST

One-to-One Mandatory

Every trainee has one and only one sponsor and every sponsor sponsors one and only one trainee.
EmpId ....... .... Sp_Id

trainee

Not Null!
is_sponsored_by

sponsor
ER to Relational

Sp_Id

....... ....

..........

1999-2002, NCST

One-to-One Optional

Every dept has one and only one manager; an employee can be the manager of at most one dept.

employee

Emp_Id ........

.....

............

manages

DeptId ....... ....

Emp_Id

department
Not Null!
ER to Relational 1999-2002, NCST 4

One-to-One Optional

Some of the consultants are provided PCs at their desks.

consultant

EmpId ....... .... PC_Id

Null Allowed! provided_with


PC_Id ....... .... ..........

PC
ER to Relational

1999-2002, NCST

One-to-Many Optional

Every employee is alloted a department. A department however, staffs none, one or many employees.
Dept ....... .... ..........

department

staffs

EmpId ....... .... Dept

employee
Not Null!
ER to Relational 1999-2002, NCST 6

One-to-Many Optional

A consultant can have at most one secretary. A secretary may work for several consultants.

secretary

Sec_Id ....... ....

..........

works_for

C_Id

....... .... Sec_Id

consultant
Null Allowed!
ER to Relational 1999-2002, NCST 7

Many-to-Many Optional

A professional association may have none, one or more consultants as members. Similarly, a consultant may be a member of none, one or many professional associations. PA_Id ....... .... ..........

Prof_asso c
is_member_of

is_member_of C_Id PA_Id

....... ....

consultant
ER to Relational

C_Id

....... ....

1999-2002, NCST

Recursive One-to-One Mandatory

Every trainee has another trainee as a partner.


references

is_partner_of EmpId ....... .... Prtnr_Id

trainee
Not Null!

ER to Relational

1999-2002, NCST

Recursive One-to-One Optional

An employee may have another employee as a spouse.

references

is_married_to EmpId ....... .... Spouse_Id

employee
Null Allowed.

ER to Relational

1999-2002, NCST

10

Recursive One-to-Many Optional

Consultants are divided into groups with each group having a leader.

references

leads EmpId ....... .... Ldr_Id

consultant Null Allowed.

ER to Relational

1999-2002, NCST

11

Recursive Many-to-Many Optional

Several projects employ similar skill sets. i.e., projects share skills.

shares_skills_with

project
shares_skills_with Proj1Id Proj2Id SkillId

Proj_Id ....... ....

....... ....

ER to Relational

1999-2002, NCST

12

Ternary One-One-One Mandatory

Consultants use casebooks for projects. Each consultant uses a different casebook for different projects. EmpId ....... ....

consultant
uses_casebook uses_casebook EmpId Proj_Id Book_Id

casebook
Book_Id ....... ......

project
Proj_Id ....... ....

ER to Relational

1999-2002, NCST

13

Ternary Many-Many-Many Optional

Employees use a wide range of different skills for each project.


EmpId ....... ....

employee
uses_skill uses_skill EmpId Proj_Id Skill_Id

skills
Skill_Id ....... ......

project
Proj_Id ....... ....

ER to Relational

1999-2002, NCST

14

Specialisation Hierarchies

A consultant is either an accountant or a lawyer but not none and not both.
does not map to a table

consultant
(d)

accountant

lawyer

lawyer

Emp_Id .......

.....

accountant Emp_Id ....... ......

ER to Relational

1999-2002, NCST

15

Specialisation Hierarchies

A consultant is either an accountant or a lawyer but not none and not both.

consultant
(d)

accountant
consultant Emp_Id ....... ......

lawyer
acct

A/L

.... ... lwyr .... ... Type

ER to Relational

1999-2002, NCST

16

Specialisation Hierarchies

A consultant is either an accountant or a lawyer or none, but not both.


consultant Emp_Id ....... .....

consultant
(d)

accountant

lawyer

lawyer

Emp_Id .......

.....

accountant Emp_Id ....... ......

ER to Relational

1999-2002, NCST

17

Specialisation Hierarchies

A consultant is either an accountant or a lawyer or none or both

consultant
(o)

accountant
consultant Emp_Id ....... ......

lawyer
acct .... ... lwyr .... ...

Boolean

A L

ER to Relational

1999-2002, NCST

18

ER to Relational : a recap

generally, every entity translates into a distinct table. a relationship between two entities implies a migration of key from one entity to another. in a 1:many relationship, the key migrates to the entity at whose end cardinality is >1.
1999-2002, NCST 19

ER to Relational

ER to Relational : a recap

a many:many relationship is best avoided; where it does exist, the relation itself maps to a distinct table. table capturing the many:many relationship should preferably take the same name as the relationship.

ER to Relational

1999-2002, NCST

20

Data Definition Language

SQL : Structured Query Language.


Components of SQL:

Data Definition Language Data Manipulation Language Transaction Control Component and others.......

Data Definition Language (DDL) is used to set up the schema.


1999-2002, NCST 21

ER to Relational

DDL Syntax
SQL: the standard SQL2 (SQL92)
v/s

SQL: implementations
SQL*Plus, mySQL, SQL Server . . . .

Although the implementations are built around the SQL standard, they have their own nuances.

case insensitive free-format support for ad-hoc queries


ER to Relational 1999-2002, NCST 22

DDL Syntax
create table tablename ( col1_name datatype, col2_name datatype, ...... coln_name datatype)

where datatype is a valid data-type supported by the DBMS product in use : char, varchar2, date, long, etc.
ER to Relational 1999-2002, NCST 23

DDL : Example
create table employee ( emp_id varchar2(10), emp_fname varchar2(20), emp_lname varchar2(20), emp_address varchar2(80), emp_city varchar2(15), emp_email varchar2(30))

ER to Relational

1999-2002, NCST

24

Enforcing Integrity Constraints

Integrity Constraints capture business semantics; modeled by the relationships.


Declarative constraints enforce integrity at the field level or row level or table level
e.g. column sex can take values M or F only columns empno and datepd constitute the primary for the table

ER to Relational

1999-2002, NCST

25

Enforcing Integrity Constraints

Triggers* help enforce integrity constraints at the database/table/row level (although it can also be used for enforcing integrity at the field level)
e.g. the total outstandings of a customer should not exceed the credit limit specified for him.

* triggers are procedural, and fire (execute) when a specified event occurs - like an update on a row, or a deletion of row, etc..
ER to Relational 1999-2002, NCST 26

Declarative Constraints

Enforcing entity integrity (primary key)


create table employee ( emp_id varchar2(10) primary key, emp_name varchar2(20), emp_address varchar2(80)); create table salary_pmts ( sal_emp varchar2(20), sal_datepd date, sal_amount number, primary key (sal_emp, sal_datepd));

ER to Relational

1999-2002, NCST

27

Enforcing referential integrity (foreign key)


create table salary_pmts ( sal_emp_id varchar2(10) references employee (emp_id), sal_datepd date, sal_amount number, primary key (sal_emp_id, sal_datepd)); create table salary_pmts ( sal_emp_id varchar2(10), sal_datepd date, sal_amount number, primary key (sal_emp_id, sal_datepd), foreign key (sal_emp_id) references employee (emp_id));

ER to Relational

1999-2002, NCST

28

Enforcing other business rules


create table employee ( emp_id varchar2(10) primary key, emp_name varchar2(20), emp_address varchar2(80) emp_sex varchar2(1) check ( sex in (M,F)), emp_birthdt date, emp_joindt date, check ((emp_joindt - emp_birthdt/365)>18));

all examples use SQL*Plus


ER to Relational 1999-2002, NCST 29

References
Fundamentals of Database Systems
R. Elmasri , S. Navathe
Addison Wesley