Normal forms

102
Rushdi Shams, Dept of CSE, KU ET 1 Database Systems Database Systems Normal Forms Normal Forms Version 1.0 Version 1.0

Transcript of Normal forms

Page 1: Normal forms

Rushdi Shams, Dept of CSE, KUET 1

Database Database SystemsSystems

Normal FormsNormal Forms

Version 1.0Version 1.0

Page 2: Normal forms

2Rushdi Shams, Dept of CSE, KUET

NormalizationNormalization

It It avoids duplication of dataavoids duplication of data. Hence it is a . Hence it is a tool to avoid redundancy of datatool to avoid redundancy of data..

Theorists gave loads of “do this”, “do that” Theorists gave loads of “do this”, “do that” thing when relational model was first thing when relational model was first invoked. One of those were normal forms. invoked. One of those were normal forms. Applying those theories on databases are Applying those theories on databases are called normalizatoin.called normalizatoin.

But the fact is, But the fact is, in commercial in commercial environment, loads of works are done by environment, loads of works are done by not maintaining the normalization not maintaining the normalization theories 100%!theories 100%!

Page 3: Normal forms

3Rushdi Shams, Dept of CSE, KUET

Why Normalization?Why Normalization?

Page 4: Normal forms

4Rushdi Shams, Dept of CSE, KUET

Why Normalization? Why Normalization? (continued)(continued)

What if we want to delete the student no What if we want to delete the student no 34668? 34668?

Alas! We will lose information about Alas! We will lose information about DEDUCTIVE DATABASE, its lecturer name DEDUCTIVE DATABASE, its lecturer name and many more!and many more!

What if we want to update the lecturer to What if we want to update the lecturer to NICK WHITTAKER? We will have to put staff NICK WHITTAKER? We will have to put staff name as well as staff no for that!name as well as staff no for that!

What if we want to insert a new student with What if we want to insert a new student with number 38989? We cannot insert any student number 38989? We cannot insert any student who does not have any assessment criteria!who does not have any assessment criteria!

Page 5: Normal forms

5Rushdi Shams, Dept of CSE, KUET

AnomaliesAnomalies An anomaly is a bad thing. Because it An anomaly is a bad thing. Because it

tends the data in a database tends the data in a database LOGICALLY get corrupted!LOGICALLY get corrupted!

There are mainly 3 types of anomaliesThere are mainly 3 types of anomalies1.1. Insert anomalyInsert anomaly2.2. Delete anomalyDelete anomaly3.3. Update anomalyUpdate anomaly

The reason why I switched to discuss The reason why I switched to discuss about anomaly- you will understand about anomaly- you will understand later!later!

Page 6: Normal forms

6Rushdi Shams, Dept of CSE, KUET

Insert AnomalyInsert Anomaly

It is caused when an insertion takes It is caused when an insertion takes place in a child table, where there is place in a child table, where there is no record in the parent table!no record in the parent table!

Page 7: Normal forms

7Rushdi Shams, Dept of CSE, KUET

Insert Anomaly Insert Anomaly (continued)(continued)

What if you try to What if you try to insert a book of J K insert a book of J K Rawlings into the Rawlings into the table left without table left without putting her name putting her name into the table into the table below?below?

Page 8: Normal forms

8Rushdi Shams, Dept of CSE, KUET

Delete AnomalyDelete Anomaly

Caused when a data in the parent Caused when a data in the parent table is deleted before deleting all table is deleted before deleting all the related data from its child (ren) the related data from its child (ren) table(s).table(s).

Well, if you prefer a cascaded Well, if you prefer a cascaded deletion, no one would mind!deletion, no one would mind!

Page 9: Normal forms

9Rushdi Shams, Dept of CSE, KUET

Delete Anomaly Delete Anomaly (continued)(continued)

Before deleting the Before deleting the data from the table data from the table below, if you below, if you delete data from delete data from table in right, what table in right, what do you think it is do you think it is gonna happen?gonna happen?

Page 10: Normal forms

10Rushdi Shams, Dept of CSE, KUET

Update AnomalyUpdate Anomaly

It is kind of like delete anomaly. It is kind of like delete anomaly. Both the parent table and child(ren) Both the parent table and child(ren) table(s) should get updated table(s) should get updated concurrently!concurrently!

Page 11: Normal forms

11Rushdi Shams, Dept of CSE, KUET

More JargonsMore Jargons

Well, before starting with Well, before starting with normalizations, you must need to normalizations, you must need to know some jargons. You already are know some jargons. You already are aware of anomalies. Let’s get into aware of anomalies. Let’s get into some more!some more!

Page 12: Normal forms

12Rushdi Shams, Dept of CSE, KUET

Functional DependencyFunctional Dependency

Y is functionally dependent on X if Y is functionally dependent on X if the values of Y are determined by the values of Y are determined by the values of X.the values of X.

