Anda di halaman 1dari 38

Chapter 3

Relational Model
Chapter 3 in Textbook

Database Design
Steps in building a database for an application:

Real-world
domain

Conceptual
model

DBMS data
model

Lilac Safadi
Relational Model

Create
Schema
(DDL)

Load data
(DML)

Relational Model
In the relational model, all data is logically structured
within relations (tables).

Lilac Safadi
Relational Model

Relational Data Structure


Attributes

STUDENT
StudentNo

Tuples

LName

FName Initial

DOB

GPA

4170010

Al-Saleh

Amal

M.

04-06-78 3.91

4182000

Al-Ghanem Nora

A.

02-12-79 4.20

4182034

Al-Fahad

Laila

A.

01-11-74 4.01

4188134

Saod

Amal

F.

22-04-73 3.01

4189860

Rashed

Rana

I.

30-01-78 2.31

4192134

Al-Fahad

Rania

M.

19-03-79 3.50

Degree

Lilac Safadi
Relational Model

Relational Data Structure


Relation is a table with columns & rows.
Holds information about entities.
Attribute is a named column of a relation.
Domain is the set of allowable values for one
or more attributes. Every attribute in a relation
is defined on a domain.
Tuple is a row of a relation.
Lilac Safadi
Relational Model

Relational Data Structure


Degree of a relation is the number of
attributes it contains.
Cardinality of a relation is the number of
tuples it contains.
Relational database is a collection of
normalized relations with distinct relation
names.
Lilac Safadi
Relational Model

Domains
STUDENT
StudentNo

LName

FName Initial

DOB

GPA

4170010

Al-Saleh

Amal

M.

04-06-78 3.91

4182000

Al-Ghanem Nora

A.

02-12-79 4.20

4182034

Al-Fahad

A.

01-11-74 4.01

Attribute

Laila

Domain Name

Definition

StudentNo Student Name

Digits: size 7

LName

Last Name

Character: size 15

FName

First Name

Character: size 15

Initial

Initial

Character: size 3

DOB

Date of Birth

Date: range 01-01-20, format dd-mm-yy

GPA

Great Point Average

Real: size 3, decimal 2, range 0-5

Lilac Safadi
Relational Model

Properties of Relations
The relation has a name that is distinct from all other
relation names in the relational DB
Each cell of the relation contains exactly single
value
Each attribute has a distinct name
The values of an attribute are all of the same
domain
Each tuple is distinct. There are no duplicate tuples
The order of attributes has no significance
the order of tuples has no significance; theoretically.
Lilac Safadi
Relational Model

Relational Keys
Candidate key (CK) is an attribute, or set of
attributes, that uniquely identifies a tuple, and no
proper subset is a CK within the relation
Primary Key (PK) is the CK that is selected to
identify tuples uniquely within the relation
Foreign Key (FK) is an attribute, or set of attributes,
within one relation that matches the CK of some
relation. Used to represent relationship between
tuples of two relations.
Lilac Safadi
Relational Model

Relational Keys
STUDENT
StudentNo

Primary
Key

LName

FName Initial

DOB

GPA

Dept

4170010

Al-Saleh

Amal

M.

04-06-78 3.91

D001

4182000

Al-Ghanem Nora

A.

02-12-79 4.20

D001

4182034

Al-Fahad

Laila

A.

01-11-74 4.01

D002

4188134

Saod

Amal

F.

22-04-73 3.01

D003

4189860

Rashed

Rana

I.

30-01-78 2.31

D001

Foreign
Key

DEPARTMENT
DeptNo

Department Name

Location

D001 Computer Science

Build # 20

D002 Business Administration

Build # 45

D003 Science

Build # 6
Lilac Safadi
Relational Model

10

DB Relations
Relation schema is a named relation defined by
a set of attributes
If A1, A2, .., An are a set of attributes, then relation
schema R is:
R = (A1, A2, .., An)
Relational schema is a set of relation schemas,
each with a distinct name
If R1, R2, .., Rn are a set of relation schemas, then
relational schema R is:
R= {RLilac
R2, .., Rn}
1, Safadi
11
Relational Model

Relation Schema
STUDENT
StudentNo

LName

FName Initial

DOB

GPA

Dept

4170010

Al-Saleh

Amal

M.

04-06-78 3.91

D001

4182000

Al-Ghanem Nora

A.

02-12-79 4.20

D001

4182034

Al-Fahad

Laila

A.

01-11-74 4.01

D002

4188134

Saod

Amal

F.

22-04-73 3.01

D003

4189860

Rashed

Rana

I.

30-01-78 2.31

D001

STUDENT (StudentNo, Lname, Fname, Initial, DOB, GPA, Dept)

Lilac Safadi
Relational Model

12

Relation Schema
DEPARTMENT
DeptNo

Department Name

Location

D001 Computer Science

Build # 20

D002 Business Administration

Build # 45

D003 Science

Build # 6

DEPARTMENT (DeptNo, Department Name, Location)

