Professional Documents
Culture Documents
PROIECT
PROIECT
DATABASE PROJECT
FOOD DELIVERY
The purpose of the project is to keep track of the entities involved in a food delivery firm. The
database for this firm was designed in SQL Develover and it contains fundamental notions of
SQL.
The project presents the state of the orders, the clients and the employees of the firm. Using 6
tables, I stored all the information that is needed in order for the firm to properly function.
The data in those tables includes information about the name of the customers, needed for the
safe delivery( the delivery employee can ask the name on which the order was placed in order
to be sure that it reached the right customer). It also include the phone number of each
customer in order to call if there are problems like a buzzer not working. Each employee has a
salary, a driver’s license and an assigned car in order to reach the destinations.Each order
must be placed online or by phone, must be paid cash or card and must be assigned to one
customer who ordered it and one employee who must deliver it. Also, some orders may
include discounts based on the restaurants the customer orders from or the final price of it.
Other information about the system is included also and will be shown briefly in the content
of the project.
The information about the employees (employee_id, first_name, last_name, hire_date,
phone_number, salary, driving_license), the orders (order_id, order_mode, payment_mode,
customer_id, deliver_employee_id, order_final_price) and the customers(customer_id,
full_name, phone_number, address) is easily accessible and the connection between them is
facile, due to the relations created using facilities of SQL.
2.DATABASE SCHEMA
EMPLOYEES
TRANSPORTATION
Employee_id Number(3)
First_name Varchar2(40) Employee_id Number(3)
Last_name Varchar2(40)
Car_used Varchar2
Hire_date Date
Phone_numbe Varchar(40) Car_registration Varchar2
r
Salary Number(6) Year_of_purchase Date
Driver_license Varchar2(3)
ORDERS
Order_id Number(3) FOOD
Order_mode Varchar2 Order_id Number(3)
Payment_mode Varchar2 Restaurant_name Varchar2
Customer_id Number(3) Order_items Number(3)
Deliver_emp_id Number(3) Restaurant_discount Number(5)
Order_final_pric Varchar2(20)
e
ORDER_INFORMATION
Customers
Order_id Number(3) Customer_id Number(3)
First_name Varchar2
Restaurant_name Varchar2 Last_name Varchar2
Phone_numbe Varchar2
Discounts Number(5) r
Address Varchar2
3.CREATING, ALTERING AND DROPPING THE TABLES
I.CREATING TABLES
1) Creating table EMPLOYEES:
CREATE TABLE EMPLOYEES
(employee_id number(3) Primary Key,
first_name varchar2(40) Not Null,
last_name varchar2(40) Not Null,
hire_date date,
phone_number varchar2(40),
salary number(6),
driving_license varchar2(3),
CONSTRAINT driving_license_CK CHECK(driving_license IN('Yes','No'))
);
2) Creating table CUSTOMERS:
CREATE TABLE CUSTOMERS
(customer_id number(3) Primary Key,
first_name varchar2(40) NOT NULL,
last_name varchar2(40) NOT NULL,
phone_number varchar2(40) UNIQUE,
address varchar2(60)
);
3) Creating table ORDERS:
CREATE TABLE ORDERS
(order_id number(3) Primary Key,
order_mode varchar2(10),
payment_mode varchar2(4),
customer_id number(3),
deliver_emp_id number(3),
order_final_price varchar2(10),
CONSTRAINT deliver_FK Foreign Key(deliver_emp_id) REFERENCES
EMPLOYEES(employee_id),
CONSTRAINT order_price_FK Foreign Key(customer_id) REFERENCES
CUSTOMERS(customer_id)
);
4) Creating table TRANSPORT:
CREATE TABLE TRANSPORT
(employee_id number(3) UNIQUE,
car_used varchar2(20),
car_registration varchar2(20),
year_of_purchase date,
CONSTRAINT employee_id_FK Foreign Key(employee_id) REFERENCES
EMPLOYEES(employee_id)
);
5) Creating table FOOD:
CREATE TABLE FOOD
(restaurant_name varchar2(10) Primary Key,
order_id number(3),
order_items varchar2(40) Not Null,
restaurant_discount number(10),
CONSTRAINT order_id_FK Foreign Key(order_id) REFERENCES ORDERS(order_id)
);
6) Creating table ORDER_INFORMATION:
CREATE TABLE ORDER_INFORMATION
(order_id number(3),
restaurant_name varchar2(40),
discounts number(10),
CONSTRAINT order_id_2_FK Foreign Key(order_id) REFERENCES
ORDERS(order_id),
CONSTRAINT rest_name_FK Foreign Key(restaurant_name) REFERENCES
FOOD(restaurant_name)
);
SCRIPT OUTPUT:
Since the drop unused column deletes the column order_price, set unused precedently, the
ALTER TABLE ORDER_INFORMATION DROP COLUMN order_price; won’t work
because it has nothing to delete.
B.CONSTRAINTS ALTERING
We are going to add a new constraint for the phone number in table employees,because it
must start with „07” in order to be an available number :
ALTER TABLE EMPLOYEES ADD CONSTRAINT phone_CK
CHECK( phone_number LIKE '07%');
Now,all of the phone numbers in Employees must start with „07”.
I left the driving_license constraint in the screenshot too(the create table command worked-
proof)
ALTER TABLE EMPLOYEES DROP CONSTRAINT driving_license_CK ;
4.DML STATEMENTS
I.Insert command
I inserted 10 names in the EMPLOYEES table, including my own(NOTE: more
employees may be added across the project):
INSERT INTO EMPLOYEES (employee_id, first_name, last_name, hire_date,
phone_number, salary, driving_license) VALUES(1,’Marina’, ’Giju’, TO_DATE(’2019-
10-01’,’YYYY-MM-DD’),’0752077513’,2000,’No’);
INSERT INTO EMPLOYEES (employee_id, first_name, last_name, hire_date,
phone_number, salary, driving_license) VALUES(2,’Andreea’, ’Comaneci’,
TO_DATE(’2019-11-03’,’YYYY-MM-DD’),’0754654513’,1200,’Yes’);
INSERT INTO EMPLOYEES (employee_id, first_name, last_name, hire_date,
phone_number, salary, driving_license) VALUES(3,’Bianca’, ’Draghici’,
TO_DATE(’2019-11-03’,’YYYY-MM-DD’),’0752068359’,1200,’Yes’);
INSERT INTO EMPLOYEES (employee_id, first_name, last_name, hire_date,
phone_number, salary, driving_license) VALUES(4,’Catalin’, ’Enescu’,
TO_DATE(’2018-12-10’,’YYYY-MM-DD’),’0761579513’,5000,’Yes’);
INSERT INTO EMPLOYEES (employee_id, first_name, last_name, hire_date,
phone_number, salary, driving_license) VALUES(5,’Dorian’, ’Florescu’,
TO_DATE(’2017-07-30’,’YYYY-MM-DD’),’0722544326’,5500,’Yes’);
INSERT INTO EMPLOYEES (employee_id, first_name, last_name, hire_date,
phone_number, salary, driving_license) VALUES(6,’Elena’, ’Hoblea’, TO_DATE(’2019-
01-21’,’YYYY-MM-DD’),’0743214367’,2100,’No’);
INSERT INTO EMPLOYEES (employee_id, first_name, last_name, hire_date,
phone_number, salary, driving_license) VALUES(7,’Florin’, ’Ionescu’,
TO_DATE(’2019-04-18’,’YYYY-MM-DD’),’0752543213’,2000,’No’);
INSERT INTO EMPLOYEES (employee_id, first_name, last_name, hire_date,
phone_number, salary, driving_license) VALUES(8,’George’, ’Jianu’, TO_DATE(’2018-
10-05’,’YYYY-MM-DD’),’076547513’,3000,’Yes’);
INSERT INTO EMPLOYEES (employee_id, first_name, last_name, hire_date,
phone_number, salary, driving_license) VALUES(9,’Horia’, ’Lotvici’, TO_DATE(’2018-
07-01’,’YYYY-MM-DD’),’0752052343’,3000,’No’);
INSERT INTO EMPLOYEES (employee_id, first_name, last_name, hire_date,
phone_number, salary, driving_license) VALUES(10,’Ioana’, ’Marinescu’,
TO_DATE(’2019-10-20’,’YYYY-MM-DD’),’0735763897’,1000,’No’);
The screenshot with my name in it from the table EMPLOYEES.
Inserting record in TRANSPORTATION table
INSERT INTO
TRANSPORTATION(employee_id,car_used,car_registration,year_of_purchase)
VALUES(1,'Skoda','B-100-MDA',TO_DATE('2017-01-01','YYYY-MM-DD'));
INSERT INTO
TRANSPORTATION(employee_id,car_used,car_registration,year_of_purchase)
VALUES(2,'Fiat','B-90-GMA',TO_DATE('2017-01-01','YYYY-MM-DD'));
INSERT INTO
TRANSPORTATION(employee_id,car_used,car_registration,year_of_purchase)
VALUES(3,'Toyota','B-86-KAD',TO_DATE('2018-01-01','YYYY-MM-DD'));
INSERT INTO
TRANSPORTATION(employee_id,car_used,car_registration,year_of_purchase)
VALUES(4,'Ford','B-23-FGH',TO_DATE('2018-01-01','YYYY-MM-DD'));
INSERT INTO
TRANSPORTATION(employee_id,car_used,car_registration,year_of_purchase)
VALUES(5,'Honda','B-73-DEF',TO_DATE('2018-01-01','YYYY-MM-DD'));
INSERT INTO
TRANSPORTATION(employee_id,car_used,car_registration,year_of_purchase)
VALUES(6,'Hyundai','B-37-DSA',TO_DATE('2018-01-01','YYYY-MM-DD'));
INSERT INTO
TRANSPORTATION(employee_id,car_used,car_registration,year_of_purchase)
VALUES(7,'Skoda','B-68-ERT',TO_DATE('2018-01-01','YYYY-MM-DD'));
INSERT INTO
TRANSPORTATION(employee_id,car_used,car_registration,year_of_purchase)
VALUES(8,'Nissan','B-41-GBD',TO_DATE('2019-01-01','YYYY-MM-DD'));
INSERT INTO
TRANSPORTATION(employee_id,car_used,car_registration,year_of_purchase)
VALUES(9,'Nissan','B-59-CHE',TO_DATE('2019-01-01','YYYY-MM-DD'));
INSERT INTO
TRANSPORTATION(employee_id,car_used,car_registration,year_of_purchase)
VALUES(10,'Nissan','B-12-SED',TO_DATE('2019-01-01','YYYY-MM-DD'));
Costumers:
INSERT INTO CUSTOMERS(customer_id,first_name,last_name,phone_number,address)
VALUES (110,'Radu','Comarniceanu','0788954873','Bvd. Gheorghe Sincai');
INSERT INTO CUSTOMERS(customer_id,first_name,last_name,phone_number,address)
VALUES (111,'Denisa','Gaman','0761548364','Bvd. Mircea Eliade');
INSERT INTO CUSTOMERS(customer_id,first_name,last_name,phone_number,address)
VALUES (112,'Emma','Vasile','0772654395','Bvd. Primaverii');
INSERT INTO CUSTOMERS(customer_id,first_name,last_name,phone_number,address)
VALUES (113,'Andrei','Doman','0779253756','Str. Olso');
INSERT INTO CUSTOMERS(customer_id,first_name,last_name,phone_number,address)
VALUES (114,'Sebastian','Stancu','0730681426','Str. Duiosiei');
INSERT INTO CUSTOMERS(customer_id,first_name,last_name,phone_number,address)
VALUES (115,'Bogdan','Leoveanu','0722857376','Str. Episcopiei');
INSERT INTO CUSTOMERS(customer_id,first_name,last_name,phone_number,address)
VALUES (116,'Olga','Stan','0768345912','Bvd. Ion C. Bratianu');
INSERT INTO CUSTOMERS(customer_id,first_name,last_name,phone_number,address)
VALUES (117,'Oana','Dan','0780465783','Bvd. Carol I');
INSERT INTO CUSTOMERS(customer_id,first_name,last_name,phone_number,address)
VALUES (118,'Vlad','Mateescu','0748957231','Bvd. Regina Maria');
INSERT INTO CUSTOMERS(customer_id,first_name,last_name,phone_number,address)
VALUES (119,'Mihaela','Balan','0766688893','Bvd. Tineretului');
II.UPDATE COMMAND
UPDATING FINAL PRICES IN ORDERS TABLE
I want to update the final price, using the restaurant discounts from FOOD table. The
discounts represent the percentage that is reduced from the total price if the order value is
bigger than 100.
UPDATE ORDERS
SET order_final_price = order_final_price-(((SELECT restaurant_discount FROM FOOD
WHERE order_id=100)/100)*order_final_price)
WHERE order_id=100 AND order_final_price > 100;
UPDATE ORDERS
SET order_final_price = order_final_price-(((SELECT restaurant_discount FROM FOOD
WHERE order_id=100)/100)*order_final_price)
WHERE order_id=100 AND order_final_price > 100;
UPDATE ORDERS
SET order_final_price = order_final_price-(((SELECT restaurant_discount FROM FOOD
WHERE order_id=102)/100)*order_final_price)
WHERE order_id=102 AND order_final_price > 100;
UPDATE ORDERS
SET order_final_price = order_final_price-(((SELECT restaurant_discount FROM FOOD
WHERE order_id=103)/100)*order_final_price)
WHERE order_id=103 AND order_final_price > 100;
UPDATE ORDERS
SET order_final_price = order_final_price-(((SELECT restaurant_discount FROM FOOD
WHERE order_id=104)/100)*order_final_price)
WHERE order_id=104 AND order_final_price > 100;
UPDATE ORDERS
SET order_final_price = order_final_price-(((SELECT restaurant_discount FROM FOOD
WHERE order_id=105)/100)*order_final_price)
WHERE order_id=105 AND order_final_price > 100;
UPDATE ORDERS
SET order_final_price = order_final_price-(((SELECT restaurant_discount FROM FOOD
WHERE order_id=106)/100)*order_final_price)
WHERE order_id=106 AND order_final_price > 100;
UPDATE ORDERS
SET order_final_price = order_final_price-(((SELECT restaurant_discount FROM FOOD
WHERE order_id=106)/100)*order_final_price)
WHERE order_id=106 AND order_final_price > 100;
UPDATE ORDERS
SET order_final_price = order_final_price-(((SELECT restaurant_discount FROM FOOD
WHERE order_id=107)/100)*order_final_price)
WHERE order_id=107 AND order_final_price > 100;
UPDATE ORDERS
SET order_final_price = order_final_price-(((SELECT restaurant_discount FROM FOOD
WHERE order_id=108)/100)*order_final_price)
WHERE order_id=108 AND order_final_price > 100;
UPDATE ORDERS
SET order_final_price = order_final_price-(((SELECT restaurant_discount FROM FOOD
WHERE order_id=109)/100)*order_final_price)
WHERE order_id=109 AND order_final_price > 100;
As we can see, only the orders with a higher value than 100 were updated with the discounts.
III.DELETE COMMAND
A new employee inserted a record in the EMPLOYEES table by mistake, when learning how
to use the database:
INSERT INTO EMPLOYEES(employee_id,first_name,last_name, salary) VALUES
(12,'a','b', 2000);
It needs to be deleted now:
DELETE FROM EMPLOYEES
WHERE last_name = 'b';
4. SELECT COMMAND
A.UPDATE+SELECT
As we are in the holiday season, the restaurants decided to a apply a discount for all
the orderes that contain sauces, removing the price of the sauce(5) from the final price
if the order final price si greater than 50(with the discount from the restaurant applied).
UPDATE ORDERS
SET order_final_price = order_final_price - 5
WHERE order_id IN (SELECT order_id FROM FOOD WHERE order_items LIKE
'%Sauce%')
AND order_final_price > 50 ;
2 rows have been updated
Values of the final price for order 103 and order 106 have been updated
Create a new column in the table FOOD that shows the status of the
order(Delivered/Not Delivered). 4 new orders were placed, the status being NOT
DELIVERED. Add the column in order_information.
ALTER TABLE FOOD ADD status_of_the_order VARCHAR2 (15);
UPDATE FOOD
SET status_of_the_order = 'Delivered'
WHERE order_id=100;
UPDATE FOOD
SET status_of_the_order = 'Delivered'
WHERE order_id=101;
UPDATE FOOD
SET status_of_the_order = 'Delivered'
WHERE order_id=102;
UPDATE FOOD
SET status_of_the_order = 'Not Delivered'
WHERE order_id=103;
UPDATE FOOD
SET status_of_the_order = 'Delivered'
WHERE order_id=104;
UPDATE FOOD
SET status_of_the_order = 'Not Delivered'
WHERE order_id=105;
UPDATE FOOD
SET status_of_the_order = 'Delivered'
WHERE order_id=106;
UPDATE FOOD
SET status_of_the_order = 'Not Delivered'
WHERE order_id=107;
UPDATE FOOD
SET status_of_the_order = 'Delivered'
WHERE order_id=108;
UPDATE FOOD
SET status_of_the_order = 'Not Delivered'
WHERE order_id=109;
select * from food;
INSERT INTO
FOOD(restaurant_name,order_id,order_item,restaurant_discount,status_of_the_order)
VALUES ('Gastro',120,'French fries,Onion Rings,Chicken Wings',10,'Not Dellivered');
INSERT INTO
FOOD(restaurant_name,order_id,order_item,restaurant_discount,status_of_the_order)
VALUES ('Bistro',121,'Pork,Onion Rings,Sauce',15,'Not Dellivered');
INSERT INTO
FOOD(restaurant_name,order_id,order_item,restaurant_discount,status_of_the_order)
VALUES ('Sweets',122,'Pancake, Peanut Butter and Jelly',20,'Not Dellivered');
INSERT INTO
FOOD(restaurant_name,order_id,order_item,restaurant_discount,status_of_the_order)
VALUES ('McDonalds',123,'Big Tasty Menu,Sauce Tabasco',0,'Not Dellivered');
B.INSERT +SELECT
INSERT INTO ORDER_INFORMATION(order_id,restaurant_name,discounts)
SELECT order_id,restaurant_name,restaurant_discount FROM FOOD
WHERE order_id BETWEEN 119 AND 124;
select* from order_information;
Since all the orders food,except the last 4 ones are not current (the not delivered ones
are not delivered because the client did not respond or other reasons), replace their
order_status with (null) and then create another table, named „Completed_orders”,
populated with the non-current orders.
UPDATE FOOD
SET status_of_the_order = NULL
WHERE order_id <=119;
select * FROM food;
C. CREATE+SELECT
CREATE TABLE COMPLETED_ORDERS
AS SELECT restaurant_name,order_id,order_items FROM FOOD
WHERE status_of_the_order IS NULL;
select * from completed_orders;
Calculate the profit,knowing that the total cost for each order is 20% of the total price
of the order
SELECT SUM(order_final_price-((20*order_final_price)/100)) total_profit
FROM ORDERS;
Display the employees who managed more than one order and the number of orders
they managed
SELECT COUNT(order_id) number_of_orders_managed, deliver_emp_id
FROM ORDERS
GROUP BY deliver_emp_id
HAVING COUNT(order_id) >=2 ;
TO_CHAR,EXTRACT,SUBSTR,SYSDATE
Display the days in which the firm cars were purchased and how many of them were
purchased that day
SELECT TO_CHAR(year_of_purchase,'DD-MM-YYYY'),
COUNT(TO_CHAR( year_of_purchase,'DD-MM-YYYY'))
FROM TRANSPORTATION
GROUP BY TO_CHAR(year_of_purchase,'DD-MM-YYYY');
No matter the position the employees have, once they have one year of employment,
their salary grows with a certain sum,no matter the rank starting this year. Display the
number of employees hired on all the months of the year in order to check how much
it costs to do that for every month
SELECT EXTRACT(Month FROM hire_date) Month, COUNT(EXTRACT(Month
FROM hire_date))
FROM EMPLOYEES
GROUP BY EXTRACT(Month FROM hire_date);
SELECT TO_CHAR(SYSDATE, 'DD-MM-YYYY HH:MM:SS')
FROM DUAL;
DECODE,CASE,NVL
Considering the fact that some employees don’t have a driver’s license, the boss
decided to give 20% bonus to the ones who have a driver’s license and only 10% to
ones who don’t own on on the next pay day. Display the last name of the employees
and the bonus they will recive
SELECT last_name, DECODE(driving_license,'Yes',0.2,'No',0.1)
FROM EMPLOYEES;
Regardless of the department they work in, the employees are junior, team
manager,director. If the salary is greater or equal to 3000,, the employee is department
director. If the salary is between 1200 and 3000, the employee is team manager and if
the salary is lower or equal to 1200, the employee is a junior. Display the position of
every employee
SELECT salary, last_name,
CASE
WHEN salary >= 3000 THEN 'The employee is department director'
WHEN salary BETWEEN 1200 AND 3000 THEN 'The employee is team manager on
his department'
ELSE 'The employee is junior in his department'
END
FROM EMPLOYEES;
Display all the non current orders as completed
SELECT order_id, NVL(status_of_the_order, 'COMPLETED')
FROM FOOD
WHERE status_of_the_order IS NULL ;
TABLE VIEW
CREATE VIEW EMPLOYEES_VIEW
AS SELECT FIRST_NAME,LAST_NAME,SALARY
FROM EMPLOYEES;
SELECT * FROM EMPLOYEES_VIEW;