MODULO SQL & Database Design Master SeriesLAB

Designing a Scalable Ride-Sharing Database

Designing a Scalable Ride-Sharing Database

=====================================================

In this masterclass, we will delve into the world of database design for a ride-sharing application. We will explore the key components, relationships, and scalability considerations for such a database.

Entity Relationship Diagram (ERD)


Before we dive into the database design, let's create an Entity Relationship Diagram (ERD) to visualize the key entities and their relationships.

  • Riders: Individuals who use the ride-sharing service.
  • Drivers: Individuals who provide transportation services.
  • Vehicles: Cars, SUVs, or other vehicles used for transportation.
  • Trips: A single ride from a pickup location to a drop-off location.
  • Payments: Transactions related to each trip.

ERD Relationships

  • A rider can take multiple trips (one-to-many).
  • A driver can provide multiple trips (one-to-many).
  • A vehicle can be used for multiple trips (one-to-many).
  • A trip is associated with one rider, one driver, and one vehicle (many-to-one).
  • A payment is associated with one trip (many-to-one).

Database Schema


Now that we have our ERD, let's create a database schema to implement the design. We will use a relational database management system (RDBMS) like MySQL or PostgreSQL.

Riders Table

CREATE TABLE Riders (
    RiderID INT PRIMARY KEY,
    Name VARCHAR(255) NOT NULL,
    Email VARCHAR(255) UNIQUE NOT NULL,
    Phone VARCHAR(20) NOT NULL,
    PasswordHash VARCHAR(255) NOT NULL,
    CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UpdatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

Drivers Table

CREATE TABLE Drivers (
    DriverID INT PRIMARY KEY,
    Name VARCHAR(255) NOT NULL,
    Email VARCHAR(255) UNIQUE NOT NULL,
    Phone VARCHAR(20) NOT NULL,
    PasswordHash VARCHAR(255) NOT NULL,
    LicenseNumber VARCHAR(20) NOT NULL,
    CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UpdatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

Vehicles Table

CREATE TABLE Vehicles (
    VehicleID INT PRIMARY KEY,
    Make VARCHAR(50) NOT NULL,
    Model VARCHAR(50) NOT NULL,
    Year INT NOT NULL,
    LicensePlate VARCHAR(20) NOT NULL,
    CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UpdatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

Trips Table

CREATE TABLE Trips (
    TripID INT PRIMARY KEY,
    RiderID INT NOT NULL,
    DriverID INT NOT NULL,
    VehicleID INT NOT NULL,
    PickupLocation VARCHAR(255) NOT NULL,
    DropOffLocation VARCHAR(255) NOT NULL,
    TripDistance DECIMAL(10, 2) NOT NULL,
    TripTime INT NOT NULL,
    Status VARCHAR(20) NOT NULL CHECK (Status IN ('pending', 'in_progress', 'completed', 'cancelled')),
    CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UpdatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (RiderID) REFERENCES Riders(RiderID),
    FOREIGN KEY (DriverID) REFERENCES Drivers(DriverID),
    FOREIGN KEY (VehicleID) REFERENCES Vehicles(VehicleID)
);

Payments Table

CREATE TABLE Payments (
    PaymentID INT PRIMARY KEY,
    TripID INT NOT NULL,
    PaymentMethod VARCHAR(20) NOT NULL CHECK (PaymentMethod IN ('credit_card', 'paypal', 'cash')),
    PaymentAmount DECIMAL(10, 2) NOT NULL,
    PaymentStatus VARCHAR(20) NOT NULL CHECK (PaymentStatus IN ('pending', 'processed', 'failed')),
    CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UpdatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (TripID) REFERENCES Trips(TripID)
);

Indexing and Constraints


To improve query performance and data integrity, we need to add indexes and constraints to our tables.

Indexes

  • Create an index on the RiderID column in the Trips table to speed up queries by rider.
  • Create an index on the DriverID column in the Trips table to speed up queries by driver.
  • Create an index on the VehicleID column in the Trips table to speed up queries by vehicle.
CREATE INDEX idx_Trips_RiderID ON Trips (RiderID);
CREATE INDEX idx_Trips_DriverID ON Trips (DriverID);
CREATE INDEX idx_Trips_VehicleID ON Trips (VehicleID);

Constraints

  • Add a unique constraint on the Email column in the Riders table to prevent duplicate email addresses.
  • Add a unique constraint on the Email column in the Drivers table to prevent duplicate email addresses.
  • Add a unique constraint on the LicensePlate column in the Vehicles table to prevent duplicate license plates.
ALTER TABLE Riders ADD CONSTRAINT uc_Riders_Email UNIQUE (Email);
ALTER TABLE Drivers ADD CONSTRAINT uc_Drivers_Email UNIQUE (Email);
ALTER TABLE Vehicles ADD CONSTRAINT uc_Vehicles_LicensePlate UNIQUE (LicensePlate);

Scalability Considerations


As the ride-sharing service grows, the database will need to handle increasing amounts of data and traffic. Here are some scalability considerations:

  • Sharding: Split the database into smaller, independent pieces called shards, each containing a portion of the data. This can help distribute the load and improve performance.
  • Replication: Create multiple copies of the database to ensure high availability and redundancy. This can help prevent data loss and improve performance.
  • Caching: Store frequently accessed data in memory to reduce the load on the database and improve performance.
  • Load Balancing: Distribute incoming traffic across multiple servers to improve performance and availability.

Query Examples


Here are some example queries to demonstrate how to interact with the database:

Get all trips for a rider

SELECT * FROM Trips WHERE RiderID = 1;

Get all drivers with a specific license number

SELECT * FROM Drivers WHERE LicenseNumber = 'ABC123';

Get all vehicles with a specific make and model

SELECT * FROM Vehicles WHERE Make = 'Toyota' AND Model = 'Camry';

Get all payments for a trip

SELECT * FROM Payments WHERE TripID = 1;

⚔️ YOUR MISSION

Design a ride-sharing database to support a new ride-sharing service in a large city. The database should be able to handle a high volume of requests and provide efficient querying capabilities. Implement the database schema and populate it with sample data.

SQL practice editor
Read the exercise prompt
-- Create the database schema
CREATE TABLE Riders (
    RiderID INT PRIMARY KEY,
    Name VARCHAR(255) NOT NULL,
    Email VARCHAR(255) UNIQUE NOT NULL,
    Phone VARCHAR(20) NOT NULL,
    PasswordHash VARCHAR(255) NOT NULL,
    CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UpdatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

CREATE TABLE Drivers (
    DriverID INT PRIMARY KEY,
    Name VARCHAR(255) NOT NULL,
    Email VARCHAR(255) UNIQUE NOT NULL,
    Phone VARCHAR(20) NOT NULL,
    PasswordHash VARCHAR(255) NOT NULL,
    LicenseNumber VARCHAR(20) NOT NULL,
    CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UpdatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

CREATE TABLE Vehicles (
    VehicleID INT PRIMARY KEY,
    Make VARCHAR(50) NOT NULL,
    Model VARCHAR(50) NOT NULL,
    Year INT NOT NULL,
    LicensePlate VARCHAR(20) NOT NULL,
    CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UpdatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

CREATE TABLE Trips (
    TripID INT PRIMARY KEY,
    RiderID INT NOT NULL,
    DriverID INT NOT NULL,
    VehicleID INT NOT NULL,
    PickupLocation VARCHAR(255) NOT NULL,
    DropOffLocation VARCHAR(255) NOT NULL,
    TripDistance DECIMAL(10, 2) NOT NULL,
    TripTime INT NOT NULL,
    Status VARCHAR(20) NOT NULL CHECK (Status IN ('pending', 'in_progress', 'completed', 'cancelled')),
    CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UpdatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (RiderID) REFERENCES Riders(RiderID),
    FOREIGN KEY (DriverID) REFERENCES Drivers(DriverID),
    FOREIGN KEY (VehicleID) REFERENCES Vehicles(VehicleID)
);

CREATE TABLE Payments (
    PaymentID INT PRIMARY KEY,
    TripID INT NOT NULL,
    PaymentMethod VARCHAR(20) NOT NULL CHECK (PaymentMethod IN ('credit_card', 'paypal', 'cash')),
    PaymentAmount DECIMAL(10, 2) NOT NULL,
    PaymentStatus VARCHAR(20) NOT NULL CHECK (PaymentStatus IN ('pending', 'processed', 'failed')),
    CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UpdatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (TripID) REFERENCES Trips(TripID)
);

-- Populate the database with sample data
INSERT INTO Riders (RiderID, Name, Email, Phone, PasswordHash)
VALUES
(1, 'John Doe', 'john.doe@example.com', '123-456-7890', 'password123'),
(2, 'Jane Doe', 'jane.doe@example.com', '987-654-3210', 'password123');

INSERT INTO Drivers (DriverID, Name, Email, Phone, PasswordHash, LicenseNumber)
VALUES
(1, 'Bob Smith', 'bob.smith@example.com', '555-123-4567', 'password123', 'ABC123'),
(2, 'Alice Johnson', 'alice.johnson@example.com', '555-901-2345', 'password123', 'DEF456');

INSERT INTO Vehicles (VehicleID, Make, Model, Year, LicensePlate)
VALUES
(1, 'Toyota', 'Camry', 2015, 'ABC123'),
(2, 'Honda', 'Civic', 2018, 'DEF456');

INSERT INTO Trips (TripID, RiderID, DriverID, VehicleID, PickupLocation, DropOffLocation, TripDistance, TripTime, Status)
VALUES
(1, 1, 1, 1, '123 Main St', '456 Elm St', 10.5, 30, 'completed'),
(2, 2, 2, 2, '789 Oak St', '321 Maple St', 15.2, 45, 'completed');

INSERT INTO Payments (PaymentID, TripID, PaymentMethod, PaymentAmount, PaymentStatus)
VALUES
(1, 1, 'credit_card', 15.00, 'processed'),
(2, 2, 'paypal', 20.00, 'processed');
WAITING FOR SIGNAL...
BACKFINISH MISSION