You are on page 1of 15

DBMS Project Report

PES University

Database Management Systems

UE18CS252

Submitted By

PES2201800512 Rashaad Ahmed Khan


Introduction
In the system, customer can rent a car based on make and model. The system provides
customer to have different pick-up and drop-off locations and will impose late fee if the rental
car is returned beyond the return date and time. The customers can purchase car rental
insurance which is optional and can use up to one discount coupon to their final bill. Customers
who have membership will be by default given a 10% discount in their final bill. We will see
detailed description below.

• Car rental agency should have collection of cars.


• Each car should belong to a particular Car Category and each car will belong to a
particular location.
• Customer, based on his location and car category preferences, rents a car.
• Based on his location and car category preferences, list of cars available to rent will be
shown along with available date and time (from and to).
• Customer will select a car from the suggestions and should be able to reserve it for
rent.
• When a customer reserves a car, he/she should be able to optionally purchase a Car
Insurance Plan and should be able to apply at most one discount code.
• If a customer is also a member of the car rental agency and has a membership ID then
he/she will be given a default 10% discount in additional to the discount code applied.
Therefore, the total discount percentage will be 10 plus the discount percentage given
by the discount code applied.
• Billing is generated when a car is returned.
• Customer can return the car before the due date, on the due date or he/she can return
it late also.
• If a customer returns a car after the due date, additional late fee is calculated and
added to the bill.
• A default 8.25% tax is applied on the amount which also includes the late fee and this
tax is added to obtain the total amount to which the discount will be applied and a final
amount is obtained.
• Once the car is returned it becomes available for the booking.
• A booking can be cancelled until 5 days before the actual pick up.
• Company may have several discount plans like weekend discount, corporate discount
etc.
• Car price will be calculated based on the selected make and model.
Data Model
FD and Normalization

First normal form (1NF): It is a property of a relation in a relational database. A relation is in


first normal form if and only if the domain of each attribute contains only atomic (indivisible)
values, and the value of each attribute contains only a single value from that domain.

Second normal form (2NF): It is a normal form used in database normalization. A relation
is in the second normal form if it fulfils the following two requirements: It is in first normal form
and every non-primary attribute is fully functionally dependent on the primary key.

Third normal form: A relation is in third normal form if it is in 2NF and no non-key attribute is
transitively dependent on the primary key.

Boyce–Codd normal form (or BCNF or 3.5NF): Is a normal form used in database
normalization. It is a slightly stronger version of the third normal form (3NF). If a relational
schema is in BCNF then all redundancy based on functional dependency has been removed

• Customer_Details Relation:
1. DL_number -> Fname, Mname, Lname, Phone_number, Email_id, Street, City, State,
Zipcode, Membership_id, Membership_type
2. Zipcode -> State,City

• Car Relation:
1. Registration_number -> Model, Make, Model_year, Car_category_name, Loc_id,
Mileage, Availability_flag
2. Model -> Make

• Car_Category Relation:
1. Category_name -> No_of_luggage, No_of_person, Cost_per_day,
Late_fee_per_hour

• Location _Details Relation:


1. Location_id -> Name,Street,City,State,Zipcode
2. Zipcode -> State,City

• Booking_Details Relation:
1. Booking_id -> From_dt_time, Ret_dt_time, Amount, Booking_status, Pickup_loc,
Drop_loc, Reg_num, DL_num, Ins_code, Act_ret_dt_time, Discount_code

• Billing_Details Relation:
1. Bill_id -> Bill_date, Bill_status, Discount_amt, Total_amt, Tax_amt, Booking_id,
Total_late_fee

• Discount_Details Relation:
1. Discount_code -> Discount_name,Expiry_date,Discount_percentage
2. Discount_name -> Discount_code ,Expiry_date,Discount_percentage

• Rental_Car_Insurance Relation:
1. Insurance_code -> Insurance_name,Coverage_type,Cost_per_day
2. Insurance_name -> Insurance_code ,Coverage_type,Cost_per_day
Functional dependencies that violated normalization rules:

• Customer_Details Relation:
1. DL_number -> Zipcode
2. Zipcode -> State, City

• Car Relation:
1. Registration_number -> Model_name
2. Model_name -> Make

• Location_Details Relation
1. Location_id -> Zipcode
2. Zipcode -> State, City
DDL
Create a database and tables.

create database car_database;


use car_database;

Create table customer_details.

