sql normalization database design lecture 5. copyright 2006page 2 sql normalization database design...

11
SQL Normalization SQL Normalization Database Design Database Design Lecture 5 Lecture 5

Upload: barnaby-gregory

Post on 28-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SQL Normalization Database Design Lecture 5. Copyright 2006Page 2 SQL Normalization Database Design 1 st Normal Form 1 st Normal Form 2 nd Normal Form

SQL NormalizationSQL NormalizationDatabase DesignDatabase Design

Lecture 5Lecture 5

Page 2: SQL Normalization Database Design Lecture 5. Copyright 2006Page 2 SQL Normalization Database Design 1 st Normal Form 1 st Normal Form 2 nd Normal Form

Copyright 2006Copyright 2006 Page Page 22

SQL NormalizationSQL Normalization

Database DesignDatabase Design

11stst Normal Form Normal Form 22ndnd Normal Form Normal Form 33rdrd Normal Form Normal Form Boyce-Codd Normal Form (BCNF)Boyce-Codd Normal Form (BCNF) 44thth Normal Form Normal Form 55thth Normal Form Normal Form 66thth Normal Form Normal Form Domain Key Normal Form (DKNF)Domain Key Normal Form (DKNF)

Page 3: SQL Normalization Database Design Lecture 5. Copyright 2006Page 2 SQL Normalization Database Design 1 st Normal Form 1 st Normal Form 2 nd Normal Form

Copyright 2006Copyright 2006 Page Page 33

SQL NormalizationSQL Normalization

Database Design: 1Database Design: 1stst Normal Form Normal Form

11stst Normal Form has 8 rules: Normal Form has 8 rules: Rows contain data for instances of an entity.Rows contain data for instances of an entity. Columns contain attributes that describe the data Columns contain attributes that describe the data

of an entity or its domain.of an entity or its domain. Attributes in a given row, also known as a cell in Attributes in a given row, also known as a cell in

spreadsheets, contain a single value.spreadsheets, contain a single value. All attributes define data by a single data type.All attributes define data by a single data type. All attributes have a unique name in an entity.All attributes have a unique name in an entity. Attributes are arbitrarily ordered.Attributes are arbitrarily ordered. Rows of data have no implicit or explicit ordering Rows of data have no implicit or explicit ordering

to access or use.to access or use. Two rows may not contain duplicate data.Two rows may not contain duplicate data.

Page 4: SQL Normalization Database Design Lecture 5. Copyright 2006Page 2 SQL Normalization Database Design 1 st Normal Form 1 st Normal Form 2 nd Normal Form

Copyright 2006Copyright 2006 Page Page 44

SQL NormalizationSQL Normalization

Database Design: 1Database Design: 1stst Normal Form Normal Form

11stst Normal Form: Normal Form: Summary: A list of columns that (a) contain Summary: A list of columns that (a) contain

only an instance of a data type; and (b) only an instance of a data type; and (b) define a collectively unique row.define a collectively unique row.

Design Key #1: A single instance means Design Key #1: A single instance means that delimited values are not stored into a that delimited values are not stored into a column, like a comma separated values column, like a comma separated values (CSV) list.(CSV) list.

Design Key #2: All columns taken together Design Key #2: All columns taken together define a unique key that identifies a data define a unique key that identifies a data structure.structure.

Page 5: SQL Normalization Database Design Lecture 5. Copyright 2006Page 2 SQL Normalization Database Design 1 st Normal Form 1 st Normal Form 2 nd Normal Form

Copyright 2006Copyright 2006 Page Page 55

SQL NormalizationSQL Normalization

Database Design: 2Database Design: 2ndnd Normal Form Normal Form

22ndnd Normal Form: Normal Form: Summary: A row is in 2Summary: A row is in 2ndnd normal form when the normal form when the

list of all non-key columns (or attributes) are list of all non-key columns (or attributes) are fully dependent on the list of key columns, and fully dependent on the list of key columns, and the row meets the definition of 1the row meets the definition of 1stst normal form. normal form.

Design Key #1: The list of key columns defines Design Key #1: The list of key columns defines the natural key that identifies a unique row.the natural key that identifies a unique row.

Design Key #2: A surrogate key (e.g., an Design Key #2: A surrogate key (e.g., an automated numbering sequence) may replace automated numbering sequence) may replace the natural key.the natural key.

Page 6: SQL Normalization Database Design Lecture 5. Copyright 2006Page 2 SQL Normalization Database Design 1 st Normal Form 1 st Normal Form 2 nd Normal Form

Copyright 2006Copyright 2006 Page Page 66

SQL NormalizationSQL Normalization

Database Design: 3Database Design: 3rdrd Normal Form Normal Form

33rdrd Normal Form: Normal Form: Summary: A row is in 2Summary: A row is in 2ndnd normal form when (a) normal form when (a)

there is no transitive dependency, which is a there is no transitive dependency, which is a functional dependency; and (b) the row meets the functional dependency; and (b) the row meets the definition of 2definition of 2ndnd normal form. normal form.

Design Key #1: A functional dependency means Design Key #1: A functional dependency means that the domain is a that the domain is a super domain of two domainssuper domain of two domains, , and deleting a row from the base domain removes and deleting a row from the base domain removes an instance from the functionally dependent an instance from the functionally dependent domain.domain.

Design Key #2: Functional dependencies can Design Key #2: Functional dependencies can cause cause deletion anomaliesdeletion anomalies..

Page 7: SQL Normalization Database Design Lecture 5. Copyright 2006Page 2 SQL Normalization Database Design 1 st Normal Form 1 st Normal Form 2 nd Normal Form

Copyright 2006Copyright 2006 Page Page 77

SQL NormalizationSQL Normalization

