normalization

23
Normalization Normalization What is What is Normalization Normalization ? ? Normalization Levels Normalization Levels First Normal Form First Normal Form Second Normal Form Second Normal Form Third Normal Formal Third Normal Formal Referential Integrity Referential Integrity

Upload: more

Post on 13-Jan-2016

27 views

Category:

Documents


0 download

DESCRIPTION

Normalization. What is Normalization ? Normalization Levels First Normal Form Second Normal Form Third Normal Formal Referential Integrity. What Is Normalization?. Definition process of reducing data redundancy in a relational database Process - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Normalization

NormalizationNormalization

What is What is NormalizationNormalization??

Normalization LevelsNormalization Levels– First Normal FormFirst Normal Form– Second Normal FormSecond Normal Form– Third Normal FormalThird Normal Formal

Referential IntegrityReferential Integrity

Page 2: Normalization

What Is Normalization?What Is Normalization?DefinitionDefinition– process of reducing data redundancy in a relational process of reducing data redundancy in a relational

databasedatabase

ProcessProcess– by organizing data into tables of various by organizing data into tables of various Normal Normal

FormsForms

BenefitsBenefits– greater organization of databasegreater organization of database

– reduction of redundant datareduction of redundant data

– data consistency in databasedata consistency in database

– more flexible database designmore flexible database design

– better handle on database securitybetter handle on database security

Page 3: Normalization

Normal FormsNormal Forms

DefinitionDefinition– way of measuring the extent to which way of measuring the extent to which

database has been constrained (to reduce database has been constrained (to reduce redundancy)redundancy)

Levels of NormalizationLevels of Normalization– First Normal Form (1NF)First Normal Form (1NF)– Second Normal Form (2NF)Second Normal Form (2NF)– Third Normal Form (3NF)Third Normal Form (3NF)

Page 4: Normalization

First Normal FormFirst Normal FormInformal DescriptionInformal Description

Each record is uniqueEach record is uniqueAll attribute values are atomicAll attribute values are atomic

Objective of 1NF isObjective of 1NF isto divide the database into tablesto divide the database into tablesso that each row is unique (with primary key)so that each row is unique (with primary key)and each field is atomic (single item per cell)and each field is atomic (single item per cell)

What about this? (Employees Table)What about this? (Employees Table)lastName: TomlastName: TomfirstName: SmithfirstName: Smithaddress: 123 Apple St.address: 123 Apple St.city: San Franciscocity: San Francisco

Page 5: Normalization

emp_idlast_namefirst_namemiddle_nameaddresscitystatezipphonepagerpositiondate_hirepay_ratedate_last_raise

cust_idcust_namecust_addresscust_citycust_statecust_zipcust_phonecust_faxorder_numquantityorder_dateprod_idprod_descripcost

Company_Database

(Examples in this section are adapted from R. Stephens & R. Plew, Teach Yourself SQL in 24 Hours, SAMS, p. 47-49)

Page 6: Normalization

emp_idlast_namefirst_namemiddle_nameaddresscitystatezipphonepagerpositionposition_descripdate_hirepay_ratedate_last_raise

cust_idcust_namecust_addresscust_citycust_statecust_zipcust_phonecust_faxorder_numquantityorder_date

prod_idprod_descripcost

Company_DatabaseEmployees

emp_idlast_namefirst_namemiddle_nameaddresscitystatezipphonepagerpositionposition_descripdate_hirepay_ratedate_last_raise

Customers

cust_idcust_namecust_addresscust_citycust_statecust_zipcust_phonecust_faxorder_numquantityorder_date

Products

prod_idprod_descripcost

Page 7: Normalization

Second Normal FormSecond Normal FormInformal DescriptionInformal Description

All of the strictly informational attributes are attributes of All of the strictly informational attributes are attributes of the entities in the table schemethe entities in the table scheme

Objective of 2NF isObjective of 2NF isto take the data items that are only partly dependent on to take the data items that are only partly dependent on primary keyprimary key

and forming a separate table with themand forming a separate table with them

What about this? (Customers Table)What about this? (Customers Table)custID: 12345custID: 12345custLName: ChewcustLName: ChewcustFName: MarycustFName: MaryorderItem: refrigeratororderItem: refrigeratororderQuant: 2orderQuant: 2

Page 8: Normalization

Company_Database

Employees

emp_idlast_namefirst_namemiddle_nameaddresscitystatezipphonepagerpositiondate_hirepay_ratedata_last_raise

Employees

emp_idlast_namefirst_namemiddle_nameaddresscitystatezipphonepager

Employee_Paysemp_idpositionposition_descripdate_hirepay_ratedate_last_raise

Page 9: Normalization

Company_Database

Customers

cust_idcust_namecust_addresscust_citycust_statecust_zipcust_phonecust_faxorder_numquantityorder_date

Customers

cust_idcust_namecust_addresscust_citycust_statecust_zipcust_phonecust_fax

Orders

cust_idorder_numquantityorder_date

Page 10: Normalization

Third Normal FormThird Normal Form

Informal DescriptionInformal DescriptionStrictly informational attributes depends only on a Strictly informational attributes depends only on a primary keyprimary key

Objective of 3NF isObjective of 3NF isto take data items in a table that are not dependent on to take data items in a table that are not dependent on the primary keythe primary key

and form a separate table with themand form a separate table with them

What about this?What about this?

Page 11: Normalization

Company_Database

Employee_Paysemp_idpositiondate_hirepay_ratedata_last_raise Positions

pos_idpositionposition_descrip

Employee_Paysemp_iddate_hirepay_ratedate_last_raise

