1st Normal Form or 1NF:
Each Column Type is Unique.
Sometimes expressed as ... "No repeating columns"
create table{
bookTitle
,author1
,author2
,author3
.....
}
What about 2nd & 3rd normal forms?
2NF and 3NF are all about being dependent on the primary key.
Realize(recall) that a primary key can be made up of multiple columns.
"The data depends on the key [1NF],
the whole key [2NF] and nothing
but the key [3NF]"
---- 2NF -----
Suppose Sewanee's CS program gets really really big. Takes over the campus really!
And we have a table containing courses offered with the following data:
|
(Primary Key) | |
|
|---|
| CourseID | Semester | #Students | CourseName | InstructorName | InstructorOffice
|
|---|
| 157 | 2029-A | 100 | Programming | Dalero | Woods 130
|
| 157 | 2029-B | 100 | Programming | SP Carl | Woods 136
|
| 284 | 2029-A | 200 | Databases | HM Walker | Woods 128
|
| 284 | 2030-B | 150 | Databases | Dalero | Woods 130
|
| 320 | 2029-A | 120 | Algorithms | SV Smith | Spencer 008
|
The table as it is already satisfies 1NF but what about 2NF?
Consider the first few columns.
|
(Primary Key) | | uh-oh...
|
|---|
| CourseID | Semester | #Students | CourseName
|
|---|
| 157 | 2029-A | 100 | Programming
|
| 157 | 2029-B | 100 | Programming
|
| 284 | 2029-A | 200 | Databases
|
| 284 | 2030-B | 150 | Databases
|
| 320 | 2029-A | 120 | Algorithms
|
This is not in 2NF, because the fourth column does not rely upon the entire
key - but only a part of it. The course name is dependent on the Course's ID,
but has nothing to do with which semester it's been taken in. Thus, as you can see,
we have duplicate information - several rows telling us that 157 is
programming, and 284 is Databases. So we fix that by putting the course name
into another table, where CourseID is the ENTIRE key.
| (Primary Key) |
|
| CourseID | Course Name
|
|---|
| 157 | Programming
|
| 284 | Databases
|
| 320 | Algorithms
|
No redundancy!
---- 3NF -----
Okay, so let's say add the name of the instructor of the course,
and some details about them back into the table of sample data.
This one _is_ in 2NF even with those extra columns.
The instructor depends on both course & semester and the instructor's office
depends on the instructor. So... indirectly the office DOES depend
on ENTIRE primary key.
|
(Primary Key) | | | uh-oh...
|
|---|
| CourseID | Semester | #Students | InstructorName | InstructorOffice
|
|---|
| 157 | 2029-A | 100 | Dalero | Woods 130
|
| 157 | 2029-B | 100 | SP Carl | Woods 136
|
| 284 | 2029-A | 200 | CV Jones | Woods 128
|
| 284 | 2030-B | 150 | Dalero | Woods 130
|
| 320 | 2029-A | 120 | SV Smith | Spencer 008
|
BUT... 3NF says the dependency CANNOT be indirect. So, as usual, to fix this,
we do much the same as we did in 2NF - take InstructorName out of this table,
and put it in its own, which has InstructorID as the key.
| (Primary Key) |
|
| InstructorID | InstructorName | InstructorOffice
|
|---|
| 332 | Dalero | Woods 130
|
| 599 | SP Carl | Woods 136
|
| 495 | CV Jones | Woods 128
|
| 242 | SV Smith | Spencer 008
|
No redundancy!!
How are the above two examples different?
Yes, the dependency in both cases is on a single field.
CourseName on CourseID (and not both CourseID,Semester)
InstructorName on InstructorID (and only indirectly on CourseID,Semester)
However, in non-2NF example:
dependency is on part of the primary key
while in non-3NF example (which _is_ in 2NF):
dependency is on a field that is not a part of the primary key (and also notice
that in that example it does satisfy 2NF; this is to show that even if you check for
2NF you should also check for 3NF)
In both cases to normalize, the solution is another table which does not exhibit
update anomalies (example of update anomaly: in 2NF example, what happens if you
update CourseName for ( 157,2029-A ), but not for ( 157,2029-B )?
You get inconsistent=meaningless=unusable data).
If the distinction between 2NF and 3NF seems subtle -- well, it is -- get over it.
But if you memorize "the key, the whole key and nothing but the key",
you have mnemonics for 1NF, 2NF and 3NF. Hope that helps!