Database Design: 3Database Design: 3rdrd Normal Form Normal Form Functional Dependency: Functional Dependency:

A 2A 2ndnd normal table exists when normal table exists when the domain contains both:the domain contains both:

A A RENTAL_IDRENTAL_ID that identifies that identifies two domains – the item rented two domains – the item rented and its rental price.and its rental price.

A deletion of A deletion of RENTAL_IDRENTAL_ID 991 991 results in deleting the rental results in deleting the rental and the item rental price.and the item rental price.

The The ITEM_IDITEM_ID column is a column is a functional dependency or functional dependency or transitive dependency.transitive dependency.

Create two 3Create two 3rdrd normal form normal form tables by dividing the table tables by dividing the table into its separate domains.into its separate domains.

The The ITEM_IDITEM_ID becomes the becomes the primary key in one table and primary key in one table and foreign key in the other.foreign key in the other.

RENTAL_ID ITEM_ID CHARGE

991 5-Day Rental 5

992 3-Day Rental 3

993 1-Day Rental 1

994 3-Day Rental 3

995 1-Day Rental 1

Page 8: SQL Normalization Database Design Lecture 5. Copyright 2006Page 2 SQL Normalization Database Design 1 st Normal Form 1 st Normal Form 2 nd Normal Form

Copyright 2006Copyright 2006 Page Page 88

SQL NormalizationSQL Normalization

Database Design: 3Database Design: 3rdrd Normal Form Normal Form

Functional Dependency Resolution: Functional Dependency Resolution: The The ITEM_IDITEM_ID column becomes a primary key and unique description of column becomes a primary key and unique description of

any row in a 3any row in a 3rdrd normal form table. normal form table. The The CHARGECHARGE column becomes wholly dependent on the new primary key. column becomes wholly dependent on the new primary key. The The RENTAL_IDRENTAL_ID column remains the primary key of a reduced domain column remains the primary key of a reduced domain

and the and the ITEM_IDITEM_ID becomes a foreign key to the new 3 becomes a foreign key to the new 3rdrd normal form normal form table.table.

The functional dependency of The functional dependency of RENTAL_IDRENTAL_ID CHARGECHARGE is eliminated. is eliminated.

ITEM_ID CHARGE

5-Day Rental 5

3-Day Rental 3

1-Day Rental 1

RENTAL_ID ITEM_ID

991 5-Day Rental

992 3-Day Rental

993 1-Day Rental

994 3-Day Rental

995 1-Day Rental

Page 9: SQL Normalization Database Design Lecture 5. Copyright 2006Page 2 SQL Normalization Database Design 1 st Normal Form 1 st Normal Form 2 nd Normal Form

Copyright 2006Copyright 2006 Page Page 99

SQL NormalizationSQL Normalization

Database Design: BCNFDatabase Design: BCNF

Boyce-Codd Normal Form (BCNF):Boyce-Codd Normal Form (BCNF): Summary: A row is in BCNF when (a) relations Summary: A row is in BCNF when (a) relations

have a single determinant as a candidate key; and have a single determinant as a candidate key; and (b) the row meets the definition of 3(b) the row meets the definition of 3rdrd normal form. normal form.

Design Key #1: A determinant lets you identify a Design Key #1: A determinant lets you identify a row by combining it with one or more other row by combining it with one or more other columns to find a unique row in a relation, which is columns to find a unique row in a relation, which is also known as a super key.also known as a super key.

Design Key #2: A determinant is one or more Design Key #2: A determinant is one or more attribute that does not qualify as a candidate key attribute that does not qualify as a candidate key for a table but defines an internally or externally for a table but defines an internally or externally unique relation for a table.unique relation for a table.

Page 10: SQL Normalization Database Design Lecture 5. Copyright 2006Page 2 SQL Normalization Database Design 1 st Normal Form 1 st Normal Form 2 nd Normal Form

Copyright 2006Copyright 2006 Page Page 1010

SQL NormalizationSQL Normalization

Database Design: BCNFDatabase Design: BCNF Determinant Problem: Determinant Problem:

A 3A 3rdrd normal table exists when normal table exists when the domain contains both:the domain contains both:

A A RENTAL_IDRENTAL_ID that identifies that identifies two domains – the item rented two domains – the item rented and its type – DVD or VHS.and its type – DVD or VHS.

A deletion of A deletion of RENTAL_IDRENTAL_ID 991 991 results in deleting the rental results in deleting the rental and the VHS item and the VHS item TYPETYPE of a of a TITLETITLE..

The The TYPETYPE and and TITLETITLE column column are only inserted when there are only inserted when there is a is a RENTAL_IDRENTAL_ID, which means , which means the type of rental cannot be the type of rental cannot be inserted before it is rented.inserted before it is rented.

Create two BCNF tables by Create two BCNF tables by dividing the table into its dividing the table into its separate domains.separate domains.

RENTAL_ID TYPE TITLE

991 VHS STAR WARS IV

992 VHS STAR WARS V

993 DVD STAR WARS VI

994 DVD STAR WARS IV

995 VHS STAR WARS III

Page 11: SQL Normalization Database Design Lecture 5. Copyright 2006Page 2 SQL Normalization Database Design 1 st Normal Form 1 st Normal Form 2 nd Normal Form

Copyright 2006Copyright 2006 Page Page 1111

SummarySummary

11stst Normal Form Normal Form 22ndnd Normal Form Normal Form 33rdrd Normal Form Normal Form Boyce-Codd Normal FormBoyce-Codd Normal Form 44thth Normal Form Normal Form 55thth Normal Form Normal Form 66thth Normal Form Normal Form Domain Key Normal Form Domain Key Normal Form

(DKNF)(DKNF)