Page 12: Normalization

QuizQuiz

Normalization is the process of grouping data into Normalization is the process of grouping data into logically related data into tables to reduce redundancy. logically related data into tables to reduce redundancy. (T/F)(T/F)

Having no duplicate or redundant data in a database, and Having no duplicate or redundant data in a database, and having everything in the database normalized, is always having everything in the database normalized, is always the best way to go. (T/F)the best way to go. (T/F)

If data is in the third normal form, it is automatically in the If data is in the third normal form, it is automatically in the first and second normal forms. (T/F)first and second normal forms. (T/F)

What is the major advantage of denormalized database What is the major advantage of denormalized database versus a normalized database?versus a normalized database?

What are some major disadvantages of denormalization?What are some major disadvantages of denormalization?

Page 13: Normalization

Exercise: What Type of Relationships Do the Tables Have?

Positionspos_idpositionposition_descrip

Employee_Payspay_iddate_hirepay_ratedate_last_raise

Orders

order_numquantityorder_date

Customers

cust_idcust_namecust_addresscust_citycust_statecust_zipcust_phonecust_fax

Employees

emp_idlast_namefirst_namemiddle_nameaddresscitystatezipphonepager

Page 14: Normalization

Exercise: Normalize the following data.

Take the following data and normalize it. Keep in mind that, in a real DB, there would be many more items than what is given here.

Employees:

Angela Smith, secretary, 317-545-65879, RR 1 Box 73, Greensburg, IN, 47890, $9.50/hour, started Jan. 22, 1996, SSN is 323149669

Jack Lee Nelson, salesman, 3334 N. Main St., Brownsburg, IN, 45687, 317-852-9901, $35,000.00/year, data started 10/28/95, SSN is 312567342

Customers:

Robert’s Games & Things, 5612 Lafayette Rd., Indianapolis, IN, 46224, 317-291-7888, customer ID is 432A

Reed’s Dairy Bar, 4556 W 10th St., Indianapolis, IN, 46245, 317-271-9823, customer ID is 117A

CustomerOrders:Customer ID is 117A, date of last order is 2/20/1997, product ordered was napkins, and product ID is 661

Page 15: Normalization

Solutions:

Employees Customers Orders

SsnnamestreetcitystatezipphoneNumsalaryhourlyRatestartDateposition

customerIDnamestreetcitystatezipphoneNum

orderIDcustomerIDproductIDproductDescripdateOrdered

Page 16: Normalization

Functional DependencyFunctional Dependency

DefinitionDefinition– If A and B are attributes of relation R, then B If A and B are attributes of relation R, then B

is functionally dependent on A if and only if is functionally dependent on A if and only if each value in R has associated with it exactly each value in R has associated with it exactly one value of B in R.one value of B in R.

– A A B ( A determines B) B ( A determines B)

Page 17: Normalization

Student(stuID, stuName, major, credit, status, SSN)

stuIDS1001S1003S1006S1010S1060

stuNameSmith, TomJones, MaryLee, PamelaBurns, EdwardJones, Mary

statusSenSenFreshJunFresh

MajorHistoryMathCISArtCIS

Credits9095156325

SSN100429500010124567088520876099320985064624738

stuID stuNamestuID stuName, major, credit,s status, SSNSSN stuID, stuName, major, credits, status, SSNcredits status

status credits (Not true)

Page 18: Normalization

Classes(course#, stuID, stuName, facID, sched, room, grade)

Course#ART103AART103AART103ACIS201ACIS201AHST205A

stuIDS1001S1010S1006S1003S1006S1001

schedMWF9MWF9MWF9TH10TH10MWF11

stuNameSmith, TomBurns, EdwardLee, PamelaJones, MaryLee, PamelaSmith, Tom

facIDF101F101F101F105F105F202

gradeA

BAC

course#, stuID stuName, facID, sched, room, gradecourse# facID, sched, roomstuID stuName

RoomH221H221H221M110M110H221

Page 19: Normalization

Second Normal Form (2NF)Second Normal Form (2NF)

A relation is 2NF if & only if it is in first A relation is 2NF if & only if it is in first normal form & all non-key attributes are normal form & all non-key attributes are fully functionally dependent on the keyfully functionally dependent on the key

Note: if R is 1NF and the key consists of a Note: if R is 1NF and the key consists of a single attribute, the relation is single attribute, the relation is automatically 2NF.automatically 2NF.

Page 20: Normalization

Full Functional DependenceFull Functional Dependence

In a relation R, attribute B or R is In a relation R, attribute B or R is fully fully functionally dependentfunctionally dependent on an attribute or on an attribute or set of attributes A of R, if B is functionally set of attributes A of R, if B is functionally dependent on A but not functionally dependent on A but not functionally dependent on any proper subset of Adependent on any proper subset of A

Page 21: Normalization

Classes(course#, stuID, stuName, facID, sched, room, grade)

course#, stuID stuName, facID, sched, room, gradecourse# facID, sched, roomstuID stuName

classes2(course#, stuID, grade)course(course#, facID, sched, room)students(stuID, stuName)

Page 22: Normalization

Third Normal Form (3NF)Third Normal Form (3NF)

A relation R is 3NF if it is 2NF and no non-A relation R is 3NF if it is 2NF and no non-key attribute is transitively dependent on key attribute is transitively dependent on the key.the key.

Transitive dependenceTransitive dependenceA A B B C CstdID stdID credits credits status status

Page 23: Normalization

student(stuID, stuName, major, credit, status)

stuID credits status

stuID status

students2(stuID, stuName, major, credits)stats(credits, status)