Y=X+1. In this case, Y is functionally Y=X+1. In this case, Y is functionally dependent on Xdependent on X

y=f(x). In this generic case, y is y=f(x). In this generic case, y is functionally dependent on x as well.functionally dependent on x as well.

Page 13: Normal forms

13Rushdi Shams, Dept of CSE, KUET

Functional Dependency Functional Dependency (continued)(continued)

In this case, CURRENCY is In this case, CURRENCY is functionally dependent on FXCODEfunctionally dependent on FXCODE

Page 14: Normal forms

14Rushdi Shams, Dept of CSE, KUET

DeterminantDeterminant

If X determines the values of Y, then If X determines the values of Y, then X will be called the determinant of YX will be called the determinant of Y

Y=X+1. In this case, X is Y=X+1. In this case, X is determinant of Y.determinant of Y.

y=f(x). In this case, x is determinant y=f(x). In this case, x is determinant of y as well.of y as well.

Page 15: Normal forms

15Rushdi Shams, Dept of CSE, KUET

Determinant (continued)Determinant (continued)

In this case, FXCODE is determinant In this case, FXCODE is determinant of CURRENCY.of CURRENCY.

Page 16: Normal forms

16Rushdi Shams, Dept of CSE, KUET

Transitive DependencyTransitive Dependency

If the value of Z is determined by Y If the value of Z is determined by Y and the value of Y is determined by and the value of Y is determined by X, then there is a transitive X, then there is a transitive dependency between X and Z.dependency between X and Z.

This dependency thus states the This dependency thus states the indirect dependency.indirect dependency.

BackBack

Page 17: Normal forms

17Rushdi Shams, Dept of CSE, KUET

Transitive Dependency Transitive Dependency (continued)(continued)

In this case, RATE is dependent on In this case, RATE is dependent on CURRENCY. CURRENCY is CURRENCY. CURRENCY is dependent on COUNTRY. So, RATE dependent on COUNTRY. So, RATE is transitively dependent on is transitively dependent on COUNTRY.COUNTRY.

Page 18: Normal forms

18Rushdi Shams, Dept of CSE, KUET

Candidate keyCandidate key It is the It is the minimal setminimal set of super key. of super key.

Say, from this example, you only can have Say, from this example, you only can have super keys as followings (though you may super keys as followings (though you may have other combinations, but just shorten have other combinations, but just shorten that this time)-that this time)-{roll, name}, {roll, name, dept}, {roll, {roll, name}, {roll, name, dept}, {roll, name, year, semester}name, year, semester}

In this case, the candidate key is {roll, name}!In this case, the candidate key is {roll, name}!

RollRoll NamNamee

DeptDept YearYear SemeSemesterster

Page 19: Normal forms

19Rushdi Shams, Dept of CSE, KUET

Full Functional Full Functional DependencyDependency

If X determines Y but X with If X determines Y but X with combination of Z does not determine combination of Z does not determine Y- then, there is a full functional Y- then, there is a full functional dependency between X and Y.dependency between X and Y.

So, X cannot be a composite key.So, X cannot be a composite key.

Nb. If you make a key with 2 or Nb. If you make a key with 2 or more columns, then that is called a more columns, then that is called a composite key.composite key.

Page 20: Normal forms

20Rushdi Shams, Dept of CSE, KUET

Full Functional Dependency Full Functional Dependency (continued)(continued)

In this case, In this case, POPULATION POPULATION depends on depends on COUNTRY not on the COUNTRY not on the combination of combination of RATE and RATE and COUNTRY. So, COUNTRY. So, POPULATION is fully POPULATION is fully functional dependent functional dependent on COUNTRY and on COUNTRY and also you can say that, also you can say that, POPULATION is not POPULATION is not fully functional fully functional dependent on RATE dependent on RATE and COUNTRY!and COUNTRY!

Page 21: Normal forms

21Rushdi Shams, Dept of CSE, KUET

11stst Normal Form: 1NF Normal Form: 1NF Academic Definitions:Academic Definitions:1. Eliminate repeating groups such that

all records in all tables can be identified uniquely by a primary key in each table. In other words, all fields other than the primary key must depend on the primary key.

2. A relation is in first normal form if and only if every non-key attribute is functionally dependent upon the primary key

Page 22: Normal forms

22Rushdi Shams, Dept of CSE, KUET

1 NF (continued)1 NF (continued)

3. A relation is in first normal form if the domains on which its attributes are defined are scalar

BackBack

Page 23: Normal forms

23Rushdi Shams, Dept of CSE, KUET

1 NF (continued)1 NF (continued)

Easy Definition:Easy Definition:

1 NF is a process of removing repeating 1 NF is a process of removing repeating groups from a table by creating a new groups from a table by creating a new tabletable

They are linked by a one to many They are linked by a one to many relationshiprelationship

