2024-05-05 10:44:28 +02:00
|
|
|
PRAGMA foreign_keys = OFF;
|
|
|
|
|
|
|
|
-- Drop everything...
|
|
|
|
DROP TABLE IF EXISTS pallets;
|
|
|
|
DROP TABLE IF EXISTS raw_materials_deliveries;
|
|
|
|
DROP TABLE IF EXISTS raw_materials;
|
|
|
|
DROP TABLE IF EXISTS orders;
|
|
|
|
DROP TABLE IF EXISTS customers;
|
|
|
|
DROP TABLE IF EXISTS cookies;
|
|
|
|
|
2024-05-03 05:30:52 +02:00
|
|
|
--------------------------------------------
|
|
|
|
-- Recipe/Cookie related tables
|
|
|
|
--------------------------------------------
|
|
|
|
|
2024-05-04 13:36:27 +02:00
|
|
|
-- Holds the different types of cookies we can make.
|
2024-05-04 13:57:55 +02:00
|
|
|
CREATE TABLE IF NOT EXISTS cookies (
|
2024-05-05 09:37:33 +02:00
|
|
|
cookie_id INTEGER PRIMARY KEY,
|
2024-05-04 13:38:10 +02:00
|
|
|
cookie_name VARCHAR(50) NOT NULL UNIQUE
|
2024-05-04 13:36:27 +02:00
|
|
|
);
|
|
|
|
|
2024-05-03 05:30:52 +02:00
|
|
|
-- Our known customers, may need more fields
|
2024-05-03 01:59:07 +02:00
|
|
|
CREATE TABLE IF NOT EXISTS customers (
|
2024-05-05 09:37:33 +02:00
|
|
|
customer_id INTEGER PRIMARY KEY,
|
2024-05-04 13:33:24 +02:00
|
|
|
customer_name VARCHAR(50) NOT NULL,
|
2024-05-04 13:34:22 +02:00
|
|
|
customer_address VARCHAR(50) NOT NULL
|
2024-04-22 13:27:42 +02:00
|
|
|
);
|
|
|
|
|
2024-05-03 05:30:52 +02:00
|
|
|
-- Orders from customers.
|
2024-05-03 08:16:21 +02:00
|
|
|
CREATE TABLE IF NOT EXISTS orders (
|
2024-05-05 09:37:33 +02:00
|
|
|
order_id INTEGER PRIMARY KEY,
|
2024-05-04 13:33:24 +02:00
|
|
|
customer_id INT NOT NULL,
|
|
|
|
cookie_id INT NOT NULL,
|
|
|
|
order_date DATE NOT NULL DEFAULT CURRENT_DATE CHECK (order_date >= CURRENT_DATE),
|
2024-05-04 13:57:55 +02:00
|
|
|
FOREIGN KEY (customer_id) REFERENCES customers(customer_id),
|
|
|
|
FOREIGN KEY (cookie_id) REFERENCES cookies(cookie_id)
|
2024-04-22 13:27:42 +02:00
|
|
|
);
|
|
|
|
|
2024-05-03 05:30:52 +02:00
|
|
|
--------------------------------------------
|
2024-05-05 11:52:55 +02:00
|
|
|
-- Raw materials, ingredients and recipes
|
2024-05-03 05:30:52 +02:00
|
|
|
--------------------------------------------
|
2024-05-05 11:39:28 +02:00
|
|
|
-- Notes: the unit type can be defined in terms
|
|
|
|
-- of volume or weight instead. Here we choose
|
|
|
|
-- to use static si-prefixes in relevant tables.
|
|
|
|
|
2024-05-05 11:52:55 +02:00
|
|
|
-- What types of ingredients do we handle.
|
|
|
|
-- Not currently used, but kept as an example.
|
2024-05-05 11:39:28 +02:00
|
|
|
CREATE TABLE IF NOT EXISTS ingredients (
|
|
|
|
ingredient_id INTEGER PRIMARY KEY,
|
|
|
|
ingredient_name VARCHAR(50) NOT NULL UNIQUE,
|
|
|
|
preferred_unit VARCHAR(50) NOT NULL CHECK (preferred_unit IN ('g', 'ml'))
|
|
|
|
);
|
|
|
|
|
2024-05-05 11:52:55 +02:00
|
|
|
-- What ingredients are in what cookies?
|
|
|
|
-- Glues together the cookies and ingredients, a 'recipe'.
|
2024-05-05 11:39:28 +02:00
|
|
|
CREATE TABLE IF NOT EXISTS recipe_contents (
|
|
|
|
cookie_id INT NOT NULL,
|
|
|
|
ingredient_id INT NOT NULL,
|
|
|
|
quantity INT NOT NULL,
|
|
|
|
unit VARCHAR(50) NOT NULL CHECK (unit IN ('g', 'ml')),
|
|
|
|
PRIMARY KEY (cookie_id, ingredient_id),
|
|
|
|
FOREIGN KEY (cookie_id) REFERENCES cookies(cookie_id),
|
|
|
|
FOREIGN KEY (ingredient_id) REFERENCES ingredients(ingredient_id)
|
|
|
|
);
|
2024-05-03 05:30:52 +02:00
|
|
|
|
|
|
|
-- Describes ingredients and stock.
|
2024-05-05 11:52:55 +02:00
|
|
|
-- This should reference the ingredients table, but we'll keep it simple for now.
|
2024-05-04 13:33:24 +02:00
|
|
|
CREATE TABLE IF NOT EXISTS raw_materials (
|
2024-05-05 09:37:33 +02:00
|
|
|
ingredient_id INTEGER PRIMARY KEY,
|
2024-05-04 13:33:24 +02:00
|
|
|
ingredient_name VARCHAR(50) NOT NULL UNIQUE,
|
|
|
|
ingredient_quantity INT NOT NULL,
|
|
|
|
unit VARCHAR(50) NOT NULL CHECK (unit IN ('g', 'ml'))
|
2024-04-22 13:27:42 +02:00
|
|
|
);
|
|
|
|
|
2024-05-04 13:33:24 +02:00
|
|
|
-- When did we get the ingredients?
|
|
|
|
CREATE TABLE IF NOT EXISTS raw_materials_deliveries (
|
2024-05-05 09:37:33 +02:00
|
|
|
delivery_id INTEGER PRIMARY KEY,
|
2024-05-04 13:33:24 +02:00
|
|
|
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)
|
2024-04-22 13:27:42 +02:00
|
|
|
);
|
|
|
|
|
2024-05-03 05:30:52 +02:00
|
|
|
--------------------------------------------
|
|
|
|
-- Pallet related tables
|
|
|
|
--------------------------------------------
|
|
|
|
|
|
|
|
-- Pallets are used to store cookies for delivery
|
2024-05-05 07:25:10 +02:00
|
|
|
-- Order related columns are unused for now.
|
2024-05-03 05:30:52 +02:00
|
|
|
CREATE TABLE IF NOT EXISTS pallets (
|
2024-05-05 09:37:33 +02:00
|
|
|
pallet_id INTEGER PRIMARY KEY,
|
2024-05-04 13:33:24 +02:00
|
|
|
cookie_id INT NOT NULL,
|
2024-05-05 11:39:28 +02:00
|
|
|
order_id INT, -- This should be not null
|
2024-05-04 13:33:24 +02:00
|
|
|
status VARCHAR(50) NOT NULL CHECK (status IN ('freezer', 'delivered', 'blocked')),
|
|
|
|
production_date DATE NOT NULL,
|
2024-05-04 13:57:55 +02:00
|
|
|
FOREIGN KEY (cookie_id) REFERENCES cookies(cookie_id)
|
2024-05-05 11:39:28 +02:00
|
|
|
FOREIGN KEY (order_id) REFERENCES orders(order_id)
|
2024-04-22 13:27:42 +02:00
|
|
|
);
|
2024-05-05 10:44:28 +02:00
|
|
|
|
2024-05-05 12:51:01 +02:00
|
|
|
-- Connects pallets to orders
|
|
|
|
CREATE TABLE IF NOT EXISTS deliveries (
|
|
|
|
delivery_date DATE DEFAULT NOW,
|
|
|
|
order_id INT NOT NULL,
|
|
|
|
pallet_id INT NOT NULL,
|
|
|
|
FOREIGN KEY (order_id) REFERENCES orders(order_id),
|
|
|
|
FOREIGN KEY (pallet_id) REFERENCES pallets(pallet_id),
|
|
|
|
PRIMARY KEY (order_id, pallet_id)
|
|
|
|
);
|
|
|
|
|
2024-05-05 11:39:28 +02:00
|
|
|
--------------------------------------------
|
|
|
|
-- Views
|
|
|
|
--------------------------------------------
|
|
|
|
|
|
|
|
-- Pallet
|
|
|
|
CREATE VIEW IF NOT EXISTS pallets_view AS
|
|
|
|
SELECT
|
2024-05-05 12:51:01 +02:00
|
|
|
pallets.pallet_id,
|
2024-05-05 11:39:28 +02:00
|
|
|
cookie_name,
|
2024-05-05 12:51:01 +02:00
|
|
|
pallets.order_id,
|
2024-05-05 11:39:28 +02:00
|
|
|
status,
|
|
|
|
production_date,
|
|
|
|
delivery_date
|
|
|
|
FROM pallets
|
2024-05-05 12:51:01 +02:00
|
|
|
LEFT JOIN cookies ON pallets.cookie_id = cookies.cookie_id
|
|
|
|
LEFT JOIN deliveries ON pallets.pallet_id = deliveries.pallet_id;
|
2024-05-05 11:39:28 +02:00
|
|
|
|
2024-05-05 10:44:28 +02:00
|
|
|
PRAGMA foreign_keys = ON;
|