Normlaization

6,308 views
5,832 views

Published on

Published in: Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
6,308
On SlideShare
0
From Embeds
0
Number of Embeds
191
Actions
Shares
0
Downloads
219
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Normlaization

  1. 1. Database Design – Lecture 8 Normalization
  2. 2. Lecture Objectives <ul><li>Review basic rules for converting Entities to Tables </li></ul><ul><li>Review how to address Relationships between entities </li></ul><ul><li>Overview of Normalization </li></ul>
  3. 3. Converting ERD into Relational Database Model <ul><li>Entities to Tables </li></ul><ul><ul><li>Create a separate table for each entity and consider all the attributes you defined in ERD as the table columns </li></ul></ul><ul><ul><li>Define a unique identifier as the table PK. Could be an existing attribute that can be used for PK otherwise create a generic PK. </li></ul></ul><ul><ul><li>Do not include derived attributes in the tables. </li></ul></ul><ul><ul><li>Ensure all attributes are atomic. </li></ul></ul><ul><ul><li>Create a new table to support multi-valued attributes. </li></ul></ul>
  4. 4. Converting ERD into Relational Database Model <ul><li>Relationships </li></ul><ul><ul><li>For 1:M connectivity, include the PK of the ’1’ as an FK attribute in the ‘M’ table. Add additional attributes to the ‘M’ table, as required. </li></ul></ul><ul><ul><li>For M:N connectivity, create a bridge table. Include the PKs from both entities as a composite PK in the bridge table. These attributes will also be FKs in the bridge table. Add additional attributes to the bridge table, as required. </li></ul></ul>
  5. 5. Converting ERD into Relational Database Model <ul><li>Relationships </li></ul><ul><ul><li>For Strong-Weak relationship, include PK of strong entity as part of the PK of the weak entity, thus giving it a composite primary key. It will also be an FK in the weak entity. </li></ul></ul><ul><ul><li>For Supertype/Subtype relationships, create a new entity. Attributes unique to the subtype are moved to the new table and are removed from the supertype entity. Both entities will have the same primary keys. </li></ul></ul>
  6. 6. Converting ERD into Relational Database Model <ul><li>Relationships </li></ul><ul><ul><li>For recursive relationships (1:1, 1:M), add additional attributes as required. For M:N recursive relationship, create a new entity with a composite primary key. Create a primary key for the new entity and make a composite primary key using it and the primary key from the ‘1’ entity. The primary key from the ‘1’ will also be a FK in the new entity. </li></ul></ul>
  7. 7. Normalization <ul><li>Normalization can be used in two ways: </li></ul><ul><ul><li>As a process to validate the table structures created through the conversion of entity relationship diagrams to relational tables </li></ul></ul><ul><ul><li>As a process to create entities from table structures created by user views </li></ul></ul>
  8. 8. Normalization <ul><li>Normalization is a process for assigning attributes to entities </li></ul><ul><ul><li>Reduces data redundancies </li></ul></ul><ul><ul><li>Helps eliminate data anomalies </li></ul></ul><ul><ul><li>Produces controlled redundancies to link tables </li></ul></ul><ul><li>Works through a series of stages called normal forms </li></ul><ul><li>Works with views of data </li></ul>
  9. 9. Database Tables and Normalization <ul><li>Normalization stages </li></ul><ul><ul><li>1NF - First normal form </li></ul></ul><ul><ul><ul><li>Eliminate repeating groups </li></ul></ul></ul><ul><ul><ul><li>Identify PK  result is functional dependency </li></ul></ul></ul><ul><ul><li>2NF - Second normal form </li></ul></ul><ul><ul><ul><li>Eliminate partial dependencies </li></ul></ul></ul><ul><ul><li>3NF - Third normal form </li></ul></ul><ul><ul><ul><li>Eliminate transitive dependencies </li></ul></ul></ul><ul><ul><li>BCNF – Boyce-Codd normal form </li></ul></ul><ul><ul><ul><li>Eliminate dependencies whereby a non-key attribute can identify a key attribute. </li></ul></ul></ul>
  10. 10. Need for Normalization Figure 5.2 possible primary keys? <ul><li>Table entries have data inconsistencies (note blank entries) </li></ul><ul><li>Table displays data anomalies: </li></ul><ul><ul><li>Update - Modifying JOB_CLASS </li></ul></ul><ul><ul><li>Insertion - New employee must be assigned project </li></ul></ul><ul><ul><li>Deletion – If an employee is deleted, other vital data lost </li></ul></ul>
  11. 11. Conversion to 1NF <ul><li>Repeating groups must be eliminated </li></ul><ul><li>Proper primary key developed </li></ul><ul><ul><li>Uniquely identifies attribute values - rows ( functional dependency) </li></ul></ul>
  12. 12. Data Organization: 1NF Figure 5.2 Eliminate repeating groups – populate every cell of the ‘table’.
  13. 13. Conversion to 1NF <ul><li>Identify dependencies </li></ul><ul><ul><li>Desirable dependencies based on primary key </li></ul></ul><ul><ul><li>Less desirable dependencies </li></ul></ul><ul><ul><ul><li>Partial (later) </li></ul></ul></ul><ul><ul><ul><ul><li>based on part of composite primary key </li></ul></ul></ul></ul><ul><ul><ul><li>Transitive (later) </li></ul></ul></ul><ul><ul><ul><ul><li>one nonprime attribute depends on another nonprime attribute </li></ul></ul></ul></ul>
  14. 14. Dependency Diagram (1NF) First Normal Form: identify dependencies Transitive Dependency (non-key attribute depends on another non-key attribute) Note: Capital letters refer to primary key, lower case letters refer to attributes. Partial Dependency: attributes dependent on part of the primary key f e d C B A
  15. 15. Dependency Diagram (1NF) First Normal Form: identify all dependencies Transitive Dependency (non-key attribute depends on another non-key attribute) Functional Dependency: A, B, C, d, e, f Partial Dependency: A, d, e Transitive Dependency: d, f Partial Dependency: attributes dependent on part of the primary key f e d C B A
  16. 16. Data Organization: 1NF Figure 5.2 Eliminate repeating groups – populate every cell of the ‘table’.
  17. 17. Dependency Diagram (1NF) Functional Dependency: each attribute is uniquely identified by, or is dependent on the primary key. Primary key is a composite primary key and is made up of PROJ_NUM and EMP_NUM EMPLOYEE_PROJECT (PROJ_NUM (pk), EMP_NUM (pk), PROJ_NAME, EMP_NAME, JOB_CLASS, CHG_HOUR, HOURS) PROJ_NUM EMP_NUM PROJ_NAME EMP_NAME JOB_CLASS CHG_HOUR HOURS
  18. 18. 1NF Summarized <ul><li>All key attributes defined </li></ul><ul><li>No repeating groups in table </li></ul><ul><li>All attributes dependent on primary key (functional dependency) </li></ul>EMPLOYEE_PROJECT (PROJ_NUM (pk), EMP_NUM (pk), PROJ_NAME, EMP_NAME, JOB_CLASS, CHG_HOUR, HOURS)
  19. 19. Dependency Diagram Continued PROJ_NUM EMP_NUM PROJ_NAME EMP_NAME JOB_CLASS CHG_HOUR HOURS 1NF 2NF
  20. 20. Conversion to 2NF <ul><li>Start with 1NF format: </li></ul><ul><ul><li>Write each key component on separate line </li></ul></ul><ul><ul><li>Write original key on last line </li></ul></ul><ul><ul><li>Each component is new table </li></ul></ul><ul><ul><li>Write dependent attributes after each key </li></ul></ul>PROJECT (PROJ_NUM (pk) , PROJ_NAME) EMPLOYEE (EMP_NUM (pk), EMP_NAME, JOB_CLASS, CHG_HOUR) EMPLOYEE_PROJECT (PROJ_NUM (pk, fk), EMP_NUM (pk, fk), HOURS) Attribute of hours is dependent on composite primary key
  21. 21. 2NF Summarized <ul><li>In 1NF </li></ul><ul><li>Includes no partial dependencies </li></ul><ul><ul><li>Attributes dependent on a portion of primary key </li></ul></ul><ul><li>Still possible to exhibit transitive dependency </li></ul><ul><ul><li>Attributes may be functionally dependent on non-key attributes </li></ul></ul>
  22. 22. Dependency Diagram Continued PROJ_NUM EMP_NUM PROJ_NAME EMP_NAME JOB_CLASS CHG_HOUR HOURS 1NF 2NF 3NF
  23. 23. Conversion to 3NF <ul><li>Create separate tables to eliminate transitive functional dependencies </li></ul><ul><li>Identify any additional attributes needed in new table </li></ul>JOB (JOB_CLASS (pk), JOB_DESCRIPTION, CHG_HOUR) PROJECT (PROJ_NUM (pk) , PROJ_NAME) EMPLOYEE (EMP_NUM (pk), EMP_NAME, JOB_CLASS (fk)) EMPLOYEE_PROJECT (PROJ_NUM (pk, fk), EMP_NUM (pk, fk), HOURS) New attribute
  24. 24. 3NF Summarized <ul><li>In 2NF </li></ul><ul><li>Contains no transitive dependencies </li></ul>
  25. 25. Normalization and Database Design <ul><li>Normalization should be part of the design process </li></ul><ul><li>E-R Diagram provides macro view </li></ul><ul><li>Normalization provides micro view of entities </li></ul><ul><ul><li>Focuses on characteristics of specific entities </li></ul></ul><ul><ul><li>May yield additional entities </li></ul></ul><ul><li>Difficult to separate normalization from E-R diagramming </li></ul><ul><li>Business rules must be determined </li></ul>
  26. 26. Normalization Steps <ul><li>1NF </li></ul><ul><ul><li>All key attributes are defined </li></ul></ul><ul><ul><li>No repeating groups </li></ul></ul><ul><ul><li>All attributes are functionally dependent on the primary key </li></ul></ul><ul><li>2NF </li></ul><ul><ul><li>Table is in 1NF </li></ul></ul><ul><ul><li>No partial dependencies </li></ul></ul>
  27. 27. Normalization Steps <ul><li>3NF </li></ul><ul><ul><li>Table is in 2NF </li></ul></ul><ul><ul><li>No transitive dependencies </li></ul></ul><ul><ul><li>New PKs identified where appropriate </li></ul></ul>

×