Lilac Safadi
Relational Model

13

E/R

Relational Model

Entity Type

Relational Model

Represent each entity type with a relation


Entity type attributes become the relation attributes

STUDENT (StudentNo, Lname, Fname, Initial, DOB, GPA, Dept)


DEPARTMENT (DeptNo, Department Name, Location)

Lilac Safadi
Relational Model

15

Weak Entity Type

Relational Model

A weak entity type relation must include its key and its
strong entity type PK as a FK. The combination of the two
keys form the PK of the weak entity.
LName

FName

EmpNo

DepNo

FName

DOB
EMPLOYEE

has

DEPENDENT

EMPLOYEE (EmpNo, Lname, Fname, DOB)


DEPENDENT (DepNo, EmpNo, FName)
Lilac Safadi
Relational Model

16

1:1 Relationship

Relational Model

Identify an entity type (S) (preferably total participator)


Include the PK of the other entity (T) as a FK in S
Add attributes that describes the relationship
LName

EmpNo

FName

DOB StartD

EndD

EMPLOYEE

(0,1)

Name
BrnNo

manage

(1,1)

BRANCH

EMPLOYEE(EmpNo, Lname, Fname, DOB)


BRANCH(BrnNo, Name, EmpNo, StartDate, EndDate)
Lilac Safadi
Relational Model

17

1:M Relationship

Relational Model

Identify a participating entity type (S) on the m-side


Include the PK of the other entity type (T) as a FK in S
Add attributes that describes the relationship
LName

FName

EmpNo

Name
BrnNo

DOB

EMPLOYEE

allocate

BRANCH

EMPLOYEE(EmpNo, Lname, Fname, DOB, BrnNo)


BRANCH(BrnNo, Name)
Lilac Safadi
Relational Model

18

M:N Relationship

Relational Model

Create a relation R to represent the relationship


Include the PK of participating entity types (T & S) as FK in R.
The combination of the two FK will form the PK of R
Add attributes that describes the relationship
LName

FName

EmpNo

DOB

EMPLOYEE

Name
ProjNo

hours

work-on

PROJECT

EMPLOYEE(EmpNo, Lname, Fname, DOB)


PROJECT(ProjNo, Name)
Work-on(EmpNo,ProjNo, hours)
Lilac Safadi
Relational Model

19

n-ary Relationship

Relational Model

Create a relation R to represent the relationship


Include the PK of the participating entities as FK in R. The
combination of all FK form the PK of R.
Add attributes that describes the relationship
StartD

BizNo

BUSINESS

LawNo

EndD

contract

SupNo

SUPPLIER

Lawyer

BUSINESS(BizNo) LAWYER(LawNo) SUPPLIER(SupNo)


contract(BizNo, SupNo, LawNo, StartDate, EndDate)
Lilac Safadi
Relational Model

20

Composite Attribute

Relational Model

Include its simple components in the relation

LName

initial

FName

name

DOB

emp_no

EMPLOYEE
EMPLOYEE(EmpNo, Fname, initial, Lname, DOB)

Lilac Safadi
Relational Model

21

MultiValue Attribute

Relational Model

Suppose A is a relation that contains the multivalued attribute


Create a relation R to represent the attribute
Include the PK of A as FK in R
The PK of R is the combination of the PK of A (FK) & the
multivalued attribute
DOB
EmpNo

Tel_no

EMPLOYEE
EMPLOYEE(EmpNo, DOB)
TELEPHONE(EmpNo, tel_no)
Lilac Safadi
Relational Model

22

EER
Relational Model
Mandatory/NonDisjoint
Suppose specialization with subclasses (S1, S2, .., Sm} & a
superclass C
Create a relation L to represent C with PK & attributes
Include the unshared attributes for each subclass Si, 1 i m

Add discriminator to distinguish the type of each tuple

Lilac Safadi
Relational Model

23

EER
Relational Model
Mandatory/NonDisjoint
Salary
EmpNo
Fname

LName
EMPLOYEE

DOB

o
Typing
Speed

SECRETARY

TECHNICIAN

ENGINEER

EngType

TGrade
EMPLOYEE(EmpNo, Fname, Lname, DOB, Salary,TypingSpeed,TGrade,
EngType, Secretary Flag, Technician Flag, Engineer Flag)
Lilac Safadi
Relational Model

24

EER
Relational Model
Mandatory/Disjoint
Suppose specialization with subclasses (S1, S2, .., Sm} & a
superclass C
Create a relation Li, 1 i m, to represent each combination
of super/subclass

Lilac Safadi
Relational Model

25

EER
Relational Model
Mandatory/Disjoint
Salary
EmpNo

Fname

LName

EMPLOYEE

DOB

d
Typing
Speed

SECRETARY

TECHNICIAN

ENGINEER

EngType

