14
Texas State Technical College DISCOVER! Second Normal Form - 2NF Second Normal Form - 2NF It has to be the whole key.

Texas State Technical College DISCOVER! Second Normal Form - 2NF It has to be the whole key

Embed Size (px)

Citation preview

Page 1: Texas State Technical College DISCOVER! Second Normal Form - 2NF It has to be the whole key

Texas State Technical College

DISCOVER!

Second Normal Form - 2NFSecond Normal Form - 2NF

It has to be the whole key.

Page 2: Texas State Technical College DISCOVER! Second Normal Form - 2NF It has to be the whole key

Texas State Technical College

DISCOVER!

Second Normal Form (2NF)

2NF is an extension to 1NF that increases 2NF is an extension to 1NF that increases the organization in the database.the organization in the database.

A database is 2NF when all of the contained A database is 2NF when all of the contained tables are 2NF.tables are 2NF.

The focus of 2NF is to eliminate partial The focus of 2NF is to eliminate partial dependencies on primary keys.dependencies on primary keys.

A table that is 1NF and uses a singular A table that is 1NF and uses a singular primary key is automatically 2NF.primary key is automatically 2NF.

OverviewOverview

Page 3: Texas State Technical College DISCOVER! Second Normal Form - 2NF It has to be the whole key

Texas State Technical College

DISCOVER!

In order for a table to be considered 2NFIn order for a table to be considered 2NF

• Must be 1NFMust be 1NF

• All fields should be dependent or related to All fields should be dependent or related to the whole primary keythe whole primary key

• If a composite primary key is used, there must If a composite primary key is used, there must not be a field that is only related to a portion not be a field that is only related to a portion of the primary keyof the primary key

The Technical DefinitionThe Technical Definition

Second Normal Form (2NF)

Page 4: Texas State Technical College DISCOVER! Second Normal Form - 2NF It has to be the whole key

Texas State Technical College

DISCOVER!

In order for a table to be considered 2NFIn order for a table to be considered 2NF• No repeating groups (1NF)No repeating groups (1NF)

• All fields must be directly related to the entire All fields must be directly related to the entire primary key.primary key.

• Tables that utilize a singular primary key are Tables that utilize a singular primary key are automatically 2NF, provided that they are 1NF.automatically 2NF, provided that they are 1NF.

The Layman’s DefinitionThe Layman’s Definition

Second Normal Form (2NF)

Page 5: Texas State Technical College DISCOVER! Second Normal Form - 2NF It has to be the whole key

Texas State Technical College

DISCOVER!

Are known as Are known as composite primary keyscomposite primary keys and can be and can be problematicproblematic..

It is a primary key that is composed of two or more It is a primary key that is composed of two or more fields.fields.

It is very common that fields in a composite key are It is very common that fields in a composite key are also also foreign keysforeign keys..

These fields are both foreign keys and partial These fields are both foreign keys and partial primary keys.primary keys.

Non-Singular Primary KeysNon-Singular Primary Keys

Second Normal Form (2NF)

Page 6: Texas State Technical College DISCOVER! Second Normal Form - 2NF It has to be the whole key

Texas State Technical College

DISCOVER!

Sometimes their use is more logical than creating a Sometimes their use is more logical than creating a new primary key.new primary key.

Sometimes values in fields produce a unique Sometimes values in fields produce a unique identifier when combined that is more useful identifier when combined that is more useful than a random or sequential number.than a random or sequential number.

For ExampleFor ExampleName + Date is easier to remember and use as a Name + Date is easier to remember and use as a primary key than an automatically generated 10 digit primary key than an automatically generated 10 digit number.number.

John-Davis-14may07 John-Davis-14may07 VSVS 3245873245 3245873245

Why Use Composite Primary Keys?Why Use Composite Primary Keys?

Second Normal Form (2NF)

Page 7: Texas State Technical College DISCOVER! Second Normal Form - 2NF It has to be the whole key

Texas State Technical College

DISCOVER!

Composite primary keys are the source of the Composite primary keys are the source of the problem that requires normalization to 2NF.problem that requires normalization to 2NF.

Only tables with composite primary keys have to be Only tables with composite primary keys have to be converted to 2NF.converted to 2NF.

Avoiding the use of composite keys avoids the Avoiding the use of composite keys avoids the problemsproblems• Linking a foreign key to two or more fieldsLinking a foreign key to two or more fields• Having to guarantee a database is 2NFHaving to guarantee a database is 2NF

It can be summed up asIt can be summed up asIt’s about the key, the It’s about the key, the wholewhole key. key.

2NF2NF

Second Normal Form (2NF)

Page 8: Texas State Technical College DISCOVER! Second Normal Form - 2NF It has to be the whole key

Texas State Technical College

DISCOVER!

In the example below, the table is not 2NF In the example below, the table is not 2NF because it contains partial dependencies.because it contains partial dependencies.

For Example…For Example… [ DMV ]

Registration

SSNpk/fk VINpk/fk Registeredpk Expires County Mfg Make

Text Text Date Date Text Text Text

521443684 A14567 14-Jan-02 13-Jan-03 Taylor Ford Focus