Create primary keys on both tablesCreate primary keys on both tables

The child table will have a composite The child table will have a composite primary key (one of its own, other is its primary key (one of its own, other is its foreign key from its parent table)foreign key from its parent table)

Page 24: Normal forms

24Rushdi Shams, Dept of CSE, KUET

What does 1 NF do?What does 1 NF do? Academic Way:Academic Way: Eliminates repeating groupsEliminates repeating groups Defines primary keysDefines primary keys No duplicate entry allowedNo duplicate entry allowed All fields other than primary key depend on All fields other than primary key depend on

primary keyprimary key All columns must contain a single value All columns must contain a single value

(see (see Definition 3Definition 3)) All values in a column must have the same All values in a column must have the same

data typedata type Create a table to move repeating data from Create a table to move repeating data from

the current tablethe current table

Page 25: Normal forms

25Rushdi Shams, Dept of CSE, KUET

Not Scalar- Not 1 NFNot Scalar- Not 1 NF

Page 26: Normal forms

26Rushdi Shams, Dept of CSE, KUET

Repeating Groups- Not 1 Repeating Groups- Not 1 NFNF

Page 27: Normal forms

27Rushdi Shams, Dept of CSE, KUET

0 NF to 1 NF0 NF to 1 NF

Remove repeating Remove repeating groups. Split table groups. Split table into tables with into tables with one to many one to many relationship.relationship.

Page 28: Normal forms

28Rushdi Shams, Dept of CSE, KUET

0 NF to 1 NF (continued)0 NF to 1 NF (continued)

Creating primary Creating primary key on parent table key on parent table and composite and composite primary key on primary key on child table (where child table (where it has a foreign key it has a foreign key referenced to referenced to parent table)parent table)

Page 29: Normal forms

29Rushdi Shams, Dept of CSE, KUET

Our 1 NFOur 1 NF

Page 30: Normal forms

30Rushdi Shams, Dept of CSE, KUET

Can you do it?Can you do it?

Page 31: Normal forms

31Rushdi Shams, Dept of CSE, KUET

Yes! You Can!!Yes! You Can!!

Page 32: Normal forms

32Rushdi Shams, Dept of CSE, KUET

Second Normal Form- 2 Second Normal Form- 2 NFNF

Academic Definition:Academic Definition:1. A relation is in second normal form if

and only if it is in first normal form and every non-key attribute is fully functionally dependent on the primary key

2.2. A relation is in second normal form if A relation is in second normal form if it is in first normal formit is in first normal form and, in and, in addition, addition, all its attributes are all its attributes are dependent on the entire candidate key dependent on the entire candidate key

Page 33: Normal forms

33Rushdi Shams, Dept of CSE, KUET

What does 2 NF do?What does 2 NF do? Academic Way:Academic Way: The table is in 1 NFThe table is in 1 NF All non-key values must be functionally

dependent on the primary key. In other words, non-key fields not dependent on the primary key are not allowed.

Partial dependencies must be removed. A partial dependency is a special type of functional dependency that exists when a field is dependant on a part of a composite primary key.

Create a table with partially dependent part of the primary key and its dependents.

Page 34: Normal forms

34Rushdi Shams, Dept of CSE, KUET

Which is not 2 NF?Which is not 2 NF?

Solution!Solution!

Page 35: Normal forms

35Rushdi Shams, Dept of CSE, KUET

1 NF to 2 NF1 NF to 2 NF

Page 36: Normal forms

36Rushdi Shams, Dept of CSE, KUET

Separating TablesSeparating Tables

Page 37: Normal forms

37Rushdi Shams, Dept of CSE, KUET

Providing Many to One Providing Many to One RelationRelation

Page 38: Normal forms

38Rushdi Shams, Dept of CSE, KUET

Providing One to Many Providing One to Many RelationshipRelationship

Page 39: Normal forms

39Rushdi Shams, Dept of CSE, KUET

Can You Do It?Can You Do It?

Page 40: Normal forms

40Rushdi Shams, Dept of CSE, KUET

Yes! You Can!!Yes! You Can!!

Page 41: Normal forms

41Rushdi Shams, Dept of CSE, KUET

Remember Me? I was not 2 Remember Me? I was not 2 NF!!NF!!

See Me!See Me!

Page 42: Normal forms

42Rushdi Shams, Dept of CSE, KUET

33rdrd Normal Form- 3 NF Normal Form- 3 NF

Academic Way:Academic Way: The table must be in 2 NFThe table must be in 2 NF Eliminate transitive dependency (Eliminate transitive dependency (

see Transitive Dependencysee Transitive Dependency)) Create a new table to contain any Create a new table to contain any

separated columnsseparated columns

Page 43: Normal forms

43Rushdi Shams, Dept of CSE, KUET

3 NF3 NF

