Microsoft SQL Server
Chapter 5 - DDL (Data Definition Language)
DDL ASSIGNMENT2 - Food Delivery System
Objective:
You are required to address a set of Data Definition Language (DDL) tasks for a Food Delivery System. Each query focuses on distinct DDL tasks, including the creation of tables, application of constraints, and establishment of relationships between tables.
Requirements:
You will need to complete at least 20 different DDL tasks, covering table creation, primary keys, foreign keys, constraints, and indexes. Each task represents a key part of designing the structure of the Food Delivery System.
Question 1: Create the CUSTOMER Table
Write a DDL SQL statement to create a CUSTOMER table with the following requirements:
customer_idas a primary key with auto-increment functionality.first_nameas a string of maximum length 255 characters, not null.last_nameas a string of maximum length 255 characters, not null.emailas a unique string, not null.phone_numberas a string, not null.genderas a string, not null.addressas a string of maximum length 500 characters, not null.created_atas a datetime field, default it to system timestamp, not null.
Question 2: Create the RESTAURANT Table with Constraints
Create a RESTAURANT table with the following specifications:
restaurant_idas a primary key with auto-increment functionality.restaurant_nameas a string with a maximum length of 255 characters, not null.locationas a string with a maximum length of 255 characters, null.contact_numberas a string, null.created_atas a datetime field, default it to system timestamp, not null.
Question 3: Create the MENU_ITEM Table
Define a MENU_ITEM table with the following details:
menu_item_idas a primary key with auto-increment functionality.item_nameas a string of maximum length 255 characters, not null.item_categoryas a string of maximum length 255 characters, not null.priceas a decimal with precision (10,2), not null.restaurant_idas a foreign key referencing the RESTAURANT table, not null.created_atas a datetime field, default it to system timestamp, not null.- Add a
CHECKconstraint to ensure that theitem_categoryis in (Desserts, Soup, Drinks, Main Course, Starter).
Question 4: Create the COUPON Table
Write a DDL SQL statement to create a COUPON table:
coupon_idas the primary key with auto-increment functionality.coupon_codeas a string, unique and not null.coupon_nameas a string, unique and not null.discount_percentageas a decimal (5,2), not null.start_dateas a date, not null.end_dateas a date, not null.active_flagas a boolean, default to true, not null.created_atas a datetime field, default it to system timestamp, not null.- Add a
CHECKconstraint to ensure that thediscount_percentageis between 0 and 100.
Question 5: Create the ORDER Table with a Foreign Key
Define an ORDER table:
order_idas a primary key with auto-increment functionality.order_dateas a timestamp with the default value of the current timestamp.total_priceas a decimal (10,2), not null.statusas a string (e.g., "Pending", "Delivered", "Cancelled"), not null.customer_idas a foreign key referencing the CUSTOMER table, not null.
Question 6: Define the ORDER_ITEM Table
Write the SQL statement to create a many-to-many relationship between orders and menu items. This will require the ORDER_ITEM table, which includes:
order_idas a foreign key referencing the ORDER table.menu_item_idas a foreign key referencing the MENU_ITEM table, not null.quantityas an integer, not null.created_atas a datetime field, default it to system timestamp, not null.- Set up a composite primary key using
order_idandmenu_item_id.
Question 7: Define the ORDER_COUPON Table
Write the SQL statement to create a many-to-many relationship between orders and menu items. This will require the ORDER_COUPON table, which includes:
order_idas a foreign key referencing the ORDER table.coupon_idas a foreign key referencing the COUPON table.created_atas a datetime field, default it to system timestamp, not null.
Question 8: Create the PAYMENT Table
Write a DDL SQL statement to create a PAYMENT table with the following details:
payment_idas the primary key with auto-increment functionality.payment_dateas a timestamp, defaulting to the current timestamp.amountas a decimal (10, 2), not null.discount_amountas a decimal (10, 2), null.payment_methodas a string (e.g., "Credit Card", "Cash", "Online"), not null.order_idas a foreign key referencing the ORDER table, not null.
Question 9: Create the REFUND Table with Constraints
Write a DDL SQL statement to create a REFUND table:
refund_idas a primary key with auto-increment functionality.refund_amountas a decimal (10, 2), not null.refund_dateas a date field, not null.order_idas a number, not null.created_atas a datetime field, default it to system timestamp, not null.- Add a
CHECKconstraint to ensure therefund_amountis greater than 0.
Question 10: Create the DELIVERY_AGENT Table
Create a DELIVERY_AGENT table:
agent_idas a primary key with auto-increment functionality.first_nameas a string with a maximum length of 255 characters, not null.last_nameas a string with a maximum length of 255 characters, not null.phone_numberas a string, not null.emailas a string, not null.vehicle_numberas a string with a maximum length of 50 characters, not null.created_atas a datetime field, default it to system timestamp, not null.
Question 11: Define the DELIVERY Table with Relationships
Write the SQL statement to create a DELIVERY table that includes:
delivery_idas a primary key with auto-increment functionality.delivery_dateas a timestamp with the default value as the current date.pickup_timeas a time, null.delivered_timeas a time, null.statusas a string (e.g., "In Transit", "Delivered"), not null.agent_idas a foreign key referencing the DELIVERY_AGENT table, not null.order_idas a foreign key referencing the ORDER table.created_atas a datetime field, not null.
Question 12: Create the REVIEW Table
Define a REVIEW table with the following specifications:
review_idas a primary key with auto-increment functionality.ratingas an integer, not null.review_textas a string of maximum length 500 characters, nullable.customer_idas a foreign key referencing the CUSTOMER table, not null.restaurant_idas a foreign key referencing the RESTAURANT table, not null.created_atas a datetime field, default it to system timestamp, not null.
Question 13: Create the AUDIT_LOG Table
Define an AUDIT_LOG table to track changes in the food delivery system:
log_idas a primary key with auto-increment functionality.log_dateas a date field, not null.actionas a string to describe the action performed.user_idas a foreign key referencing the CUSTOMER table.
Question 14: Create the CUSTOMER_LOGIN Table
Write a DDL SQL statement to create a CUSTOMER_LOGIN table (one-to-one relationship with employee table):
customer_idas a foreign key referencing the CUSTOMER table.login_idcan use alpha numeric login id or email as login id, not null.passwordmust encrypt the password before storing it in the database table, not null.active_flaguse numeric, 0 or 1, you can lock the customer login when required, not null.last_login_datetimeas a timestamp, last successfull login date for a given employee.
Question 15: Add a Column to the CUSTOMER Table
Write a SQL statement to add a
loyalty_points column of type INT to the CUSTOMER table.Question 16: Alter a Column in the COUPON Table
Write a SQL statement to alter the
coupon_code column in the COUPON table, increasing its length to 50 characters.Question 17: Drop a Column from the DELIVERY_AGENT Table
Write a SQL statement to drop the
email column from the DELIVERY_AGENT table.Question 18: Rename the AUDIT_LOG Table
Write a SQL statement to rename the AUDIT_LOGS table to FOOD_OUTLET.
Question 19: Drop the CUSTOMER_LOGIN Table
Write a SQL statement to drop the CUSTOMER_LOGIN table.
Question 20: Add a Primary Key on the ORDER_COUPON Table
- Set up a composite primary key using
order_idandcoupon_id.
Question 21: Add a Foreign Key column to the ORDER Table
Add a foreign key in the ORDER table to reference the COUPON table.
Question 22: Alter the REFUND Table to Add a Foreign Key
Write a SQL statement to add a
order_id foreign key referencing the ORDER table.Question 23: Alter the CUSTOMER_LOGIN Table to drop a Foreign Key
Write a SQL statement to add a
order_id foreign key referencing the CUSTOMER table.Question 24: Add a CHECK Constraint on the CUSTOMER Table
Write a SQL statement to add a
CHECK constraint to the CUSTOMER table ensuring that the gender is either M or F.Question 25: Add a CHECK Constraint on the REVIEW Table
Write a SQL statement to add a
CHECK constraint to the REVIEW table ensuring that the rating is between 1 and 5.Question 26: Add a CHECK Constraint on the COUPON Table
Write a SQL statement to add a
UNIQUE constraint to the COUPON table ensuring that the coupon_code is not repeated.Question 27: Add a default Constraint on the DELIVERY Table
Write a SQL statement to add a current system timestamp
DEFAULT constraint to the DELIVERY table on created_at column.Question 28: Add Indexes to the CUSTOMER and RESTAURANT Tables
Write SQL statements to:
- Add an index on the
last_namecolumn in the CUSTOMER table. - Add an index on the
restaurant_namecolumn in the RESTAURANT table.
Question 29: Drop an Index from the RESTAURANT Table
Write a SQL statement to drop the index on the
restaurant_name column in the FOOD_OUTLET table.Question 30: Enforce UNIQUE constraints on all applicable tables.
Apply UNIQUE constraints to columns across the entire database wherever duplicate data is not permitted.
GOOD LUCK WITH YOUR ASSIGNMENT!!!
Don't forget to contact us if you need any further assistance with your assignments, and most importantly, for a manual review and approval of your work.
Sample ERD Data Model for Food Delivery System

To gain complete access, login with gmail or outlook, no need of signup. click here

Comments Not Found