SlideShare a Scribd company logo
How to Fake a Database Design
How do I spell “normalization”?
OSCON 2014
Curtis "Ovid" Poe
http://allaroundtheworld.fr/
Copyright 2014, http://www.allaroundtheworld.fr/
March 18, 2022
Good Database Schemas
• Generally normalized
• Denormalized only as necessary
• No duplicate data
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Typical Developer Schemas
• A steaming pile of ones and zeros
• … with a “family friendly” background
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Source: http://commons.wikimedia.org/wiki/File:Spaghetti-prepared.jpg
Database Normalization
• Remove redundancy
• Create logical relations
• Decomposing data to atomic elements
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Only Covering 3NF
1. Remove repeating groups of data
2. Remove partial key dependencies
3. Remove data unrelated to key
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
How to Feel Stupid
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
“It is shown that if a relation
schema is in third normal form and
every key is simple, then it is in
projection-join normal form
(sometimes called fifth normal
form), the ultimate normal form
with respect to projections and
joins.”
Simple Conditions for Guaranteeing Higher Normal
Forms in Relational Databases — C. J. Date
http://commons.wikimedia.org/wiki/File:%22I_should_have_gone_to_the_pro_station%22_-_NARA_-
_514564.tif
‘Nuff of that – Let’s Get Started
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
I’m going to discuss “how”, not “why”,
because I only have 50 minutes.
Faking a Database Design
• Forget everything you know about Excel
• Focus on nouns (sort of)
• Duplicate data is a design flaw
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Real-World Problem
• Client wanted a rewrite of recipes site
• They sent us their Access (!) database
• Main objects:
– customers
– recipes
– orders
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Our “DBA” Said This Was OK
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Our “DBA” also lost his job shortly thereafter
Back to the plot …
• Customers
• Orders
• Recipes
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Nouns == Tables(*)
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Nouns == Tables(*)
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Rule #1
1. Nouns == tables
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
What’s with the customer_id?
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
It’s a foreign key
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
One-to-many
relationship
Our DDL (Data Definition Language)
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
CREATE TABLE orders (
order_id SERIAL PRIMARY KEY,
customer_id INTEGER NOT NULL,
order_date TIMESTAMP WITH TIME ZONE NOT NULL,
FOREIGN KEY (customer_id)
REFERENCES customer(customer_id)
);
Rule #2
1. Nouns == tables
2. Another table’s ID must have a FK constraint
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Oh dog, no!
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
But “What if”?
1. fettuccinne
2. fettuchini
3. fettucini
4. fettucinne
5. fetuchine
6. fetuchinney
7. fetuchinni
8. fetucine
9. fetucini
10. fetucinni
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
https://www.flickr.com/photos/ykjc9/3485366680/sizes/l
Searching
SELECT recipe_id, name FROM recipes
WHERE
ingredient1 IN ( 'fettuccinne', 'fettuchini', 'fettucini', 'fettucinne', 'fetuchine', 'fetuchinney',
'fetuchinni', 'fetucine', 'fetucini', 'fetucinni')
OR
ingredient2 IN ( 'fettuccinne', 'fettuchini', 'fettucini', 'fettucinne', 'fetuchine', 'fetuchinney',
'fetuchinni', 'fetucine', 'fetucini', 'fetucinni')
OR
ingredient3 IN ( 'fettuccinne', 'fettuchini', 'fettucini', 'fettucinne', 'fetuchine', 'fetuchinney',
'fetuchinni', 'fetucine', 'fetucini', 'fetucinni')
OR
ingredient4 IN ( 'fettuccinne', 'fettuchini', 'fettucini', 'fettucinne', 'fetuchine', 'fetuchinney',
'fetuchinni', 'fetucine', 'fetucini', 'fetucinni')
OR
ingredient5 IN ( 'fettuccinne', 'fettuchini', 'fettucini', 'fettucinne', 'fetuchine', 'fetuchinney',
'fetuchinni', 'fetucine', 'fetucini', 'fetucinni')
OR
ingredient6 IN ( 'fettuccinne', 'fettuchini', 'fettucini', 'fettucinne', 'fetuchine', 'fetuchinney',
'fetuchinni', 'fetucine', 'fetucini', 'fetucinni')
OR
ingredient7 IN ( 'fettuccinne', 'fettuchini', 'fettucini', 'fettucinne', 'fetuchine', 'fetuchinney',
'fetuchinni', 'fetucine', 'fetucini', 'fetucinni')
OR
ingredient8 IN ( 'fettuccinne', 'fettuchini', 'fettucini', 'fettucinne', 'fetuchine', 'fetuchinney',
'fetuchinni', 'fetucine', 'fetucini', 'fetucinni');
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
It’s “fettuccine”, in case
you were wondering
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Searching
SELECT recipe_id, name FROM recipes
WHERE ingredient1 = 'fettuccine'
OR ingredient2 = 'fettuccine'
OR ingredient3 = 'fettuccine'
OR ingredient4 = 'fettuccine'
OR ingredient5 = 'fettuccine'
OR ingredient6 = 'fettuccine'
OR ingredient7 = 'fettuccine'
OR ingredient8 = 'fettuccine';
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Ingredients Table
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Rule #3
1. Nouns == tables
2. Another table’s ID must have a FK constraint
3. Lists of things get their own table
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Lookup Table
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Many-to-many relationship
Searching
SELECT recipe_id, name
FROM recipes r
JOIN recipe_ingredients ri ON ri.recipe_id = r.recipe_id
JOIN ingredients i ON i.ingredient_id =
ri.ingredient_id
WHERE i.name = 'fettuccine';
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Our DDL (Data Definition Language)
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
CREATE TABLE recipes_ingredients (
recipe_ingredient_id SERIAL PRIMARY KEY,
recipe_id INTEGER NOT NULL,
ingredient_id INTEGER NOT NULL,
UNIQUE(recipe_id, ingredient_id),
FOREIGN KEY (recipe_id)
REFERENCES recipes(recipe_id),
FOREIGN KEY (ingredient_id)
REFERENCES ingredients(ingredient_id)
);
Our DDL (Data Definition Language)
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
CREATE TABLE recipes_ingredients (
recipe_id INTEGER NOT NULL,
ingredient_id INTEGER NOT NULL,
PRIMARY KEY (recipe_id, ingredient_id),
FOREIGN KEY (recipe_id)
REFERENCES recipes(recipe_id),
FOREIGN KEY (ingredient_id)
REFERENCES recipes(ingredient_id)
);
Rule #4
1. Nouns == tables
2. Another table’s ID must have a FK constraint
3. Lists of things get their own table
4. Many-to-many == lookup table (with FKs)
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
So How Do We Order Recipes?
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Orders With Recipes
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
How Many of Which Ingredient?
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Our simple “customers”, “orders”, and “recipes”
database has grown to seven tables.
And it will keep growing.
So Far
• Every noun has its own table (*)
• Lookup tables join related tables
• And generally have some of unique constraint
• Other table’s ids have foreign key constraints
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Database Tips
• We’ve covered the main rules
• They only cover structure
• Now to dive deeper
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Equality ≠ Identity
• No duplication == not duplicating identity
• Are identical twins the same person?
• Are two guys named “John” the same guy?
• This is important and easy to get wrong
• For example …
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
How do you get the total of an order?
• Assume each recipe has a price
• Store total in the order? (hint: no)
• Store price on the recipe? (hint: yes)
• Is that enough?
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Orders Total
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Calculating the Order Total?
SELECT o.order_id, sum(i.price)
FROM orders o
JOIN orders_recipes orr
ON orr.order_id = o.order_id
JOIN recipes r
ON r.recipe_id = orr.recipe_id
GROUP BY o.order_id
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
What if the price changes?
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Orders Total
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Calculating the Order Total
SELECT o.order_id, sum(orr.price)
FROM orders o
JOIN orders_recipes orr
ON orr.order_id = o.order_id
GROUP BY o.order_id
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Equality is not Identity
• Order item price isn’t item price
• What if the item price changes?
• What if you give a discount on the order item?
• A subtle, but common bug
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Rule #5
1. Nouns == tables
2. Another table’s ID must have a FK constraint
3. Lists of things get their own table
4. Many-to-many == lookup table (with FKs)
5. Watch for equal values that aren’t identical
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Naming
• Names are important
• Identical columns should have identical names
• Names should hint at use
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Bad Naming
SELECT name, 'too cold'
FROM areas
WHERE temperature < 32;
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
ID Names
orders.order_id
versus
orders.id
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
ID Names
SELECT o.id, sum(i.price)
FROM orders o
JOIN orders_recipes orr
ON orr.order_id = o.id
JOIN recipes r
on r.id = o.id
GROUP BY o.order_id
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
ID Names
SELECT o.id, sum(i.price)
FROM orders o
JOIN orders_recipes orr
ON orr.order_id = o.id
JOIN recipes r
on r.id = o.id
GROUP BY o.order_id
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Conceptually Similar to …
SELECT name
FROM customer
WHERE id > weight;
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
ID Names
SELECT thread.*
FROM email thread
JOIN email selected ON selected.id = thread.id
JOIN character recipient ON recipient.id = thread.recipient_id
JOIN station_area sa ON sa.id = recipient.id
JOIN station st ON st.id = sa.id
JOIN star origin ON origin.id = thread.id
JOIN star destination ON destination.id = st.id
LEFT JOIN route
ON ( route.from_id = origin.id AND route.to_id = destination.id )
WHERE selected.id = ?
AND ( thread.sender_id = ?
OR ( thread.recipient_id = ?
AND ( origin.id = destination.id
OR ( route.distance IS NOT NULL
AND
now() >= thread.datesent
+ ( route.distance * interval '30 seconds' )
))))
ORDER BY datesent ASC, thread.parent_id ASC NULLS FIRST
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Rule #6
1. Nouns == tables
2. Another table’s ID must have a FK constraint
3. Lists of things get their own table
4. Many-to-many == lookup table (with FKs)
5. Watch for equal values that aren’t identical
6. Name columns as descriptively as possible
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Summary
• Nouns == tables (*)
• FK constraints
• Proper naming is important
• Your DBAs will thank you
• Your apps will be more robust
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
?
http://www.slideshare.net/ovid/
Bonus Slides!
Super-duper important stuff I wasn’t
sure I had time to cover because it’s
going to make your head hurt.
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Avoid NULL Values
• Every column should have a type
• NULLs, by definition, are unknown values
• Thus, their type is unknown
• But … every column should have a type?
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Our employees Table
CREATE TABLE employees (
employee_id SERIAL PRIMARY KEY,
name CHARACTER VARYING(255) NOT NULL,
salary MONEY NULL
);
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Giving Bonuses
• $1,000 bonus to all employees
• … if they make less than $40,000/year
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Get Employees For Bonus
SELECT employee_id, name
FROM employee
WHERE salary < 40000;
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Bad SQL
• Won’t return anyone with a NULL salary
• Why is the salary NULL?
– What if it’s confidential?
– What if they’re a contractor and in that table?
– What if they’re an unpaid slave intern?
– What if it’s unknown when the data was entered?
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
NULLs tell you nothing
supplier_id city
s1 ‘London’
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
part_id city
p1 NULL
suppliers table
parts table
Example via “Database In Depth” by C.J. Date
NULLs tell you nothing
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
part_id city
p1 NULL
parts table
Example via “Database In Depth” by C.J. Date
SELECT part_id
FROM parts;
SELECT part_id
FROM parts
WHERE city = city;
NULLs tell you nothing
supplier_id city
s1 ‘London’
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
part_id city
p1 NULL
Example via “Database In Depth” by C.J. Date
SELECT s.supplier_id, p.part_id
FROM suppliers s, parts p
WHERE p.city <> s.city -- can’t compare NULL
OR p.city <> 'Paris’; -- can’t compare NULL
NULLs tell you lies
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
Example via “Database In Depth” by C.J. Date
SELECT s.supplier_id, p.part_id
FROM suppliers s, parts p
WHERE p.city <> s.city -- can’t compare NULL
OR p.city <> 'Paris’; -- can’t compare NULL
• We get no rows because we can’t compare a NULL city
• The unknown city is Paris or it isn't.
• If it’s Paris, the first condition is true
• If it’s not Paris, the second condition is true
• Thus, the WHERE clause must be true, but it’s not
Rule #7
1. Nouns == tables
2. Another table’s ID must have an FK constraint
3. Lists of things get their own table
4. Many-to-many == lookup table (with FKs)
5. Watch for equal values that aren’t identical
6. Name columns as descriptively as possible
7. Avoid NULL columns like the plague
March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/