Easy Way:Easy Way: Meet the requirements by 1 NF and Meet the requirements by 1 NF and

2 NF2 NF Remove columns that are not fully Remove columns that are not fully

dependent on the primary keydependent on the primary key

Page 44: Normal forms

44Rushdi Shams, Dept of CSE, KUET

3 NF (case study)3 NF (case study)

Imagine we have a table that Imagine we have a table that contains the following columns-contains the following columns-

1.1. Order numberOrder number

2.2. Customer numberCustomer number

3.3. Unit priceUnit price

4.4. QuantityQuantity

5.5. Total Total

Page 45: Normal forms

45Rushdi Shams, Dept of CSE, KUET

3 NF (case study): Checking 3 NF (case study): Checking if it is in 1 NFif it is in 1 NF

1.1. Order numberOrder number

2.2. Customer numberCustomer number

3.3. Unit priceUnit price

4.4. QuantityQuantity

5.5. Total Total Are there duplicate columns? No!Are there duplicate columns? No! Do we have a primary key? Yes, the Do we have a primary key? Yes, the

order number!order number! Fine, it is in 1 NF then!!Fine, it is in 1 NF then!!

Page 46: Normal forms

46Rushdi Shams, Dept of CSE, KUET

3 NF (case study): Checking 3 NF (case study): Checking if it is in 2 NFif it is in 2 NF

1.1. Order numberOrder number2.2. Customer numberCustomer number3.3. Unit priceUnit price4.4. QuantityQuantity5.5. Total Total Are there any static of data that Are there any static of data that

apply to multiple rows? No!apply to multiple rows? No! Fine, we are in 2 NF then!!Fine, we are in 2 NF then!!

Page 47: Normal forms

47Rushdi Shams, Dept of CSE, KUET

3 NF (case study): Checking 3 NF (case study): Checking if it is in 3 NFif it is in 3 NF

1.1. Order numberOrder number

2.2. Customer numberCustomer number

3.3. Unit priceUnit price

4.4. QuantityQuantity

5.5. Total Total Are all of the columns fully depend Are all of the columns fully depend

on the primary key (order on the primary key (order number)?number)?

Page 48: Normal forms

48Rushdi Shams, Dept of CSE, KUET

3 NF (case study): Checking 3 NF (case study): Checking if it is in 3 NF (continued)if it is in 3 NF (continued)

The customer number varies with the order The customer number varies with the order number and it doesn't appear to depend number and it doesn't appear to depend upon any of the other fields upon any of the other fields

What about the unit price? This field could What about the unit price? This field could be dependent upon the customer number in be dependent upon the customer number in a situation where we charged each a situation where we charged each customer a set price. However, looking at customer a set price. However, looking at the data above, it appears we sometimes the data above, it appears we sometimes charge the same customer different prices. charge the same customer different prices. Therefore, the unit price is fully dependent Therefore, the unit price is fully dependent upon the order number upon the order number

Page 49: Normal forms

49Rushdi Shams, Dept of CSE, KUET

3 NF (case study): Checking 3 NF (case study): Checking if it is in 3 NF (continued)if it is in 3 NF (continued)

The quantity of items also varies from The quantity of items also varies from order to order, so we're OK there. order to order, so we're OK there.

What about the total? It looks like we What about the total? It looks like we might be in trouble here. The total might be in trouble here. The total can be derived by multiplying the unit can be derived by multiplying the unit price by the quantity, therefore it's price by the quantity, therefore it's not fully dependent upon the primary not fully dependent upon the primary key. We must remove it from the table key. We must remove it from the table to comply with the third normal form. to comply with the third normal form.

Page 50: Normal forms

50Rushdi Shams, Dept of CSE, KUET

3 NF (case study): Making 3 NF (case study): Making it a 3 NFit a 3 NF

1.1. Order numberOrder number2.2. Customer numberCustomer number3.3. Unit priceUnit price4.4. QuantityQuantity Now, the table is in 3 NF. What Now, the table is in 3 NF. What

about total? Well, there is no need to about total? Well, there is no need to put it on a separate table! You can put it on a separate table! You can make a select command to grab the make a select command to grab the total by multiplying unit price with total by multiplying unit price with quantity- can’t you?quantity- can’t you?

Page 51: Normal forms

51Rushdi Shams, Dept of CSE, KUET

3 NF3 NF

Did you see the Did you see the irrelevant columns irrelevant columns in the 2 tables?in the 2 tables?

You need to You need to separate them!separate them!

Page 52: Normal forms

52Rushdi Shams, Dept of CSE, KUET

3 NF (continued)3 NF (continued)

Eliminated the Eliminated the irrelevant columns irrelevant columns and making them a and making them a separate table.separate table.

Page 53: Normal forms

53Rushdi Shams, Dept of CSE, KUET

3 NF (continued)3 NF (continued)

