0% found this document useful (0 votes)
14 views2 pages

Table For All Database

This document contains Python code to connect to a MySQL database and create tables for a railway reservation system including passengers, trains, reservations, tickets, and cancelled tickets and trains.

Uploaded by

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

Table For All Database

This document contains Python code to connect to a MySQL database and create tables for a railway reservation system including passengers, trains, reservations, tickets, and cancelled tickets and trains.

Uploaded by

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

import mysql.

connector

# Connect to MySQL server


connection = mysql.connector.connect(
host="localhost",
user="root",
password="admin"
)

# Create a database
def create_database(cursor):
try:
cursor.execute("CREATE DATABASE IF NOT EXISTS railway_reservation")
print("Database created successfully")
except mysql.connector.Error as err:
print(f"Error: {err}")

# Use the railway_reservation database


def use_database(cursor):
try:
cursor.execute("USE railway_reservation")
print("Using railway_reservation database")
except mysql.connector.Error as err:
print(f"Error: {err}")

# Create tables
def create_tables(cursor):
# Table for passengers
cursor.execute("""
CREATE TABLE IF NOT EXISTS passengers (
passenger_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
age INT,
gender ENUM('Male', 'Female', 'Other'),
phone_number VARCHAR(15)
)
""")

# Table for trains


cursor.execute("""
CREATE TABLE IF NOT EXISTS trains (
train_id INT AUTO_INCREMENT PRIMARY KEY,
train_name VARCHAR(255) NOT NULL,
departure_station VARCHAR(255) NOT NULL,
arrival_station VARCHAR(255) NOT NULL,
departure_time TIME,
arrival_time TIME
)
""")

# Table for reservations


cursor.execute("""
CREATE TABLE IF NOT EXISTS reservations (
reservation_id INT AUTO_INCREMENT PRIMARY KEY,
passenger_id INT,
train_id INT,
seat_number VARCHAR(10),
booking_date DATE,
FOREIGN KEY (passenger_id) REFERENCES passengers(passenger_id),
FOREIGN KEY (train_id) REFERENCES trains(train_id)
)
""")

# Table for tickets


cursor.execute("""
CREATE TABLE IF NOT EXISTS tickets (
ticket_id INT AUTO_INCREMENT PRIMARY KEY,
reservation_id INT,
status ENUM('Confirmed', 'Pending', 'Cancelled'),
FOREIGN KEY (reservation_id) REFERENCES reservations(reservation_id)
)
""")

# Table for cancelled tickets


cursor.execute("""
CREATE TABLE IF NOT EXISTS cancelled_tickets (
cancelled_ticket_id INT AUTO_INCREMENT PRIMARY KEY,
ticket_id INT,
cancellation_reason VARCHAR(255),
FOREIGN KEY (ticket_id) REFERENCES tickets(ticket_id)
)
""")

# Table for cancelled trains


cursor.execute("""
CREATE TABLE IF NOT EXISTS cancelled_trains (
cancelled_train_id INT AUTO_INCREMENT PRIMARY KEY,
train_id INT,
cancellation_reason VARCHAR(255),
FOREIGN KEY (train_id) REFERENCES trains(train_id)
)
""")

print("Tables created successfully")

# Connect to the database


cursor = connection.cursor()

# Create the database


create_database(cursor)

# Use the database


use_database(cursor)

# Create tables
create_tables(cursor)

# Commit changes and close the connection


connection.commit()
connection.close()

You might also like