Tải bản đầy đủ (.pdf) (59 trang)

4 relational data model section 2

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (2.02 MB, 59 trang )

DATABASE SYSTEMS

RELATIONAL DATA MODEL

Spring 2014

Nguyen Ngoc Thien An


Contents
2

Overview of Relational Data Model
ER- & EER-to-Relational Mapping
Relational Algebra


3

ER- & EER-to-Relational Mapping

Reading Suggestion: [1] Chapter 9


ER- & EER-to-Relational Mapping
4

Step 1: Regular
Entity Types

Step 2: Weak


Entity Types

Step 3: Binary
1:1 Relationship
Types

Step 6:
Multivalued
Attributes

Step 5: Binary
M:N
Relationship
Types

Step 4: Binary
1:N Relationship
Types

Step 7: N-ary
Relationship
Types

Step 8:
Specialization or
Generalization

Step 9: Union
Types
(Categories)



Minit
Fname
SSN

Number_of_employees
N

Lname
Name

Address

1

WORKS_FOR

Name

Locations

Sex
EMPLOYEE

Start_date

Salary

1


Number

DEPARTMENT

1

MANAGES

1

Bdate

CONTROLS

Hours
1

N
SUPERVISION

N

N

N
WORKS_ON

PROJECT


1
Name
DEPENDENTS_OF

Number

N
DEPENDENT
5

Relationship

Name

Sex

Birth_date

Location

ERD of
Company
Database


EMPLOYEE
Fname

Minit


Lname

SSN Bdate Address Sex Salary Super_SSN Dno

DEPARTMENT

Dname Dnumber

Mgr_SSN Mgr_start_date

DEPT_LOCATIONS
Dnumber Dlocation

PROJECT
Pname

Pnumber

Plocation Dnum

WORKS_ON
Essn Pno Hours

DEPENDENT
6

Essn Dependent_name

Sex Bdate Relationship


Relational
Schema of
Company
Database


ER- & EER-to-Relational Mapping
7

Step 1: Regular
Entity Types

Step 2: Weak
Entity Types

Step 3: Binary
1:1 Relationship
Types

Step 6:
Multivalued
Attributes

Step 5: Binary
M:N
Relationship
Types

Step 4: Binary
1:N Relationship

Types

Step 7: N-ary
Relationship
Types

Step 8:
Specialization or
Generalization

Step 9: Union
Types
(Categories)


Step 1: Regular Entity
8



Step 1: Mapping of Regular (Strong) Entity Types


Each regular entity type E  Create one new relation S.
Simple attributes of E  Attributes of S.
 Composite attributes of E  Include only their simple component
attributes in S.
 Choose one of the key attributes of E as the primary key for S.
 The other key attributes of E??? (Homework!!!)





The new relation is called an entity relation.




Each tuple represents an entity instance.

E.g.:




Regular entity types EMPLOYEE, DEPARTMENT, and PROJECT 
relations EMPLOYEE, DEPARTMENT, and PROJECT.
Ssn, Dnumber, and Pnumber are the primary keys for the relations
EMPLOYEE, DEPARTMENT, and PROJECT.
Knowledge that Dname of DEPARTMENT and Pname of PROJECT
are secondary keys is kept for possible use later in the design.


Minit
Fname
SSN

Number_of_employees
N


Lname
Name

Address

1

WORKS_FOR

Name

Locations

Sex
EMPLOYEE

Start_date

Salary

1

Number

DEPARTMENT

1

MANAGES


1

Bdate

CONTROLS

Hours
1

N
SUPERVISION

N

N

N
WORKS_ON

PROJECT

1
Name
DEPENDENTS_OF

Number

N
DEPENDENT
9


Relationship

Name

Sex

Birth_date

Location

Step 1:
Regular
Entity


EMPLOYEE
Fname

Minit

Lname

SSN Bdate Address Sex Salary Super_SSN Dno

DEPARTMENT

Dname Dnumber

Mgr_SSN Mgr_start_date