Can you see the Can you see the irrelevant column irrelevant column here?here?

You need to You need to separate them to separate them to make it a 3 NF!make it a 3 NF!

Page 54: Normal forms

54Rushdi Shams, Dept of CSE, KUET

3 NF (continued)3 NF (continued)

Page 55: Normal forms

55Rushdi Shams, Dept of CSE, KUET

3 NF (continued)3 NF (continued)

Did you see the Did you see the column that does column that does not depend on the not depend on the primary key?primary key?

You need to You need to separate that separate that column from the column from the table to make it 3 table to make it 3 NFNF

Page 56: Normal forms

56Rushdi Shams, Dept of CSE, KUET

3 NF (continued)3 NF (continued)

Page 57: Normal forms

57Rushdi Shams, Dept of CSE, KUET

Can you do it?Can you do it?

Page 58: Normal forms

58Rushdi Shams, Dept of CSE, KUET

Yes! You Can!!Yes! You Can!!

Page 59: Normal forms

59Rushdi Shams, Dept of CSE, KUET

Be warned!Be warned!

3 NF is said to be the most crucial 3 NF is said to be the most crucial and most ambiguous, most complex and most ambiguous, most complex part of normalization! part of normalization!

Page 60: Normal forms

60Rushdi Shams, Dept of CSE, KUET

Bad thing of too much Bad thing of too much normalizationnormalization

Did you take a look at the number of Did you take a look at the number of table increased from 0 NF to 3 NF? If it is table increased from 0 NF to 3 NF? If it is a big enterprise database, then can you a big enterprise database, then can you imagine how far this can go?imagine how far this can go?

Too many tables lead us to too many joinsToo many tables lead us to too many joins Joins are one of the complex codings and Joins are one of the complex codings and

time consuming subtle entities in time consuming subtle entities in database systemsdatabase systems

So, try to normalize, but don’t normalize So, try to normalize, but don’t normalize in a way that you have 1000 tables!in a way that you have 1000 tables!

Page 61: Normal forms

61Rushdi Shams, Dept of CSE, KUET

Beyond 3 NFBeyond 3 NF

Most software development firms Most software development firms dealing with database systems do dealing with database systems do not go beyond 3 NF and many of not go beyond 3 NF and many of them even try not to reach 3 NF!!them even try not to reach 3 NF!!

Page 62: Normal forms

62Rushdi Shams, Dept of CSE, KUET

Boyce Codd Normal Form Boyce Codd Normal Form (BCNF)(BCNF)

It is a variation of 3 NFIt is a variation of 3 NF In a 3NF table, all attributes are dependent In a 3NF table, all attributes are dependent

on a key, a whole key and nothing but a key on a key, a whole key and nothing but a key A table is said to be in BCNF if and only if A table is said to be in BCNF if and only if

it is in 3NF and every non-trivial, left-it is in 3NF and every non-trivial, left-irreducible functional dependency has a irreducible functional dependency has a candidate key as its determinant candidate key as its determinant

In more informal terms, a table is in BCNF In more informal terms, a table is in BCNF if it is in 3NF and the only determinants if it is in 3NF and the only determinants are the candidate keys are the candidate keys

Page 63: Normal forms

63Rushdi Shams, Dept of CSE, KUET

BCNF (continued)BCNF (continued)

Page 64: Normal forms

64Rushdi Shams, Dept of CSE, KUET

BCNF (continued)BCNF (continued)

Primary key is product category and Primary key is product category and product subcategoryproduct subcategory

The table is in 3NF, for each of its non-key The table is in 3NF, for each of its non-key attributes is non-transitively dependent on attributes is non-transitively dependent on the full key (Product Category, Product the full key (Product Category, Product Subcategory). Subcategory).

Page 65: Normal forms

65Rushdi Shams, Dept of CSE, KUET

BCNF (continued)BCNF (continued)

Yet the table has redundant data. Look at Yet the table has redundant data. Look at column 1, column 3 and column 4 for the column 1, column 3 and column 4 for the last 4 rows!last 4 rows!

Page 66: Normal forms

66Rushdi Shams, Dept of CSE, KUET

BCNF (continued)BCNF (continued)

Indeed, Range Code is dictated by the combination of Indeed, Range Code is dictated by the combination of Product Category and Stock Category Product Category and Stock Category

Is stock category a candidate key? NO! Then it is not in Is stock category a candidate key? NO! Then it is not in BCNF!!BCNF!!

Page 67: Normal forms

67Rushdi Shams, Dept of CSE, KUET

BCNF (continued)BCNF (continued) The point about BCNF is that a candidate

key is, by definition, a unique key, and thus a potential primary key.

If a table contains more than one candidate key (primary key), it has a problem according to BCNF. I disagree!

BCNF divides a table up into multiple tables to ensure that no single table has more than one potential primary key

