Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
288 views27 pages

Smart Parking System in SQL

The Smart Parking System project aims to design and implement a relational database management system to enhance parking management in urban areas by automating processes such as user registration, slot booking, and payment transactions. The system addresses inefficiencies of traditional parking systems by ensuring real-time tracking of slot availability and data integrity through a well-structured relational schema. Key components include user and parking lot management, booking functionalities, and performance optimization strategies.

Uploaded by

Prabhu Narayan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
288 views27 pages

Smart Parking System in SQL

The Smart Parking System project aims to design and implement a relational database management system to enhance parking management in urban areas by automating processes such as user registration, slot booking, and payment transactions. The system addresses inefficiencies of traditional parking systems by ensuring real-time tracking of slot availability and data integrity through a well-structured relational schema. Key components include user and parking lot management, booking functionalities, and performance optimization strategies.

Uploaded by

Prabhu Narayan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Smart Parking System Assignment

Report: A Database Design and Implementation Project

Smart Parking System: Relational Database


Management System Project

Madhur Mahajan

Roll No: 2023A1R084

Model Institute of Engineering & Technology, Jammu

CSE Semester 4

May 2025
Table of Contents

Contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

Problem Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

Functional Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

Non-Functional Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2

Stakeholders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

Conceptual Design with ER Diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

Relational Schema Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

Normalization Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9

Creating Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Inserting Sample Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Sample Queries for Functionality Demonstration . . . . 11

Database Implementation Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

Indexing for Performance Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

Identifying Columns for Indexing . . . . . . . . . . . . . . . . . . . . 17


Creating Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Impact of Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Trade-Offs of Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Sample Queries to Test Indexing . . . . . . . . . . . . . . . . . . . . 19

Testing & Performance Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

Functional Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Performance Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Scalability Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

Results and Optimizations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

0
Smart Parking System: Relational Database
Management System End Sem Project
Madhur Mahajan
Roll No: 2023A1R084
Model Institute of Engineering & Technology, Jammu
CSE Semester 4

Introduction
The rapid urbanization and increasing vehicle ownership in modern cities have escalated
the demand for efficient p arking m anagement s olutions. T raditional p arking systems,
often reliant on manual processes, suffer from inefficiencies such as prolonged search times
for available slots, errors in payment processing, and suboptimal space utilization. The
Smart Parking System is a database-driven solution designed to address these challenges
by leveraging the principles of Relational Database Management Systems (RDBMS). This
project aims to create a robust backend system to manage user registrations, parking slot
allocations, bookings, and payment transactions seamlessly.
This project integrates concepts of database design with practical implementation
using MySQL. The system ensures data integrity, scalability, and real-time tracking of
parking slot avail-ability, thereby enhancing user convenience and operational e iciency.

Problem Statement
Parking facilities in urban areas face significant challenges due to limited space and in-
creasing demand. Manual parking management systems are prone to errors, such as
double-booking of slots or inaccurate payment records, and often fail to provide real-time
information on slot availability. These ine iciencies lead to user frustration, wasted time,
and reduced revenue for parking facility operators. The Smart Parking System aims to
address these issues by developing a relational database system that automates parking
management processes, ensuring accuracy, e iciency, and user satisfaction.

1
Objectives
The primary objective of this project is to design and implement a relational database
system that supports the following functionalities:

• User registration and management of vehicle details.

• Definition and management of multiple parking lots with unique locations and slot
capacities.

• Real-time tracking of available and occupied parking slots.

• Booking and reservation of parking slots with accurate timestamp logging.

• Recording and processing of payment transactions associated with bookings.

Functional Requirements
The system must fulfill the following functional requirements:

1. User Management: Allow users to register with details such as name, email,
phone number, and vehicle number, ensuring unique identification and secure data
storage.

2. Parking Lot Management: Enable the creation of multiple parking lots, each
with a unique name, location, and total slot capacity.

3. Slot Management: Track the availability status of individual parking slots within
each lot, updating the status upon booking or release.

4. Booking Management: Facilitate user bookings by reserving a specific slot for a


defined period, recording booking and release timestamps.

5. Payment Processing: Record payment details for each booking, including the
amount, payment time, and status (e.g., pending, completed, or failed).

Non-Functional Requirements
In addition to functional requirements, the system must meet the following non-functional
requirements:

• Scalability: The database should handle a growing number of users, parking lots,
and transactions without performance degradation.

• Data Integrity: Ensure consistency and accuracy through primary key, foreign
key, and check constraints.

