4 th normal form by: karen mcvay. review of nfs 1nf all values of the columns are atomic. that is,...

25
4 4 TH TH NORMAL FORM NORMAL FORM By: Karen McVay By: Karen McVay

Post on 21-Dec-2015

220 views

Category:

Documents


2 download

TRANSCRIPT

44THTH NORMAL FORM NORMAL FORM

By: Karen McVayBy: Karen McVay

REVIEW OF NFsREVIEW OF NFs

1NF 1NF All values of the columns All values of the columns are atomic. That is, they contain are atomic. That is, they contain no repeating values. no repeating values.

2NF 2NF it is in 1NF and every non- it is in 1NF and every non-key column is fully dependent key column is fully dependent upon the primary key. upon the primary key.

REVIEW OF NF Cont…REVIEW OF NF Cont…

3NF 3NF it is already in 2NF and every non-key it is already in 2NF and every non-key column is non transitively dependent upon its column is non transitively dependent upon its primary key. In other words, all non-key primary key. In other words, all non-key attributes are functionally dependent only upon attributes are functionally dependent only upon the primary key. the primary key.

BCNF BCNF A relation is in BCNF if every A relation is in BCNF if every determinant is a candidate key. This is an determinant is a candidate key. This is an improved form of third normal form. improved form of third normal form.

Determinant: an attribute on which some other Determinant: an attribute on which some other attribute is fully functionally dependentattribute is fully functionally dependent

4th Normal Form4th Normal Form

A Boyce Codd normal form relation A Boyce Codd normal form relation is in fourth normal form if is in fourth normal form if

(a)(a) there is no multi value there is no multi value dependency in the relation or dependency in the relation or

(b)(b) there are multi value dependency there are multi value dependency but the attributes, which are but the attributes, which are multi value dependent on a multi value dependent on a specific attribute, are dependent specific attribute, are dependent between themselves. between themselves.

44thth Normal Form Cont… Normal Form Cont…

This is best discussed through mathematical This is best discussed through mathematical notation. notation.

Assume the following relationAssume the following relation

R(a:pk1, b:pk2, c:pk3)R(a:pk1, b:pk2, c:pk3)

Recall that a relation is in BCNF if all its Recall that a relation is in BCNF if all its determinant are candidate keys, in other words determinant are candidate keys, in other words each determinant can be used as a primary each determinant can be used as a primary key. key.

Because relation Because relation RR has only one determinant has only one determinant (a, (a, b, c)b, c), which is the composite primary key and , which is the composite primary key and since the primary is a candidate key therefore since the primary is a candidate key therefore R is in BCNF.R is in BCNF.

44thth Normal Form Cont… Normal Form Cont…

Now R may or may not be in fourth normal form. Now R may or may not be in fourth normal form.   1. If R contains 1. If R contains no multi value dependencyno multi value dependency then R will be then R will be

in Fourth normal form.in Fourth normal form.  2. Assume R has the following two-multi value dependencies:2. Assume R has the following two-multi value dependencies:  a --->> b a --->> b and and a --->> c a --->> c   In this case R will be in the fourth normal form if In this case R will be in the fourth normal form if bb and and c c

dependent on each otherdependent on each other.. However if b and However if b and c are independent of each other c are independent of each other then then R R

is notis not in in fourth fourth normal formnormal form and the relation has to be and the relation has to be projected to following two non-loss projections. These non-projected to following two non-loss projections. These non-loss projections will be in fourth normal form.loss projections will be in fourth normal form.

Many-to-many relationships Many-to-many relationships

Fourth Normal Form applies to Fourth Normal Form applies to situations involving many-to-many situations involving many-to-many relationships. relationships.

In relational databases, many-to-In relational databases, many-to-many relationships are expressed many relationships are expressed through cross-reference tables.through cross-reference tables.

4th Normal Form 4th Normal Form Cont…Cont…

Note about FDs and Note about FDs and MVDsMVDs Every Functional Dependency is a MVDEvery Functional Dependency is a MVD

(if (if AA11AA22…A…Ann B B11BB22…B…Bnn , then , then AA11AA22…A…Ann B B11BB22…B…Bnn ) )

FDs rule out certain tuples (i.e. if FDs rule out certain tuples (i.e. if A A B B

then two tuples will not have the same then two tuples will not have the same value for A and different values for B)value for A and different values for B)

MVDs do not rule out tuples. They MVDs do not rule out tuples. They guarantee that certain tuples must exist. guarantee that certain tuples must exist.

