dbms- a walk through 2

35
NORMALIZATION (3NF AND BCNF)

Upload: buttercup-gal

Post on 09-Apr-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 1/35

NORMALIZATION

(3NF AND BCNF)

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 2/35

EX AMPL

E

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 3/35

Insertion anomaly: if we want to insert the informationof a new salesperson, we cannot insert this information

until a customer is assigned to that personDeletion anomaly: if we delete the only customerassigned to a salesperson, we drop the informationabout the salesperson

Update anomaly: if the region of a salesperson changes, we have to search and find each customer assigned tohim.

 ANOMALIES

 

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 4/35

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 5/35

If an attribute is indirectly dependent on primary key,

then we say, there exists a transitive dependency.

For example:

If AB, BC then by transitivity theorem A C.

i.e. If A is the primary key, C is transitively dependent on

 A and functionally dependent on B which is a nonprimeattribute.

 TRANS

ITIV E

DE

PE

NDE

NCY 

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 6/35

 A relation r (R) is in third normal form (3NF) if and

only if the following conditions are satisfied

simultaneously.1. R elation R is already in 2NF

2. No non-prime attribute is transitively dependent on

the key or

No non-prime attribute functionally determines any 

other non-prime attribute

 THIRD NORMAL FORM

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 7/35

 THIRD NORMAL FORM (3NF)

1) Meet all the requirements of the 1NF

2) Meet all the requirements of the 2NF

3) Remove columns that are dependent upon thenon primary key.

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 8/35

DECOMPOSING THE SALES RELA

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 9/35

CustID Name

CustID

Salesperson

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 10/35

EX AMPL

Beer

Beer Brewery Strength City Region

Choice Websters XX York North West

Old Bob Websters XXX York North West

Landlord Taylors XXX Leeds North West

Directors Fremlins X London South East

 Wobbly Joy  Sam

Smith

XXXX York North West

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 11/35

functional dependencies(beer, brewery, strength, city, region)

brewery p city

cityp region

beer p brewery, strength, city, region

EX AMPL

E..

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 12/35

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 13/35

Sales order

(SalesOrderNo, Date, CustomerNo, CustomerName,

CustomerAdd, ClerkNo, ClerkName)

EXERCI

SE 

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 14/35

CERTIFICATION-PROGRAM table(area, course, section, time, location)

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 15/35

This relation is in 3NF because neither of the

nonprime attributes functionally determines the

other attributes.However, if there is only one location per city then

Location Area. If we delete the last tuple of the

relation we lose information about the location of 

the educational center.

 ANOMALY 

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 16/35

Student number Course Teacher

101 History Singh

101 Art Asha102 Geography Aanchal

103 Sociology Smith

104 Economics Pradeep

105 History Singh

106 History Rekha

EX AMPL

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 17/35

 A relation r (R) is in Boyce-Codd form (BCNF) if 

and only if the following conditions are met

simultaneously1.The relation is in 1NF

2.For every functional dependency X A, we

have that either A X or X is a super key of r.

In other words, every functional dependency is

a trivial dependency or if it is not trivial

dependency then X must be a super key.

BOYCE-CODD NORMAL FORM

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 18/35

 A relation is in BCNF if every determinant is a

candidate key.

 A determinant is a column on which some of the

columns are fully functional dependent.

 ALTE

RNATE

DE

FINITION

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 19/35

Student

number(pk)

 Teacher(pk)

101 Singh

101 Asha

102 Aanchal

103 Smith

104 Pradeep

105 Singh

106 Rekha

  Teacher (pk) Course

Singh History  

  Asha Art  Aanchal Geography 

Smith Sociology  

Pradeep Economics

Rekha History  

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 20/35

EX

 AMPLE

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 21/35

Set 1:

Manufacturer (id, name)

M-part (id, item-no, qty)

Set 2:

Manufacturer (id, name)

M-part (name, item-no, qty)

S

OLUTION

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 22/35

ClientInterview

ClientClient

NoNo

interviewinterview

DateDate

interviewinterview

TimeTimestaffNostaffNo roomNoroomNo

CR76 13-May-02 10.30 SG5 G101