• Performance: Optimize query execution to support real-time slot availability up-


dates and transaction processing.

• Usability: Provide a clear and intuitive database schema that can be easily inte-
grated with a front-end application.

2
Stakeholders
The primary stakeholders of the Smart Parking System include:

• Users: Individuals who need to park their vehicles and seek a hassle-free booking
experience.

• Parking Facility Operators: Entities responsible for managing parking lots and
ensuring efficient slot utilization.

• System Administrators: Personnel tasked with maintaining the database, en-


suring data integrity, and optimizing performance.

Table 1: Stakeholder Requirements

Stakeholder Requirements
Users Easy registration, real-time slot availability,
secure payment processing
Parking Facility Oper- Accurate slot tracking, automated booking
ators management, revenue tracking
System Administra- Scalable database, robust constraints, effi-
tors cient query performance

3
Conceptual Design with ER Diagram
The ER diagram provides a visual representation of the system’s entities, their attributes,
and the relationships between them. The system comprises five main entities: Users,
Parking Lots, Parking Slots, Bookings, and Payments. Below is the description of the ER
diagram, with a placeholder space for the diagram itself.

Entities and Attributes:

• Users: user_id (PK), name, email (Unique), phone, vehicle_number

• Parking Lots: lot_id (PK), lot_name, location, total_slots

• Parking Slots: slot_id (PK), lot_id (FK → Parking Lots), slot_number, is_available

• Bookings: booking_id (PK), user_id (FK → Users), slot_id (FK → Parking


Slots), booking_time, release_time

• Payments: payment_id (PK), booking_id (FK → Bookings), amount, payment_time,


payment_status

Relationships and Cardinality:

• Users → Bookings: One user can have multiple bookings (1:N).

• Bookings → Parking Slots: One booking reserves exactly one slot (1:1).

• Parking Slots → Parking Lots: Multiple slots belong to one parking lot (N:1).

• Bookings → Payments: One booking is associated with one payment (1:1).

Diagram Representation:

• Entities are represented with rectangles.

• Attributes are represented with ovals.

• Relationships are represented with diamonds.

• Primary Keys are underlined.

• Foreign Keys are represented with linked connections to their parent entity.

4
Figure 1: Entity-Relationship Diagram for Smart Parking System

• Cardinality is marked with (1:N, N:1, 1:1) annotations.

Below is the placeholder space for the ER diagram:


The diagram (to be embedded in Figure 1) illustrates the relationships and structure
described above, ensuring a clear understanding of the database design before proceeding
to the logical design phase.

Relational Schema Design


Based on the ER diagram, the logical design phase involves mapping the conceptual model
into a relational schema. The schema is designed with normalization in mind to eliminate
redundancy and ensure data integrity. The following tables are defined, including their
attributes, data types, and constraints:

• Users: Stores user and vehicle details.

– user_id (INT, PRIMARY KEY, AUTO_INCREMENT): Unique identifier


for each user.
– name (VARCHAR(100), NOT NULL): User’s full name.
– email (VARCHAR(100), UNIQUE, NOT NULL): User’s email address.
– phone (VARCHAR(15), NOT NULL): User’s contact number.
– vehicle_number (VARCHAR(20), NOT NULL): Vehicle registration number.
– Constraints: chk_valid_email ensures email format includes ’@’ and ’.’, chk_phone_length
ensures phone number length is between 10 and 15 digits.