DEPT_LOCATIONS
Dnumber Dlocation

PROJECT
Pname

Pnumber

Plocation Dnum

WORKS_ON
Essn Pno Hours

DEPENDENT
10

Essn Dependent_name

Sex Bdate Relationship

Step 1:
Regular
Entity


ER- & EER-to-Relational Mapping
11

Step 1: Regular

Entity Types

Step 2: Weak
Entity Types

Step 3: Binary
1:1 Relationship
Types

Step 6:
Multivalued
Attributes

Step 5: Binary
M:N
Relationship
Types

Step 4: Binary
1:N Relationship
Types

Step 7: N-ary
Relationship
Types

Step 8:
Specialization or
Generalization


Step 9: Union
Types
(Categories)


Step 2: Weak Entity
12



Step 2: Mapping of Weak Entity Types


Each weak entity type W with owner entity type(s) Ei(s) 
Create one new relation S.




Simple attributes and simple components of composite
attributes of W  Attributes of S.
The primary key attribute(s) of the relation(s) corresponding to
Ei(s)  Foreign key attribute(s) of S.






This takes care of mapping the identifying relationship type of W.


The primary key of S is the combination of the primary
key(s) of the owner(s) and the partial key of W, if any.

E.g.:





Weak entity type DEPENDENT  Relation DEPENDENT.
The primary key SSN of EMPLOYEE relation  Foreign key
attribute of DEPENDENT (renamed to Essn).
The primary key of DEPENDENT is {Essn, Dependent_name}.


Minit
Fname
SSN

Number_of_employees
N

Lname
Name

Address

1


WORKS_FOR

Name

Sex
EMPLOYEE

Start_date

Salary

1

Number

DEPARTMENT

1

MANAGES

1

Bdate

CONTROLS

Hours

Owner’s

PK

Locations

1

N
SUPERVISION

N

N

N
WORKS_ON

PROJECT

1
Name
DEPENDENTS_OF

Primary
Key

13

Number

N

DEPENDENT

Partial
PK
Relationship

Name

Sex

Birth_date

Location

Step 2:
Weak
Entity


EMPLOYEE
Fname

Minit

Lname

SSN Bdate Address Sex Salary Super_SSN Dno

DEPARTMENT


Dname Dnumber

Mgr_SSN Mgr_start_date

DEPT_LOCATIONS
Dnumber Dlocation

PROJECT
Pname

Pnumber

Plocation Dnum

WORKS_ON
Essn Pno Hours

DEPENDENT
14

Essn Dependent_name

Sex Bdate Relationship

Step 2:
Weak
Entity


ER- & EER-to-Relational Mapping

15

Step 1: Regular
Entity Types

Step 2: Weak
Entity Types

Step 3: Binary
1:1
Relationship
Types

Step 6:
Multivalued
Attributes

Step 5: Binary
M:N
Relationship
Types

Step 4: Binary
1:N Relationship
Types

Step 7: N-ary
Relationship
Types


Step 8:
Specialization or
Generalization

Step 9: Union
Types
(Categories)


Step 3: Binary 1:1 Relationship (1)
16



Step 3: Mapping of Binary 1:1 Relationship Types



R is a relationship type with two entity types E1 and E2.
Case 1: At least one entity type has total participation in R.






E1: the entity type participates totally in R.
Include the primary key of the relation corresponding to E2 as
a foreign key in the relation representing E1.
Include all the simple attributes and simple components of

composite attributes of R in the relation of E1.
E.g.: DEPARTMENT’s participation in the 1:1 relationship type
MANAGES is total.




Include the primary key of EMPLOYEE as foreign key in
DEPARTMENT and rename it Mgr_ssn.
Include the simple attribute Start_date in the DEPARTMENT and
rename it Mgr_start_date.


Step 3: Binary 1:1 Relationship (2)
17

PK1
E1

1

1

R

E1(PK1,…,PK2, A)

Step 3 –
Case 1


PK1
E1

PK2

A

E2(PK2,…)
PK2

A
1

R

