Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Normalization. What is Normalization Normalization allows us to organize data so that it: Normalization allows us to organize data so that it:

Similar presentations


Presentation on theme: "Database Normalization. What is Normalization Normalization allows us to organize data so that it: Normalization allows us to organize data so that it:"— Presentation transcript:

1 Database Normalization

2 What is Normalization Normalization allows us to organize data so that it: Normalization allows us to organize data so that it: Allows faster access (dependencies make sense)Allows faster access (dependencies make sense) Reduced space (less redundancy)Reduced space (less redundancy)

3 Data Normalization Primarily a tool to validate and improve a logical design so that it satisfies certain constraints that avoid unnecessary duplication of data The process of decomposing relations with anomalies to produce smaller, well- structured relations

4 Results of Normalization Removes the following modification anomalies (integrity errors) with the database Insertion Deletion Update

5 ANOMALIES Insertion inserting one fact in the database requires knowledge of other facts unrelated to the fact being inserted Deletion Deleting one fact from the database causes loss of other unrelated data from the database Update Updating the values of one fact requires multiple changes to the database

6 Insertion Anomaly  Suppose we want to enter the new Job 'Cooker' having $800 as a salary. We cannot enter the data into the EMPLOYEE relation until an employee enrolls in this Job.  This restriction seems silly. Why should we have to wait until someone takes the Job before we can record its salary? This situation is called insertion anomaly. It is due to the relational model constraint that disables 'Null values' for the primary key (EMPID). EMPIDNAMEJOBSALAR Y 200SmithDriver1500 300JohnGuardian2000 600GeorgeGuardian2000 700JeanGardener2500 450MikeGardener2500 EMPLOYEE

7 Insertion Anomaly JobSalary Driving1500 Guardian2000 Gardener2500 EmpIdNameJob 200Smithdriving 300JohnGuardian 600GeorgeGuardian 700JeanGardener 450MikeGardener EMP-JOBJOB-SAL These anomalies can be eliminated by decomposing the employee relation into the two ones shown in the following figure.

8 ANOMALIES EXAMPLES TABLE: COURSE COURSE#SECTION#C_NAME CIS564072Database Design CIS564073Database Design CIS570072Oracle Forms CIS564074Database Design

9 ANOMALIES EXAMPLES I nsertion: Suppose our College has approved a new course called CIS 563: SQL & PL/SQL. Can this information about the new course be entered (inserted) into the table COURSE in its present form? COURSE#SECTION#C_NAME CIS564072Database Design CIS564073Database Design CIS570072Oracle Forms CIS564074Database Design

10 Deletion anomaly For the data in following figure (where we have only one driver), if we delete the employee number 200, we will lose not only that this employee is a driver, but also that driving costs' $1500. This is called a deletion anomaly; we are losing more information than we want to. We lose facts about two entities with one deletion. EMPIDNAMEJOBSALARY 200SmithDriver1500 300JohnGuardian2000 600GeorgeGuardian2000 700JeanGardener2500 450MikeGardener2500 EMPLOYEE

11 Update anomaly Example1: If the salary of the job "Guardian" changes from $2000 to $2100, the updating operation must be repeated with each Guardian employee. EMPIDNAMEJOBSALARY 200SmithDriver1500 300JohnGuardian2000 600GeorgeGuardian2000 700JeanGardener2500 450MikeGardener2500 EMPLOYEE

12 ANOMALIES EXAMPLES Update: Suppose the course name (C_Name) for CIS 564 got changed to Database Management. How many times do you have to make this change in the COURSE table in its current form? COURSE#SECTION#C_NAME CIS564072Database Design CIS564073Database Design CIS570072Oracle Forms CIS564074Database Design