Formal DefinitionsFormal Definitions

Fourth Normal FormFourth Normal Form- if - if RR is valid BCNF and… is valid BCNF and…- given the “non-trivial” MVD: - given the “non-trivial” MVD: AA11AA22…A…Ann B B11BB22…B…Bnn

{{AA11AA22…A…Ann} is a superkey} is a superkey A MVD: A MVD: AA11AA22…A…Ann B B11BB22…B…Bnn for a Relation for a Relation RR is “ is “non-non-

trivialtrivial” if:” if:1.1. none of the none of the BBs are among the s are among the AAss2.2. Not all of the attributes of Not all of the attributes of RR are among the are among the AAs s

and and BBss

A MVD is “A MVD is “trivialtrivial” if it contains all the variations ” if it contains all the variations of of AA11AA22…A…Ann xx B B11BB22…B…Bn.n.

A relation cannot be decomposed any further A relation cannot be decomposed any further (under 4NF rules) if it has a trivial MVD(under 4NF rules) if it has a trivial MVD

Consider a case of class enrollment. Each Consider a case of class enrollment. Each student can be enrolled in one or more student can be enrolled in one or more classes and each class can contain one or classes and each class can contain one or more students. more students.

Clearly, there is a many-to-many Clearly, there is a many-to-many relationship between classes and relationship between classes and students. This relationship can be students. This relationship can be represented by a Student/Class cross-represented by a Student/Class cross-reference table:reference table:

{StudentID, ClassID}{StudentID, ClassID}

Example 1Example 1

Example 1 Cont…Example 1 Cont…

The key for this table is the combination The key for this table is the combination of StudentID and ClassID. To avoid of StudentID and ClassID. To avoid violation of 2NF, all other information violation of 2NF, all other information about each student and each class is about each student and each class is stored in separate Student and Class stored in separate Student and Class tables, respectively.tables, respectively.

Note that each StudentID determines not Note that each StudentID determines not a unique ClassID, but a well-defined, finite a unique ClassID, but a well-defined, finite setset of values. This kind of behavior is of values. This kind of behavior is referred to as referred to as multi-valued multi-valued dependencydependency of ClassID on StudentID. of ClassID on StudentID.

Consider another example with two many-to-many relationships, Consider another example with two many-to-many relationships, between students and classes and between classes and teachers.between students and classes and between classes and teachers.

Example 2Example 2

Students Classes* *

Also, a many-to-many relationship between Also, a many-to-many relationship between students and teachers is implied. students and teachers is implied.

Classes Teachers* *

However, the business rules do not constrain However, the business rules do not constrain this relationship in any way—the combination of this relationship in any way—the combination of StudentID and TeacherID does not contain any StudentID and TeacherID does not contain any additional information beyond the information additional information beyond the information implied by the student/class and class/teacher implied by the student/class and class/teacher relationships. relationships.

Consequentially, the student/class and Consequentially, the student/class and class/teacher relationships are independent of class/teacher relationships are independent of each other—these relationships have no each other—these relationships have no additional constraints. The following table is, additional constraints. The following table is, then, in violation of 4NF:then, in violation of 4NF:

{StudentID, ClassID, TeacherID}{StudentID, ClassID, TeacherID}

Example 2 Cont…Example 2 Cont…

As an example of the anomalies that As an example of the anomalies that can occur, realize that it is not possible can occur, realize that it is not possible to add a new class taught by some to add a new class taught by some teacher without adding at least one teacher without adding at least one student who is enrolled in this class.student who is enrolled in this class.

To achieve 4NF, represent each To achieve 4NF, represent each independent many-to-many relationship independent many-to-many relationship through its own cross-reference table.through its own cross-reference table.

44thth NF and Anomalies NF and Anomalies

44thth Normal Form and Normal Form and anomalies Cont…anomalies Cont…

Case 1:Case 1:

Assume the following relation:Assume the following relation:Employee (Eid:pk1, Language:pk2, Employee (Eid:pk1, Language:pk2,

Skill:pk3) Skill:pk3)

  

No multi value dependency, No multi value dependency, therefore R is in fourth therefore R is in fourth normal form.normal form.

case 2: Assume the following relation with multi-value dependency: Employee (Eid:pk1, Languages:pk2, Skills:pk3) Eid --->> Languages Eid --->> Skills

Languages and Skills are dependent.This says an employee speak several languages and has several skills. However for each skill a specific language is used when that skill is practiced.

