-- Create the database
CREATE DATABASE FoodOrderingSystem;
USE FoodOrderingSystem;
-- Create Customer table
CREATE TABLE Customer (
customer_id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
phone VARCHAR(15) NOT NULL,
address TEXT NOT NULL
);
-- Create Restaurant table
CREATE TABLE Restaurant (
restaurant_id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
location VARCHAR(100) NOT NULL,
contact_number VARCHAR(15) NOT NULL
);
-- Create FoodItem table
CREATE TABLE FoodItem (
item_id INT PRIMARY KEY AUTO_INCREMENT,
restaurant_id INT NOT NULL,
item_name VARCHAR(100) NOT NULL,
price DECIMAL(10,2) NOT NULL,
category VARCHAR(50) NOT NULL,
FOREIGN KEY (restaurant_id) REFERENCES Restaurant(restaurant_id)
);
-- Create DeliveryAgent table
CREATE TABLE DeliveryAgent (
agent_id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
phone VARCHAR(15) NOT NULL
);
-- Create Orders table
CREATE TABLE Orders (
order_id INT PRIMARY KEY AUTO_INCREMENT,
customer_id INT NOT NULL,
order_date DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
total_amount DECIMAL(10,2) NOT NULL,
status VARCHAR(20) NOT NULL DEFAULT 'Pending',
FOREIGN KEY (customer_id) REFERENCES Customer(customer_id)
);
-- Create OrderDetails table
CREATE TABLE OrderDetails (
order_id INT NOT NULL,
item_id INT NOT NULL,
quantity INT NOT NULL DEFAULT 1,
PRIMARY KEY (order_id, item_id),
FOREIGN KEY (order_id) REFERENCES Orders(order_id),
FOREIGN KEY (item_id) REFERENCES FoodItem(item_id)
);
-- Create Review table
CREATE TABLE Review (
review_id INT PRIMARY KEY AUTO_INCREMENT,
customer_id INT NOT NULL,
restaurant_id INT NOT NULL,
rating INT NOT NULL CHECK (rating BETWEEN 1 AND 5),
comments TEXT,
FOREIGN KEY (customer_id) REFERENCES Customer(customer_id),
FOREIGN KEY (restaurant_id) REFERENCES Restaurant(restaurant_id)
);
-- Create Offer table
CREATE TABLE Offer (
offer_id INT PRIMARY KEY AUTO_INCREMENT,
restaurant_id INT,
item_id INT,
discount_percent DECIMAL(5,2) NOT NULL,
valid_till DATE NOT NULL,
FOREIGN KEY (restaurant_id) REFERENCES Restaurant(restaurant_id),
FOREIGN KEY (item_id) REFERENCES FoodItem(item_id),
CHECK (discount_percent BETWEEN 0 AND 100)
);
-- Insert sample customers
INSERT INTO Customer (name, email, phone, address) VALUES
('John Doe', '[email protected]', '9876543210', '123 Main St, City A'),
('Jane Smith', '[email protected]', '8765432109', '456 Oak Ave, City B'),
('Bob Johnson', '[email protected]', '7654321098', '789 Pine Rd, City C'),
('Alice Williams', '[email protected]', '6543210987', '321 Elm St, City D'),
('Charlie Brown', '[email protected]', '5432109876', '654 Maple Dr, City E');
-- Insert sample restaurants
INSERT INTO Restaurant (name, location, contact_number) VALUES
('Tasty Bites', 'Downtown', '1234567890'),
('Spice Garden', 'Mall Road', '2345678901'),
('Burger King', 'Central Plaza', '3456789012');
-- Insert sample food items
INSERT INTO FoodItem (restaurant_id, item_name, price, category) VALUES
(1, 'Chicken Biryani', 250.00, 'Main Course'),
(1, 'Butter Naan', 30.00, 'Bread'),
(1, 'Paneer Tikka', 180.00, 'Starter'),
(2, 'Veg Pizza', 200.00, 'Main Course'),
(2, 'Garlic Bread', 80.00, 'Starter'),
(2, 'Chocolate Brownie', 120.00, 'Dessert'),
(3, 'Whopper Burger', 150.00, 'Main Course'),
(3, 'French Fries', 90.00, 'Side');
-- Insert sample delivery agents
INSERT INTO DeliveryAgent (name, phone) VALUES
('Rahul Sharma', '9876543210'),
('Priya Patel', '8765432109'),
('Amit Singh', '7654321098');
-- Insert sample orders
INSERT INTO Orders (customer_id, order_date, total_amount, status) VALUES
(1, '2023-10-01 12:30:00', 280.00, 'Delivered'),
(2, '2023-10-01 13:15:00', 520.00, 'Delivered'),
(3, '2023-10-02 19:45:00', 390.00, 'In Transit'),
(4, '2023-10-03 20:30:00', 240.00, 'Pending'),
(5, '2023-10-04 18:00:00', 670.00, 'Delivered');
-- Insert sample order details
INSERT INTO OrderDetails (order_id, item_id, quantity) VALUES
(1, 1, 1),
(1, 2, 1),
(2, 4, 2),
(2, 5, 1),
(3, 7, 2),
(3, 8, 1),
(4, 3, 1),
(4, 2, 2),
(5, 1, 2),
(5, 6, 1);
-- Insert sample reviews
INSERT INTO Review (customer_id, restaurant_id, rating, comments) VALUES
(1, 1, 4, 'Good food but delivery was late'),
(2, 2, 5, 'Excellent pizza and service'),
(3, 3, 3, 'Burgers were okay, fries were cold');
-- Insert sample offers
INSERT INTO Offer (restaurant_id, item_id, discount_percent, valid_till) VALUES
(1, NULL, 15.00, '2023-10-31'),
(NULL, 7, 20.00, '2023-10-15');
-- 1. Display all customers
SELECT * FROM Customer;
-- 2. List all food items along with their restaurant names
SELECT fi.item_id, fi.item_name, fi.price, fi.category, r.name AS restaurant_name
FROM FoodItem fi
JOIN Restaurant r ON fi.restaurant_id = r.restaurant_id;
-- 3. Show all orders placed by a specific customer (e.g., customer_id = 1)
SELECT o.order_id, o.order_date, o.total_amount, o.status
FROM Orders o
WHERE o.customer_id = 1;
-- 4. Find the total number of food items available in each restaurant
SELECT r.restaurant_id, r.name, COUNT(fi.item_id) AS total_items
FROM Restaurant r
LEFT JOIN FoodItem fi ON r.restaurant_id = fi.restaurant_id
GROUP BY r.restaurant_id, r.name;
-- 5. Display orders with their total amount greater than ₹500
SELECT * FROM Orders
WHERE total_amount > 500;
-- 6. List delivery agents who have delivered more than 3 orders
-- Note: This assumes we have a delivery tracking table which wasn't in the schema
-- For this assignment, we'll modify to show all delivery agents since we don't have order-agent mapping
SELECT da.agent_id, da.name, da.phone
FROM DeliveryAgent da
WHERE da.agent_id IN (
SELECT DISTINCT agent_id FROM DeliveryAssignment
GROUP BY agent_id
HAVING COUNT(order_id) > 3
);
-- Alternative simpler query since we don't have the delivery tracking table:
SELECT * FROM DeliveryAgent;
-- 7. Show all reviews for a particular restaurant (e.g., restaurant_id = 1)
SELECT r.review_id, c.name AS customer_name, r.rating, r.comments
FROM Review r
JOIN Customer c ON r.customer_id = c.customer_id
WHERE r.restaurant_id = 1;
-- 8. Find the most expensive food item and its restaurant
SELECT fi.item_name, fi.price, r.name AS restaurant_name
FROM FoodItem fi
JOIN Restaurant r ON fi.restaurant_id = r.restaurant_id
ORDER BY fi.price DESC
LIMIT 1;
-- 9. Display all offers currently valid today (assuming today is 2023-10-05)
SELECT o.offer_id,
COALESCE(r.name, 'All Restaurants') AS restaurant_name,
COALESCE(fi.item_name, 'All Items') AS item_name,
o.discount_percent, o.valid_till
FROM Offer o
LEFT JOIN Restaurant r ON o.restaurant_id = r.restaurant_id
LEFT JOIN FoodItem fi ON o.item_id = fi.item_id
WHERE o.valid_till >= CURDATE();
-- 10. Calculate the total revenue generated from all orders
SELECT SUM(total_amount) AS total_revenue FROM Orders;