13 ANOMALIES JobSalary Driving1500 Guardian2000 Gardener2500 EmpIdNameJob 200Smithdriving 300JohnGuardian 600GeorgeGuardian 700JeanGardener 450MikeGardener EMP-JOBJOB-SAL Following Figure shows the relations issued from the decomposition phase. Now the previous anomalies are avoided so then we can :  Delete the employee Smith from EMP-JOB without losing the fact that driving has a salary $1500,  Insert a new job in the JOB-SAL table without enrolled employees,  Modify only one tuple in the JOB-SAL table instead of many tuples in the initial relation.

14 ANOMALIES So, a table (relation) is a stable (‘good’) table only if it is free from any of these anomalies at any point in time. You have to ensure that each and every table in a database is always free from these modification anomalies. And, how do you ensure that? ‘Normalization’ theory helps.

15 NORMAL FORMS 1 NF 2NF 3NF BCNF (Boyce-Codd Normal Form) 4NF 5NF

16 Normal Forms Normalization is done through changing or transforming data into various Normal Forms. Normalization is done through changing or transforming data into various Normal Forms. There are 5 Normal Forms but we almost never use 4NF or 5NF. There are 5 Normal Forms but we almost never use 4NF or 5NF. We will only be concerned with 1NF, 2NF, and 3NF. We will only be concerned with 1NF, 2NF, and 3NF.

17 Normal Forms For a database to be in a normal form, it must meet all requirements of the previous forms: For a database to be in a normal form, it must meet all requirements of the previous forms: Eg. For a database to be in 2NF, it must already be in 1NF. For a database to be in 3NF, it must already be in 1NF and 2NF.Eg. For a database to be in 2NF, it must already be in 1NF. For a database to be in 3NF, it must already be in 1NF and 2NF.

18

19 Sample Data Data that is not atomic means: Data that is not atomic means: We can’t easily sort the dataWe can’t easily sort the data We can’t easily search or index the dataWe can’t easily search or index the data We can’t easily change the dataWe can’t easily change the data We can’t easily reference the data in other tablesWe can’t easily reference the data in other tables

20

21 Sample Data We still can’t easily sort, search, or index our employees.We still can’t easily sort, search, or index our employees. What if a manager has more than 2 employees, 10 employees, 100 employees? We’d need to add columns to our database just for these cases.What if a manager has more than 2 employees, 10 employees, 100 employees? We’d need to add columns to our database just for these cases. It is still hard to reference our employees in other tables.It is still hard to reference our employees in other tables.

22

23 First Normal Form 1NF means that we must: 1NF means that we must: Eliminate duplicate columns from the same table, andEliminate duplicate columns from the same table, and Create separate tables for each group of related data into separate tables, each with a unique row identifier (primary key)Create separate tables for each group of related data into separate tables, each with a unique row identifier (primary key) Let’s get started by making our columns atomic… Let’s get started by making our columns atomic…

24

25 Primary Key The best primary key would be the Employee column. The best primary key would be the Employee column. Every employee only has one manager, therefore an employee is unique. Every employee only has one manager, therefore an employee is unique. Students should now say that the Employee is the Primary Key since there are now multiple manager values in the table. Only Employee is unique.

26 First Normal Form Congratulations! Congratulations! The fact that all our data and columns is atomic and we have a primary key means that we are in 1NF! The fact that all our data and columns is atomic and we have a primary key means that we are in 1NF! Students should now say that the Employee is the Primary Key since there are now multiple manager values in the table. Only Employee is unique.

27

28

29 Moving to Second Normal Form A database in 2NF must also be in 1NF: A database in 2NF must also be in 1NF: Data must be atomicData must be atomic Every row (or tuple) must have a unique primary keyEvery row (or tuple) must have a unique primary key Plus: Plus: Subsets of data that apply to multiple rows (repeating data) are moved to separate tablesSubsets of data that apply to multiple rows (repeating data) are moved to separate tables

30

31