TGrade
SECRETARY(EmpNo, Fname, Lname, DOB, Salary,TypingSpeed)
TECHNICIAN(EmpNo, Fname, Lname, DOB, Salary,Tgrade)
ENGINEER(EmpNo, Fname, Lname, DOB, Salary, EngType)
Lilac Safadi
Relational Model

26

EER
Relational Model
Optional/NonDisjoint
Suppose specialization with subclasses (S1, S2, .., Sm} & a
superclass C
Create a relation L to represent C with PK & attributes
Create a relation Li to represent each subclass Si, 1 i m

Lilac Safadi
Relational Model

27

EER
Relational Model
Optional/NonDisjoint
Salary
EmpNo
Fname

LName
EMPLOYEE

DOB

o
Typing
Speed

SECRETARY

TECHNICIAN

ENGINEER

EngType

TGrade

EMPLOYEE(EmpNo, Fname, Lname, DOB, Salary)


SUB-EMP(EmpNo, TypingSpeed,TGrade, EngType,
Secretary Flag, Technician Flag, Engineer Flag)
Lilac Safadi
Relational Model

28

EER
Relational Model
Optional/Disjoint
Suppose specialization with subclasses (S1, S2, .., Sm} & a
superclass C
Create a relation L1 to represent C with PK & attributes
Create a relation L2 to represent all subclasses Si, 1 i m

Add discriminator to distinguish the type of each tuple

Lilac Safadi
Relational Model

29

EER
Relational Model
Optional/Disjoint
Salary
EmpNo
Fname

LName
EMPLOYEE

DOB

d
Typing
Speed

SECRETARY

TECHNICIAN

ENGINEER

EngType

TGrade

EMPLOYEE(EmpNo, Fname, Lname, DOB, Salary)


SECRETARY(EmpNo, TypingSpeed)
TECHNICIAN(EmpNo, Tgrade)
ENGINEER(EmoNo, EngType)
Lilac Safadi
Relational Model

30

E/R

Relational Model

E/R Model

Relational Model

Entity Type

Relation

1:1 or 1:M Relationship Type

FK or relation

M:N Relationship Type

Relation & 2 FK

n-ary Relationship Type

Relation & n FK

Simple attribute

Attribute

Composite attribute

Simple component attribute

Multivalued attribute

Relation and FK

Value set

Domain

Key attribute

PK
Lilac Safadi
Relational Model

31

Views
Base relation is a named relation corresponding to an
entity in the conceptual schema, whose tuples are
physically stored in the DB
View is a derived relation. Virtual, may not exist, but
dynamically derived from one or more base relations.
The only information about a view that is stored in the
database is its structure.

The external model can consist of both conceptual


level relations (base relations) and derived views.
Lilac Safadi
Relational Model

32

Views
STUDENT_GPA
StudentNo GPA

View

4170010

3.91

4182000

4.20

4182034

4.01

4188134

3.01

STUDENT
StudentNo

Base
Relation

LName

FName Initial

DOB

GPA

Dept

4170010

Al-Saleh

Amal

M.

04-06-78 3.91

D001

4182000

Al-Ghanem Nora

A.

02-12-79 4.20

D001

4182034

Al-Fahad

Laila

A.

01-11-74 4.01

D002

4188134

Saod

Amal

F.

22-04-73 3.01

D003

Lilac Safadi
Relational Model

33

Purpose of Views
Provides security mechanism by hiding parts of the DB
from certain users
Customize data to users needs, so that the same data
can be seen by different users in different ways
Simplify complex operations. It allow you to work with
data from different tables simultaneously.
Supports logical data independence

Lilac Safadi
Relational Model

34

Updating Views
All updates to a base relation should be immediately
reflected in all views that reference that base relation.
All updates to a view should be reflected in the underlying
base relation, under the following rules:
- Updates are allowed through a view defined using a
simple query involving a single base relation & containing
either the PK or a CK of the base relation
- Update are NOT allowed through views involving multiple
base relations
- Updates are not allowed through views involving
aggregation or grouping operations
Lilac Safadi
Relational Model

35

Relational Integrity
Data integrity refers to the validity, consistency,
and accuracy of the data in the database.
Integrity rules are constraints that apply to all
instances of the DB.
Two integrity rules for the relational model:
- Entity integrity
- Referential integrity

Lilac Safadi
Relational Model

36

Relational Integrity
Entity Integrity: Ensures that there are no duplicate
records within the table. In a base relation, no attribute
of a PK can be null
Referential Integrity: If a FK exists in a relation, either
the FK value must match a CK value of some tuple in
its home relation or the FK value must be wholly null.
Enterprise constraints: rules specified by the users
or DBA of the DB based on the ways an organization
perceives and uses its data
(e.g.number of staff working in a branch is at most 20)
Lilac Safadi
Relational Model

37

Summary of Relational Model

Relational database
Relation, attribute, tuple, degree, cardinality
Primary Key, Foreign Key
Relation schema, Relational database
schema
Relational Integrity, Entity Integrity,
Referential Integrity, Enterprise Constraints
Views
Lilac Safadi
Relational Model

38