it-3101 database management systems

43
IT-3101 Database Management Systems By- Jesmin Akhter Assistant Professor, IIT, Jahangirnagar University

Upload: peri

Post on 14-Feb-2016

64 views

Category:

Documents


2 download

DESCRIPTION

IT-3101 Database Management Systems. By- Jesmin Akhter Assistant Professor, IIT, Jahangirnagar University. Lecture 05 Relational Database Design Normalization-Part-2. Outline. Overview of Relational DBMS Normalization. Part_ID  Description Part_ID  Price Part_ID, Comp_ID  No. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: IT-3101  Database Management Systems

IT-3101 Database Management

SystemsBy-

Jesmin AkhterAssistant Professor, IIT, Jahangirnagar University

Page 2: IT-3101  Database Management Systems

Lecture 05 Relational Database Design

Normalization-Part-2

Page 3: IT-3101  Database Management Systems

Outline Overview of Relational DBMS

Normalization

Page 4: IT-3101  Database Management Systems

PART

Part_ID Descr Price Comp_ID No

Example 3: Determine NF

Part_ID Description Part_ID Price Part_ID, Comp_ID No

Comp_ID and No are not determined by the primary key; therefore, the relation is NOT in 1 NF. No sense

in looking at partial or transitive dependencies.

Part_ID Descr Price Comp_ID No

Page 5: IT-3101  Database Management Systems

Example 3: Determine NF

Part_ID Description Part_ID Price Part_ID, Comp_ID No

PART

Part_ID Descr Price Comp_ID No

In your solution you will write the following justification:

1) There are M/V attributes; therefore, not 1NF

Conclusion: The relation is not normalized.

Page 6: IT-3101  Database Management Systems

Bringing a Relation to 1NF

STUDENT

Stud_ID Name Course_ID Units

101 Lennon MSI 250 3.00

101 Lennon MSI 415 3.00

125 Johnson MSI 331 3.00

Page 7: IT-3101  Database Management Systems

Bringing a Relation to 1NF

STUDENT

Stud_ID Name Course_ID Units

101 Lennon MSI 250 3.00

101 Lennon MSI 415 3.00

125 Johnson MSI 331 3.00

Option 1: Make a determinant of the repeating group (or the multivalued attribute) a part of the primary key.

Composite Primary Key

Page 8: IT-3101  Database Management Systems

Bringing a Relation to 1NF Option 2: Remove the entire repeating group from

the relation. Create another relation which would contain all the attributes of the repeating group, plus the primary key from the first relation. In this new relation, the primary key from the original relation and the determinant of the repeating group will comprise a primary key. STUDENT

Stud_ID Name Course_ID Units

101 Lennon MSI 250 3.00

101 Lennon MSI 415 3.00

125 Johnson MSI 331 3.00

Page 9: IT-3101  Database Management Systems

Bringing a Relation to 1NF

STUDENT_COURSE

Stud_ID Course Units

101 MSI 250 3

101 MSI 415 3

125 MSI 331 3

STUDENT

Stud_ID Name

101 Lennon

125 Jonson

Page 10: IT-3101  Database Management Systems

Bringing a Relation to 2NF

STUDENT

Stud_ID Name Course_ID Units

101 Lennon MSI 250 3.00

101 Lennon MSI 415 3.00

125 Johnson MSI 331 3.00

Composite Primary Key

Page 11: IT-3101  Database Management Systems

Bringing a Relation to 2NF

STUDENT

Stud_ID Name Course_ID Units

101 Lennon MSI 250 3.00

101 Lennon MSI 415 3.00

125 Johnson MSI 331 3.00

Composite Primary Key

Goal: Remove Partial DependenciesPartial

Dependencies

Page 12: IT-3101  Database Management Systems

Bringing a Relation to 2NF

STUDENT

Stud_ID Name Course_ID Units

101 Lennon MSI 250 3.00

101 Lennon MSI 415 3.00

125 Johnson MSI 331 3.00

Remove attributes that are dependent from the part but not the whole of the primary key from the original relation. For each partial dependency, create a new relation, with the corresponding part of the primary key from the original as the primary key.

Page 13: IT-3101  Database Management Systems

Bringing a Relation to 2NFCUSTOMER

Stud_ID Name Course_ID Units

101 Lennon MSI 250 3.00