More Related Content

What's hot

Screaming Frog + Xpath: BrightonSEO April 2019
Screaming Frog + Xpath: BrightonSEO April 2019Screaming Frog + Xpath: BrightonSEO April 2019
Screaming Frog + Xpath: BrightonSEO April 2019
Sabine Langmann
 
Mongo db report
Mongo db reportMongo db report
Mongo db report
Hyphen Call
 
Taller mongodb
Taller mongodbTaller mongodb
Paige Hobart - How to do GOOD Keyword Research - Search Advertising Show 2021
Paige Hobart - How to do GOOD Keyword Research - Search Advertising Show 2021Paige Hobart - How to do GOOD Keyword Research - Search Advertising Show 2021
Paige Hobart - How to do GOOD Keyword Research - Search Advertising Show 2021
Paige Hobart
 
Search Query Processing: The Secret Life of Queries, Parsing, Rewriting & SEO
Search Query Processing: The Secret Life of Queries, Parsing, Rewriting & SEOSearch Query Processing: The Secret Life of Queries, Parsing, Rewriting & SEO
Search Query Processing: The Secret Life of Queries, Parsing, Rewriting & SEO
Koray Tugberk GUBUR
 
Data Pitfalls - Brighton SEO - Katie Swann.pptx
Data Pitfalls - Brighton SEO - Katie Swann.pptxData Pitfalls - Brighton SEO - Katie Swann.pptx
Data Pitfalls - Brighton SEO - Katie Swann.pptx
KatieSwann5
 
