Relationnel Model

21
Bogdan Shishedjiev Relational Model 1 Relationnel Model

description

Relationnel Model. Relational Database. General Concepts. Relation, Attribute, Domain Cartesian product of domains D 1 ,D 2 ,…,D n gives n-uples or tuples {d 1 ,d 2 ,…,d n : d i  D i } Relation is a subset of the Cartesian product of a list of domains - PowerPoint PPT Presentation

Transcript of Relationnel Model

Page 1: Relationnel Model

Bogdan Shishedjiev Relational Model

1

Relationnel Model

Page 2: Relationnel Model

Bogdan Shishedjiev Relational Model 2

STUDENT PROFESSOR

NO_ST NAME ADRESSE NO_PROF NAME CATEGORY

1215 Anne Grenoble 101 Lyons Professor

1218 Pierre Paris 105 Cole Assist.Prof.

1230 Jean Marseille 110 Dunhill Assoc.Prof.

UNITE INSCRIPTION

NO_UV TITRE NO_ENS NO_ET NO_UV AN BOOL

152 Databases 110 1215 152 1996 Passed

210 Operational systems

105 1215 210 1996 Failed

212 Languages 101 1215 210 1997 Passed

255 Architecture 105 1218 152 1997 Passed

1230 210 1997 Failed

Relational Database

Page 3: Relationnel Model

Bogdan Shishedjiev Relational Model 3

General Concepts

• Relation, Attribute, Domain

• Cartesian product of domains D1,D2,…,Dn gives n-uples or tuples {d1 ,d2 ,…,dn: di Di}

• Relation is a subset of the Cartesian product of a list of domains

• The attribute is the column of the relation and it has a name.

• Relational Schema (intention), extension (relation)• Relational database

Page 4: Relationnel Model

Bogdan Shishedjiev Relational Model 4

Example

Unit Part Quantity Car Chassis 1 Car Motor 1 Car Spring 2 Car Wheel 5 Motor Cylinder Head 1 Motor Piston 4 Motor Connecting Rod 4 Wheel Tyre 1 Wheel Wheel Rim 1

D1 = {Car, Motor, Wheel, Cylinder Head, Spring, Chassis, Piston, Connecting Rod, Tyre, Wheel Rim}, D2 ={1..8}

R(Unit:D1,Part:D1,Quantity:D2)

Page 5: Relationnel Model

Bogdan Shishedjiev Relational Model 5

Functional Dependency

• Let R(X, Y, Z). There is functional dependency between X and Y or XY if and only if for any values of X,Y and Z – (a,b,c) and (a’,b’,c’)

R(a,b,c) and R(a,b’,c’) b=b’• Key

If in R(A1,A2,…,An) X A1,A2,…,An and there is no YX Y A1,A2,…,An . X is a candidate key (superkey)

• Primary key

Page 6: Relationnel Model

Bogdan Shishedjiev Relational Model 6

Functional Dependency

• Axioms (Armstrong)– (R1) Reflexivity : if Y X then X Y

– (R2) augmentation : if X Y and W is any set of attributes then XW YW

– (R3) transitivity : if X Y and Y Z then X Z

– (R4) pseudo-transitivity : if X Y and YW Z then XW Z

– (R5) union : if X Y and X Z then X YZ

– (R6) decomposition : if X YZ then X Y and X Z

Page 7: Relationnel Model

Bogdan Shishedjiev Relational Model 7

Integrity Constraints

• Entity Integrity

• Domain Integrity

• Referential Integrity – Foreign key

Refering relation

Refered relation

Nonkey attrubute

key

Page 8: Relationnel Model

Bogdan Shishedjiev Relational Model 8

Relational AlgebraNotationX – set of attrubutesA(X) – The set of all possible tuples of XL/Y – The restriction of the realization L over the subset Y X

X={ PART:D1, SUPPLIER:D2 } Y={PART D1, PROJECT:D3 }Z={PART D1, SUPPLIER D2, PROJECT D3 }

- D1 = { nut, bolt, screw}- D2 = { Peter, Paul, Mary }- D3 = { a, b, c }

R : PART SUPPLIER S : PART PROJECT

nut Peter nut a

nut Paul nut b

bolt Mary bolt a

Page 9: Relationnel Model

Bogdan Shishedjiev Relational Model 9

Relational Algebra