4th Normal Form and 4th Normal Form and anomalies Cont…anomalies Cont…

Thus employee 100 when he/she teaches speaks English but when he cooks speaks French. This relation is in fourth normal form and does not suffer from any anomalies.

EidEid LanguageLanguage SkillSkill

100100 English English TeachingTeaching

100100 KurdishKurdish PoliticPolitic

100100 FrenchFrench CookingCooking

200200 EnglishEnglish CookingCooking

200200 ArabicArabic SingingSinging

case 3: case 3: Assume the following relation with Assume the following relation with multi-value dependency:multi-value dependency:  Employee (Eid:pk1, Languages:pk2, Skills:pk3)Employee (Eid:pk1, Languages:pk2, Skills:pk3)

Eid --->> LanguagesEid --->> Languages Eid --->> Eid --->> SkillsSkills

Languages and Skills are Languages and Skills are independentindependent..

4th Normal Form and 4th Normal Form and anomalies Cont…anomalies Cont…

EidEid LanguageLanguage SkillSkill

100100 EnglishEnglish TeachingTeaching

100100 KurdishKurdish PoliticPolitic

100100 EnglishEnglish PoliticPolitic

100100 KurdishKurdish TeachingTeaching

200200 ArabicArabic SingingSinging

4th Normal Form and 4th Normal Form and anomalies Cont…anomalies Cont…

This relation is This relation is notnot in fourth normal form and in fourth normal form and suffers from all three types of anomalies.suffers from all three types of anomalies.

Insertion anomaly:Insertion anomaly: To insert row (200 English Cooking) To insert row (200 English Cooking) we have to insert two extra rows (200 Arabic cooking), we have to insert two extra rows (200 Arabic cooking), and (200 English Singing) otherwise the database will and (200 English Singing) otherwise the database will be inconsistent. Note the table will be as follow:be inconsistent. Note the table will be as follow:

EidEid LanguagLanguagee

SkillSkill

100100 EnglishEnglish TeachingTeaching

100100 KurdishKurdish PoliticsPolitics

100100 EnglishEnglish PoliticsPolitics

100100 KurdishKurdish TeachingTeaching

200200 ArabicArabic SingingSinging

200200 EnglishEnglish CookingCooking

200200 ArabicArabic CookingCooking

200200 EnglishEnglish SingingSinging

Deletion anomalyDeletion anomaly: If employee 100 discontinue : If employee 100 discontinue politic skill we have to delete two rows: politic skill we have to delete two rows:

(100 Kurdish Politic), and (100 English Politic) (100 Kurdish Politic), and (100 English Politic) otherwise the database will be inconsistent.otherwise the database will be inconsistent.

EidEid LanguageLanguage SkillSkill

100100 EnglishEnglish TeachingTeaching

100100 KurdishKurdish PoliticsPolitics

100100 EnglishEnglish PoliticsPolitics

100100 KurdishKurdish TeachingTeaching

200200 ArabicArabic SingingSinging

200200 EnglishEnglish CookingCooking

200200 ArabicArabic CookingCooking

200200 EnglishEnglish SingingSinging

More anomaliesMore anomalies

Update anomaly:Update anomaly: If employee If employee 200 changes his skill from 200 changes his skill from singing to dancing we have to singing to dancing we have to make changes in more than make changes in more than one place.one place.

The relation is projected to the following two The relation is projected to the following two non-loss projections which are in forth normal non-loss projections which are in forth normal formform

Emplyee_Language(Eid:pk1, Languages:pk2)Emplyee_Language(Eid:pk1, Languages:pk2)

EidEid LanguageLanguage

100100 EnglishEnglish

100100 KurdishKurdish

200200 ArabicArabic

Emplyee_Language(Eid:pk1, Skills:pk2)Emplyee_Language(Eid:pk1, Skills:pk2)

EidEid SkillSkill

100100 TeachingTeaching

100100 PoliticPolitic

200200 SingingSinging

Cont…Cont…

ReferencesReferences

Functional Dependency Functional Dependency (Normalization)(Normalization) http://www.emunix.emich.edu/~khttp://www.emunix.emich.edu/~khailany/files/Normalization.htmhailany/files/Normalization.htm

Multivalued Dependencies (Ozmar Zaine):Multivalued Dependencies (Ozmar Zaine):http://www.cs.sfu.ca/CC/354/zaianhttp://www.cs.sfu.ca/CC/354/zaiane/material/notes/Chapter7/node13.e/material/notes/Chapter7/node13.htmlhtml