How to produce great multilingual content, even when you can't read it | Laur...
How to produce great multilingual content, even when you can't read it | Laur...How to produce great multilingual content, even when you can't read it | Laur...
How to produce great multilingual content, even when you can't read it | Laur...
Oban International
 
Using Tags & Taxonomies to super charge your eCommerce SEO
Using Tags & Taxonomies to super charge your eCommerce SEOUsing Tags & Taxonomies to super charge your eCommerce SEO
Using Tags & Taxonomies to super charge your eCommerce SEO
Michael King
 
Swipe left: Why your content is getting ghosted
Swipe left: Why your content is getting ghostedSwipe left: Why your content is getting ghosted
Swipe left: Why your content is getting ghosted
Eleni Cashell
 
Giulia Panozzo | BrightonSEO Measurefest | October 2022
Giulia Panozzo | BrightonSEO Measurefest | October 2022Giulia Panozzo | BrightonSEO Measurefest | October 2022
Giulia Panozzo | BrightonSEO Measurefest | October 2022
GiuliaPanozzo1
 
Everything You Didn't Know About Entity SEO
Everything You Didn't Know About Entity SEO Everything You Didn't Know About Entity SEO
Everything You Didn't Know About Entity SEO
Sara Taher
 
10 Do's and 5 Don'ts for Small Biz Local SEO Success
10 Do's and 5 Don'ts for Small Biz Local SEO Success10 Do's and 5 Don'ts for Small Biz Local SEO Success
10 Do's and 5 Don'ts for Small Biz Local SEO Success
Claire Carlile Marketing
 
