customers
People who order food.
A delivery app is an order system with one twist: every order belongs to exactly one restaurant. This schema keeps menus per restaurant and order lines that tie an order to the items in it.
People who order food.
Places that prepare the food.
Dishes a restaurant sells, each with a price.
One order from one customer to one restaurant.
The dishes in an order and how many of each. Links orders and menu items.
menu_items.restaurant_id references restaurants.id: one row in restaurants can have many rows in menu_items.orders.customer_id references customers.id: one row in customers can have many rows in orders.orders.restaurant_id references restaurants.id: one row in restaurants can have many rows in orders.order_lines is a junction table between orders and menu_items: 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,
name varchar(120) NOT NULL,
phone varchar(30) NOT NULL
);
CREATE TABLE restaurants (
id bigint PRIMARY KEY,
name varchar(160) NOT NULL,
cuisine varchar(60) NOT NULL
);
CREATE TABLE menu_items (
id bigint PRIMARY KEY,
restaurant_id bigint NOT NULL REFERENCES restaurants (id),
name varchar(160) NOT NULL,
price numeric(8,2) NOT NULL
);
CREATE TABLE orders (
id bigint PRIMARY KEY,
customer_id bigint NOT NULL REFERENCES customers (id),
restaurant_id bigint NOT NULL REFERENCES restaurants (id),
status varchar(20) NOT NULL DEFAULT 'placed',
placed_at timestamptz NOT NULL
);
CREATE TABLE order_lines (
order_id bigint NOT NULL REFERENCES orders (id),
menu_item_id bigint NOT NULL REFERENCES menu_items (id),
quantity int NOT NULL,
PRIMARY KEY (order_id, menu_item_id)
);
New to ER diagrams? Read the crow’s foot notation guide.