From 570366c8e72c97af162b488c133d0c5fb43c3c60 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Fri, 3 May 2024 05:30:52 +0200 Subject: [PATCH] Reworking tables --- app/Migrations/0010-tables.sql | 149 ++++++++++++++++----------------- app/Migrations/0020-data.sql | 24 +++++- 2 files changed, 94 insertions(+), 79 deletions(-) diff --git a/app/Migrations/0010-tables.sql b/app/Migrations/0010-tables.sql index 67fc49c..112dfbc 100644 --- a/app/Migrations/0010-tables.sql +++ b/app/Migrations/0010-tables.sql @@ -1,88 +1,85 @@ +-------------------------------------------- +-- Recipe/Cookie related tables +-------------------------------------------- + +-- Our known customers, may need more fields CREATE TABLE IF NOT EXISTS customers ( customer_id int PRIMARY KEY, - name varchar(100), - address varchar(255) + customer_name varchar(100), + customer_address varchar(255) + -- ...Perhaps more columns ); -CREATE TABLE IF NOT EXISTS products ( - product_id int PRIMARY KEY, - name varchar(100) -); - -CREATE TABLE IF NOT EXISTS recipes ( - recipe_name varchar(100), - recipe_year year, - ingredients int, - product_id int, - PRIMARY KEY (recipe_name, recipe_year), - FOREIGN KEY (ingredients) REFERENCES ingredients(ingredient_id), - FOREIGN KEY (product_id) REFERENCES products(product_id) -); - -CREATE TABLE IF NOT EXISTS ingredients ( - ingredient_id int PRIMARY KEY, - raw_material_name varchar(100), - amount int, - unit varchar(50), - FOREIGN KEY (raw_material_name) REFERENCES raw_materials(raw_material_name) -); - -CREATE TABLE IF NOT EXISTS raw_materials ( - raw_material_name varchar(100) PRIMARY KEY, - quantity int, - last_delivery_date_time datetime -); - -CREATE TABLE IF NOT EXISTS pallets_produced ( - pallet_id int PRIMARY KEY, - product_id int, - production_date_time datetime, - FOREIGN KEY (product_id) REFERENCES products (product_id) -); - -CREATE TABLE IF NOT EXISTS pallets_delivered ( - delivered_id int PRIMARY KEY, - pallet_id int, - delivery_date_time datetime, - FOREIGN KEY (pallet_id) REFERENCES pallets_produced (pallet_id), - FOREIGN KEY (delivered_id) REFERENCES truck (pallet) -); - -CREATE TABLE IF NOT EXISTS truck ( - truck_id int PRIMARY KEY, - capacity int, - pallet int -); - -CREATE TABLE IF NOT EXISTS loading_bill ( - loadingbill_id int PRIMARY KEY, - address varchar(100), - customer varchar(100), - truck_id int, - FOREIGN KEY (truck_id) REFERENCES truck (truck_id) -); - -CREATE TABLE IF NOT EXISTS orders ( +-- Orders from customers. +-- Keep in mind that the delivery_date may be NULL +CREATE TABLE IF NOT EXISTS Orders ( order_id int PRIMARY KEY, customer_id int, - product_id int, - quantity int, - order_date_time datetime, - FOREIGN KEY (customer_id) REFERENCES customers (customer_id), - FOREIGN KEY (product_id) REFERENCES products (product_id) + order_date date DEFAULT NOW(), + delivery_date date, -- Set when the order hits the truck + FOREIGN KEY (customer_id) REFERENCES customers(customer_id) + -- ...Perhaps more columns ); -CREATE TABLE IF NOT EXISTS blocked_products ( - blocked_product_id int PRIMARY KEY, - product_id int, - blocked_date_time datetime, - FOREIGN KEY (product_id) REFERENCES products (product_id) +-------------------------------------------- +-- Recipe/Cookie related tables +-------------------------------------------- + +-- Recipes for all the cookies (essentially a list of cookies) +CREATE TABLE IF NOT EXISTS recipes ( + recipe_id int PRIMARY KEY, + recipe_name varchar(100), -- Cookie name ); -CREATE TABLE IF NOT EXISTS pallet_traceability ( - trace_id int PRIMARY KEY, - location varchar(100), - location_date datetime, +-- "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 +CREATE TABLE IF NOT EXISTS ingredients ( + ingredient_id int PRIMARY KEY, + ingredient_name varchar(100), + amount int, + unit varchar(50), +); + +-- 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, + PRIMARY KEY (recipe_id, ingredient_id), +); + +-------------------------------------------- +-- Pallet related tables +-------------------------------------------- + +-- Pallets are used to store cookies for delivery +CREATE TABLE IF NOT EXISTS pallets ( + pallet_id int PRIMARY KEY, + recipe_id int, + order_id int, + FOREIGN KEY (recipe_id) REFERENCES recipes(recipe_id), + FOREIGN KEY (order_id) REFERENCES Orders(order_id) +); + +-- What does the pallet contain? +CREATE TABLE IF NOT EXISTS pallet_contents ( pallet_id int, - FOREIGN KEY (pallet_id) REFERENCES pallets_produced (pallet_id) + 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) ); + +-- 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, + delivery_date date DEFAULT NOW(), + FOREIGN KEY (order_id) REFERENCES Orders(order_id) +); \ No newline at end of file diff --git a/app/Migrations/0020-data.sql b/app/Migrations/0020-data.sql index 2cac31f..2d34cb0 100644 --- a/app/Migrations/0020-data.sql +++ b/app/Migrations/0020-data.sql @@ -1,4 +1,22 @@ -- Inserts here -INSERT INTO Customers (CustomerID, Name, Address) VALUES - (1, 'John Doe', '123 Main St'), - (2, 'Jane Smith', '456 Elm St'); \ No newline at end of file +INSERT INTO + customers (customer_id, customer_name, customer_address) +VALUES + (1, 'Bjudkakor AB', 'Ystad'), + (2, 'Finkakor AB', 'Helsingborg'), + (3, 'Gästkakor AB', 'Hässleholm'), + (4, 'Kaffebröd AB', 'Landskrona'), + (5, 'Kalaskakor AB', 'Trelleborg'), + (6, 'Partykakor AB', 'Kristianstad'), + (7, 'Skånekakor AB', 'Perstorp'), + (8, 'Småbröd AB', 'Malmö'); + +INSERT INTO + recipes (name) +VALUES + ('Nut ring'), + ('Nut cookie'), + ('Amneris'), + ('Tango'), + ('Almond delight'), + ('Berliner'); \ No newline at end of file