32 Moving to Second Normal Form The CustID determines all the data in the row, but U.S. Zip codes determines the City and State. (eg. A given Zip code can only belong to one city and state so storing Zip codes with a City and State is redundant) The CustID determines all the data in the row, but U.S. Zip codes determines the City and State. (eg. A given Zip code can only belong to one city and state so storing Zip codes with a City and State is redundant) This means that City and State are Functionally Dependent on the value in Zip code and not only the primary key. This means that City and State are Functionally Dependent on the value in Zip code and not only the primary key.

33 Moving to Second Normal Form To be in 2NF, this repeating data must be in its own table. To be in 2NF, this repeating data must be in its own table. So: So: Let’s create a Zip code table that maps Zip codes to their City and State.Let’s create a Zip code table that maps Zip codes to their City and State. Note that Canadian Postal Codes are different: the same city and state can have many different postal codes.Note that Canadian Postal Codes are different: the same city and state can have many different postal codes.

34 Customer Table Zip Code Table

35 Advantages of 2NF Saves space in the database by reducing redundancies Saves space in the database by reducing redundancies If a customer calls, you can just ask them for their Zip code and you’ll know their city and state! (No more spelling mistakes) If a customer calls, you can just ask them for their Zip code and you’ll know their city and state! (No more spelling mistakes) If a City name changes, we only need to make one change to the database. If a City name changes, we only need to make one change to the database.

36 Summary So Far… 1NF: 1NF: All data is atomicAll data is atomic All rows have a unique primary keyAll rows have a unique primary key 2NF: 2NF: Data is in 1NFData is in 1NF Subsets of data in multiple columns are moved to a new tableSubsets of data in multiple columns are moved to a new table These new tables are related using foreign keysThese new tables are related using foreign keys

37 Moving to 3NF To be in 3NF, a database must be: To be in 3NF, a database must be: In 2NFIn 2NF All columns must be fully functionally dependent on the primary key (There are no transitive dependencies)All columns must be fully functionally dependent on the primary key (There are no transitive dependencies)

38

39

40

41 Maybe price is dependent on the ProdID and Quantity: The more you buy of a given product the cheaper that product becomes! Maybe price is dependent on the ProdID and Quantity: The more you buy of a given product the cheaper that product becomes! So we ask the business manager and she tells us that this is the case. So we ask the business manager and she tells us that this is the case.

42 We say that Price has a transitive dependency on ProdID and Quantity. We say that Price has a transitive dependency on ProdID and Quantity. This means that Price isn’t just determined by the OrderID. It is also determined by the size (or quantity) of the order (and of course what is ordered).This means that Price isn’t just determined by the OrderID. It is also determined by the size (or quantity) of the order (and of course what is ordered).

43

44

45

46

47

48

49 Congratulations! We’re now in 3NF! Congratulations! We’re now in 3NF! We can also quickly figure out what price to offer our customers for any quantity they want. We can also quickly figure out what price to offer our customers for any quantity they want.

50 To Summarize (again) A database is in 3NF if: A database is in 3NF if: It is in 2NFIt is in 2NF It has no transitive dependenciesIt has no transitive dependencies A transitive dependency exists when one attribute (or field) is determined by another non-key attribute (or field) A transitive dependency exists when one attribute (or field) is determined by another non-key attribute (or field) We remove fields with a transitive dependency to a new table and link them by a foreign key. We remove fields with a transitive dependency to a new table and link them by a foreign key.

51 Summarizing A database is in 2NF if: A database is in 2NF if: It is in 1NFIt is in 1NF There is no repeating data in its tables.There is no repeating data in its tables. Put another way, if we use a composite primary key, then all attributes are dependent on all parts of the key. Put another way, if we use a composite primary key, then all attributes are dependent on all parts of the key.

52 And Finally… A database is in 1NF if: A database is in 1NF if: All its attributes are atomic (meaning they contain only a single unit or type of data), andAll its attributes are atomic (meaning they contain only a single unit or type of data), and All rows have a unique primary key.All rows have a unique primary key.


Download ppt "Database Normalization. What is Normalization Normalization allows us to organize data so that it: Normalization allows us to organize data so that it:"

Similar presentations


Ads by Google