5
• Parking Lots: Stores details of each parking location.
– lot_id (INT, PRIMARY KEY, AUTO_INCREMENT): Unique identifier for
each parking lot.
– lot_name (VARCHAR(100), NOT NULL): Name of the parking lot.
– location (VARCHAR(255), NOT NULL): Location of the parking lot.
– total_slots (INT, NOT NULL, CHECK (total_slots > 0)): Total number of
slots in the lot.
– Constraints: unq_lot_location ensures the combination of lot name and loca-
tion is unique.
• Parking Slots: Stores slot-level information linked to parking lots.
– slot_id (INT, PRIMARY KEY, AUTO_INCREMENT): Unique identifier for
each slot.
– lot_id (INT, NOT NULL, FOREIGN KEY): References lot_id in Parking
Lots.
– slot_number (INT, NOT NULL): Slot number within the lot.
– is_available (BOOLEAN, DEFAULT TRUE): Indicates slot availability.
– Constraints: unq_slot_per_lot ensures slot numbers are unique within each
lot, foreign key with ON DELETE CASCADE.
• Bookings: Records each reservation with timestamps.
– booking_id (INT, PRIMARY KEY, AUTO_INCREMENT): Unique identifier
for each booking.
– user_id (INT, NOT NULL, FOREIGN KEY): References user_id in Users.
– slot_id (INT, NOT NULL, FOREIGN KEY): References slot_id in Parking
Slots.
– booking_time (DATETIME, NOT NULL, DEFAULT CURRENT_TIMESTAMP):
Time of booking.
– release_time (DATETIME, NULL): Time of slot release.
– Constraints: chk_valid_booking_time ensures release time is either null or
greater than or equal to booking time, foreign keys with ON DELETE CAS-
CADE.
• Payments: Stores transaction data linked to bookings.
– payment_id (INT, PRIMARY KEY, AUTO_INCREMENT): Unique identi-
fier for each payment.
– booking_id (INT, NOT NULL, FOREIGN KEY): References booking_id in
Bookings.
– amount (DECIMAL(10,2), NOT NULL, CHECK (amount > 0)): Payment
amount.
– payment_time (DATETIME, NOT NULL, DEFAULT CURRENT_TIMESTAMP):
Time of payment.

6
– payment_status (ENUM(’pending’, ’completed’, ’failed’), DEFAULT ’pend-
ing’): Status of the payment.
– Constraints: Foreign key with ON DELETE CASCADE.

This schema ensures data integrity through primary keys, foreign keys with cascading
deletions, and business rules enforced by CHECK constraints.

Normalization Analysis
The relational schema is evaluated to ensure it meets at least the 3rd Normal Form (3NF):

• Users Table:

– 1NF: All attributes are atomic.


– 2NF: Single candidate key (user_id), no partial dependencies.
– 3NF: No transitive dependencies; all attributes depend directly on user_id.
– Conclusion: The Users table is in 3NF.

• Parking Lots Table:

– 1NF: All attributes are atomic.


– 2NF: Single candidate key (lot_id), no partial dependencies.
– 3NF: No transitive dependencies; all attributes depend on lot_id.
– Conclusion: The Parking Lots table is in 3NF.

• Parking Slots Table:

– 1NF: All attributes are atomic.


– 2NF: Single candidate key (slot_id), no partial dependencies.
– 3NF: All attributes depend on slot_id. The foreign key lot_id does not create
a transitive dependency.
– Conclusion: The Parking Slots table is in 3NF.

• Bookings Table:

– 1NF: All attributes are atomic.


– 2NF: Single candidate key (booking_id), no partial dependencies.
– 3NF: All attributes depend on booking_id. Foreign keys do not introduce
transitive dependencies.
– Conclusion: The Bookings table is in 3NF.

7
• Payments Table:
– 1NF: All attributes are atomic.
– 2NF: Single candidate key (payment_id), no partial dependencies.
– 3NF: All attributes depend on payment_id. The foreign key booking_id is
appropriate.
– Conclusion: The Payments table is in 3NF.

Overall Conclusion: The schema is in 3NF, with no redundancy or transitive de-


pendencies. The use of foreign keys with ON DELETE CASCADE ensures referential
integrity.

8
Implementation
The implementation begins with creating the database tables, followed by populating
them with sample data to simulate real-world usage.

Creating Tables
The following SQL statements create the tables for the Smart Parking System, as defined
in the schema:

MySQL WORKBENCH IMPLEMENTATION

1 -- Create Users Table


2 CREATE TABLE Users (
3 user_id INT AUTO_INCREMENT PRIMARY KEY ,
4 name VARCHAR (100) NOT NULL ,
5 email VARCHAR (100) UNIQUE NOT NULL ,
6 phone VARCHAR (15) NOT NULL ,
7 vehicle_number VARCHAR (20) NOT NULL ,
8 CONSTRAINT chk_valid_email CHECK (email LIKE '%@%.% '),
9 CONSTRAINT chk_phone_length CHECK ( LENGTH (phone) BETWEEN 10 AND
15)
10 );
11

12 -- Create Parking Lots Table


13 CREATE TABLE Parking_Lots (
14 lot_id INT AUTO_INCREMENT PRIMARY KEY ,
15 lot_name VARCHAR (100) NOT NULL ,
16 location VARCHAR (255) NOT NULL ,
17 total_slots INT NOT NULL CHECK ( total_slots > 0),
18 CONSTRAINT unq_lot_location UNIQUE (lot_name , location )
19 );
20

