Reworking tables
This commit is contained in:
parent
5988b459b4
commit
570366c8e7
2 changed files with 94 additions and 79 deletions
|
@ -1,88 +1,85 @@
|
||||||
|
--------------------------------------------
|
||||||
|
-- Recipe/Cookie related tables
|
||||||
|
--------------------------------------------
|
||||||
|
|
||||||
|
-- Our known customers, may need more fields
|
||||||
CREATE TABLE IF NOT EXISTS customers (
|
CREATE TABLE IF NOT EXISTS customers (
|
||||||
customer_id int PRIMARY KEY,
|
customer_id int PRIMARY KEY,
|
||||||
name varchar(100),
|
customer_name varchar(100),
|
||||||
address varchar(255)
|
customer_address varchar(255)
|
||||||
|
-- ...Perhaps more columns
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS products (
|
-- Orders from customers.
|
||||||
product_id int PRIMARY KEY,
|
-- Keep in mind that the delivery_date may be NULL
|
||||||
name varchar(100)
|
CREATE TABLE IF NOT EXISTS Orders (
|
||||||
);
|
|
||||||
|
|
||||||
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 (
|
|
||||||
order_id int PRIMARY KEY,
|
order_id int PRIMARY KEY,
|
||||||
customer_id int,
|
customer_id int,
|
||||||
product_id int,
|
order_date date DEFAULT NOW(),
|
||||||
quantity int,
|
delivery_date date, -- Set when the order hits the truck
|
||||||
order_date_time datetime,
|
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
|
||||||
FOREIGN KEY (customer_id) REFERENCES customers (customer_id),
|
-- ...Perhaps more columns
|
||||||
FOREIGN KEY (product_id) REFERENCES products (product_id)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS blocked_products (
|
--------------------------------------------
|
||||||
blocked_product_id int PRIMARY KEY,
|
-- Recipe/Cookie related tables
|
||||||
product_id int,
|
--------------------------------------------
|
||||||
blocked_date_time datetime,
|
|
||||||
FOREIGN KEY (product_id) REFERENCES products (product_id)
|
-- 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 (
|
-- "The company has a raw materials warehouse in which
|
||||||
trace_id int PRIMARY KEY,
|
-- all ingredients used in their production are stored."
|
||||||
location varchar(100),
|
|
||||||
location_date datetime,
|
-- 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,
|
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)
|
||||||
);
|
);
|
|
@ -1,4 +1,22 @@
|
||||||
-- Inserts here
|
-- Inserts here
|
||||||
INSERT INTO Customers (CustomerID, Name, Address) VALUES
|
INSERT INTO
|
||||||
(1, 'John Doe', '123 Main St'),
|
customers (customer_id, customer_name, customer_address)
|
||||||
(2, 'Jane Smith', '456 Elm St');
|
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');
|
Loading…
Add table
Reference in a new issue