E-Commerce SEO Horror Stories : How to tackle the most common issues 
at scal...
E-Commerce SEO Horror Stories : How to tackle the most common issues 
at scal...E-Commerce SEO Horror Stories : How to tackle the most common issues 
at scal...
E-Commerce SEO Horror Stories : How to tackle the most common issues 
at scal...
Aleyda Solís
 
Shining a light on the dark funnel
Shining a light on the dark funnelShining a light on the dark funnel
Shining a light on the dark funnel
Riaz Kanani
 
Źródła wiedzy o wydatkach reklamowych w Polsce
Źródła wiedzy o wydatkach reklamowych w PolsceŹródła wiedzy o wydatkach reklamowych w Polsce
Źródła wiedzy o wydatkach reklamowych w Polsce
Polish Internet Research
 
How Search Works
How Search WorksHow Search Works
How Search Works
Ahrefs
 
How to Build a Semantic Search System
How to Build a Semantic Search SystemHow to Build a Semantic Search System
How to Build a Semantic Search System
Trey Grainger
 
Software Testing for SEO
Software Testing for SEOSoftware Testing for SEO
Software Testing for SEO
Michael King
 
Amazon SEO Information
Amazon SEO InformationAmazon SEO Information
Amazon SEO Information
GauriPawaskar
 
How to control googlebot
How to control googlebotHow to control googlebot
How to control googlebot
Serge Bezborodov
 

What's hot (20)

Screaming Frog + Xpath: BrightonSEO April 2019
Screaming Frog + Xpath: BrightonSEO April 2019Screaming Frog + Xpath: BrightonSEO April 2019
Screaming Frog + Xpath: BrightonSEO April 2019
 
Mongo db report
Mongo db reportMongo db report
Mongo db report
 
Taller mongodb
Taller mongodbTaller mongodb
Taller mongodb
 
Paige Hobart - How to do GOOD Keyword Research - Search Advertising Show 2021
Paige Hobart - How to do GOOD Keyword Research - Search Advertising Show 2021Paige Hobart - How to do GOOD Keyword Research - Search Advertising Show 2021
Paige Hobart - How to do GOOD Keyword Research - Search Advertising Show 2021
 
Search Query Processing: The Secret Life of Queries, Parsing, Rewriting & SEO
Search Query Processing: The Secret Life of Queries, Parsing, Rewriting & SEOSearch Query Processing: The Secret Life of Queries, Parsing, Rewriting & SEO
Search Query Processing: The Secret Life of Queries, Parsing, Rewriting & SEO
 
Data Pitfalls - Brighton SEO - Katie Swann.pptx
Data Pitfalls - Brighton SEO - Katie Swann.pptxData Pitfalls - Brighton SEO - Katie Swann.pptx
Data Pitfalls - Brighton SEO - Katie Swann.pptx
 
How to produce great multilingual content, even when you can't read it | Laur...
How to produce great multilingual content, even when you can't read it | Laur...How to produce great multilingual content, even when you can't read it | Laur...
How to produce great multilingual content, even when you can't read it | Laur...
 
Using Tags & Taxonomies to super charge your eCommerce SEO
Using Tags & Taxonomies to super charge your eCommerce SEOUsing Tags & Taxonomies to super charge your eCommerce SEO
Using Tags & Taxonomies to super charge your eCommerce SEO
 
Swipe left: Why your content is getting ghosted
Swipe left: Why your content is getting ghostedSwipe left: Why your content is getting ghosted
Swipe left: Why your content is getting ghosted
 
Giulia Panozzo | BrightonSEO Measurefest | October 2022
Giulia Panozzo | BrightonSEO Measurefest | October 2022Giulia Panozzo | BrightonSEO Measurefest | October 2022
Giulia Panozzo | BrightonSEO Measurefest | October 2022
 
Everything You Didn't Know About Entity SEO
Everything You Didn't Know About Entity SEO Everything You Didn't Know About Entity SEO
Everything You Didn't Know About Entity SEO
 
10 Do's and 5 Don'ts for Small Biz Local SEO Success
10 Do's and 5 Don'ts for Small Biz Local SEO Success10 Do's and 5 Don'ts for Small Biz Local SEO Success
10 Do's and 5 Don'ts for Small Biz Local SEO Success
 
E-Commerce SEO Horror Stories : How to tackle the most common issues 
at scal...
E-Commerce SEO Horror Stories : How to tackle the most common issues 
at scal...E-Commerce SEO Horror Stories : How to tackle the most common issues 
at scal...
E-Commerce SEO Horror Stories : How to tackle the most common issues 
at scal...
 
Shining a light on the dark funnel
Shining a light on the dark funnelShining a light on the dark funnel
Shining a light on the dark funnel
 
Źródła wiedzy o wydatkach reklamowych w Polsce
Źródła wiedzy o wydatkach reklamowych w PolsceŹródła wiedzy o wydatkach reklamowych w Polsce
Źródła wiedzy o wydatkach reklamowych w Polsce
 
How Search Works
How Search WorksHow Search Works
How Search Works
 
How to Build a Semantic Search System
How to Build a Semantic Search SystemHow to Build a Semantic Search System
How to Build a Semantic Search System
 
Software Testing for SEO
Software Testing for SEOSoftware Testing for SEO
Software Testing for SEO
 
Amazon SEO Information
Amazon SEO InformationAmazon SEO Information
Amazon SEO Information
 
