CrustyCookiesAPI/app/Migrations/create-schema.sql

83 lines
2.4 KiB
MySQL
Raw Normal View History

2024-05-03 05:30:52 +02:00
--------------------------------------------
-- Recipe/Cookie related tables
--------------------------------------------
-- Our known customers, may need more fields
2024-05-03 01:59:07 +02:00
CREATE TABLE IF NOT EXISTS customers (
customer_id int PRIMARY KEY,
2024-05-03 05:30:52 +02:00
customer_name varchar(100),
customer_address varchar(255)
);
2024-05-03 05:30:52 +02:00
-- Orders from customers.
-- Keep in mind that the delivery_date may be NULL
2024-05-03 08:16:21 +02:00
CREATE TABLE IF NOT EXISTS orders (
2024-05-03 05:30:52 +02:00
order_id int PRIMARY KEY,
customer_id int,
2024-05-03 08:16:21 +02:00
order_date date DEFAULT NOW,
2024-05-03 05:30:52 +02:00
delivery_date date, -- Set when the order hits the truck
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);
2024-05-03 05:30:52 +02:00
--------------------------------------------
-- Recipe/Cookie related tables
--------------------------------------------
-- Recipes for all the cookies (essentially a list of cookies)
2024-05-03 01:59:07 +02:00
CREATE TABLE IF NOT EXISTS recipes (
2024-05-03 05:30:52 +02:00
recipe_id int PRIMARY KEY,
2024-05-03 08:34:18 +02:00
recipe_name varchar(100) UNIQUE -- Cookie name
);
2024-05-03 05:30:52 +02:00
-- "The company has a raw materials warehouse in which
-- all ingredients used in their production are stored."
-- Describes ingredients and stock.
-- Each ingredient has 'amount' of 'unit' in stock
2024-05-03 01:59:07 +02:00
CREATE TABLE IF NOT EXISTS ingredients (
ingredient_id int PRIMARY KEY,
2024-05-03 05:30:52 +02:00
ingredient_name varchar(100),
amount int,
2024-05-03 08:16:21 +02:00
unit varchar(50)
);
2024-05-03 05:30:52 +02:00
-- Describes what ingredients goes into what recipe
-- Each recipe requires 'amount' of 'ingredient'
CREATE TABLE IF NOT EXISTS recipe_contents (
recipe_id int,
ingredient_id int,
amount int,
2024-05-03 08:16:21 +02:00
PRIMARY KEY (recipe_id, ingredient_id)
);
2024-05-03 05:30:52 +02:00
--------------------------------------------
-- Pallet related tables
--------------------------------------------
-- Pallets are used to store cookies for delivery
CREATE TABLE IF NOT EXISTS pallets (
2024-05-03 01:59:07 +02:00
pallet_id int PRIMARY KEY,
2024-05-03 05:30:52 +02:00
recipe_id int,
order_id int,
FOREIGN KEY (recipe_id) REFERENCES recipes(recipe_id),
FOREIGN KEY (order_id) REFERENCES Orders(order_id)
);
2024-05-03 05:30:52 +02:00
-- What does the pallet contain?
CREATE TABLE IF NOT EXISTS pallet_contents (
2024-05-03 01:59:07 +02:00
pallet_id int,
2024-05-03 05:30:52 +02:00
ingredient_id int,
amount int,
PRIMARY KEY (pallet_id, ingredient_id),
FOREIGN KEY (pallet_id) REFERENCES pallets(pallet_id),
FOREIGN KEY (ingredient_id) REFERENCES ingredients(ingredient_id)
);
2024-05-03 05:30:52 +02:00
-- Has an order been delivered?
-- When the truck is loaded, a delivery is considered done
CREATE TABLE IF NOT EXISTS delivery_bill (
delivery_id int PRIMARY KEY,
order_id int,
2024-05-03 08:16:21 +02:00
delivery_date date DEFAULT NOW,
2024-05-03 05:30:52 +02:00
FOREIGN KEY (order_id) REFERENCES Orders(order_id)
);