E1(PK1,…,PK2,A)

E1(PK1,…)

E2

1

E2

E2(PK2,…)

OR
E2(PK2,…,PK1,A)



Step 3: Binary 1:1 Relationship (3)
18



Case 2: Both entity types have optional participation
in R.
Option 1: the same way in the case 1.
 Option 2:






Create a new relation S representing R.
 Include the primary keys of the relations representing E1
and E2 as foreign keys in S.
 One of them becomes the primary key of S. The other is
a unique key S.
 Include all the simple attributes and simple components
of composite attributes of R in S.
The new relation is called a relationship relation.
 Each tuple in S represents an instance of R.


Step 3: Binary 1:1 Relationship (4)
19


PK1

PK2

A
1

E1

E1(PK1,…)

R

E2(PK2,…)

1

E2

R(PK1,PK2,A)

OR
E1(PK1,…)

E2(PK2,…)

R(PK1,PK2,A)

Step 3 – Case 2 – Option 2



Step 3: Binary 1:1 Relationship (5)
20



Case 2: Both entity types have optional participation
in R.


Usage:




Option 1:
 Drawback: May cause excessive NULL values in the
foreign key.
 Usually used when the relation containing the foreign key
has a high percentage of instances participating in R.
Option 2:
 Drawback: Having an extra relation, and requiring an
extra join operation when combining related tuples from
the tables.
 Usually used when the relations of E1 and E2 have a low
percentage of instances participating in R.


Minit
Fname

SSN

Number_of_employees
N

Lname
Name

Address

1

WORKS_FOR

Name

Locations

Sex
EMPLOYEE

Start_date

Salary

1

Number

DEPARTMENT


1

MANAGES

1

Bdate

CONTROLS

Hours
1

N
SUPERVISION

N

N

N
WORKS_ON

PROJECT

1
Name
DEPENDENTS_OF


Number

N
DEPENDENT
21

Relationship

Name

Sex

Birth_date

Location

Step 3:
Binary 1:1
Relationship


EMPLOYEE
Fname

Minit

Lname

SSN Bdate Address Sex Salary Super_SSN Dno


DEPARTMENT

Dname Dnumber

Mgr_SSN Mgr_start_date

DEPT_LOCATIONS
Dnumber Dlocation

PROJECT
Pname

Pnumber

Plocation Dnum

WORKS_ON
Essn Pno Hours

DEPENDENT
22

Essn Dependent_name

Sex Bdate Relationship

Step 3:
Binary 1:1
Relationship



ER- & EER-to-Relational Mapping
23

Step 1: Regular
Entity Types

Step 2: Weak
Entity Types

Step 3: Binary
1:1 Relationship
Types

Step 6:
Multivalued
Attributes

Step 5: Binary
M:N
Relationship
Types

Step 4: Binary
1:N
Relationship
Types

Step 7: N-ary
Relationship

Types

Step 8:
Specialization or
Generalization

Step 9: Union
Types
(Categories)


Step 4: Binary 1:N Relationship (1)
24



Step 4: Mapping of Binary 1:N Relationship Types


Case 1: The entity type E1 of N-side has total participation
in R.




Case 2: The entity type E1 of N-side has partial
participation in R.







The same way in the case 1 of the step 3.
The same way in the case 2 of the step 3.
Usually use the option 1 when E1 has a high percentage of
instances participating in R.
Otherwise: use the option 2.

E.g.: 1:N relationship types SUPERVISION, CONTROLS,
WORKS_FOR:




WORKS_FOR, CONTROLS: include the primary key Dnumber
of DEPARTMENT as foreign key in EMPLOYEE (called Dno)
and in PROJECT (called Dnum).
SUPERVISION: include the primary key of EMPLOYEE as
foreign key in EMPLOYEE itself (called Super_ssn).


Step 4: Binary 1:N Relationship (2)
25

PK1
E1

PK2


A
N

R

E1(PK1,…,PK2,A)

1

E2

E2(PK2,…)

Step 4 – Case 1


×