customers
People who place orders. Email is the natural lookup key, so it usually gets a unique index.
This example models the core of an online shop: who buys, what is sold, and what each order contains. It is a good starting point for a store, a marketplace, or any order-based system.
People who place orders. Email is the natural lookup key, so it usually gets a unique index.
Groups products so shoppers can browse them.
Items for sale. Each belongs to one category and has a price and a stock level.
One purchase by one customer, with a status and a total.
The line items inside an order. It links orders and products, so one order can hold many products and a product can appear in many orders.
products.category_id references categories.id: one row in categories can have many rows in products.orders.customer_id references customers.id: one row in customers can have many rows in orders.order_items is a junction table between orders and products: many-to-many.Copy it into your database, or open it in Crowsfoot to edit the diagram, add notes, and export again.
CREATE TABLE customers (
id bigint PRIMARY KEY,
email varchar(255) NOT NULL,
name varchar(120) NOT NULL,
created_at timestamptz NOT NULL
);
CREATE TABLE categories (
id bigint PRIMARY KEY,
name varchar(80) NOT NULL
);
CREATE TABLE products (
id bigint PRIMARY KEY,
category_id bigint NOT NULL REFERENCES categories (id),
name varchar(160) NOT NULL,
price numeric(10,2) NOT NULL,
stock int NOT NULL DEFAULT 0
);
CREATE TABLE orders (
id bigint PRIMARY KEY,
customer_id bigint NOT NULL REFERENCES customers (id),
status varchar(20) NOT NULL DEFAULT 'pending',
total numeric(10,2) NOT NULL,
placed_at timestamptz NOT NULL
);
CREATE TABLE order_items (
order_id bigint NOT NULL REFERENCES orders (id),
product_id bigint NOT NULL REFERENCES products (id),
quantity int NOT NULL,
unit_price numeric(10,2) NOT NULL,
PRIMARY KEY (order_id, product_id)
);
New to ER diagrams? Read the crow’s foot notation guide.