BCNF prohibits a table from having two possible primary keys.

Page 68: Normal forms

68Rushdi Shams, Dept of CSE, KUET

Anomaly problemAnomaly problem

In this table, say, studentNo and Area is In this table, say, studentNo and Area is primary keyprimary key

Page 69: Normal forms

69Rushdi Shams, Dept of CSE, KUET

Anomaly problemAnomaly problem

What if 123456 wants to change her subject to What if 123456 wants to change her subject to information systems? We will lose the data-information systems? We will lose the data-

who is taking the course computer science?who is taking the course computer science? This is update anomalyThis is update anomaly

Page 70: Normal forms

70Rushdi Shams, Dept of CSE, KUET

Anomaly problemAnomaly problem

What if staff 250 also will take computer What if staff 250 also will take computer science? If there is no student enrolled in science? If there is no student enrolled in that subject, then we cannot insert that that subject, then we cannot insert that information in this table!!information in this table!!

This is insert anomalyThis is insert anomaly

Page 71: Normal forms

71Rushdi Shams, Dept of CSE, KUET

Anomaly problemAnomaly problem

What if 123456 wants to withdraw from the What if 123456 wants to withdraw from the course Software Engineering? We will lose course Software Engineering? We will lose the data-the data-Who will take the course Software Engineering!!Who will take the course Software Engineering!!

This is delete anomalyThis is delete anomaly

Page 72: Normal forms

72Rushdi Shams, Dept of CSE, KUET

Why anomaly?Why anomaly?

The anomalies occurred because, The anomalies occurred because, there are two overlapping candidate there are two overlapping candidate keys- student number and area!keys- student number and area!

Page 73: Normal forms

73Rushdi Shams, Dept of CSE, KUET

Normalizing to BCNFNormalizing to BCNF Suppose a relation R is not in Suppose a relation R is not in BCNFBCNF. Then there must . Then there must

be some functional dependency X → A where X is not be some functional dependency X → A where X is not a superkey.a superkey.

In this case, the relation can be decomposed into the In this case, the relation can be decomposed into the two relations two relations

1.1. R1= R − AR1= R − A2.2. R2= XAR2= XA where R − A simply means dropping the attribute A where R − A simply means dropping the attribute A

from the schema and XA is the relation whose from the schema and XA is the relation whose attributes are those in X together with Aattributes are those in X together with A

Then examine the new tables if you can apply 1 and 2 Then examine the new tables if you can apply 1 and 2 on them again!on them again!

When you cannot apply 1 and 2 on them, then it’s in When you cannot apply 1 and 2 on them, then it’s in BCNF!!BCNF!!

Page 74: Normal forms

74Rushdi Shams, Dept of CSE, KUET

BCNF ExampleBCNF Example

Product category+stock category determines range Product category+stock category determines range code.code.

So, make two tables.So, make two tables.1.1. Product category, product subcategory, stock Product category, product subcategory, stock

category, range subcodecategory, range subcode2.2. Product category, stock category, range codeProduct category, stock category, range code

Page 75: Normal forms

75Rushdi Shams, Dept of CSE, KUET

BCNF example BCNF example (continued)(continued)

Page 76: Normal forms

76Rushdi Shams, Dept of CSE, KUET

BCNF example BCNF example (continued)(continued)

Now, observe this new table. In it, the range Now, observe this new table. In it, the range subcode depends on stock category and product subcode depends on stock category and product subcategory. So, make two tables.subcategory. So, make two tables.

1.1. Product category, product subcategory, stock Product category, product subcategory, stock categorycategory

2.2. Product subcategory, stock category, range subcodeProduct subcategory, stock category, range subcode

Page 77: Normal forms

77Rushdi Shams, Dept of CSE, KUET

BCNF example BCNF example (continued)(continued)

Page 78: Normal forms

78Rushdi Shams, Dept of CSE, KUET

Our tables in BCNFOur tables in BCNF

Page 79: Normal forms

79Rushdi Shams, Dept of CSE, KUET

Fourth Normal Form (4 Fourth Normal Form (4 NF)NF)

A table must be in 3 NF or BCNF with 3 NFA table must be in 3 NF or BCNF with 3 NF Multi-valued dependencies must be transformed

into functional dependencies. This implies that one value and not multiple values are dependent on a primary key.

Eliminate multiple sets of multiple valued or multi-valued dependencies, sometimes described as non-trivial multi-valued dependencies.

NF and has no multiple sets of multi-valued dependencies. In other words, 4NF states that no entity can have more than a single one-to-many relationship within an entity if the one-to-many attributes are independent of each other.

Page 80: Normal forms

80Rushdi Shams, Dept of CSE, KUET

4 NF Example4 NF Example

In this table, there In this table, there are 3 columns. are 3 columns. Employee, skill Employee, skill and skilllanguageand skilllanguage

