make db consistent with readme
This commit is contained in:
		
							parent
							
								
									bd4257e790
								
							
						
					
					
						commit
						dd1781fa71
					
				
					 1 changed files with 35 additions and 52 deletions
				
			
		| 
						 | 
					@ -4,50 +4,41 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
-- Our known customers, may need more fields
 | 
					-- 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,
 | 
				
			||||||
  customer_name varchar(100),
 | 
					    customer_name VARCHAR(50) NOT NULL,
 | 
				
			||||||
  customer_address varchar(255)
 | 
					    customer_address VARCHAR(50) NOT NULL,
 | 
				
			||||||
);
 | 
					);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
-- Orders from customers.
 | 
					-- Orders from customers.
 | 
				
			||||||
-- Keep in mind that the delivery_date may be NULL
 | 
					 | 
				
			||||||
CREATE TABLE IF NOT EXISTS orders (
 | 
					CREATE TABLE IF NOT EXISTS orders (
 | 
				
			||||||
  order_id int PRIMARY KEY,
 | 
					    order_id INT PRIMARY KEY,
 | 
				
			||||||
  customer_id int,
 | 
					    customer_id INT NOT NULL,
 | 
				
			||||||
  order_date date DEFAULT NOW,
 | 
					    cookie_id INT NOT NULL,
 | 
				
			||||||
  delivery_date date, -- Set when the order hits the truck
 | 
					    order_date DATE NOT NULL DEFAULT CURRENT_DATE CHECK (order_date >= CURRENT_DATE),
 | 
				
			||||||
  FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
 | 
					    FOREIGN KEY (customer_id) REFERENCES customer(customer_id),
 | 
				
			||||||
 | 
					    FOREIGN KEY (cookie_id) REFERENCES cookie(cookie_id)
 | 
				
			||||||
);
 | 
					);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
--------------------------------------------
 | 
					--------------------------------------------
 | 
				
			||||||
-- Recipe/Cookie related tables
 | 
					-- 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) UNIQUE -- Cookie name
 | 
					 | 
				
			||||||
);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
-- "The company has a raw materials warehouse in which 
 | 
					 | 
				
			||||||
--  all ingredients used in their production are stored."
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
-- Describes ingredients and stock.
 | 
					-- Describes ingredients and stock.
 | 
				
			||||||
-- Each ingredient has 'amount' of 'unit' in stock
 | 
					CREATE TABLE IF NOT EXISTS raw_materials (
 | 
				
			||||||
CREATE TABLE IF NOT EXISTS ingredients (
 | 
					    ingredient_id INT PRIMARY KEY,
 | 
				
			||||||
  ingredient_id int PRIMARY KEY,
 | 
					    ingredient_name VARCHAR(50) NOT NULL UNIQUE,
 | 
				
			||||||
  ingredient_name varchar(100),
 | 
					    ingredient_quantity INT NOT NULL,
 | 
				
			||||||
  amount int,
 | 
					    unit VARCHAR(50) NOT NULL CHECK (unit IN ('g', 'ml'))
 | 
				
			||||||
  unit varchar(50)
 | 
					 | 
				
			||||||
);
 | 
					);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
-- Describes what ingredients goes into what recipe
 | 
					-- When did we get the ingredients?
 | 
				
			||||||
-- Each recipe requires 'amount' of 'ingredient'
 | 
					CREATE TABLE IF NOT EXISTS raw_materials_deliveries (
 | 
				
			||||||
CREATE TABLE IF NOT EXISTS recipe_contents (
 | 
					    delivery_id INT PRIMARY KEY,
 | 
				
			||||||
  recipe_id int,
 | 
					    ingredient_id INT NOT NULL,
 | 
				
			||||||
  ingredient_id int,
 | 
					    delivery_date DATE NOT NULL,
 | 
				
			||||||
  amount int,
 | 
					    delivery_quantity INT NOT NULL,
 | 
				
			||||||
  PRIMARY KEY (recipe_id, ingredient_id)
 | 
					    unit VARCHAR(50) NOT NULL CHECK (unit IN ('g', 'ml')),
 | 
				
			||||||
 | 
					    FOREIGN KEY (ingredient_id) REFERENCES raw_materials(ingredient_id)
 | 
				
			||||||
);
 | 
					);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
--------------------------------------------
 | 
					--------------------------------------------
 | 
				
			||||||
| 
						 | 
					@ -56,28 +47,20 @@ CREATE TABLE IF NOT EXISTS recipe_contents (
 | 
				
			||||||
 | 
					
 | 
				
			||||||
-- Pallets are used to store cookies for delivery
 | 
					-- Pallets are used to store cookies for delivery
 | 
				
			||||||
CREATE TABLE IF NOT EXISTS pallets (
 | 
					CREATE TABLE IF NOT EXISTS pallets (
 | 
				
			||||||
  pallet_id int PRIMARY KEY,
 | 
					    pallet_id INT PRIMARY KEY,
 | 
				
			||||||
  recipe_id int,
 | 
					    cookie_id INT NOT NULL,
 | 
				
			||||||
  order_id int,
 | 
					    order_id INT NOT NULL,
 | 
				
			||||||
  FOREIGN KEY (recipe_id) REFERENCES recipes(recipe_id),
 | 
					    status VARCHAR(50) NOT NULL CHECK (status IN ('freezer', 'delivered', 'blocked')),
 | 
				
			||||||
  FOREIGN KEY (order_id) REFERENCES Orders(order_id)
 | 
					    production_date DATE NOT NULL,
 | 
				
			||||||
);
 | 
					    FOREIGN KEY (cookie_id) REFERENCES cookie(cookie_id)
 | 
				
			||||||
 | 
					    FOREIGN KEY (order_id) REFERENCES orders(order_id)
 | 
				
			||||||
-- What does the pallet contain?
 | 
					 | 
				
			||||||
CREATE TABLE IF NOT EXISTS pallet_contents (
 | 
					 | 
				
			||||||
  pallet_id int,
 | 
					 | 
				
			||||||
  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?
 | 
					-- Has an order been delivered?
 | 
				
			||||||
-- When the truck is loaded, a delivery is considered done
 | 
					CREATE TABLE IF NOT EXISTS deliveries (
 | 
				
			||||||
CREATE TABLE IF NOT EXISTS delivery_bill (
 | 
					    delivery_id INT PRIMARY KEY,
 | 
				
			||||||
  delivery_id int PRIMARY KEY,
 | 
					    pallet_id INT NOT NULL,
 | 
				
			||||||
  order_id int,
 | 
					    customer_name VARCHAR(50) NOT NULL,
 | 
				
			||||||
  delivery_date date DEFAULT NOW,
 | 
					    delivery_date DATE NOT NULL DEFAULT CURRENT_DATE CHECK (delivery_date >= CURRENT_DATE),
 | 
				
			||||||
  FOREIGN KEY (order_id) REFERENCES Orders(order_id)
 | 
					    FOREIGN KEY (pallet_id) REFERENCES pallets(pallet_id)
 | 
				
			||||||
);
 | 
					);
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue