0% found this document useful (0 votes)
5 views5 pages

SQL Train Booking System Schema

The document outlines the SQL schema for a train reservation system, including the creation of tables for Train, Customer, Station, and Booked_seat, each with specific constraints and relationships. It also includes a sequence for generating PNR numbers and examples of inserting records into the Booked_seat table. Additionally, there are procedures for calculating and updating ticket prices for customers.

Uploaded by

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

SQL Train Booking System Schema

The document outlines the SQL schema for a train reservation system, including the creation of tables for Train, Customer, Station, and Booked_seat, each with specific constraints and relationships. It also includes a sequence for generating PNR numbers and examples of inserting records into the Booked_seat table. Additionally, there are procedures for calculating and updating ticket prices for customers.

Uploaded by

itsshweta2812
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

CREATE TABLE TRAIN(

Train_code number(5) primary key check(length(train_code)=5),

Train_name varchar2(50) unique not null,

source_code varchar2(4) check(length(source_code)<=4),

destination_code varchar2(4) check(length(destination_code)<=4),

source_name varchar2(50) not null,

destination_name varchar2(50) not null,

train_type varchar2(10) check (train_type in('superfast','express','passenger')),

source_start_time date not null,

destination_end_time date not null ,

check( source_start_time<destination_end_time)

);

CREATE TABLE Customer

(
Customer_code NUMBER PRIMARY KEY,

Customer_name VARCHAR(100) NOT NULL,

Customer_age NUMBER(3) CHECK(Customer_age>0 and Customer_age<=120) NOT NULL,

Customer_mob NUMBER(10) NOT NULL CHECK(LENGTH(Customer_mob) >=10),

Customer_email VARCHAR(255) CHECK (Customer_email LIKE '%@%'),

Customer_gender CHAR(1) CHECK(Customer_gender IN ('M','F','O'))

);

CREATE TABLE Station (

Station_Code VARCHAR2(10) PRIMARY KEY,

Station_name VARCHAR(50) NOT NULL,

Train_Code NUMBER(5),

Platform_No NUMBER,

Train_Name VARCHAR2(50),

Train_Arrival DATE,

Train_Departure DATE,
Schedule_arrival DATE,

Train_delay DATE,

CONSTRAINT FK_Station_Train_Code FOREIGN KEY (Train_Code) REFERENCES Train (Train_Code),

--CONSTRAINT FK_Station_Train_Name FOREIGN KEY (Train_Name) REFERENCES Train (Train_Name),

CONSTRAINT CHK_Arrival_Before_Departure CHECK (Train_Arrival < Train_Departure),

CONSTRAINT CHK_Platform_Positive CHECK (Platform_No > 0),

CONSTRAINT CHK_Arrival_Not_Null CHECK (Train_Arrival IS NOT NULL),

CONSTRAINT CHK_Departure_Not_Null CHECK (Train_Departure IS NOT NULL)

);

CREATE TABLE Booked_seat(

Train_code number(5),

Pnr_no NUMBER,

Customer_code number,

Station_code varchar2(10) ,

Seat_no NUMBER NOT NULL check(seat_no between 1 and 140),

Ticket_price FLOAT NOT NULL CHECK (ticket_price>0),

Travel_date DATE NOT NULL,

CONSTRAINT PK_Booked_Seat PRIMARY KEY (Pnr_no),


CONSTRAINT FK_Booked_Seat_Train FOREIGN KEY (Train_code) REFERENCES Train (Train_code),

CONSTRAINT FK_Booked_seat_Customer FOREIGN KEY (Customer_code) REFERENCES Customer


(Customer_code),

CONSTRAINT FK_Booked_seat_Station FOREIGN KEY (Station_code) REFERENCES Station (Station_code)

);

create sequence Pnr_no_sequence

start with 10000

increment by 1

nocache

nocycle;

-- Inserting 5 records into the Booked_seat table using the Pnr_no_sequence

INSERT INTO Booked_seat (Train_code, Pnr_no, Customer_code, Station_code, Seat_no, Ticket_price,


Travel_date)

VALUES

(10001, Pnr_no_sequence.NEXTVAL, 101, 'STA001', 1, 25.50, TO_DATE('2024-02-01', 'YYYY-MM-DD')),

(10002, Pnr_no_sequence.NEXTVAL, 102, 'STA002', 2, 30.00, TO_DATE('2024-02-05', 'YYYY-MM-DD')),

(10003, Pnr_no_sequence.NEXTVAL, 103, 'STA003', 3, 45.75, TO_DATE('2024-02-10', 'YYYY-MM-DD')),

(10004, Pnr_no_sequence.NEXTVAL, 104, 'STA004', 4, 28.80, TO_DATE('2024-02-15', 'YYYY-MM-DD')),

(10005, Pnr_no_sequence.NEXTVAL, 105, 'STA005', 5, 50.25, TO_DATE('2024-02-20', 'YYYY-MM-DD'));

-- Call the CalculateTicketPrice function to get the ticket price for a specific customer

DECLARE
v_customer_id NUMBER := 123; -- Replace with the actual customer ID

v_ticket_price FLOAT;

BEGIN

v_ticket_price := CalculateTicketPrice(v_customer_id);

DBMS_OUTPUT.PUT_LINE('Ticket Price for Customer ' || v_customer_id || ': $' || v_ticket_price);

