# Krusty Cookies > Krusty Kookies is a bakery which specializes in cookies, and they need a database to keep track of their production and deliveries. ### Base tables Unsuprisingly, we will need a cookie table. ```sql CREATE TABLE cookie ( cookie_id INT PRIMARY KEY, cookie_name VARCHAR(50) NOT NULL UNIQUE, ); ``` Last i checked, a commercial bakery needs customers: ```sql CREATE TABLE customer ( customer_id INT PRIMARY KEY, customer_name VARCHAR(50) NOT NULL, customer_address VARCHAR(50) NOT NULL, ); ``` We could also have a recipe table that relates ingredients to cookies. But instead, we just keep track of inventory (raw materials) and let the business logic handle orders/production by subtracting a certain set of ingredients from the inventory, and adding the corresponding pallets to the "freezer". ```sql CREATE TABLE 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 a pallet is produced, the raw materials storage must be updated, and the company must be able to check the amount in store of each ingredient, and to see when, and **how much of**, an ingredient was last delivered into storage. Because of the 'how much of' part, we cannot simply record a last_delivery field in the raw_materials table. We will use a separate table to keep track of increments in inventory (raw material deliveries). ```sql CREATE TABLE 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) ); ``` When recieving new inventory, we need to initiate a transaction that updates the inventory table, and adds a new row to the raw_mat_delivery table. ### Pallets and orders > The cookies are baked in large quantities, and then quickly frozen and packaged in bags with 15 cookies in each bag. The bags are put into boxes, with 10 bags per box. Finally, the boxes are stacked on pallets, where each pallet contains 36 boxes. 15 x 10 x 36 = 5400 cookies per pallet. So, to produce a pallet: calculate the total material cost and subtract it from the inventory. > The company only delivers to to wholesale Links to an external site. customers, and a typical order looks like “send 10 pallets of Tango cookies, and 6 pallets of Berliners to Kalaskakor AB” – pallets are the unit of all orders (i.e., you can’t break up a pallet in an order). This is reiterating the fact that 'pallet' is the atomic unit of orders. Furthermore: > A pallet is considered to be produced when the pallet label is read at the entrance to the deep-freeze storage. The pallet number, product name, and date and time of production are registered in the database. The pallet number is unique. Conceptually, what happens before the cookies arrive in the freezer is not interesting to us. We only care about the final pallets. The number of cookies per pallet is also of intrest, to keep track of the inventory (recipes and subsequent raw_materials subtractions are handled by the business logic). Either we have pallets in the freezer, or we make more pallets. If we do not have enough inventory to make a pallet, we can't make a pallet, and the order is rejected. ```sql CREATE TABLE 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) ); ``` > When the truck is fully loaded, the driver receives a loading bill containing customer names, addresses, and the number of pallets of each product that is to be delivered to each customer. A transport may contain deliveries intended for different customers. This suggests that we may need to relate pallets to customers via a truck entity, however, this truck entity is never referenced, so we can omit it entirely. > On delivery, pallets are transported from the deep-freeze storeroom via a loading ramp to the freezer trucks – each truck loads 60 pallets. The entry to the loading ramp contains a bar code reader which reads the pallet label. Pallets must be loaded in production date order. Again, the truck as an entity is irrelevant and we only need a table to keep track of what pallet was delivered to what customer along with a date. ```sql CREATE TABLE 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) ); ``` > Orders must be registered in the database, and, for production planning purposes, the company must be able to see all orders which are to be delivered during a specific time period. Note that the individial pallets hold the delivery date. ```sql CREATE TABLE 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) ); ``` > The company continuously take random samples among the products, and the samples are analyzed in their laboratory. If a sample doesn’t meet their quality standards, all pallets containing that product which have been produced during a specific time interval are blocked. A blocked pallet may not be delivered to customers. So, our pallet table needs a status column. This conveniently fits as an enum of 'freezer', 'delivered' and 'blocked'. > All pallets must be traceable, for instance, the company needs to be able to see all information about a pallet with a given number (the contents of the pallet, the location of the pallet, if the pallet is delivered and in that case to whom, etc.). They must also be able to see which pallets contain a certain product and which pallets have been produced during a certain time interval. This should all be possible with a simple join query. > Blocked products are of special interest. The company needs to find out **which products are blocked**, and also which pallets contain a certain blocked product. > Finally, they must be able to check **which pallets have been delivered** to a given customer, and the date and time of delivery. > Orders must be registered in the database, and, for production planning purposes, the company must be able to see all orders which are to be **delivered during a specific time period**. These are all trivial queries.