Conceptual, logical, and physical levels, and the real, compounding cost of a model that was never designed on purpose.
⏱ 10 min readTopics chapter readerLevel · Foundations
01 · Overview
Start here
⏱ 6 min · Topic 1 of 10
A data model is the plan for your tables. It says which tables you have, what one row means, and how the tables connect.
You make the plan before you put any data in the tables. A good plan gives right numbers. A bad plan gives wrong numbers, even when every row is correct.
This chapter uses the same shop as the SQL chapters. You already know these three tables. Look at them as a plan, not just as data.
customers (5 rows)
customer_id
name
country
1
Asha
IN
2
Ben
GB
3
Carla
US
4
Dev
NULL
5
Omar
IN
orders (6 rows)
order_id
customer_id
status
amount
101
1
paid
40.00
102
1
paid
25.00
103
2
refunded
60.00
104
3
paid
15.50
105
3
pending
NULL
106
4
paid
80.00
order_items (7 rows)
order_id
product
quantity
101
Pen
2
101
Notebook
1
102
Bag
1
103
Lamp
1
104
Pen
3
106
Chair
1
106
Lamp
1
You will learn four things in this chapter.
What a data model is.
Why a wrong model gives wrong numbers.
The three levels of a model.
The questions to ask before you draw anything.
02 · Definition
A plan for your tables
⏱ 8 min · Topic 2 of 10
A data model describes the things a business cares about, and how they connect. Each such thing is called an entity. In the shop, the entities are customer, order and order item.
An attribute is one fact about an entity. A customer has a name and a country. An order has a status and an amount.
A relationship says how two entities connect. One customer places many orders. Each order belongs to one customer.
In a database, an entity becomes a table and an attribute becomes a column.
Two column labels appear in the diagram. PK marks the primary key, the column that identifies each row. FK marks a foreign key, a column that points to a row in another table.
The diagram below shows the shop as a model. Each box is a table.
The shop as a model
customers
ColumnTypeKeys
customer_idPK
name
countryNULL
orders
ColumnTypeKeys
order_idPK
customer_idFK
status
amountNULL
order_items
ColumnTypeKeys
order_idPKFK
productPK
quantity
customers to orders: one customer, many orders. orders to order_items: one order, many items.
Look at order 101 in the orders table at the top of this chapter. Its customer_id is 1. That is the customer_id of Asha in customers.
The model is the shape. The rows are the data that fill the shape. A good shape helps correct rows give correct answers.
A model also leaves things out on purpose. The shop does not store the color of a bag, because nobody asks about it. Keep only what the business needs.
03 · Stakes
Wrong numbers have two causes
⏱ 11 min · Topic 3 of 10
A good model still gives wrong numbers if the query mixes grain. A bad model gives wrong numbers even with a good query. Grain is what one row stands for.
A total that is too big
In the SQL joins chapter, a join repeated an order once for each of its items. That is called fan-out. Here you see what it does to a total.
The question is simple: what is the total amount of all orders? First, the sum after a join to order_items.
Total amount after a join to order_itemsworked example
SQL
Input data
orders joined to order_items (7 rows)7 rows
order_id
amount
product
101
40.00
Pen
101
40.00
Notebook
102
25.00
Bag
103
60.00
Lamp
104
15.50
Pen
106
80.00
Chair
106
80.00
Lamp
Order 101 and order 106 each have two items, so their amount appears twice. Order 105 has no items, so it is gone.
The true total is smaller. Each order amount must count once. Run the sum on orders alone.
Total amount from orders aloneworked example
SQL
selectsum(amount)astotal_amountfromorders;
Result · 1 row
total_amount
220.50
40 + 25 + 60 + 15.50 + 80 = 220.50. The NULL amount of order 105 is skipped.
The join gave 340.50. The right answer is 220.50. The extra 120.00 is the amount of order 101 (40.00) and order 106 (80.00), counted a second time.
The cause is two different grains in one query. In orders, one row is one order.
In the joined rows, one row is one item. The amount belongs to the order, not to the item.
The same fact in two places
Another problem comes from the model itself. Here is a single table that holds the customer country on every order.
orders_flat (a bad design, 2 rows)
order_id
customer_name
country
amount
101
Asha
IN
40.00
102
Asha
GB
25.00
Asha moves to GB. Someone changes order 102 and forgets order 101. Now the table says Asha lives in two countries.
In the real model, you change the Asha row in customers once. Only one row says where Asha lives.
Watch out
Two things give wrong numbers. A query that mixes grain, like the join above. A model that stores one fact in many places, like orders_flat. Say the grain first. Then check where each fact lives.
04 · The framework
Three levels of detail
⏱ 11 min · Topic 4 of 10
You can describe a model with more or less detail. There are three standard levels: conceptual, logical and physical. They describe the same shop, and the customer stays a customer at each level.
The three levels
Level
It answers
Who reads it
The shop
Conceptual
What things exist? How do they connect?
The business
A customer places orders. An order has items.
Logical
Which columns? Which keys? Which rules?
People who design tables
Tables with columns and keys, no data types
Physical
How does one database store it?
Engineers
CREATE TABLE with types and rules
Conceptual: the sentences
The conceptual level uses plain sentences, with no columns. Anyone in the business can read it and say yes or no.
A customer places orders.
An order contains items.
Logical: columns and keys
The logical level adds the columns, the keys and the rules. It does not name a database. Here is the shop at this level.
Logical level: the same shop
customers
ColumnTypeKeys
customer_idPK
name
countryNULL
orders
ColumnTypeKeys
order_idPK
customer_idFK
status
amountNULL
order_items
ColumnTypeKeys
order_idPKFK
productPK
quantity
Columns, keys and which column points where. No data types, no database.
Look at the keys. The conceptual level has none. Only now can an engineer see that orders.customer_id points to customers.
Physical: real tables
The physical level writes the tables for one database. It adds data types and rules the database checks for you.
Three lines in the code that follows matter most. Read them first.
"references customers" is the foreign key.
"primary key (order_id, product)" says one order has one row per product.
"check (quantity > 0)" stops a quantity of zero or less.
Each rule is a decision from the logical level. country and amount allow NULL. Dev has no country, and order 105 has no amount yet.
numeric(10, 2) means a number with up to 10 digits, and 2 of them after the decimal point. It fits money such as 15.50.
Work in this order: conceptual, then logical, then physical. People who start with CREATE TABLE often argue about data types before they agree what a customer is.
05 · Before you draw
Ask questions first
⏱ 7 min · Topic 5 of 10
Do not start with tables. Start with questions. Imagine your manager says: "Show me sales by customer country."
Ask these five questions. They give you the entities, the grain and the keys before you draw one box.
What does the business want to ask? Which numbers, and split by what?
What things exist? These become entities.
What is one row in each table? This is the grain.
What identifies one row? This is the key.
How do the things connect?
Now answer them for the manager request, using the shop.
The five questions, answered for the shop
Question
Answer
What is asked?
Total amount of paid orders, by customer country.
What things exist?
Customer, order, order item.
What is one row?
orders: one order. order_items: one product in one order.
What identifies a row?
customer_id, order_id, and order_id with product.
How do they connect?
orders.customer_id points to customers. order_items.order_id points to orders.
The answers show the plan. The amount lives in orders, and the country lives in customers. You do not need order_items at all.
We count paid orders only. Ask the manager if that is right. Then join the two tables.
Paid orders are 101 and 102 (Asha, IN: 40 + 25), 104 (Carla, US: 15.50) and 106 (Dev, no country: 80).
Dev has no country, so order 106 has a NULL group. Ask the business whether that group should be called unknown.
06 · Interview
Design tables for X
⏱ 4 min · Topic 6 of 10
Interviewers often say: "Design the tables for an online shop." Many candidates draw a star schema at once. A star schema is a report layout with one central table. It is the wrong first move.
Use the five questions from the last section. Say each answer. Last, ask: is this for running the shop, or for reports?
Interview question
Design the tables for an online shop. A strong answer spends the first two minutes on questions. It states one row per order and one row per item before it draws any table.
07 · This week
What comes next
⏱ 3 min · Topic 7 of 10
This chapter gave you the frame. The next chapters teach the parts one by one. You do not need them yet.
The parts of a model, and where you learn them
Part
The question it answers
Chapter
Entities
What things exist, and how do they connect?
2. Entities, Relationships & Cardinality
Keys
What makes one row different from every other row?
3. Keys & Identity
Normalization
How do I store each fact once?
5. Normalization & Denormalization
OLTP and OLAP
OLTP means running the shop, one small change at a time. OLAP means analysis, reading many rows at once. Which one is this data for?
7. OLTP vs OLAP: Choosing How To Structure Data
08 · Recap
Words to remember
⏱ 3 min · Topic 8 of 10
Words from this chapter
Word
Meaning
In the shop
Data model
The plan for tables and how they connect
customers, orders, order_items
Entity
A thing the business cares about
Customer
Attribute
One fact about an entity
Country of a customer
Relationship
How two entities connect
One customer places many orders
Primary key
The column that identifies each row
order_id in orders
Foreign key
A column that points to a row in another table
customer_id in orders
Grain
What one row stands for
One row in orders is one order
Fan-out
A join that repeats a row once per match
Order 101 appears twice with its two items
Conceptual, logical, physical
Three levels of detail
Sentences, then columns and keys, then CREATE TABLE
Say the grain of a table before you use it. If you cannot say it, the numbers will probably be wrong.
09 · Practice
Practice lab
⏱ 3 min · Topic 9 of 10
Each exercise opens a design canvas. Draw the tables, then run the review. The review checks your model against real rows.
The first one is already finished. Start there to see what a correct model looks like.
You now know what a data model is, why it matters, and the three levels. Next you learn to find the entities and draw how they connect.
⏱ 3 min · Topic 10 of 10
Next chapter
Entities, Relationships & Cardinality
You now know what a data model is, why it matters, and the three levels. Next you learn to find the entities and draw how they connect.
Chapter 2 covers entities, attributes and relationships. You learn one-to-one, one-to-many and many-to-many, and the extra table that many-to-many needs.