Projection(1) Z = Y(2) YR ={LA(Y); L'A(X), (L'/Y = L) и (L' R) }

R : PART SUPPLIER PROJECT

nut Paul a

nut Paul b

bolt Peter a

Let Y={ PARTD1, SUPPLIERD2 }

YR : PART SUPPLIER

nut Paul

bolt Peter

Page 10: Relationnel Model

Bogdan Shishedjiev Relational Model 10

Relational Algebra

• Selection(1) Y =X(2) ER = { L A(X) / (L R) and (E(L) = true) }

R : CLASS NAME CITY BIRTH. SPORT

6 peter london 11.10.79 judo

6 peter london 11.10.79 fencing

6 jacob london 05.03.78 swimming

6 paul london 06.07.79 football

5 george Bristol 01.04.77 football

E = (CITY=‘london') (BIRTH≤ '31.08.79') ((SPORT='judo') (SPORT='football'))

ER : CLASS NAME CITY BIRTH SPORT

6 paul london 06.07.79 football

Page 11: Relationnel Model

Bogdan Shishedjiev Relational Model 11

Relational Algebra

• Product or Inner Join (1) Z=XY(2) R*S = { LA(Z) that (L/X R) and (L/Y S) }

R*S : PART SUPPLIER PROJECT

nut Peter a

nut Peter b

nut Paul a

nut Paul b

bolt Mary a

R : PART SUPPLIER S : PART PROJECT

nut Peter nut a

nut Paul nut b

bolt Mary bolt a

Page 12: Relationnel Model

Bogdan Shishedjiev Relational Model 12

Relational Algebra

• Union, Intersection, Difference The schema is identical

R : PART SUPPLIER S : PART SUPPLIER

nut peter nut peter

nut paul bolt peter

bolt mary bolt mary

RS : PART SUPPLIER

nut peter

nut paul

bolt mary

bolt peter

RS : PART SUPPLIER

nut peter

bolt mary

SR : PART SUPPLIER

bolt peter

Page 13: Relationnel Model

Bogdan Shishedjiev Relational Model 13

Relational Algebra• Cartesian Product - XY=

(1) Z=XY(2) RS = R*S

R : PART SUPPLIER T : PI SUPPLIER

nut peter nut peter

nut paul nut paul

bolt mary bolt mary

S : PART PROJECT

nut a

nut b

bolt a

TS : PI SUPPLIER PART PROJECT

nut peter nut a

nut paul nut a

bolt mary nut a

nut peter nut b

nut paul nut b

bolt mary nut b

nut peter bolt a

nut paul bolt a

bolt mary bolt a

Page 14: Relationnel Model

Bogdan Shishedjiev Relational Model 14

Relational Algebra

• Equi-join

T(⋈PART=PI) S : PI SUPPLIER PART PROJECT

nut peter nut a

nut paul nut a

nut peter nut b

nut paul nut b

bolt mary bolt a

T : PI SUPPLIER

nut peter

nut paul

bolt mary

S : PART PROJECT

nut a

nut b

bolt a

Page 15: Relationnel Model

Bogdan Shishedjiev Relational Model 15

Normalization

• Good schema– Universal RelationFIRM ( N_employee, N_department, chef, contract_type)HOSPITAL(N_patient, Name, Ward, Doctor)

– Update anomaly

– Deletion anomaly

– Insertion anomaly

– Redundancy

Page 16: Relationnel Model

Bogdan Shishedjiev Relational Model 16

Decomposition

• Decomposition– Definition

R = R1*R2*R3*…*Rn

– Lossless decomposition

– Preservation of dependencies

Page 17: Relationnel Model

Bogdan Shishedjiev Relational Model 17

DecompositionR N° name age address speciality

1 A 30 x1 S1

2 B 30 x2 S2

3 C 30 x3 S2

4 D 40 x4 S1

R1 âge speciality R2 N name addr R3 N age

30 S1 1 A x1 1 30

30 S2 2 B x2 2 30

40 S1 3 C x3 3 30

4 D x4 4 40

R'=R1*R2*R3 N° name address age speciality

1 A x1 30 S1

1 A x1 30 S2

2 B x2 30 S1

2 B x2 30 S2

3 C x3 30 S1

3 C x3 30 S2

4 D x4 40 S1

Page 18: Relationnel Model

Bogdan Shishedjiev Relational Model 18

First Normal Form

• Definition – A relation is in 1NM if and only if all underlying domains contain scalar values only and it has no repetitive groups

No NAME CHILDFIRSTNAME AGE

500 DUPONT ANDRE 10501 DURAND JEAN 11501 DURAND PIERRE 12510 LEFEBVRE PAUL 13510 LEFEBVRE JACQUES 14

EMPLOYEE No NAME500 DUPONT

501 DURAND

510 LEFEBVRE

CHILDREN No FirstName age500 André 10

501 Jean 11

501 Pierre 12

510 Paul 13

510 Jacques 14

Page 19: Relationnel Model

Bogdan Shishedjiev Relational Model 19

First Normal Form

MOVIE(No, Name, Director, Actor1, Actor2, Actor3)

MOVIE No Name Director Actor1 Actor2 Actor3

1 The Silence of the Lambs

Jonathan Demme

Jodie Foster Anthony Hopkins

2 The Sixth Sense M. Night Shyamalan

Bruce Willis

MOVIE No Name Director

1 The Silence of the Lambs

Jonathan Demme

2 The Sixth Sense M. Night Shyamalan

CAST No Actor

1 Jodie Foster

1 Anthony Hopkins

2 Bruce Willis

Page 20: Relationnel Model

Bogdan Shishedjiev Relational Model 20

Second Normal Form

• Definition – A relation is in 2NM if and only if it is in 1NF and no non-prime attribute in the table is functionally dependent on a proper subset of a candidate key (total dependency)

• Example

STOCK (part, warehouse, quant, address) is not in 2NF

FD : part, warehouse -> quant ; warehouse -> address

DecompositionSTOCK (part, warehouse, quant)LOCAL(warehouse, address)

Page 21: Relationnel Model

Bogdan Shishedjiev Relational Model 21

Third Normal Form

• Definition – A relation is in 3NM if and only if it is in 2NF and every nonkey attribute is nontransitively dependent on the primary key

• ExamplePERSONAL(employee, name, FirstName, Service, Address)

DF : employee -> name, FirstName, Service, Address; Service -> Address

DecompositionPERSONAL(employee, name, FirstName, Service)

LOCAL(Service, Address)