CrustyCookiesAPI/app/Migrations/create-schema.sql
2024-05-04 13:38:10 +02:00

72 lines
No EOL
2.4 KiB
SQL

--------------------------------------------
-- Recipe/Cookie related tables
--------------------------------------------
-- Holds the different types of cookies we can make.
CREATE TABLE cookies (
cookie_id INT PRIMARY KEY,
cookie_name VARCHAR(50) NOT NULL UNIQUE
);
-- Our known customers, may need more fields
CREATE TABLE IF NOT EXISTS customers (
customer_id INT PRIMARY KEY,
customer_name VARCHAR(50) NOT NULL,
customer_address VARCHAR(50) NOT NULL
);
-- Orders from customers.
CREATE TABLE IF NOT EXISTS orders (
order_id INT PRIMARY KEY,
customer_id INT NOT NULL,
cookie_id INT NOT NULL,
order_date DATE NOT NULL DEFAULT CURRENT_DATE CHECK (order_date >= CURRENT_DATE),
FOREIGN KEY (customer_id) REFERENCES customer(customer_id),
FOREIGN KEY (cookie_id) REFERENCES cookie(cookie_id)
);
--------------------------------------------
-- Recipe/Cookie related tables
--------------------------------------------
-- Describes ingredients and stock.
CREATE TABLE IF NOT EXISTS raw_materials (
ingredient_id INT PRIMARY KEY,
ingredient_name VARCHAR(50) NOT NULL UNIQUE,
ingredient_quantity INT NOT NULL,
unit VARCHAR(50) NOT NULL CHECK (unit IN ('g', 'ml'))
);
-- When did we get the ingredients?
CREATE TABLE IF NOT EXISTS raw_materials_deliveries (
delivery_id INT PRIMARY KEY,
ingredient_id INT NOT NULL,
delivery_date DATE NOT NULL,
delivery_quantity INT NOT NULL,
unit VARCHAR(50) NOT NULL CHECK (unit IN ('g', 'ml')),
FOREIGN KEY (ingredient_id) REFERENCES raw_materials(ingredient_id)
);
--------------------------------------------
-- Pallet related tables
--------------------------------------------
-- Pallets are used to store cookies for delivery
CREATE TABLE IF NOT EXISTS pallets (
pallet_id INT PRIMARY KEY,
cookie_id INT NOT NULL,
order_id INT NOT NULL,
status VARCHAR(50) NOT NULL CHECK (status IN ('freezer', 'delivered', 'blocked')),
production_date DATE NOT NULL,
FOREIGN KEY (cookie_id) REFERENCES cookie(cookie_id)
FOREIGN KEY (order_id) REFERENCES orders(order_id)
);
-- Has an order been delivered?
CREATE TABLE IF NOT EXISTS deliveries (
delivery_id INT PRIMARY KEY,
pallet_id INT NOT NULL,
customer_name VARCHAR(50) NOT NULL,
delivery_date DATE NOT NULL DEFAULT CURRENT_DATE CHECK (delivery_date >= CURRENT_DATE),
FOREIGN KEY (pallet_id) REFERENCES pallets(pallet_id)
);