21 -- Create Parking Slots Table


22 CREATE TABLE Parking_Slots (
23 slot_id INT AUTO_INCREMENT PRIMARY KEY ,
24 lot_id INT NOT NULL ,
25 slot_number INT NOT NULL ,
26 is_available BOOLEAN DEFAULT TRUE ,
27 FOREIGN KEY ( lot_id ) REFERENCES Parking_Lots ( lot_id ) ON DELETE
CASCADE ,
28 CONSTRAINT unq_slot_per_lot UNIQUE (lot_id , slot_number )
29 );
30

31 -- Create Bookings Table


32 CREATE TABLE Bookings (

9
33 booking_id INT AUTO_INCREMENT PRIMARY KEY ,
34 user_id INT NOT NULL ,
35 slot_id INT NOT NULL ,
36 booking_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ,
37 release_time DATETIME NULL ,
38 FOREIGN KEY ( user_id ) REFERENCES Users( user_id ) ON DELETE
CASCADE ,
39 FOREIGN KEY ( slot_id ) REFERENCES Parking_Slots ( slot_id ) ON
DELETE CASCADE ,
40 CONSTRAINT chk_valid_booking_time CHECK (
41 release_time IS NULL OR release_time >= booking_time
42 )
43 );
44

45 -- Create Payments Table


46 CREATE TABLE Payments (
47 payment_id INT AUTO_INCREMENT PRIMARY KEY ,
48 booking_id INT NOT NULL ,
49 amount DECIMAL (10 ,2) NOT NULL CHECK ( amount > 0),
50 payment_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ,
51 payment_status ENUM('pending ', 'completed ', 'failed ') DEFAULT
'pending ',
52 FOREIGN KEY ( booking_id ) REFERENCES Bookings ( booking_id ) ON
DELETE CASCADE
53 );

Inserting Sample Data


To test the database, sample data is inserted into the tables. Note that some inser-
tions (e.g., duplicate parking lots) may violate the unq_lot_location constraint. For this
implementation, we insert unique parking lots and adjust accordingly.
1 -- Insert Parking Lots
2 INSERT INTO Parking_Lots (lot_name , location , total_slots )
3 VALUES
4 ('Lot A', 'Downtown Area ', 50) ,
5 ('Lot B', 'Airport Road ', 80) ,
6 ('Lot C', 'Mall Basement ', 100);
7

8 -- Insert Parking Slots


9 INSERT INTO Parking_Slots (lot_id , slot_number , is_available )
10 VALUES
11 (1, 101, TRUE),
12 (1, 102, TRUE),
13 (1, 103, TRUE),
14 (2, 101, TRUE),
15 (2, 102, TRUE);
16

17 -- Insert Users
18 INSERT INTO Users (name , email , phone , vehicle_number )
19 VALUES

10
20 ('John Doe ', 'john@example .com ', '9876543210 ', 'DL01AB1234 '),
21 ('Alice Smith ', 'alice@example .com ', '9876543211 ',
'MH12XY4567 '),
22 ('Rahul Verma ', 'rahul@example .com ', '9123456789 ',
'KA01CD7890 ');
23

24 -- Booking Operations with Payments


25 -- Booking 1 with Payment
26 INSERT INTO Bookings (user_id , slot_id ) VALUES (1, 1);
27 UPDATE Parking_Slots SET is_available = FALSE WHERE slot_id = 1;
28 INSERT INTO Payments ( booking_id , amount , payment_status )
29 VALUES (1, 50.00 , 'completed ');
30

31 -- Booking 2 with Payment


32 INSERT INTO Bookings (user_id , slot_id ) VALUES (2, 2);
33 UPDATE Parking_Slots SET is_available = FALSE WHERE slot_id = 2;
34 INSERT INTO Payments ( booking_id , amount , payment_status )
35 VALUES (2, 50.00 , 'completed ');

Sample Queries for Functionality Demonstration


The following SQL queries demonstrate the functionality of the Smart Parking System,
covering user interactions, slot management, payment tracking, and operational analytics.
Placeholder spaces are provided for embedding the output screenshots of each query.

Query 1: Find Available Parking Slots in a Specific Lot


