Notes on Normal Forms in Database Design
First Normal Form (1NF)
- Definition: The First Normal Form (1NF) is the foundational level of database normalization.
- Conditions for 1NF:
- Atomic Values: Each cell in a table must contain a single atomic value.
- No Repeating Groups: There should be no sets of values or groups of data in a single cell.
- Uniqueness of Records: Each record in the table must be unique.
Examples of 1NF
Bad Design Example:
| Bars Name | Address | License | Beer |
|---|---|---|---|
| Joe's Place | 1234 E Main St | beer | Bud, Corona |
| Sue's Bar | 4321 W University Ave | full | Bud, Wicked Ale, MGD |
Normalized (1NF):
- Each beer is put on a separate row:
| Bars Name | Address | License | Beer | |
|---|---|---|---|---|
| Joe's Place | 1234 E Main St | beer | Bud | |
| Joe's Place | 1234 E Main St | beer | Corona | |
| Sue's Bar | 4321 W University Ave | full | Bud | |
| Sue's Bar | 4321 W University Ave | full | Wicked Ale | |
| Sue's Bar | 4321 W University Ave | full | MGD | |
Anomalies in Database Design |
- Update Anomaly: Occurs when a single fact or data item is altered in one location but not in others, leading to inconsistencies.
- Deletion Anomaly: Happens when deletion of a record causes loss of important information, even if that information might be needed elsewhere.
Second Normal Form (2NF)
- Definition: The Second Normal Form (2NF) improves upon 1NF by eliminating partial dependencies.
- Conditions for 2NF:
- Must first satisfy 1NF.
- No Partial Dependency: All non-key attributes must depend on the entire primary key, not just a part of it.
Concept of Partial Dependency and Composite Keys
- Applies only to tables with composite primary keys (a key made up of multiple attributes).
- Every non-primary attribute must depend on the whole composite primary key to avoid partial dependencies.
Examples of 2NF
- Composite Key Example:
- BarMenu table:
| BarName | Beer | Brewer | Price |
|---------------|-------|-----------|-------|
| Joe's Place | Bud | Anheuser | 2.50 |
| Joe's Place | Corona| Crown | 3.50 |
| Sue's Bar | Bud | Anheuser | 2.00 |
| Sue's Bar | Wicked Ale | Pete's | 2.50 | - Does not satisfy 2NF because Brewer depends only on Beer, not on both Bar_Name and Beer.
- BarMenu table:
| BarName | Beer | Brewer | Price |
Resolving Partial Dependencies
Tables After Decomposition for 2NF:
Beers Table:
Beer Brewer Bud Anheuser Corona Crown Wicked Ale Pete's MGD Miller BarPrice Table:
| BarName | Beer | Price |
|---------------|------------|-------|
| Joe's Place | Bud | 2.50 |
| Joe's Place | Corona | 3.50 |
| Sue's Bar | Bud | 2.00 |
| Sue's Bar | Wicked Ale | 2.50 |
| Sue's Bar | MGD | 3.00 |
These new tables now satisfy the 2NF as they eliminate partial dependencies and ensure all attributes are dependent on the full primary key where applicable.