END;

-- Call the UpdateTicketPrice procedure to update the ticket price in the Booked_seat table

DECLARE

v_customer_id_to_update NUMBER := 123; -- Replace with the actual customer ID

v_pnr_to_update NUMBER := 456; -- Replace with the actual PNR number

BEGIN

UpdateTicketPrice(v_customer_id_to_update, v_pnr_to_update);

END;

Common questions

Powered by AI

While the schema includes crucial integrity constraints, optimization could involve refining foreign key relationships such as ensuring the referential link between 'Train_Name' in the 'Station' and 'Train' tables. Furthermore, incorporating indexes on frequently searched columns like 'Customer_email' and 'Station_name' can improve query performance. Detailing exact distances or durations for travel in the 'Booked_seat' table, rather than relying solely on train types, could offer more refined control over formula-based ticketing improvements. Streamlining data by ensuring unique constraints on 'Station_name' alongside 'Station_Code' could enhance operational clarity .

Error prevention is ensured through the use of various constraints such as primary keys, foreign keys, and checks. For example, primary keys like 'Train_code' and 'Customer_code' enforce uniqueness, preventing duplicate entries. Foreign keys ensure referential integrity, maintaining valid links between related tables, such as 'Booked_seat' referencing the 'Customer' and 'Train' tables. Data quality is further enforced with constraints on value formats and ranges, like valid date checks between arrival and departure times, unique and non-null fields, and numerically valid age and phone entries . This is vital for maintaining consistent and reliable data over time, reducing errors that could affect downstream processes and decision-making.

The 'Booked_seat' table constraints play a critical role in managing the railway booking functionalities by ensuring valid and unique seat assignments, represented by the constraint that 'Seat_no' be between 1 and 140. The unique PNR numbers are generated through 'Pnr_no_sequence', ensuring each booking is tracked uniquely. Foreign key constraints link each booking to valid customers, trains, and stations, ensuring complete and referentially intact records. The requirement that 'Ticket_price' be positive ensures economically valid transactions. These collectively ensure operational efficiency and data consistency within the booking system .

The 'Booked_seat' table serves as a junction between the 'Train' and 'Customer' tables. It contains foreign keys 'Train_code' and 'Customer_code' from these tables, establishing a relationship that allows recording which customers have booked seats on which trains. The foreign key constraint on 'Train_code' ensures that every booked seat is linked to an existing train, ensuring referential integrity. Similarly, the foreign key constraint on 'Customer_code' guarantees that only customers already present in the 'Customer' table can book seats, which prevents orphan records and maintains data consistency .

The foreign key constraints in the 'Station' table, primarily linking 'Train_Code' back to the 'Train' table, ensure relational integrity by confirming that every train arrival and departure action is associated with a registered train in the database, which substantiates the operational logic of tracking train activities. This not only guarantees that every station record has corresponding train information but also prevents anomalies such as orphan records in the station logs that could distort operational data analysis and reporting .

The constraints on the 'Train' table ensure data integrity by enforcing rules on the column values: The 'Train_code' must be a 5-digit number, ensuring uniformity and uniqueness as it is the primary key. The 'Train_name' must be unique and cannot be null, ensuring that each train has a distinct name. 'Source_code' and 'destination_code' must have a maximum length of 4 characters. 'Source_name' and 'destination_name' cannot be null, ensuring these essential fields are always filled. 'Train_type' must be one of 'superfast', 'express', or 'passenger', limiting entries to predefined types. The 'source_start_time' must be earlier than the 'destination_end_time', preventing logical errors in scheduling .

The 'Pnr_no_sequence' is an auto-incrementing sequence used to generate unique 'Pnr_no' for each entry in the 'Booked_seat' table. This automation ensures that every record has a unique Passenger Name Record (PNR) number without manual input, enhancing functionality by minimizing the risk of duplicate entries and improving data entry efficiency. It establishes a sequential order for ticket bookings, aiding in tracking and managing reservations over time .

The 'CalculateTicketPrice' function, although its specific logic is not detailed in the schema, likely computes the ticket price based on certain parameters related to a 'Customer'. Assuming it takes a 'Customer_code' as input, the function retrieves the related train details and calculates the ticket price possibly based on the distance or train type, which is further stored in the 'Booked_seat' table. Its utility lies in automating and standardizing the price computation, reducing manual errors, and ensuring consistency in pricing across the database .

The 'Station' table constraints ensure that the data related to train arrivals and departures are reliable and operationally relevant. The 'Train_Arrival' and 'Train_Departure' fields must not be null and arrivals must occur before departures, promoting logical time sequences that align with operational realities. The constraint that 'Platform_No' be greater than zero ensures validity as a platform number cannot be zero or negative. Collectively, these constraints ensure that train schedules are accurate and platforms are correctly assigned, facilitating smoother station operations .

The constraints in the 'Customer' table ensure age validity ('Customer_age' must be between 0 and 120), confirm that mobile numbers have at least 10 digits ('Customer_mob'), enforce a valid email format ('Customer_email' must contain '@'), and limit gender to specific values ('M', 'F', 'O'). Removing these constraints could lead to invalid or nonsensical data entries, such as non-existent ages, incorrect or incomplete phone numbers, invalid email formats, and undefined gender categories, all of which would degrade overall data quality and reliability .

You might also like