This query retrieves all available parking slots in a given parking lot (e.g., lot_id = 1).
1 SELECT ps.slot_id , ps. slot_number , pl.lot_name , pl. location
2 FROM Parking_Slots ps
3 JOIN Parking_Lots pl ON ps. lot_id = pl. lot_id
4 WHERE ps. is_available = TRUE AND ps. lot_id = 1;

Figure : Output of Query 1: Available Parking Slots in Lot 1

11
Query 2: View a User’s Booking History
This query shows all bookings made by a specific user (e.g., user_id = 1), including
parking slot and lot details.
1 SELECT b. booking_id , b. booking_time , b. release_time ,
2 ps. slot_number , pl.lot_name , pl. location
3 FROM Bookings b
4 JOIN Parking_Slots ps ON b. slot_id = ps. slot_id
5 JOIN Parking_Lots pl ON ps. lot_id = pl. lot_id
6 WHERE b. user_id = 1
7 ORDER BY b. booking_time DESC;

Figure : Output of Query 2: Booking History for User 1

Query 3: Check Payment Status for a Booking


This query retrieves the payment details for a specific booking (e.g., booking_id = 1).

1 SELECT p. payment_id , p.amount , p. payment_time , p. payment_status ,


2 b. booking_time , u.name , u.email
3 FROM Payments p
4 JOIN Bookings b ON p. booking_id = b. booking_id
5 JOIN Users u ON b. user_id = u. user_id
6 WHERE p. booking_id = 1;

Figure : Output of Query 3: Payment Status for Booking 1

12
Query 4: Release a Parking Slot and Update Availability
This query simulates a user releasing a parking slot (e.g., slot_id = 1) by updating the
booking’s release time and setting the slot as available again.

1 UPDATE Bookings
2 SET release_time = NOW ()
3 WHERE slot_id = 1 AND release_time IS NULL;
4

5 UPDATE Parking_Slots
6 SET is_available = TRUE
7 WHERE slot_id = 1;

Figure : Output of Query 4: Updated Booking and Slot Availability for Slot 1

Query 5: Calculate Total Revenue from Completed Payments


This query calculates the total revenue from all completed payments.

1 SELECT SUM( amount ) AS total_revenue


2 FROM Payments
3 WHERE payment_status = 'completed ';

13
Figure : Output of Query 5: Total Revenue from Completed Payments

Query 6: Find the Most Booked Parking Lot


This query identifies the parking lot with the most bookings.
1 SELECT pl.lot_id , pl.lot_name , pl.location , COUNT (b. booking_id ) AS
booking_count
2 FROM Parking_Lots pl
3 LEFT JOIN Parking_Slots ps ON pl. lot_id = ps. lot_id
4 LEFT JOIN Bookings b ON ps. slot_id = b. slot_id
5 GROUP BY pl.lot_id , pl.lot_name , pl. location
6 ORDER BY booking_count DESC
7 LIMIT 1;

Figure : Output of Query 6: Most Booked Parking Lot

Query 7: Check for Overdue Bookings


This query identifies bookings that have been active for more than 24 hours without a
release time, indicating potential overdue bookings.

14
1 SELECT b. booking_id , b. booking_time , u.name , u.email ,
2 ps. slot_number , pl. lot_name
3 FROM Bookings b
4 JOIN Users u ON b. user_id = u. user_id
5 JOIN Parking_Slots ps ON b. slot_id = ps. slot_id
6 JOIN Parking_Lots pl ON ps. lot_id = pl. lot_id
7 WHERE b. release_time IS NULL
8 AND b. booking_time < DATE_SUB (NOW (), INTERVAL 24 HOUR);

Figure : Output of Query 7: Overdue Bookings (Active for Over 24 Hours)

15
Database Implementation Overview
The Smart Parking System database was implemented in the previous section by creating
the necessary tables and inserting sample data. The tables include:

• Users: Stores user and vehicle details.

• Parking_Lots: Details of each parking location.

• Parking_Slots: Slot-level information linked to parking lots.

• Bookings: Records each reservation with timestamps.

• Payments: Stores transaction data linked to bookings.

During implementation, several constraints were enforced to maintain data integrity:

• Primary Keys and AUTO_INCREMENT ensure unique identifiers for each record.

• Foreign Keys with ON DELETE CASCADE maintain referential integrity, auto-


matically deleting dependent records when a parent record is removed (e.g., deleting
a user removes their bookings).

• CHECK constraints enforce business rules, such as valid email formats, phone num-
ber lengths, and positive payment amounts.