101 Lennon MSI 415 3.00

125 Johnson MSI 331 3.00

STUDENT_COURSE

Stud_ID Course_ID

101 MSI 250

101 MSI 415

125 MSI 331

COURSE

Course_ID Units

MSI 250 3.00

MSI 415 3.00

MSI 331 3.00

STUDENT

Stud_ID Name

101 Lennon

101 Lennon

125 Johnson

Page 14: IT-3101  Database Management Systems

Bringing a Relation to 3NF

Goal: Get rid of transitive dependencies.

EMPLOYEE

Emp_ID F_Name L_Name Dept_ID Dept_Name

111 Mary Jones 1 Acct

122 Sarah Smith 2 Mktg

Transitive Dependency

Page 15: IT-3101  Database Management Systems

Bringing a Relation to 3NF Remove the attributes, which are dependent on a

non-key attribute, from the original relation. For each transitive dependency, create a new relation with the non-key attribute which is a determinant in the transitive dependency as a primary key, and the dependent non-key attribute as a dependent.

EMPLOYEE

Emp_ID F_Name L_Name Dept_ID Dept_Name

111 Mary Jones 1 Acct

122 Sarah Smith 2 Mktg

Page 16: IT-3101  Database Management Systems

Bringing a Relation to 3NF

EMPLOYEE

Emp_ID F_Name L_Name Dept_ID111 Mary Jones 1

122 Sarah Smith 2

EMPLOYEE

Emp_ID F_Name L_Name Dept_ID Dept_Name

111 Mary Jones 1 Acct

122 Sarah Smith 2 Mktg

DEPARTMENT

Dept_ID Dept_Name

1 Acct

2 Mktg

Page 17: IT-3101  Database Management Systems

Unnormalised Normal Form (UNF)ORDERCustomer No: 001964 Order Number: 00012345Name: Mark Campbell Order Date: 14-Feb-2002Address: 1 The House

LeytonstoneE11 9ZZ

Product Product Unit Order LineNumber Description Price Quantity Total

T5060 Hook 5.00 5 25.00

PT42 Bolt 2.50 10 20.50

QZE48 Spanner 20.00 1 20.00

Order Total: 65.50