How to control googlebot
How to control googlebotHow to control googlebot
How to control googlebot
 

More from Curtis Poe

How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
Curtis Poe
 
life-off-earth.pptx
life-off-earth.pptxlife-off-earth.pptx
life-off-earth.pptx
Curtis Poe
 
Corinna-2023.pptx
Corinna-2023.pptxCorinna-2023.pptx
Corinna-2023.pptx
Curtis Poe
 
Corinna Status 2022.pptx
Corinna Status 2022.pptxCorinna Status 2022.pptx
Corinna Status 2022.pptx
Curtis Poe
 
Rummaging in the clOOset
Rummaging in the clOOsetRummaging in the clOOset
Rummaging in the clOOset
Curtis Poe
 
Rescuing a-legacy-codebase
Rescuing a-legacy-codebaseRescuing a-legacy-codebase
Rescuing a-legacy-codebase
Curtis Poe
 
Perl 6 For Mere Mortals
Perl 6 For Mere MortalsPerl 6 For Mere Mortals
Perl 6 For Mere Mortals
Curtis Poe
 
Disappearing Managers, YAPC::EU 2014, Bulgaria, Keynote
Disappearing Managers, YAPC::EU 2014, Bulgaria, KeynoteDisappearing Managers, YAPC::EU 2014, Bulgaria, Keynote
Disappearing Managers, YAPC::EU 2014, Bulgaria, Keynote
Curtis Poe
 
Are Managers An Endangered Species?
Are Managers An Endangered Species?Are Managers An Endangered Species?
Are Managers An Endangered Species?
Curtis Poe
 
The Lies We Tell About Software Testing
The Lies We Tell About Software TestingThe Lies We Tell About Software Testing
The Lies We Tell About Software Testing
Curtis Poe
 
A/B Testing - What your mother didn't tell you
A/B Testing - What your mother didn't tell youA/B Testing - What your mother didn't tell you
A/B Testing - What your mother didn't tell you
Curtis Poe
 
Test::Class::Moose
Test::Class::MooseTest::Class::Moose
Test::Class::Moose
Curtis Poe
 
A Whirlwind Tour of Test::Class
A Whirlwind Tour of Test::ClassA Whirlwind Tour of Test::Class
A Whirlwind Tour of Test::Class
Curtis Poe
 
Agile Companies Go P.O.P.
Agile Companies Go P.O.P.Agile Companies Go P.O.P.
Agile Companies Go P.O.P.
Curtis Poe
 
Econ101
Econ101Econ101
Econ101
Curtis Poe
 
Testing With Test::Class
Testing With Test::ClassTesting With Test::Class
Testing With Test::Class
Curtis Poe
 
Logic Progamming in Perl
Logic Progamming in PerlLogic Progamming in Perl
Logic Progamming in Perl
Curtis Poe
 
Inheritance Versus Roles - The In-Depth Version
Inheritance Versus Roles - The In-Depth VersionInheritance Versus Roles - The In-Depth Version
Inheritance Versus Roles - The In-Depth Version
Curtis Poe
 
Inheritance Versus Roles
Inheritance Versus RolesInheritance Versus Roles
Inheritance Versus Roles
Curtis Poe
 
Turbo Charged Test Suites
Turbo Charged Test SuitesTurbo Charged Test Suites
Turbo Charged Test Suites
Curtis Poe
 

More from Curtis Poe (20)

How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
life-off-earth.pptx
life-off-earth.pptxlife-off-earth.pptx
life-off-earth.pptx
 
Corinna-2023.pptx
Corinna-2023.pptxCorinna-2023.pptx
Corinna-2023.pptx
 
Corinna Status 2022.pptx
Corinna Status 2022.pptxCorinna Status 2022.pptx
Corinna Status 2022.pptx
 
Rummaging in the clOOset
Rummaging in the clOOsetRummaging in the clOOset
Rummaging in the clOOset
 
Rescuing a-legacy-codebase
Rescuing a-legacy-codebaseRescuing a-legacy-codebase
Rescuing a-legacy-codebase
 
Perl 6 For Mere Mortals
Perl 6 For Mere MortalsPerl 6 For Mere Mortals
Perl 6 For Mere Mortals
 
Disappearing Managers, YAPC::EU 2014, Bulgaria, Keynote
Disappearing Managers, YAPC::EU 2014, Bulgaria, KeynoteDisappearing Managers, YAPC::EU 2014, Bulgaria, Keynote
Disappearing Managers, YAPC::EU 2014, Bulgaria, Keynote
 
Are Managers An Endangered Species?
Are Managers An Endangered Species?Are Managers An Endangered Species?
Are Managers An Endangered Species?
 
The Lies We Tell About Software Testing
The Lies We Tell About Software TestingThe Lies We Tell About Software Testing
The Lies We Tell About Software Testing
 
A/B Testing - What your mother didn't tell you
A/B Testing - What your mother didn't tell youA/B Testing - What your mother didn't tell you
A/B Testing - What your mother didn't tell you
 
Test::Class::Moose
Test::Class::MooseTest::Class::Moose
Test::Class::Moose
 
A Whirlwind Tour of Test::Class
A Whirlwind Tour of Test::ClassA Whirlwind Tour of Test::Class
A Whirlwind Tour of Test::Class
 