Employee is the Employee is the primary keyprimary key

The table is in The table is in BCNF with 3NFBCNF with 3NF

Page 81: Normal forms

81Rushdi Shams, Dept of CSE, KUET

4 NF Example4 NF Example

The record for The record for employee number employee number 0122443 can be 0122443 can be written also in the written also in the form-form-

0122443: Typing, 0122443: Typing, Dictation: English, Dictation: English, French in the form French in the form employee: skill: employee: skill: skilllanguageskilllanguage

Page 82: Normal forms

82Rushdi Shams, Dept of CSE, KUET

4 NF Example4 NF Example

So, the multivalued So, the multivalued dependency is on dependency is on skill column and skill column and skilllanguage skilllanguage column!column!

To make it in 4 NF, To make it in 4 NF, you will have to you will have to simply split them simply split them into separate table, into separate table, nothing else!!nothing else!!

Page 83: Normal forms

83Rushdi Shams, Dept of CSE, KUET

4 NF Example4 NF Example

Page 84: Normal forms

84Rushdi Shams, Dept of CSE, KUET

Solving one, creates Solving one, creates another problem!another problem!

Can you say, what is the problem Can you say, what is the problem still with the tables?still with the tables?

There are still 2 entries for the same There are still 2 entries for the same employee!!employee!!

Page 85: Normal forms

85Rushdi Shams, Dept of CSE, KUET

Fifth Normal Form (5 Fifth Normal Form (5 NF)NF)

Personally, I really hate this form!!Personally, I really hate this form!! The table must have to be in 4 NF The table must have to be in 4 NF

(same old dialogue!)(same old dialogue!) The table should not contain any The table should not contain any

cyclic dependency!cyclic dependency! Cyclic dependency is like say your Cyclic dependency is like say your

table has 3 columns. Column 1 table has 3 columns. Column 1 depends on 2, 2 on 3 and 3 on 1!!depends on 2, 2 on 3 and 3 on 1!!

Also known as PJNF (Projection Also known as PJNF (Projection Normal Form)Normal Form)

Page 86: Normal forms

86Rushdi Shams, Dept of CSE, KUET

5 NF (continued)5 NF (continued)

Try begin with creating a table EmployeesTry begin with creating a table Employees All the 3 columns are primary key (kind of All the 3 columns are primary key (kind of

weird, isn’t it?? We can call it composite weird, isn’t it?? We can call it composite primary key)primary key)

Page 87: Normal forms

87Rushdi Shams, Dept of CSE, KUET

5 NF (continued)5 NF (continued)

Let’s insert some data in Employees table Let’s insert some data in Employees table then.then.

Page 88: Normal forms

88Rushdi Shams, Dept of CSE, KUET

5 NF (continued)5 NF (continued)

Employees table now look like this!Employees table now look like this! There is no multivalued dependency, so, There is no multivalued dependency, so,

the table is in 4 NF (of course also in 1-2-the table is in 4 NF (of course also in 1-2-3-BC NF!!)3-BC NF!!)

Page 89: Normal forms

89Rushdi Shams, Dept of CSE, KUET

5 NF (continued)5 NF (continued)

Now, let’s play with the basic table Employees. Now, let’s play with the basic table Employees. We will split the table into three tables. WHY? We will split the table into three tables. WHY? I will say you later.I will say you later.

We are making three tables- project_employee, We are making three tables- project_employee, project_manager, manager_employeeproject_manager, manager_employee

Page 90: Normal forms

90Rushdi Shams, Dept of CSE, KUET

Detail of Detail of project_employeeproject_employee

Page 91: Normal forms

91Rushdi Shams, Dept of CSE, KUET

Detail of Detail of project_managerproject_manager

Page 92: Normal forms

92Rushdi Shams, Dept of CSE, KUET

Detail of Detail of manager_employeemanager_employee

Page 93: Normal forms

93Rushdi Shams, Dept of CSE, KUET

The Fun!The Fun! Now, we will query the initial table Employees! It Now, we will query the initial table Employees! It

will show us the whole tablewill show us the whole table

Page 94: Normal forms

94Rushdi Shams, Dept of CSE, KUET

The Fun Continues!The Fun Continues!

Now, we will join the three new tables Now, we will join the three new tables and will get the same answer!!and will get the same answer!!

Page 95: Normal forms

95Rushdi Shams, Dept of CSE, KUET

So, what is the fun for?So, what is the fun for?

The fun is, if you have a table in 4 The fun is, if you have a table in 4 NF but with cyclic dependency, if NF but with cyclic dependency, if you can split it into x number of you can split it into x number of tables and then while joining those x tables and then while joining those x number of tables, it will give you number of tables, it will give you back the initial table, you then made back the initial table, you then made it 5 NF while splitting!it 5 NF while splitting!

Phew!!Phew!!

Page 96: Normal forms