ORDER (order-no, order-date, cust-no, cust-name, cust-add, (prod-no, prod-desc, unit-price, ord-qty, line-total)*, order-total

Page 18: IT-3101  Database Management Systems

First Normal Form (1NF)Definition: A relation is in 1NF if, and only if, all its underlying attributes contain atomic values only.

Steps from UNF to 1NF: Remove the outermost repeating group (and any nested repeated

groups it may contain) and create a new relation to contain it. Add to this relation a copy of the PK of the relation immediately

enclosing it. Name the new entity (appending the number 1 to indicate 1NF) Determine the PK of the new entity Repeat steps until no more repeating groups.

Remove repeating groups into a new relation

A repeating group is shown by a pair of brackets within the relational schema.ORDER (order-no, order-date, cust-no, cust-name, cust-add,

(prod-no, prod-desc, unit-price, ord-qty, line-total)*, order-total

Page 19: IT-3101  Database Management Systems

Example - UNF to 1NFORDER (order-no, order-date, cust-no, cust-name, cust-add,

(prod-no, prod-desc, unit-price, ord-qty, line-total)*, order-total

1. Remove the outermost repeating group (and any nested repeated groups it may contain) and create a new relation to contain it. (rename original to indicate 1NF)

ORDER-1 (order-no, order-date, cust-no, cust-name, cust-add, order-total

(prod-no, prod-desc, unit-price, ord-qty, line-total)2. Add to this relation a copy of the PK of the relation immediately enclosing it.

ORDER-1 (order-no, order-date, cust-no, cust-name, cust-add, order-total

(order-no, prod-no, prod-desc, unit-price, ord-qty, line-total)3. Name the new entity (appending the number 1 to indicate 1NF)

ORDER-LINE-1 (order-no, prod-no, prod-desc, unit-price, ord-qty, line-total)

4. Determine the PK of the new entity

ORDER-LINE-1 (order-no, prod-no, prod-desc, unit-price, ord-qty, line-total)

Page 20: IT-3101  Database Management Systems

Second Normal Form (2NF)Definition: A relation is in 2NF if, and only if, it is in 1NF and every non-key attribute is fully dependent on the primary key.

Steps from 1NF to 2NF: Remove the offending attributes that are only partially functionally

dependent on the composite key, and place them in a new relation. Add to this relation a copy of the attribute(s) which are the

determinants of these offending attributes. These will automatically become the primary key of this new relation.

Name the new entity (appending the number 2 to indicate 2NF)

Rename the original entity (ending with a 2 to indicate 2NF)

Remove partial functional dependencies into a new relation

Page 21: IT-3101  Database Management Systems

Example - 1NF to 2NFORDER-LINE-1 (order-no, prod-no, prod-desc, unit-price, ord-qty, line-total)

1. Remove the offending attributes that are only partially functionally dependent on the composite key, and place them in a new relation.

ORDER-LINE-1 (order-no, prod-no, ord-qty, line-total)

(prod-desc, unit-price)2. Add to this relation a copy of the attribute(s) which determines these offending attributes. These will automatically become the primary key of this new relation..

(prod-no, prod-desc, unit-price)

ORDER-LINE-1 (order-no, prod-no, ord-qty, line-total)

3. Name the new entity (appending the number 2 to indicate 2NF)

PRODUCT-2 (prod-no, prod-desc, unit-price)4. Rename the original entity (ending with a 2 to indicate 2NF)

ORDER-LINE-2 (order-no, prod-no, ord-qty, line-total)

Page 22: IT-3101  Database Management Systems

Third Normal Form (3NF)Definition: A relation is in 3NF if, and only if, it is in 2NF and every non-key attribute is non-transitively dependent on the primary key.

Steps from 2NF to 3NF: Remove the offending attributes that are transitively

dependent on non-key attribute(s), and place them in a new relation.

Add to this relation a copy of the attribute(s) which are the determinants of these offending attributes. These will automatically become the primary key of this new relation.

Name the new entity (appending the number 3 to indicate 3NF)

Rename the original entity (ending with a 3 to indicate 3NF)

Remove transitive dependencies into a new relation

Page 23: IT-3101  Database Management Systems

Example - 2NF to 3NFORDER-2 (order-no, order-date, cust-no, cust-name, cust-add, order-total

1. Remove the offending attributes that are transitively dependent on non-key attributes, and place them in a new relation.

(cust-name, cust-add )

ORDER-2 (order-no, order-date, cust-no, order-total

2. Add to this relation a copy of the attribute(s) which determines these offending attributes. These will automatically become the primary key of this new relation..

(cust-no, cust-name, cust-add )

ORDER-2 (order-no, order-date, cust-no, order-total

3. Name the new entity (appending the number 3 to indicate 3NF)

CUSTOMER-3 (cust-no, cust-name, cust-add )

4. Rename the original entity (ending with a 3 to indicate 3NF)

ORDER-3 (order-no, order-date, cust-no, order-total

Page 24: IT-3101  Database Management Systems

Example - Relations in 3NF

CUSTOMER-3 (cust-no, cust-name, cust-add )

ORDER-3 (order-no, order-date, cust-no, order-total

ORDER-LINE-2 (order-no, prod-no, ord-qty, line-total)

PRODUCT-2 (prod-no, prod-desc, unit-price)

CUSTOMER

ORDER

ORDER-LINE

PRODUCT

places placed by contains

part of

showsbelongs to

cust-no

order-no prod-no

order-no, prod-no

Page 25: IT-3101  Database Management Systems

Case Study on Normalization

Consider the table EMP_DEPT_PROJ

And the following dependencies which exist in the above table:

Slide 25

Page 26: IT-3101  Database Management Systems

Steps to Normalize the database D

Thus we will have 3 tables

Slide 26

Page 27: IT-3101  Database Management Systems

Step 2 Let us now identify the transitive dependency and remove it.

Slide 27

Steps to Normalize the database

Page 28: IT-3101  Database Management Systems

Step 3: Let us now identify the non key determinants and

remove them.

Thus we will have 5 tables

Slide 28

Steps to Normalize the database

Page 29: IT-3101  Database Management Systems

 Suppose The Gill Art Gallery wishes to maintain data on their customers, artists and paintings. They may have several paintings by each artist in the gallery at one time. Paintings may be bought and sold several times. In other words, the gallery may sell a painting, then buy it back at a later date and sell it to another customer.

UNF: customer [ custno, cust_name, cust_addr, cust_phone,

( artist_id, artist_name, art_title, pur_date, price) ]  1NF:

customer [ custno, cust_name, cust_addr, cust_phone] cust_art [ custno, art_code, pur_date, artist_id,

artist_name, art_title, price ]

Slide 29

Case Study on Normalization

Page 30: IT-3101  Database Management Systems

UNF: customer [ custno, cust_name, cust_addr, cust_phone,

( artist_id, artist_name, art_title, pur_date, price) ]  1NF:

customer [ custno, cust_name, cust_addr, cust_phone] cust_art [ custno, art_code, pur_date, artist_id,

artist_name, art_title, price ]o  note: the key chosen for the repeating group is the piece

of art itself (a code was assigned), however because a piece of art may be bought by a customer more than once, the purchase date was added as part of the key to make the rows unique.

Slide 30

Case Study on Normalization

Page 31: IT-3101  Database Management Systems

 1NF: customer [ custno, cust_name, cust_addr, cust_phone] cust_art [ custno, art_code, pur_date, artist_id,

artist_name, art_title, price ] 2NF:

customer [ custno, cust_name, cust_addr, cust_phone] cust_art [ custno, art_code, pur_date, price ] art [ art_code, art_title, artist_id, artist_name ]

Slide 31

Case Study on Normalization

Page 32: IT-3101  Database Management Systems

2NF: customer [ custno, cust_name, cust_addr, cust_phone] cust_art [ custno, art_code, pur_date, price ] art [ art_code, art_title, artist_id, artist_name ]

3NF: customer [ custno, cust_name, cust_street, cust_city,

cust_prov, cust_pstlcd, cust_phone] cust_art [ custno, art_code, pur_date, price ] art [ art_code, art_title, artist_id(FK) ] artist [ artist_id, artist_fname, artist_lname ]

Slide 32

Case Study on Normalization

Page 33: IT-3101  Database Management Systems

3NF: customer [ custno, cust_name, cust_street, cust_city,

cust_prov, cust_pstlcd, cust_phone] cust_art [ custno, art_code, pur_date, price ] art [ art_code, art_title, artist_id(FK) ] artist [ artist_id, artist_fname, artist_lname ]

Slide 33

Case Study on Normalization

Page 34: IT-3101  Database Management Systems

Review Class on

Relational Algebra

Slide 34

Page 35: IT-3101  Database Management Systems

Natural Join Operation – Example Relations r, s:

A B

12412

C D

aabab

B

13123

D

aaabb

E

r

A B

11112

C D

aaaab

E

s

r s

Slide 35

r.A, r.B, r.C, r.D, s.E (r.B = s.B r.D = s.D (r x s))

Page 36: IT-3101  Database Management Systems

The customer Relation

36

The branch Relation

The depositor Relation Account Relation

Page 37: IT-3101  Database Management Systems

borrower Relation

37

The Loan RelationLoan-number Branch-name amountL-11 Round Hill 900

L-14 Downtown 1500

L-15 Perryridge 1500

L-16 Perryridge 1300

L-17 Downtown 1000

L-23 Redwood 2000

L-93 Mianus 500

Page 38: IT-3101  Database Management Systems

Result of customer-name, branch-name(depositor account)

38

Result of customer-name, loan-number, amount (borrower loan)

Page 39: IT-3101  Database Management Systems

Division Operation – ExampleRelations r, s:

r s: A

B

1

2

A B

12311134612

r

s

39

Page 40: IT-3101  Database Management Systems

Another Division ExampleA B

aaaaaaaa

C D

aabababb

E

11113111

Relations r, s:

r s:

D

ab

E

11

A B

aa

C

r

s

40

Page 41: IT-3101  Database Management Systems

Find all customers who have an account at all branches located in Brooklyn city.

Example Queries

customer-name, branch-name (depositor account) branch-name (branch-city = “Brooklyn” (branch))

Slide 41

depositor account branch

Page 42: IT-3101  Database Management Systems

Result of customer-name, branch-name(depositor account)

42

Result of branch-name(branch-city = “Brooklyn”(branch))

customer-name, branch-name (depositor account) branch-name (branch-city = “Brooklyn” (branch))

Customer-nameJohnson

Page 43: IT-3101  Database Management Systems

Thank You