• UNIQUE constraints prevent duplicate entries, such as duplicate email addresses


or parking lot locations.

The sample data inserted includes users, parking lots, parking slots, bookings, and
payments, simulating real-world usage scenarios. This setup allows us to test the database’s
functionality and performance under various conditions.

Indexing for Performance Optimization


Indexing is a critical step in optimizing database performance, especially for frequently
executed queries. Indexes improve the speed of data retrieval operations by allowing the
database engine to locate rows more efficiently. However, they come with trade-offs, such
as increased storage requirements and slower write operations (e.g., INSERT, UPDATE,
DELETE).

16
Identifying Columns for Indexing
Based on the sample queries from the previous section, the following columns are fre-
quently used in WHERE clauses, JOIN conditions, and ORDER BY clauses, making
them candidates for indexing:

• Parking_Slots.lot_id: Frequently used in JOINs with Parking_Lots and filtered


in queries to find available slots in a specific lot.

• Parking_Slots.is_available: Often used in WHERE clauses to filter available


slots.

• Bookings.user_id: Used in queries to retrieve a user’s booking history.

• Bookings.slot_id: Used in JOINs with Parking_Slots to retrieve booking details.

• Payments.booking_id: Used in JOINs with Bookings to check payment status.

• Bookings.booking_time: Used in ORDER BY clauses and for filtering overdue


bookings.

Foreign key columns (e.g., lot_id, user_id, slot_id, booking_id) are particularly
important to index, as they are often used in JOIN operations. Additionally, columns used
in filtering (e.g., is_available) and sorting (e.g., booking_time) benefit from indexing.

Creating Indexes
The following SQL statements create indexes on the identified columns:
1 -- Index on Parking_Slots . lot_id for faster JOINs and filtering
2 CREATE INDEX idx_parking_slots_lot_id ON Parking_Slots ( lot_id );
3

4 -- Index on Parking_Slots . is_available for faster filtering of


available slots
5 CREATE INDEX idx_parking_slots_is_available ON Parking_Slots
( is_available );
6

7 -- Index on Bookings . user_id for faster retrieval of user - specific


bookings
8 CREATE INDEX idx_bookings_user_id ON Bookings ( user_id );
9

10 -- Index on Bookings . slot_id for faster JOINs with Parking_Slots


11 CREATE INDEX idx_bookings_slot_id ON Bookings ( slot_id );
12

13 -- Index on Payments . booking_id for faster JOINs with Bookings


14 CREATE INDEX idx_payments_booking_id ON Payments ( booking_id );
15

16 -- Index on Bookings . booking_time for faster sorting and filtering


17 CREATE INDEX idx_bookings_booking_time ON Bookings ( booking_time );

These indexes are named with the prefix idx_ to indicate they are indexes, followed
by the table and column names for clarity.

17
Impact of Indexing
To demonstrate the impact of indexing, we can compare the performance of a query
before and after adding the indexes. Consider the query to find available parking slots
in a specific lot (lot_id = 1):
1 SELECT ps.slot_id , ps. slot_number , pl.lot_name , pl. location
2 FROM Parking_Slots ps
3 JOIN Parking_Lots pl ON ps. lot_id = pl. lot_id
4 WHERE ps. is_available = TRUE AND ps. lot_id = 1;

Before Indexing: Without indexes on lot_id and is_available, the database per-
forms a full table scan on Parking_Slots, which can be slow for large datasets.
After Indexing: With idx_parking_slots_lot_id and idx_parking_slots_is_available,
the database can quickly locate the relevant rows using the indexes, significantly reducing
query execution time.
Below is a placeholder space for embedding the query execution plan or performance
metrics (e.g., from MySQL’s EXPLAIN statement) before indexing:

Figure a: Query Execution Plan Before Indexing (Find Available Slots in Lot 1)

Figure b: Query Execution Plan After Indexing (Find Available Slots in Lot 1)

18
The execution plans ( in Figures a and b) should show a reduction in the number of
rows scanned and the use of the indexes after they are created.

Trade-Offs o f Indexing
While indexes improve read performance, they have some drawbacks:
• Increased Storage: Indexes require additional disk space, proportional to the size
of the indexed columns and the number of rows.
• Slower Write Operations: INSERT, UPDATE, and DELETE operations are
slower because the indexes must be updated whenever the indexed columns are
modified.
• Maintenance Overhead: Indexes need to be maintained during database operations,
which can add overhead, especially for tables with frequent updates.
For the Smart Parking System, the trade-offs are acceptable because:
• Read operations (e.g., finding available slots, viewing booking history) are more
frequent than write operations.
• The dataset size is manageable, so the additional storage and maintenance overhead
of indexes are minimal.