CR76 13-May-02 12.00 SG5 G101

CR74 13-May-02 12.00 SG37 G102

CR56 1-Jul-02 10.30 SG5 G102

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 23/35

FD1 clientNo, interviewDate interviewTime, staffNo,

roomNo (Primary Key)

FD2 staffNo, interviewDate, interviewTime

clientNo(Candidate key)

FD3 roomNo, interviewDate, interviewTime clientNo,

staffNo (Candidate key)

FD4 staffNo, interviewDate roomNo (not a candidatekey) As a consequence the ClientInterview relation may 

suffer from update anomalies. For example, two tuples have to be

updated if the roomNo need be changed for staffNo SG5 on the

13-May-02.

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 24/35

To transform the ClientInterview relation to BCNF, wemust remove the violating functional dependency bycreating two new relations called Interview and StaffRoomas shown below

Interview (clientNo, interviewDate, interviewTime,staffNo)

StaffRoom(staffNo, interviewDate, roomNo)

BCNF Interview and StaffRoom relations

EX

 AMPLE

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 25/35

ClientNoClientNo interviewDateinterviewDate interviewTimeinterviewTime staffNostaffNo

CR76 13-May-02 10.30 SG5

CR76 13-May-02 12.00 SG5

CR74 13-May-02 12.00 SG37

CR56 1-Jul-02 10.30 SG5

staffNostaffNo interviewDateinterviewDate roomNoroomNo

SG5 13-May-02 G101

SG37 13-May-02 G102

SG5 1-Jul-02 G102

Interview

StaffRoom

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 26/35

CONCURR ENCY MANAGEMENT

Serializability Test

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 27/35

Read lock (shared locks): a transaction T wishing

only to read an item A executes RLOCK A which preventsany other transaction from writing a new value of A while

T is reading A.

Write Lock (exclusive locks): a transaction wishing to

change the value of item A first obtains a write lock byexecuting WLOCK A. when some transaction holds a write

lock on an item no other transaction can obtain a readlock or write lock on that item.

Both locks can be removed using UNLOCK statement.

LOCK S

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 28/35

Input: a schedule S for a set of transactions T1, Tk

Output: a determination whether S is serializable and if 

so an equivalent serial schedule

Method: we construct a precedence graph G as follows

the nodes correspond to the transactions as before. The

arcs are determined by the following rules

1)Suppose in S transaction Ti, read locks A and Tj is the

next transaction to write lock A then place an arc from Ti

to Tj.

SERIALIZABILITY TEST FOR SERIALIZABILITY TEST FOR 

SCHEDULES WITH READ/WRITE SCHEDULES WITH READ/WRITE 

LOCKSLOCKS

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 29/35

2. Suppose in S, transaction Ti write locks A and Tj is thenext transaction to write lock A. then draw an arc Ti to

Tj. Further Tm be any transaction that reads locks Aafter Ti unlocks its write lock but before Tj write locks A. if there is no Tj, then Tm is any transaction to readlock A after Ti unlocks A. then draw an arc Ti to Tm.

If G has a cycle, then S is not serializable. If G is acyclicthen any topological sort of G is a serial order for thetransaction

CONT..

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 30/35

1 WLOCK A

2 RLOCK B

3 UNLOCK A

4 RLOCK A5 UNLOCK B

6 WLOCK B

7 RLOCK A

8 UNLOCK B9 WLOCK B

10 UNLOCK A

11 UNLOCK A

12 WLOCK A

13 UNLOCK B

14 RLOCK B

15 UNLOCK A

16 UNLOCK B

T1 T2 T3 T4

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 31/35

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 32/35

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 33/35

 T17

 T19

 T18

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 34/35

If G is acyclic, find a serial schedule as follows;

a.Initialize the serial schedule empty

b.Find a transaction Ti, such that there are no arcs enteringTi. Ti is the next transaction in the serial schedule

c.Remove Ti and all edges emerging from it. If theremaining set is empty, serial schedule is complete,otherwise return to b

SOLUTION

8/8/2019 DBMS- A Walk Through 2

http://slidepdf.com/reader/full/dbms-a-walk-through-2 35/35