3. We will not discuss
NoSQL
– Graph databases
– Tree-like data
Anything else that is non-tabular data
4. Why are we here?
Database tables are just poorly done.
– Duplicate data
– Data integrity issues
5. Bad idea
Don’t stuff multiple data in a cell
id login password phone
1 user1 password1 888-111-2222, 888-222-1111,
2 user2 password2 408-333-4444
3 user3 password3 510-555-6666
7. Bad Idea
id name manufacturer manufacturer_street
1 order1 Nvidia 2788 San Tomas Expwy
2 order2 Nvidia 2788 San Tomas Expwy
3 order3 AMD 2485 Augustine Dr
8. Better
id name manufacturer_id
1 order1 1
2 order2 1
3 order3 2
id manufacturer manufacturer_street
1 Nvidia 2788 San Tomas Expwy
2 AMD 2485 Augustine Dr
9. Third Normal Form
We are skipping Second Normal Form.
– Combining its definition with Third Normal Form
Third Normal Form definition
– One primary key per table row.
10. Third Normal Form
Third Normal Form prerequisites:
– Must already be First Normal Form
– Must already be Second Normal Form
11. Third Normal Form
Every non-key must provide a fact about the key,
the whole key, and nothing but the key
12. Third Normal Form
Every non-key must provide a fact about the key,
the whole key, and nothing but the key,
so help me Codd
16. User / Address
User has only one address
User may or may not have an address
User has many address
User has many address, and address has many user
21. Maybe address
The maybe side is also known as a weak reference.
Weak reference id becomes a foreign key
user
id
login_name
password
first_name
last_name
phone
address_id
address
id
street
city
state
zip
24. Many address
Foreign key on the many-side (clawfoot)
user
id
login_name
password
first_name
last_name
phone
address
id
street
city
state
zip
user_id
25. Many address
SELECT street, city, state, zip
FROM user
JOIN address
ON user.id = address.user_id
WHERE user.login_name = 'emceelam';
28. Many User, Many Address
Put foreign keys into an intermediary table.
user
id
login_name
password
first_name
last_name
phone
address
id
street
city
state
zip
user_address
user_id
address_id
29. Many User, Many Address
SELECT login_name, street, city, state, zip
FROM user
JOIN user_address
JOIN address
ON user.id = user_address.user_id
AND address.id = user_address.address_id
WHERE city = 'Santa Clara'
AND state = ‘CA’;
30. Denormalization
Normalization comes at a performance cost
– More joins means more full table scans
Denormalization undoes normalization
– Hopefully granting speed.