Sample Queries to Test Indexing


The following queries leverage the created indexes to demonstrate performance improve-
ments. These queries are variations of those from the previous section, optimized to take
advantage of the indexes.

Query 1: Find Available Slots with Index


This query uses the indexes on lot_id and is_available to quickly find available slots in
Lot 1.
1 SELECT ps.slot_id , ps. slot_number , pl.lot_name , pl. location
2 FROM Parking_Slots ps
3 JOIN Parking_Lots pl ON ps. lot_id = pl. lot_id
4 WHERE ps. is_available = TRUE AND ps. lot_id = 1;

The output should show the same results as before but with improved performance
due to the indexes.

Figure : Output of Indexed Query 1: Available Slots in Lot 1

19
Query 2: View User Booking History with Index
This query uses the index on user_id to retrieve the booking history for user_id = 1.

1 SELECT b. booking_id , b. booking_time , b. release_time ,


2 ps. slot_number , pl.lot_name , pl. location
3 FROM Bookings b
4 JOIN Parking_Slots ps ON b. slot_id = ps. slot_id
5 JOIN Parking_Lots pl ON ps. lot_id = pl. lot_id
6 WHERE b. user_id = 1
7 ORDER BY b. booking_time DESC;

Figure : Output of Indexed Query 2: Booking History for User 1

The output benefits from the indexes on user_id, slot_id, and booking_time,
resulting in faster filtering and sorting.

Query 3: Check for Overdue Bookings with Index


This query uses the index on booking_time to efficiently identify overdue bookings.

1 SELECT b. booking_id , b. booking_time , u.name , u.email ,


2 ps. slot_number , pl. lot_name
3 FROM Bookings b
4 JOIN Users u ON b. user_id = u. user_id
5 JOIN Parking_Slots ps ON b. slot_id = ps. slot_id
6 JOIN Parking_Lots pl ON ps. lot_id = pl. lot_id
7 WHERE b. release_time IS NULL
8 AND b. booking_time < DATE_SUB (NOW (), INTERVAL 24 HOUR);

The output shows overdue bookings, with the index on booking_time


improving the performance of the date comparison.

Figure : Output of Indexed Query 3: Overdue Bookings

20
*

Testing & Performance Evaluation


This section focuses on testing the Smart Parking System database to ensure it meets
functional requirements and performs efficiently under various conditions. The testing
process includes both functional and performance evaluations, with results analyzed to
identify areas for improvement.

Functional Testing
Functional testing verifies that the database implementation meets the specified require-
ments. The following tests were conducted:

• Data Insertion and Constraints:

– Test the insertion of data into all tables (Users, Parking_Lots, Parking_Slots,
Bookings, Payments).
– Verify that constraints are enforced:
∗ UNIQUE constraint on Users.email prevents duplicate email addresses.
∗ CHECK constraint on Payments.amount ensures amounts are positive.
∗ FOREIGN KEY constraints with ON DELETE CASCADE correctly
delete dependent records.

• Query Accuracy:

– Test the sample queries from the previous section (e.g., finding available slots,
viewing booking history, checking payment status).
– Ensure the query results match expected outputs based on the inserted sample
data.

• Booking Workflow:

– Simulate a complete booking workflow: insert a booking, update slot avail-


ability, record a payment, and release the slot.
– Verify that all tables are updated correctly and constraints are maintained.

The following SQL statements test the UNIQUE constraint on Users.email:


1 -- Attempt to insert a duplicate email ( should fail)
2 INSERT INTO Users (name , email , phone , vehicle_number )
3 VALUES ('Test User ', 'john@example .com ', '9876543212 ',
'DL01XY5678 ');

The output shows an error message indicating a violation of the UNIQUE


constraint on the email column.
All functional tests passed, confirming t hat t he d atabase c orrectly h andles d ata in-
sertion, enforces constraints, and returns accurate query results.

21
Figure : Error Output: Attempting to Insert Duplicate Email in Users Table

Performance Testing
Performance testing evaluates the database’s efficiency, focusing on query execution times
and scalability. The tests were conducted with the sample data and indexes applied in
the previous section.

Query Execution Time Analysis