CREATE TABLE `CUSTOMER_DETAILS`(


`DL_NUMBER` char(8) NOT NULL,
`FNAME` varchar(25) NOT NULL,
`MNAME` varchar(15),
`LNAME` varchar(25) NOT NULL,
`PHONE_NUMBER` int(10) NOT NULL,
`EMAIL_ID` varchar(30) NOT NULL,
`STREET` varchar(30) NOT NULL,
`CITY` varchar(20) NOT NULL,
`STATE_NAME` varchar(20) NOT NULL,
`ZIPCODE` int(5) NOT NULL,
`MEMBERSHIP_TYPE` char(1) DEFAULT 'N' NOT NULL,
`MEMBERSHIP_ID` char(5),
CONSTRAINT `CUSTOMER` PRIMARY KEY (`DL_NUMBER`)
);
Create table car_category.

CREATE TABLE `CAR_CATEGORY` (


`CATEGORY_NAME` varchar(25) NOT NULL,
`NO_OF_LUGGAGE` int NOT NULL,
`NO_OF_PERSON` int NOT NULL,
`COST_PER_DAY` int(52) NOT NULL,
`LATE_FEE_PER_HOUR` int(52) NOT NULL,
CONSTRAINT `CARCATEGORY` PRIMARY KEY (`CATEGORY_NAME`)
);

Create table location_details.

CREATE TABLE `LOCATION_DETAILS` (


`LOCATION_ID` char(4) NOT NULL,
`LOCATION_NAME` varchar(50) NOT NULL,
`STREET` varchar(30) NOT NULL,
`CITY` varchar(20) NOT NULL,
`STATE_NAME` varchar(20) NOT NULL,
`ZIPCODE` int(5) NOT NULL,
CONSTRAINT `LOCATION` PRIMARY KEY (`LOCATION_ID`)
);

Create table car.