96Rushdi Shams, Dept of CSE, KUET

Domain Key Normal Form Domain Key Normal Form (DKNF)(DKNF)

Please god, help us! There are loads of Please god, help us! There are loads of normal forms! I cannot go beyond that! normal forms! I cannot go beyond that! Well, folks, this one is the last- I Well, folks, this one is the last- I promise!!promise!!

Domain/key normal form (DKNF) is a Domain/key normal form (DKNF) is a normal used in database normalization normal used in database normalization which requires that the database which requires that the database contains no constraints other than contains no constraints other than domain constraints and key constraints. domain constraints and key constraints.

Page 97: Normal forms

97Rushdi Shams, Dept of CSE, KUET

DKNF (continued)DKNF (continued)

This table violates the rules for being DKNFThis table violates the rules for being DKNF If Wealthy person type is Eccentric Millionaire/ evil If Wealthy person type is Eccentric Millionaire/ evil

millionaire, then the Net Worth in Dollars must be millionaire, then the Net Worth in Dollars must be 1,000,000 to 999,999,999 inclusive and if that is 1,000,000 to 999,999,999 inclusive and if that is Evil Billionaire/ Eccentric Billionaire, then it should Evil Billionaire/ Eccentric Billionaire, then it should be 1,000,000,000 or higher. be 1,000,000,000 or higher.

Page 98: Normal forms

98Rushdi Shams, Dept of CSE, KUET

So, what to do?So, what to do?

Nothing serious, the violation on domain can Nothing serious, the violation on domain can be solved easily by changing the domain of be solved easily by changing the domain of Wealthy Person Type to Eccentric and Evil- Wealthy Person Type to Eccentric and Evil- forget about Millionaire and Billionaire!!forget about Millionaire and Billionaire!!

It is then in DKNF!!It is then in DKNF!!

Page 99: Normal forms

99Rushdi Shams, Dept of CSE, KUET

The Flow chartThe Flow chart

Say, you are given a table in 0 NF. Do Say, you are given a table in 0 NF. Do the followings one by one-the followings one by one-

1.1. Are there duplicate rows? Find the Are there duplicate rows? Find the answer Noanswer No

2.2. Is there primary key? Find the answer Is there primary key? Find the answer yes (then you finished making it in 1 NF)yes (then you finished making it in 1 NF)

3.3. Are all the non-key columns functionally Are all the non-key columns functionally depend on primary key? Find the depend on primary key? Find the answer yes (then you finished making it answer yes (then you finished making it 2 NF)2 NF)

Page 100: Normal forms

100Rushdi Shams, Dept of CSE, KUET

The Flow Chart The Flow Chart (continued)(continued)

4.4. Are all the columns that are non key Are all the columns that are non key depending fully functionally upon the primary depending fully functionally upon the primary key? Find the answer yes (then you made it key? Find the answer yes (then you made it into 3 NF)into 3 NF)

5.5. Are all the determinants candidate key of the Are all the determinants candidate key of the table? Find the answer yes (then you made it table? Find the answer yes (then you made it into BCNF)into BCNF)

6.6. Are there multivalued dependency? Find the Are there multivalued dependency? Find the answer no (then you made it into 4 NF)answer no (then you made it into 4 NF)

7.7. Are there any cyclic dependency? Find the Are there any cyclic dependency? Find the answer no (then you made it into 5 NF)answer no (then you made it into 5 NF)

8.8. Are there any violation to domain Are there any violation to domain constraints? Find the answer no (then you constraints? Find the answer no (then you made it into DKNF)made it into DKNF)

Page 101: Normal forms

101Rushdi Shams, Dept of CSE, KUET

ReferencesReferences Designing Effective Database Designing Effective Database

Systems by Rebecca M. Riordan, Systems by Rebecca M. Riordan, Addison Wesley Professional, 2005Addison Wesley Professional, 2005

Database Systems by Paul Beynon-Database Systems by Paul Beynon-Devies, Palgrave Macmillan, 2004Devies, Palgrave Macmillan, 2004

www.wikipedia.orgwww.wikipedia.org Beginning Database Design by Beginning Database Design by

Gavin Powell, Wrox Publications, Gavin Powell, Wrox Publications, 20052005

Page 102: Normal forms

102Rushdi Shams, Dept of CSE, KUET

References (continued)References (continued) www.techtarget.comwww.techtarget.com http://http://databases.about.comdatabases.about.com Databases –Normalization by Gordon Databases –Normalization by Gordon

Royle, Department of Computer Royle, Department of Computer Science & Software Engineering Science & Software Engineering University of Western AustraliaUniversity of Western Australia

Database Management Systems by Database Management Systems by Prof. Holowczak, Zicklin School of Prof. Holowczak, Zicklin School of Business, Baruch College, City Business, Baruch College, City University of New YorkUniversity of New York