Try explaining Sunset Coffee's design to a teammate in a chat: "a customer has many orders, each order holds a few products, a customer also has one profile, a product can show up in many orders...". Before the fifth sentence, everyone is lost. A database design is easier to draw than to tell.
The drawing is called an ER diagram (Entity-Relationship Diagram, often shortened to ERD): a box for each thing whose data is stored, what is recorded about it, and lines connecting the things that relate to each other. It is like the floor plan a builder holds: each room is drawn as a box, and the doors between rooms are drawn as lines. Everyone on the team can read and correct it before a single CREATE TABLE is written.
Three parts: entities, attributes, relationships
Every ER diagram is built from three things. The examples come from Sunset Coffee:
| Part | Meaning | At Sunset Coffee | Later becomes |
|---|---|---|---|
| Entity | a thing whose data is stored | Customer, Profile, Order, Product | a table |
| Attribute | a piece of data recorded about that entity | name, email, price |
a column |
| Relationship | how entities connect, usually a verb | a Customer places an Order | a foreign key, or a linking table |
One attribute has a special role: the primary key, the attribute whose value is unique for each row, such as id.
Chen notation: rectangles, ovals, diamonds
There are two ways of drawing an ERD you'll often meet. The classic one, from textbooks, is Chen notation:
- An entity is drawn as a rectangle.
- An attribute is drawn as an oval attached to its entity.
- The primary key is drawn as an underlined oval: the attribute's name is underlined.
- A relationship is drawn as a diamond in the middle of the line, holding its verb.
(id) (name) (id) (ordered_at)
\ / \ /
[Customer] --1-- <places> ---N-- [Order]
In this text drawing, [ ] is a rectangle, ( ) an oval, < > a diamond, and 1 and N write the cardinality: one customer, many orders. The Lab on this page draws the full version, underline on id included.
Crow's foot notation: what everyday tools use
Tools such as dbdiagram.io, draw.io and MySQL Workbench use crow's foot notation. It is more compact: each entity becomes one box listing its attributes, with the primary key marked PK and foreign keys marked FK. A relationship is just a line, and the numbers are written by the shape of its line ends:
| Line end | Read as |
|---|---|
|| two upright bars |
exactly one |
o| a circle and a bar |
zero or one |
|< a bar and a crow's foot |
one or many |
o< a circle and a crow's foot |
zero or many |
The circle means "may be absent" (at least zero). The bar means "at least one" or "exactly one". The crow's foot means "many".
Reading the line ends
A line is read both ways, always using the end that touches the entity you are counting. The line customers ||----o< orders:
- From customers: one customer has zero or many orders (the end at orders:
o<). Budi has 2 orders, Andi has none yet, and both are fine. - From orders: one order belongs to exactly one customer (the end at customers:
||).
So the crow's foot on the orders side means one customer has many orders, not the other way round. A relationship like this is called one-to-many, written 1:N. One user with many blog posts is drawn the same way: a line from User to Post with the 1:N notation, the crow's foot on the Post side.
Every line in Sunset Coffee's ERD:
| Line | Its ends | Read as | In short |
|---|---|---|---|
| customers to profiles | || and o| |
1 customer has 0 or 1 profile | 1:1 |
| customers to orders | || and o< |
1 customer has 0 or many orders | 1:N |
| orders to order_items | || and |< |
1 order holds 1 or many items | 1:N |
| products to order_items | || and o< |
1 product appears in 0 or many items | 1:N |
order_items is a linking table: through it, one order holds many products and one product is in many orders. In Chen notation it has no box of its own, just the diamond contains with the attribute qty. Why that is comes in the Cardinality & Relationships lesson.
From diagram to tables
Once the diagram is agreed on, it translates directly: entities become tables, attributes become columns, and each line becomes a foreign key on the crow's foot side.
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE
);
CREATE TABLE profiles (
id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL UNIQUE REFERENCES customers(id), -- 1:1
address TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL REFERENCES customers(id), -- 1:N
ordered_at TEXT NOT NULL
);
CREATE TABLE products (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
price INTEGER NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER NOT NULL REFERENCES orders(id),
product_id INTEGER NOT NULL REFERENCES products(id),
qty INTEGER NOT NULL,
PRIMARY KEY (order_id, product_id)
);
||on the customers side becomesNOT NULL: every order must have a customer.- The
o|end at profiles becomesUNIQUEoncustomer_id: a customer can't have a second profile. order_itemshas two foreign keys, and the pair of them is its primary key.
SQLite can be asked which lines it knows about. The answer is four rows, the same as the four lines in the diagram:
| table | column | points_to | target_column |
|---|---|---|---|
| order_items | order_id | orders | id |
| order_items | product_id | products | id |
| orders | customer_id | customers | id |
| profiles | customer_id | customers | id |
📌 Kinds of attribute
- Simple: can't be split further, such as
name,price. - Composite: can be split, such as an address into street, city, postcode. Split it if you need to search by city.
- Derived: worked out from other attributes, such as age from a birth date. Usually not stored, just computed.
- Multi-valued: can hold many values, such as phone numbers. This must not become one column holding a list; the First Normal Form lesson moves it into its own table.
⚠️ A backwards crow's foot, and other diagram slips
- Reading the crow's foot backwards. The crow's foot touches the many side. That is also where the foreign key goes.
- Mixing the two notations. Diamonds and ovals belong to Chen. Boxes listing columns and crow's foot ends belong to crow's foot. Pick one per diagram.
- Drawing many-to-many without a linking table in crow's foot. Two crow's feet facing each other can't become tables yet; they need
order_itemsin the middle. - A wrong answer string in the exercise. The answers are exact lowercase text:
"diamond"(not"Diamond") and"underlined oval".
TL;DR: An ER diagram draws a database design: entities (which become tables), attributes (columns) and relationships (foreign keys). Chen notation uses a rectangle for an entity, an oval for an attribute, an underlined oval for the primary key, and a diamond for a relationship. Crow's foot notation uses boxes listing columns, and the line ends state the numbers: || exactly one, o| zero or one, |< one or many, o< zero or many. The crow's foot touches the many side, so one customer with many orders is 1:N with the crow's foot on the orders side, and the foreign key goes there too.
⚡ Four shapes in Chen notation
Write:
(id) (name) (id) (ordered_at)
\ / \ /
[Customer] --1-- <places> ---N-- [Order]
Guess first: in an ER diagram, what does a rectangle represent?
An entity, a thing whose data is stored, such as Customer and Order. Later it becomes a table.
Meaning: Chen notation uses a rectangle for an entity, an oval for an attribute, an underlined oval for the primary key, and a diamond for a relationship.
In the exercise, the answers are exact lowercase text: "diamond", not "Diamond".
⚡ The crow's foot sits on the many side
Write:
customers ||----o< orders
Meaning: In crow's foot notation, the line ends state the numbers: || exactly one, o< zero or many. One customer has many orders, so this is 1:N with the crow's foot on the orders side.
⚡ A line becomes a foreign key
Write:
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL REFERENCES customers(id), -- 1:N
ordered_at TEXT NOT NULL
);
Meaning: Entities become tables, attributes become columns, and each line becomes a foreign key on the crow's foot side.