DB FOUNDATIONS – ASSIGNMENT 2
Project Phase 2
Use Case: Online Food Ordering System
Objective
1) Data Definition Language (DDL)
Definition:
• DDL is a part of SQL commands used to define and manage the structure of a
database/schema. It deals with creating, altering, and deleting database
objects like tables, indexes, and schemas.
• DDL commands are automatically committed and changes are permanent.
Common Commands & Syntax:
▪ CREATE – It is used to create new database objects.
Syntax:
CREATE TABLE tableName ( column1 datatype, column2 datatype );
▪ ALTER – It modify the existing objects.
Syntax:
ALTER TABLE tableName ADD column_name datatype;
▪ DROP – It is used to delete objects permanently.
Syntax:
DROP TABLE tableName;
▪ TRUNCATE – It is used to remove all rows from a table (empty table).
Syntax:
TRUNCATE TABLE tableName;
2) Data Manipulation Language (DML)
Definition:
It is used to manipulate and manage the data stored in database tables. It
involves inserting, updating, deleting, and retrieving data in the table.
Common Commands & Syntax:
▪ INSERT - It add new records.
Syntax:
INSERT INTO tableName (column1, column2) VALUES (value1, value2);
▪ UPDATE – It Modify existing records.
Syntax:
UPDATE tableName SET column1 = value1 WHERE condition;
▪ DELETE – It Remove records.
Syntax:
DELETE FROM tableName WHERE condition;
3) Data Query Language (DQL)
Definition:
DQL is a part of SQL used to fetch/query data from a database. It mainly
consists of the SELECT statement.
Common Command & Syntax:
▪ SELECT - It retrieves data from one or more tables.
Syntax:
SELECT col1, col2 FROM tableName WHERE condition;
Recommended Tools:
1) MySQL Workbench
Definition:
MySQL Workbench is a graphical tool for MySQL databases that allows to
design, develop, and administer databases. It offers features like SQL query execution,
database modelling (ER diagrams), server administration, and user management.
Features:
• Visual database design (ER diagrams)
• SQL query editor
• Server configuration and user account management
• Data export/import options
2) Oracle LiveSQL
Definition:
Oracle LiveSQL is an online SQL learning and execution environment provided by
Oracle. It allows to run SQL queries, PL/SQL scripts, and learn database concepts
without installing any software.
Features:
• Browser-based SQL editor
• Practice environment with sample schemas
• Code sharing and tutorials
• No local installation required
Tasks to be Completed
Part A – Database & Table Creation (DDL)
Question:
It creates a new schema named FoodOrderingSystem and it creates the following
tables with appropriate data types, Primary Keys, and Foreign Keys as Customer
(customer_id, name, email, phone, address) , Restaurant (restaurant_id, name,
location, contact_number) , FoodItem (item_id, restaurant_id, item_name, price,
category) , DeliveryAgent (agent_id, name, phone) , Orders (order_id, customer_id,
order_date, total_amount, status) , OrderDetails (order_id, item_id, quantity) , Review
(review_id, customer_id, restaurant_id, rating, comments) and Offer (offer_id,
restaurant_id, item_id, discount_percent, valid_till) . It also add constraints such as
NOT NULL, UNIQUE, and proper Foreign Keys.
Create the tables:
-- Drop tables if they already exist (to avoid errors on re-run)
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE Offer CASCADE CONSTRAINTS';
EXECUTE IMMEDIATE 'DROP TABLE Review CASCADE CONSTRAINTS';
EXECUTE IMMEDIATE 'DROP TABLE OrderDetails CASCADE CONSTRAINTS';
EXECUTE IMMEDIATE 'DROP TABLE Orders CASCADE CONSTRAINTS';
EXECUTE IMMEDIATE 'DROP TABLE DeliveryAgent CASCADE CONSTRAINTS';
EXECUTE IMMEDIATE 'DROP TABLE FoodItem CASCADE CONSTRAINTS';
EXECUTE IMMEDIATE 'DROP TABLE Restaurant CASCADE CONSTRAINTS';
EXECUTE IMMEDIATE 'DROP TABLE Customer CASCADE CONSTRAINTS';
EXCEPTION
WHEN OTHERS THEN NULL;
END;
/
-- Customer Table
CREATE TABLE Customer (
customer_id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name VARCHAR2(100) NOT NULL,
email VARCHAR2(100) UNIQUE NOT NULL,
phone VARCHAR2(15) UNIQUE NOT NULL,
address VARCHAR2(250) );
-- Restaurant Table
CREATE TABLE Restaurant (
restaurant_id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name VARCHAR2(100) NOT NULL,
location VARCHAR2(250) NOT NULL,
contact_number VARCHAR2(15) UNIQUE NOT NULL
);
-- FoodItem Table
CREATE TABLE FoodItem (
item_id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
restaurant_id NUMBER NOT NULL,
item_name VARCHAR2(100) NOT NULL,
price NUMBER(8,2) NOT NULL,
category VARCHAR2(50),
CONSTRAINT fk_food_rest FOREIGN KEY (restaurant_id)
REFERENCES Restaurant (restaurant_id) ON DELETE CASCADE
);
-- DeliveryAgent Table
CREATE TABLE DeliveryAgent (
agent_id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name VARCHAR2(100) NOT NULL,
phone VARCHAR2(15) UNIQUE NOT NULL );
-- Orders Table
CREATE TABLE Orders (
order_id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
customer_id NUMBER NOT NULL,
order_date DATE DEFAULT SYSDATE,
total_amount NUMBER(10,2) NOT NULL,
status VARCHAR2(20) NOT NULL,
CONSTRAINT fk_orders_customer FOREIGN KEY (customer_id)
REFERENCES Customer (customer_id) ON DELETE CASCADE
);
-- OrderDetails Table
CREATE TABLE OrderDetails (
order_id NUMBER NOT NULL,
item_id NUMBER NOT NULL,
quantity NUMBER NOT NULL,
PRIMARY KEY (order_id, item_id),
CONSTRAINT fk_orderdetails_order FOREIGN KEY (order_id)
REFERENCES Orders (order_id) ON DELETE CASCADE,
CONSTRAINT fk_orderdetails_item FOREIGN KEY (item_id)
REFERENCES FoodItem (item_id) ON DELETE CASCADE
);
--OrderAssignment Table (Order -> DeliveryAgent mapping)
CREATE TABLE OrderAssignment (
order_id NUMBER NOT NULL,
agent_id NUMBER NOT NULL,
PRIMARY KEY (order_id, agent_id),
CONSTRAINT fk_assign_order FOREIGN KEY (order_id)
REFERENCES Orders (order_id) ON DELETE CASCADE,
CONSTRAINT fk_assign_agent FOREIGN KEY (agent_id)
REFERENCES DeliveryAgent (agent_id) ON DELETE CASCADE
);
-- Review Table
CREATE TABLE Review (
review_id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
customer_id NUMBER NOT NULL,
restaurant_id NUMBER NOT NULL,
rating NUMBER(2) CHECK (rating BETWEEN 1 AND 5),
comments VARCHAR2(255),
CONSTRAINT fk_review_customer FOREIGN KEY (customer_id)
REFERENCES Customer (customer_id) ON DELETE CASCADE,
CONSTRAINT fk_review_rest FOREIGN KEY (restaurant_id)
REFERENCES Restaurant (restaurant_id) ON DELETE CASCADE
);
-- Offer Table
CREATE TABLE Offer (
offer_id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
restaurant_id NUMBER NOT NULL,
item_id NUMBER NOT NULL,
discount_percent NUMBER(5,2) NOT NULL,
valid_till DATE NOT NULL,
CONSTRAINT fk_offer_rest FOREIGN KEY (restaurant_id)
REFERENCES Restaurant (restaurant_id) ON DELETE CASCADE,
CONSTRAINT fk_offer_item FOREIGN KEY (item_id)
REFERENCES FoodItem (item_id) ON DELETE CASCADE
);
Part B – Insert Sample Data(DML)
Question :
Insert at least 5 rows in each table. Example data: 5 customers , 3
restaurants , 8 food items , 3 delivery agents , 5 orders with corresponding
order details , 3 reviews and 2 offers
Insert given datas into the table:
-- Customers
INSERT INTO Customer (name, email, phone, address) VALUES
('Rahul Sharma', '[email protected]', '9876893210', 'Delhi');
INSERT INTO Customer (name, email, phone, address) VALUES
('Priya Mehta', '
[email protected]', '9876549011', 'Mumbai');
INSERT INTO Customer (name, email, phone, address) VALUES
('Arjun Verma', '[email protected]', '9876512212', 'Bangalore');
INSERT INTO Customer (name, email, phone, address) VALUES
('Sneha Patel', '
[email protected]', '9876563213', 'Ahmedabad');
INSERT INTO Customer (name, email, phone, address) VALUES
('Vikram Singh', '
[email protected]', '9879843214', 'Kolkata');
-- Restaurants
INSERT INTO Restaurant (name, location, contact_number) VALUES
('Tandoori Treats', 'Delhi', '9000000001');
INSERT INTO Restaurant (name, location, contact_number) VALUES
('Pizza Palace', 'Mumbai', '9000000002');
INSERT INTO Restaurant (name, location, contact_number) VALUES
('Veggie Delight', 'Bangalore', '9000000003');
-- Food Items
INSERT INTO FoodItem (restaurant_id, item_name, price, category) VALUES
(1, 'Butter Chicken', 250, 'Non-Veg');
INSERT INTO FoodItem (restaurant_id, item_name, price, category) VALUES
(1, 'Paneer Tikka', 200, 'Veg');
INSERT INTO FoodItem (restaurant_id, item_name, price, category) VALUES
(2, 'Margherita Pizza', 300, 'Veg');
INSERT INTO FoodItem (restaurant_id, item_name, price, category) VALUES
(2, 'Pepperoni Pizza', 350, 'Non-Veg');
INSERT INTO FoodItem (restaurant_id, item_name, price, category) VALUES
(2, 'Garlic Bread', 150, 'Veg');
INSERT INTO FoodItem (restaurant_id, item_name, price, category) VALUES
(3, 'Veg Burger', 120, 'Veg');
INSERT INTO FoodItem (restaurant_id, item_name, price, category) VALUES
(3, 'Pasta Alfredo', 200, 'Veg');
INSERT INTO FoodItem (restaurant_id, item_name, price, category) VALUES
(3, 'Caesar Salad', 180, 'Veg');
-- Delivery Agents
INSERT INTO DeliveryAgent (name, phone) VALUES
('Ramesh Kumar', '8000000001');
INSERT INTO DeliveryAgent (name, phone) VALUES
('Suresh Yadav', '8000000002');
INSERT INTO DeliveryAgent (name, phone) VALUES
('Anita Das', '8000000003');
-- Orders
INSERT INTO Orders (customer_id, order_date, total_amount, status) VALUES
(1, SYSDATE-5, 600, 'Delivered');
INSERT INTO Orders (customer_id, order_date, total_amount, status) VALUES
(2, SYSDATE-4, 450, 'Delivered');
INSERT INTO Orders (customer_id, order_date, total_amount, status) VALUES
(3, SYSDATE-3, 800, 'Delivered');
INSERT INTO Orders (customer_id, order_date, total_amount, status) VALUES
(4, SYSDATE-2, 550, 'Pending');
INSERT INTO Orders (customer_id, order_date, total_amount, status) VALUES
(5, SYSDATE-1, 300, 'Delivered');
-- Order Details
INSERT INTO OrderDetails VALUES (1, 1, 2);
INSERT INTO OrderDetails VALUES (1, 2, 1);
INSERT INTO OrderDetails VALUES (2, 3, 1);
INSERT INTO OrderDetails VALUES (3, 4, 2);
INSERT INTO OrderDetails VALUES (3, 5, 1);
INSERT INTO OrderDetails VALUES (4, 6, 3);
INSERT INTO OrderDetails VALUES (5, 7, 1);
-- Order Assignment (link orders to agents)
INSERT INTO OrderAssignment VALUES (1, 1);
INSERT INTO OrderAssignment VALUES (2, 1);
INSERT INTO OrderAssignment VALUES (3, 2);
INSERT INTO OrderAssignment VALUES (4, 2);
INSERT INTO OrderAssignment VALUES (5, 2);
INSERT INTO OrderAssignment VALUES (1, 2);
-- Reviews
INSERT INTO Review (customer_id, restaurant_id, rating, comments) VALUES
(1, 1, 5, 'Excellent food!');
INSERT INTO Review (customer_id, restaurant_id, rating, comments) VALUES
(2, 2, 4, 'Great pizza!');
INSERT INTO Review (customer_id, restaurant_id, rating, comments) VALUES
(3, 3, 3, 'Good but could improve');
-- Offers
INSERT INTO Offer (restaurant_id, item_id, discount_percent, valid_till) VALUES
(1, 1, 10, SYSDATE+5);
INSERT INTO Offer (restaurant_id, item_id, discount_percent, valid_till) VALUES
(2, 3, 15, SYSDATE+2);
Part C – Write Queries (DQL)
-- 1. Display all customers
SELECT * FROM Customer;
-- 2. List all food items along with their restaurant names
SELECT f.item_name, r.name AS restaurant_name
FROM FoodItem f
JOIN Restaurant r ON f.restaurant_id = r.restaurant_id;
-- 3. Show all orders placed by a specific customer (example:
customer_id = 1)
SELECT * FROM Orders WHERE customer_id = 1;
-- 4. Find the total number of food items available in each restaurant
SELECT r.name, COUNT(f.item_id) AS total_items
FROM Restaurant r
LEFT JOIN FoodItem f ON r.restaurant_id = f.restaurant_id
GROUP BY r.name;
-- 5. Display orders with total amount greater than ₹500
SELECT * FROM Orders WHERE total_amount > 500;
-- 6. List delivery agents who have delivered more than 3 orders
SELECT da.name, COUNT(oa.order_id) AS total_orders
FROM DeliveryAgent da
JOIN OrderAssignment oa ON da.agent_id = oa.agent_id
GROUP BY da.name
HAVING COUNT(oa.order_id) > 3;
-- 7. Show all reviews for a particular restaurant (example:
restaurant_id = 1)
SELECT * FROM Review WHERE restaurant_id = 1;
-- 8. Find the most expensive food item and its restaurant
SELECT f.item_name, f.price, r.name AS restaurant_name
FROM FoodItem f
JOIN Restaurant r ON f.restaurant_id = r.restaurant_id
WHERE f.price = (SELECT MAX(price) FROM FoodItem);
-- 9. Display all offers currently valid today
SELECT * FROM Offer WHERE valid_till >= TRUNC(SYSDATE);
-- 10. Calculate the total revenue generated from all orders
SELECT SUM(total_amount) AS total_revenue FROM Orders;
Output Submission for Each Queries:
1. Display all customers.
2. List all food items along with their restaurant names.
3. Show all orders placed by a specific customer.
4. Find the total number of food items available in each
restaurant.
5. Display orders with their total amount greater than ₹500.
6. List delivery agents who have delivered more than 3 orders.
7. Show all reviews for a particular restaurant.
8. Find the most expensive food item and its restaurant
9. Display all offers currently valid today
10.Calculate the total revenue generated from all orders.