The following query, which finds available parking slots in a specific lot, was tested with
and without a larger dataset:
1 SELECT ps.slot_id , ps. slot_number , pl.lot_name , pl. location
2 FROM Parking_Slots ps
3 JOIN Parking_Lots pl ON ps. lot_id = pl. lot_id
4 WHERE ps. is_available = TRUE AND ps. lot_id = 1;

To simulate a larger dataset, additional parking slots were inserted:


1 -- Insert additional parking slots for performance testing
2 INSERT INTO Parking_Slots (lot_id , slot_number , is_available )
3 VALUES
4 (1, 104 , TRUE),
5 (1, 105 , TRUE),
6 (1, 106 , FALSE );

The query execution time was measured using MySQL’s profiling t ools ( e.g., EX-
PLAIN or benchmarking). Below is a placeholder space for embedding the performance
metrics:
The metrics should show the execution time, which is expected to be low due to
the indexes on lot_id and is_available.

Figure : Performance Metrics: Execution Time of Query to Find Available Slots

22
Scalability Testing
To test scalability, the dataset was expanded by inserting more records into the Users, Bookings, and
Payments tables:

1 -- Insert additional users


2 INSERT INTO Users (name , email , phone , vehicle_number )
3 VALUES
4 ('Sarah Johnson ', 'sarah@example .com ', '9876543213 ',
'DL01PQ9012 '),
5 ('Mike Brown ', 'mike@example .com ', '9876543214 ', 'MH12RS3456 ');
6

7 -- Insert additional bookings and payments in the correct order


8 -- First booking ( should create booking_id = 3)
9 INSERT INTO Bookings (user_id , slot_id ) VALUES (4, 4);
10 UPDATE Parking_Slots SET is_available = FALSE WHERE slot_id = 4;
11 INSERT INTO Payments ( booking_id , amount , payment_status ) VALUES
(3, 75.00 , 'completed ');
12

13 -- Second booking ( should create booking_id = 4)


14 INSERT INTO Bookings (user_id , slot_id ) VALUES (5, 5);
15 UPDATE Parking_Slots SET is_available = FALSE WHERE slot_id = 5;
16 INSERT INTO Payments ( booking_id , amount , payment_status ) VALUES
(4, 60.00 , 'pending ');

The query to view a user’s booking history was then executed with the expanded
dataset:
1 SELECT b. booking_id , b. booking_time , b. release_time ,
2 ps. slot_number , pl.lot_name , pl. location
3 FROM Bookings b
4 JOIN Parking_Slots ps ON b. slot_id = ps. slot_id
5 JOIN Parking_Lots pl ON ps. lot_id = pl. lot_id
6 WHERE b. user_id = 1
7 ORDER BY b. booking_time DESC;

The results should show that the query remains efficient due to the indexes, even
with a larger dataset. However, further optimization (e.g., partitioning large tables)
may be needed for production-scale data.

Figure 3: Scalability Test: Performance of Booking History Query with Expanded Dataset

23
Results and Optimizations
The functional tests confirmed that the database operates as expected, with all con-
straints enforced and queries returning accurate results. Performance tests showed that
the indexes significantly reduced query execution times, especially for read-heavy oper-
ations. Scalability tests indicated that the system handles moderate data growth well,
but future improvements could include:

• Partitioning: For very large datasets, partitioning the Bookings table by date
range could improve performance.

• Caching: Implementing a caching layer for frequently accessed data (e.g., available
slots) could reduce database load.

• Query Optimization: Analyze and rewrite complex queries to minimize resource


usage.

24
References
The following resources were consulted during the development and documentation of
the Smart Parking System:

Books

• Silberschatz, A., Korth, H. F., & Sudarshan, S. (2010). Database System Concepts
(6th ed.). McGraw-Hill Education.
This textbook provides a foundational understanding of database management
concepts, including schema design, normalization, and query optimization, which
guided the development of the Smart Parking System.
• Elmasri, R., & Navathe, S. B. (2015). Fundamentals of Database Systems (7th ed.).
Pearson.
This book offers a comprehensive introduction to database design and implemen-
tation, emphasizing practical aspects that were applied in this project.

Websites

• Codecademy. (n.d.). What is an RDBMS and SQL?


• TechTarget. (n.d.). RDBMS (Relational Database Management System).
• W3Schools. (n.d.). MySQL RDBMS.

• TutorialsPoint. (n.d.). MySQL Tutorial.

25

You might also like