CREATE TABLE `CAR` (


`REGISTRATION_NUMBER` char(7) NOT NULL,
`MODEL_NAME` varchar(25) NOT NULL,
`MAKE` varchar(25) NOT NULL,
`MODEL_YEAR` int(4) NOT NULL,
`MILEAGE` int NOT NULL,
`CAR_CATEGORY_NAME` varchar(25) NOT NULL,
`LOC_ID` char(4) NOT NULL,
`AVAILABILITY_FLAG` char(1) NOT NULL,
CONSTRAINT `CAR` PRIMARY KEY (`REGISTRATION_NUMBER`),
CONSTRAINT `CARFK1` FOREIGN KEY (`CAR_CATEGORY_NAME`) REFERENCES
`CAR_CATEGORY`(`CATEGORY_NAME)`,
CONSTRAINT `CARFK2` FOREIGN KEY (`LOC_ID`) REFERENCES
`LOCATION_DETAILS`(`LOCATION_ID`)
);
Create table discount_details.

CREATE TABLE `DISCOUNT_DETAILS` (


`DISCOUNT_CODE` char(4) NOT NULL,
`DISCOUNT_NAME` varchar(25) NOT NULL,
`EXPIRY_DATE` date NOT NULL,
`DISCOUNT_PERCENTAGE` int(42) NOT NULL,
CONSTRAINT `DISCOUNT` PRIMARY KEY (`DISCOUNT_CODE`),
CONSTRAINT `DISCOUNT` UNIQUE (`DISCOUNT_NAME`)
);
Create table rental_car_insurance.

CREATE TABLE `RENTAL_CAR_INSURANCE` (


`INSURANCE_CODE` char(4) NOT NULL,
`INSURANCE_NAME` varchar(50) NOT NULL,
`COVERAGE_TYPE` varchar(200) NOT NULL,
`COST_PER_DAY` int(42) NOT NULL,
CONSTRAINT `INSURANCE` PRIMARY KEY (`INSURANCE_CODE`),
CONSTRAINT `INSURANCE` UNIQUE (`INSURANCE_NAME`)
);

Create table booking_details.


CREATE TABLE `BOOKING_DETAILS` (
`BOOKING_ID` char(5) NOT NULL,
`FROM_DT_TIME` TIMESTAMP NOT NULL,
`RET_DT_TIME` TIMESTAMP NOT NULL,
`AMOUNT` int(102) NOT NULL,
`BOOKING_STATUS` char(1) NOT NULL,
`PICKUP_LOC` char(4) NOT NULL,
`DROP_LOC` char(4) NOT NULL,
`REG_NUM` char(7) NOT NULL,
`DL_NUM` char(8) NOT NULL,
`INS_CODE` char(4),
`ACT_RET_DT_TIME` TIMESTAMP,
`DISCOUNT_CODE` char(4),
CONSTRAINT `BOOKING` PRIMARY KEY (`BOOKING_ID`),
CONSTRAINT `BOOKINGFK1` FOREIGN KEY (`PICKUP_LOC`) REFERENCES
`LOCATION_DETAILS`(`LOCATION_ID`),
CONSTRAINT `BOOKINGFK2` FOREIGN KEY (`DROP_LOC`) REFERENCES
`LOCATION_DETAILS`(`LOCATION_ID`),
CONSTRAINT `BOOKINGFK3` FOREIGN KEY (`REG_NUM`) REFERENCES
`CAR`(`REGISTRATION_NUMBER`),
CONSTRAINT `BOOKINGFK4` FOREIGN KEY (`DL_NUM`) REFERENCES
`CUSTOMER_DETAILS`(`DL_NUMBER`),
CONSTRAINT `BOOKINGFK5` FOREIGN KEY (`INS_CODE`) REFERENCES
`RENTAL_CAR_INSURANCE`(`INSURANCE_CODE`),
CONSTRAINT `BOOKINGFK6` FOREIGN KEY (`DISCOUNT_CODE`) REFERENCES
`DISCOUNT_DETAILS`(`DISCOUNT_CODE`)
);
Create table billing_details.

CREATE TABLE `BILLING_DETAILS` (


`BILL_ID` char(6) NOT NULL,
`BILL_DATE` DATE NOT NULL,
`BILL_STATUS` char(1) NOT NULL,
`DISCOUNT_AMOUNT` int(102) NOT NULL,
`TOTAL_AMOUNT` int(102) NOT NULL,
`TAX_AMOUNT` int(102) NOT NULL,
`BOOKING_ID` char(5) NOT NULL,
`TOTAL_LATE_FEE` int(102) NOT NULL,
CONSTRAINT `BILLING` PRIMARY KEY (`BILL_ID`),
CONSTRAINT `BILLINGF` FOREIGN KEY (`BOOKING_ID`) REFERENCES
`BOOKING_DETAILS`(`BOOKING_ID`)
);
Triggers
Create an BEFORE INSERT trigger that will not allow users to rent if age is less than 20.

DELIMITER $$
CREATE TRIGGER not_below_twenty
BEFORE INSERT ON CUSTOMER_DETAILS FOR EACH ROW
BEGIN
IF NEW.AGE < 20
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Trigger executed you are too young to
rent cars. Sorry..... ';
END IF;
END;
$$
DELIMITER ;

Create an BEFORE UPGRADE trigger that will say that the discount cannot be
increased.

DELIMITER $$
CREATE TRIGGER no_discount_can_be_edited
BEFORE UPDATE ON BILLING_DETAILS FOR EACH ROW
BEGIN
IF NEW.DISCOUNT_AMOUNT > OLD.DISCOUNT_AMOUNT
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Call from the Trigger. Discount once
stated in the bill can not be increased. ';
END IF;
END;
$$
DELIMITER ;
SQL Queries

SQL query showing the usage of right outer join to display mileage, model_name,
location_name and city from the tables location_details and car.
SELECT car.mileage, car.model_name, location_details.location_name,
location_details.city
FROM car
RIGHT OUTER JOIN location_details
ON car.loc_id = location_details.location_id;

SQL query showing the usage of left outer join to display the reg_num, amount and
discount_amount from the tables booking_details and billing_details.
SELECT booking_details.reg_num, booking_details.amount,
billing_details.discount_amount
FROM booking_details
LEFT OUTER JOIN billing_details
ON booking_details.booking_id = billing_details.booking_id;
SQL query showing the usage of correlated query to display booking_id, pickup_loc
and amount from the table booking_details.

SELECT booking_id, pickup_loc, amount


FROM booking_details
WHERE amount > (SELECT AVG(amount) FROM booking_details);

SQL query showing the usage of correlated nested query to display bill_id and total
from the table billing_details.

SELECT bill_id,
SUM(discount_amount +tax_amount) total
FROM
billing_details
INNER JOIN
booking_details
USING (booking_id)
GROUP BY booking_id
HAVING SUM(discount_amount + tax_amount) > 20;

SQL query showing the usage of aggregate query to display category_name and
average cost per day from the table car_category.

SELECT category_name,
AVG(cost_per_day)
FROM car_category
GROUP BY category_name
ORDER BY category_name;
SQL query showing the usage of aggergated query to display category_name and
maximum late fee per hour from the table car_category.

SELECT category_name, MAX(late_fee_per_hour)


FROM
car_category
GROUP BY category_name
ORDER BY MAX(late_fee_per_hour) DESC;

Conclusion
During the course of this project, I learnt a lot of practices that go into creating a
database, rules to construct a good ER diagram, how to come up with relational
schema mapping for the ER diagram, deriving functional dependencies, how to
normalize the relational schema, how to store and manipulate the data stores in the
tables in the database, make queries about joins, correlated nested queries and
aggregated queries and make complex triggers.

You might also like