884652471 RTE786 25-Mar-06 24-Mar-07 Jones Dodge Neon

996335588 QW9345 27-Oct-00 26-Oct-01 Taylor Ford Mustang

114225575 TS3232 11-Nov-04 10-Nov-05 Callahan Kia Spectra

Problem: Partial dependencies upon the primary key.

Second Normal Form (2NF)

Page 9: Texas State Technical College DISCOVER! Second Normal Form - 2NF It has to be the whole key

Texas State Technical College

DISCOVER!

Let’s verbalize the relationships.Let’s verbalize the relationships.

For Example…For Example… [ DMV ]

Registration

SSNpk/fk VINpk/fk Registeredpk Expires County Mfg Make

A Registration is the ownership by a person SSN for a vehicle VIN created on date Registered.

• SSN, VIN, and Registered are the unique identifier for a registration.

• Expires defines the date of expiration for a registration.

• The county describes the geographical locale for a registration.

• Mfg describes the manufacturer of the vehicle in the registration.

• Make describes the model of the vehicle in the registration

Second Normal Form (2NF)

Page 10: Texas State Technical College DISCOVER! Second Normal Form - 2NF It has to be the whole key

Texas State Technical College

DISCOVER!

From verbalizing we can see…From verbalizing we can see…

For Example…For Example… [ DMV ]

Registration

SSNpk/fk VINpk/fk Registeredpk Expires County Mfg Make

SSN, VIN, and Registered are the fields that compose the primary key, thus it is a composite primary key.

Expires and County refer to the whole primary key as they apply to the whole concept of a registration.

Mfg and Make are partially dependent upon the primary key because they only describe the vehicle represented by VIN.

Second Normal Form (2NF)

Page 11: Texas State Technical College DISCOVER! Second Normal Form - 2NF It has to be the whole key

Texas State Technical College

DISCOVER!

1.1. For each portion of a composite primary key, identify all For each portion of a composite primary key, identify all the partially dependent fields.the partially dependent fields.Find all the fields that are partially dependent upon the same key Find all the fields that are partially dependent upon the same key field.field.

2.2. If the key field is also a foreign key, synchronize the If the key field is also a foreign key, synchronize the two tables and remove the partially dependent fields.two tables and remove the partially dependent fields.Guarantee that the fields and their information are in the parent Guarantee that the fields and their information are in the parent table, then remove them from the child table.table, then remove them from the child table.

3.3. If the key field is not a foreign key, make it one and If the key field is not a foreign key, make it one and remove the partially dependent fields.remove the partially dependent fields.Create a table for the object, move the partially dependent fields Create a table for the object, move the partially dependent fields and data to the new table, and remove them from the child table.and data to the new table, and remove them from the child table.

4.4. Repeat the process until database is 2NF.Repeat the process until database is 2NF.The database is 2NF when all tables in the database are 2NF.The database is 2NF when all tables in the database are 2NF.

Converting to 2NFConverting to 2NF

Second Normal Form (2NF)

Page 12: Texas State Technical College DISCOVER! Second Normal Form - 2NF It has to be the whole key

Texas State Technical College

DISCOVER!

Step One: Step One: Identify partially dependent fields.Identify partially dependent fields.

• SSN has no partially dependent fields.SSN has no partially dependent fields.• VIN has two partially dependent fields.VIN has two partially dependent fields.

• MfgMfg• MakeMake

• Registered has no partially dependent fields.Registered has no partially dependent fields.

For Example…For Example… [ DMV ]

Registration

SSNpk/fk VINpk/fk Registeredpk Expires County Mfg Make

Text Text Date Date Text Text Text

Problem: Partial dependencies upon the primary key.

Second Normal Form (2NF)

Page 13: Texas State Technical College DISCOVER! Second Normal Form - 2NF It has to be the whole key

Texas State Technical College

DISCOVER!

Step Two: Step Two: Move partially dependent fields.Move partially dependent fields.

• In this case, we guarantee that there is a vehicle table In this case, we guarantee that there is a vehicle table that contains Mfg and Make.that contains Mfg and Make.

• If not, we create the table and make VIN a foreign key If not, we create the table and make VIN a foreign key linking the two.linking the two.

For Example…For Example… [ DMV ]

Registration

SSNpk/fk VINpk/fk Registeredpk Expires County

Text Text Date Date Text

Vehicle

VINpk Mfg Make

Text Text Text

FIXED: The table is now 2NF!

Second Normal Form (2NF)

Page 14: Texas State Technical College DISCOVER! Second Normal Form - 2NF It has to be the whole key

Texas State Technical College

DISCOVER!

In Summary…In Summary…

• Second normal form (2NF) dictates that all Second normal form (2NF) dictates that all fields in a table must be wholly related to fields in a table must be wholly related to the primary key.the primary key.

• A table must be 1NF before it can be 2NF.A table must be 1NF before it can be 2NF.

• Tables that are 1NF and do not use Tables that are 1NF and do not use composite primary keys are automatically composite primary keys are automatically 2NF.2NF.

Second Normal Form (2NF)