Agile Companies Go P.O.P.
Agile Companies Go P.O.P.Agile Companies Go P.O.P.
Agile Companies Go P.O.P.
 
Econ101
Econ101Econ101
Econ101
 
Testing With Test::Class
Testing With Test::ClassTesting With Test::Class
Testing With Test::Class
 
Logic Progamming in Perl
Logic Progamming in PerlLogic Progamming in Perl
Logic Progamming in Perl
 
Inheritance Versus Roles - The In-Depth Version
Inheritance Versus Roles - The In-Depth VersionInheritance Versus Roles - The In-Depth Version
Inheritance Versus Roles - The In-Depth Version
 
Inheritance Versus Roles
Inheritance Versus RolesInheritance Versus Roles
Inheritance Versus Roles
 
Turbo Charged Test Suites
Turbo Charged Test SuitesTurbo Charged Test Suites
Turbo Charged Test Suites
 

Recently uploaded

316895207-SAP-Oil-and-Gas-Downstream-Training.pptx
316895207-SAP-Oil-and-Gas-Downstream-Training.pptx316895207-SAP-Oil-and-Gas-Downstream-Training.pptx
316895207-SAP-Oil-and-Gas-Downstream-Training.pptx
ssuserad3af4
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
TheSMSPoint
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
Alberto Brandolini
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
Łukasz Chruściel
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
Philip Schwarz
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
Peter Muessig
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
Peter Muessig
 
SQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure MalaysiaSQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure Malaysia
GohKiangHock
 
What next after learning python programming basics
What next after learning python programming basicsWhat next after learning python programming basics
What next after learning python programming basics
Rakesh Kumar R
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
Patrick Weigel
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
kalichargn70th171
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
Marcin Chrost
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
Alina Yurenko
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
dakas1
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
ICS
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Julian Hyde
 
zOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL DifferenceszOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL Differences
YousufSait3
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
rodomar2
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
Green Software Development
 

Recently uploaded (20)

316895207-SAP-Oil-and-Gas-Downstream-Training.pptx
316895207-SAP-Oil-and-Gas-Downstream-Training.pptx316895207-SAP-Oil-and-Gas-Downstream-Training.pptx
316895207-SAP-Oil-and-Gas-Downstream-Training.pptx
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
 
SQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure MalaysiaSQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure Malaysia
 
What next after learning python programming basics
What next after learning python programming basicsWhat next after learning python programming basics
What next after learning python programming basics
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
 
zOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL DifferenceszOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL Differences
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
 

How to Fake a Database Design

  • 1. How to Fake a Database Design How do I spell “normalization”? OSCON 2014 Curtis "Ovid" Poe http://allaroundtheworld.fr/ Copyright 2014, http://www.allaroundtheworld.fr/ March 18, 2022
  • 2. Good Database Schemas • Generally normalized • Denormalized only as necessary • No duplicate data March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 3. Typical Developer Schemas • A steaming pile of ones and zeros • … with a “family friendly” background March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/ Source: http://commons.wikimedia.org/wiki/File:Spaghetti-prepared.jpg
  • 4. Database Normalization • Remove redundancy • Create logical relations • Decomposing data to atomic elements March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 5. Only Covering 3NF 1. Remove repeating groups of data 2. Remove partial key dependencies 3. Remove data unrelated to key March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 6. How to Feel Stupid March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/ “It is shown that if a relation schema is in third normal form and every key is simple, then it is in projection-join normal form (sometimes called fifth normal form), the ultimate normal form with respect to projections and joins.” Simple Conditions for Guaranteeing Higher Normal Forms in Relational Databases — C. J. Date http://commons.wikimedia.org/wiki/File:%22I_should_have_gone_to_the_pro_station%22_-_NARA_- _514564.tif
  • 7. ‘Nuff of that – Let’s Get Started March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/ I’m going to discuss “how”, not “why”, because I only have 50 minutes.
  • 8. Faking a Database Design • Forget everything you know about Excel • Focus on nouns (sort of) • Duplicate data is a design flaw March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 9. Real-World Problem • Client wanted a rewrite of recipes site • They sent us their Access (!) database • Main objects: – customers – recipes – orders March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 10. Our “DBA” Said This Was OK March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 11. March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/ Our “DBA” also lost his job shortly thereafter
  • 12. Back to the plot … • Customers • Orders • Recipes March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 13. Nouns == Tables(*) March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 14. Nouns == Tables(*) March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 15. Rule #1 1. Nouns == tables March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 16. What’s with the customer_id? March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 17. It’s a foreign key March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/ One-to-many relationship
  • 18. Our DDL (Data Definition Language) March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/ CREATE TABLE orders ( order_id SERIAL PRIMARY KEY, customer_id INTEGER NOT NULL, order_date TIMESTAMP WITH TIME ZONE NOT NULL, FOREIGN KEY (customer_id) REFERENCES customer(customer_id) );
  • 19. Rule #2 1. Nouns == tables 2. Another table’s ID must have a FK constraint March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 20. Oh dog, no! March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 21. But “What if”? 1. fettuccinne 2. fettuchini 3. fettucini 4. fettucinne 5. fetuchine 6. fetuchinney 7. fetuchinni 8. fetucine 9. fetucini 10. fetucinni March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/ https://www.flickr.com/photos/ykjc9/3485366680/sizes/l
  • 22. Searching SELECT recipe_id, name FROM recipes WHERE ingredient1 IN ( 'fettuccinne', 'fettuchini', 'fettucini', 'fettucinne', 'fetuchine', 'fetuchinney', 'fetuchinni', 'fetucine', 'fetucini', 'fetucinni') OR ingredient2 IN ( 'fettuccinne', 'fettuchini', 'fettucini', 'fettucinne', 'fetuchine', 'fetuchinney', 'fetuchinni', 'fetucine', 'fetucini', 'fetucinni') OR ingredient3 IN ( 'fettuccinne', 'fettuchini', 'fettucini', 'fettucinne', 'fetuchine', 'fetuchinney', 'fetuchinni', 'fetucine', 'fetucini', 'fetucinni') OR ingredient4 IN ( 'fettuccinne', 'fettuchini', 'fettucini', 'fettucinne', 'fetuchine', 'fetuchinney', 'fetuchinni', 'fetucine', 'fetucini', 'fetucinni') OR ingredient5 IN ( 'fettuccinne', 'fettuchini', 'fettucini', 'fettucinne', 'fetuchine', 'fetuchinney', 'fetuchinni', 'fetucine', 'fetucini', 'fetucinni') OR ingredient6 IN ( 'fettuccinne', 'fettuchini', 'fettucini', 'fettucinne', 'fetuchine', 'fetuchinney', 'fetuchinni', 'fetucine', 'fetucini', 'fetucinni') OR ingredient7 IN ( 'fettuccinne', 'fettuchini', 'fettucini', 'fettucinne', 'fetuchine', 'fetuchinney', 'fetuchinni', 'fetucine', 'fetucini', 'fetucinni') OR ingredient8 IN ( 'fettuccinne', 'fettuchini', 'fettucini', 'fettucinne', 'fetuchine', 'fetuchinney', 'fetuchinni', 'fetucine', 'fetucini', 'fetucinni'); March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 23. It’s “fettuccine”, in case you were wondering March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 24. Searching SELECT recipe_id, name FROM recipes WHERE ingredient1 = 'fettuccine' OR ingredient2 = 'fettuccine' OR ingredient3 = 'fettuccine' OR ingredient4 = 'fettuccine' OR ingredient5 = 'fettuccine' OR ingredient6 = 'fettuccine' OR ingredient7 = 'fettuccine' OR ingredient8 = 'fettuccine'; March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 25. Ingredients Table March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 26. Rule #3 1. Nouns == tables 2. Another table’s ID must have a FK constraint 3. Lists of things get their own table March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 27. Lookup Table March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/ Many-to-many relationship
  • 28. Searching SELECT recipe_id, name FROM recipes r JOIN recipe_ingredients ri ON ri.recipe_id = r.recipe_id JOIN ingredients i ON i.ingredient_id = ri.ingredient_id WHERE i.name = 'fettuccine'; March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 29. Our DDL (Data Definition Language) March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/ CREATE TABLE recipes_ingredients ( recipe_ingredient_id SERIAL PRIMARY KEY, recipe_id INTEGER NOT NULL, ingredient_id INTEGER NOT NULL, UNIQUE(recipe_id, ingredient_id), FOREIGN KEY (recipe_id) REFERENCES recipes(recipe_id), FOREIGN KEY (ingredient_id) REFERENCES ingredients(ingredient_id) );
  • 30. Our DDL (Data Definition Language) March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/ CREATE TABLE recipes_ingredients ( recipe_id INTEGER NOT NULL, ingredient_id INTEGER NOT NULL, PRIMARY KEY (recipe_id, ingredient_id), FOREIGN KEY (recipe_id) REFERENCES recipes(recipe_id), FOREIGN KEY (ingredient_id) REFERENCES recipes(ingredient_id) );
  • 31. Rule #4 1. Nouns == tables 2. Another table’s ID must have a FK constraint 3. Lists of things get their own table 4. Many-to-many == lookup table (with FKs) March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 32. So How Do We Order Recipes? March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 33. Orders With Recipes March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 34. How Many of Which Ingredient? March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 35. March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/ Our simple “customers”, “orders”, and “recipes” database has grown to seven tables. And it will keep growing.
  • 36. So Far • Every noun has its own table (*) • Lookup tables join related tables • And generally have some of unique constraint • Other table’s ids have foreign key constraints March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 37. Database Tips • We’ve covered the main rules • They only cover structure • Now to dive deeper March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 38. Equality ≠ Identity • No duplication == not duplicating identity • Are identical twins the same person? • Are two guys named “John” the same guy? • This is important and easy to get wrong • For example … March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 39. How do you get the total of an order? • Assume each recipe has a price • Store total in the order? (hint: no) • Store price on the recipe? (hint: yes) • Is that enough? March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 40. Orders Total March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 41. Calculating the Order Total? SELECT o.order_id, sum(i.price) FROM orders o JOIN orders_recipes orr ON orr.order_id = o.order_id JOIN recipes r ON r.recipe_id = orr.recipe_id GROUP BY o.order_id March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 42. What if the price changes? March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 43. Orders Total March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 44. Calculating the Order Total SELECT o.order_id, sum(orr.price) FROM orders o JOIN orders_recipes orr ON orr.order_id = o.order_id GROUP BY o.order_id March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 45. Equality is not Identity • Order item price isn’t item price • What if the item price changes? • What if you give a discount on the order item? • A subtle, but common bug March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 46. Rule #5 1. Nouns == tables 2. Another table’s ID must have a FK constraint 3. Lists of things get their own table 4. Many-to-many == lookup table (with FKs) 5. Watch for equal values that aren’t identical March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 47. Naming • Names are important • Identical columns should have identical names • Names should hint at use March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 48. Bad Naming SELECT name, 'too cold' FROM areas WHERE temperature < 32; March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 49. ID Names orders.order_id versus orders.id March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 50. ID Names SELECT o.id, sum(i.price) FROM orders o JOIN orders_recipes orr ON orr.order_id = o.id JOIN recipes r on r.id = o.id GROUP BY o.order_id March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 51. ID Names SELECT o.id, sum(i.price) FROM orders o JOIN orders_recipes orr ON orr.order_id = o.id JOIN recipes r on r.id = o.id GROUP BY o.order_id March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 52. Conceptually Similar to … SELECT name FROM customer WHERE id > weight; March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 53. ID Names SELECT thread.* FROM email thread JOIN email selected ON selected.id = thread.id JOIN character recipient ON recipient.id = thread.recipient_id JOIN station_area sa ON sa.id = recipient.id JOIN station st ON st.id = sa.id JOIN star origin ON origin.id = thread.id JOIN star destination ON destination.id = st.id LEFT JOIN route ON ( route.from_id = origin.id AND route.to_id = destination.id ) WHERE selected.id = ? AND ( thread.sender_id = ? OR ( thread.recipient_id = ? AND ( origin.id = destination.id OR ( route.distance IS NOT NULL AND now() >= thread.datesent + ( route.distance * interval '30 seconds' ) )))) ORDER BY datesent ASC, thread.parent_id ASC NULLS FIRST March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 54. Rule #6 1. Nouns == tables 2. Another table’s ID must have a FK constraint 3. Lists of things get their own table 4. Many-to-many == lookup table (with FKs) 5. Watch for equal values that aren’t identical 6. Name columns as descriptively as possible March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 55. Summary • Nouns == tables (*) • FK constraints • Proper naming is important • Your DBAs will thank you • Your apps will be more robust March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 56. March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/ ? http://www.slideshare.net/ovid/
  • 57. Bonus Slides! Super-duper important stuff I wasn’t sure I had time to cover because it’s going to make your head hurt. March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 58. Avoid NULL Values • Every column should have a type • NULLs, by definition, are unknown values • Thus, their type is unknown • But … every column should have a type? March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 59. Our employees Table CREATE TABLE employees ( employee_id SERIAL PRIMARY KEY, name CHARACTER VARYING(255) NOT NULL, salary MONEY NULL ); March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 60. Giving Bonuses • $1,000 bonus to all employees • … if they make less than $40,000/year March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 61. Get Employees For Bonus SELECT employee_id, name FROM employee WHERE salary < 40000; March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 62. Bad SQL • Won’t return anyone with a NULL salary • Why is the salary NULL? – What if it’s confidential? – What if they’re a contractor and in that table? – What if they’re an unpaid slave intern? – What if it’s unknown when the data was entered? March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/
  • 63. NULLs tell you nothing supplier_id city s1 ‘London’ March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/ part_id city p1 NULL suppliers table parts table Example via “Database In Depth” by C.J. Date
  • 64. NULLs tell you nothing March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/ part_id city p1 NULL parts table Example via “Database In Depth” by C.J. Date SELECT part_id FROM parts; SELECT part_id FROM parts WHERE city = city;
  • 65. NULLs tell you nothing supplier_id city s1 ‘London’ March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/ part_id city p1 NULL Example via “Database In Depth” by C.J. Date SELECT s.supplier_id, p.part_id FROM suppliers s, parts p WHERE p.city <> s.city -- can’t compare NULL OR p.city <> 'Paris’; -- can’t compare NULL
  • 66. NULLs tell you lies March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/ Example via “Database In Depth” by C.J. Date SELECT s.supplier_id, p.part_id FROM suppliers s, parts p WHERE p.city <> s.city -- can’t compare NULL OR p.city <> 'Paris’; -- can’t compare NULL • We get no rows because we can’t compare a NULL city • The unknown city is Paris or it isn't. • If it’s Paris, the first condition is true • If it’s not Paris, the second condition is true • Thus, the WHERE clause must be true, but it’s not
  • 67. Rule #7 1. Nouns == tables 2. Another table’s ID must have an FK constraint 3. Lists of things get their own table 4. Many-to-many == lookup table (with FKs) 5. Watch for equal values that aren’t identical 6. Name columns as descriptively as possible 7. Avoid NULL columns like the plague March 18, 2022 Copyright 2014, http://www.allaroundtheworld.fr/

Editor's Notes

  1. Duplicate data means identity, not equality!
  2. Any guesses as to what was in ingredient8?
  3. Note that ‘address’ and ‘directions’ aren’t separate tables. Great point for discussion. (Surprêmes de volaille aux champignons === chicken parisienne)
  4. FKs prevent crap data. How many of you have worked on databases with crap data? Well-designed databases can make it hard to add crap data.
  5. Even if you *knew* you would never need more than 8 ingredients, what do you do when you find out that macaroni, barbecue, or fettucinne are routinely misspelled?