Question For Practice Oracle (11g) With Answers

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

ITE1003 – DATABASE MANAGEMENT SYSTEMS

ASSESSMENT-01
Registration number-17BIT0308 Name-Kushagra Garg slot-L23+L24
:
Railway Reservation System -(Redesigning IRCTC database)
Train(train Number, name, source, destination,start_time, reach_time, traveltime, distance,
class,
days, type)
Ticket( PNRNo,Transactionid, from_station, To_station, date_of_journey, class
date_of_booking, total_ticket_fare,train number)
Passenger(PNR No, Serial no, Name, Age, Reservation_status)
Train_Route(Train_No, route_no, station_code, name, arrival_time, depart_time, distance,day)
Train_Ticket_fare(Train_No, class, base_fare, reservation_charge, superfast_charge,
other_charge, tatkal_charge, service_tax)

Q1) 1. Create all the tables specified above. Make underlined columns as primary key.(use number,

number(m,n), varchar(n), date, time, timestamp datatypes appropriately) (Low Level)

Insert atleast 5 rows to each table. (Check www.irctc.co.in website for actual data)

1. Use Interactive insertion for inserting rows to the table.


2. Use ADT(varray) for class and days column in Train table.

Input code with output

SQL> CREATE Or REPLACE TYPE class_type AS VARRAY(1) OF VARCHAR2(30);

2 /

Type created.

SQL> CREATE Or REPLACE TYPE day_type AS VARRAY(7) OF VARCHAR2(30);

2 /

SQL> create table Train(

2 train_number number(20) constraint Train_train_number_Pk PRIMARY KEY,name varchar(30),source


varchar(30),destination varchar(30),start_time varchar(7),reach_time varchar(7),traveltime_hours
number(10),distance_km number(10),class class_type,days day_type,type varchar(30));
Table created.

SQL> create table Ticket(

PNRNO number(10) constraint Ticket_PNRNO_Pk PRIMARY KEY,Transactionid varchar(30),from_station


varchar(30),to_station varchar(30),date_of_journey varchar(30),class varchar(30),date_of_booking
varchar(30),total_ticket_fare number(30),train_number number(20) constraint Ticket_train_number_Fk
REFERENCES Train(train_number));

Table created.

SQL> create table Passenger(

PNRNO number(30),serialno number(30),name varchar(30),Age number(3),reservation_status


varchar(10),constraint Passenger_PNRNO_Fk FOREIGN KEY(PNRNO) references
Ticket(PNRNO),constraint Passenger_Pk primary key(PNRNO,serialno));

Table created.

SQL> create table Train_Route(

train_number number(20),route_no number(20),station_code varchar(20),name


varchar(30),arrival_time varchar(10),depart_time varchar(10),distance_km number(10),day number(3),
constraint Train_Route_Pk PRIMARY KEY(train_number,route_no), constraint
Train_Route_train_number FOREIGN KEY(train_number) REFERENCES Train(train_number));

Table created.

create table Train_Ticket_fare(

train_number number(20),class varchar(30),base_fare number(10),reservation_charge


number(10),superfast_charge number(10),other_charge number(10),tatkal_charge
number(10),service_tax number(10),constraint Train_Ticket_fare_Pk primary
key(train_number,class),constraint Train_Ticket_fare_train_number foreign key(train_number)
references Train(train_number));

Table created.
a) Use Interactive insertion for inserting rows to the table.

Input code with output

SQL> insert into Train Values(

2
&train_number,&name,&source,&destination,&start_time,&reach_time,&traveltime_hours,&distance_
km,&class,&days,&type);

Enter value for train_number: 12461

Enter value for name: 'MANDOR EXPRESS'

Enter value for source: 'DELHI'

Enter value for destination: 'JODHPUR JN'

Enter value for start_time: '21:15'

Enter value for reach_time: '8:00'

Enter value for traveltime_hours: 5.33

Enter value for distance_km: 620

Enter value for class: class_type(‘sleeper')

Enter value for days:


day_type('MONDAY','TUESDAY','WEDNESDAY','THURSDAY','FRIDAY','SATURDAY','SUNDAY')

Enter value for type: 'superfast'

old 2:
&train_number,&name,&source,&destination,&start_time,&reach_time,&traveltime_hours,&distance_
km,&class,&days,&type)

new 2: 12461,'MANDOR EXPRESS','DELHI','JODHPUR JN','21:15','8:00',5.33,620,class_type('MANDOR


EXPRESS'),day_type('MONDAY','TUESDAY','WEDNESDAY','THURSDAY','FRIDAY','SATURDAY','SUNDAY'),'s
uperfast')

1 row created.

SQL> insert into Train Values(


2
&train_number,&name,&source,&destination,&start_time,&reach_time,&traveltime_hours,&distance_
km,&class,&days,&type);

Enter value for train_number: 12616

Enter value for name: 'G T EXPRESS'

Enter value for source: 'NEW DELHI'

Enter value for destination: 'CHENNAI CENTRAL'

Enter value for start_time: '18:40'

Enter value for reach_time: '06:20'

Enter value for traveltime_hours: 35.66

Enter value for distance_km: 2181

Enter value for class: class_type('sleeper')

Enter value for days:


day_type('MONDAY','TUESDAY','WEDNESDAY','THURSDAY','FRIDAY','SATURDAY','SUNDAY')

Enter value for type: 'superfast'

old 2:
&train_number,&name,&source,&destination,&start_time,&reach_time,&traveltime_hours,&distance_
km,&class,&days,&type)

new 2: 12616,'G T EXPRESS','NEW DELHI','CHENNAI


CENTRAL','18:40','06:20',35.66,2181,class_type('sleeper'),day_type('MONDAY','TUESDAY','WEDNESDAY',
'THURSDAY','FRIDAY','SATURDAY','SUNDAY'),'superfast')

1 row created.

SQL> insert into Train Values(

2
&train_number,&name,&source,&destination,&start_time,&reach_time,&traveltime_hours,&distance_
km,&class,&days,&type);

Enter value for train_number: 12015


Enter value for name: 'AJMER SHTBDI'

Enter value for source: 'NEW DELHI'

Enter value for destination: 'DAURAI'

Enter value for start_time: '06:05'

Enter value for reach_time: '13:10'

Enter value for traveltime_hours: 4.58

Enter value for distance_km: 450

Enter value for class: class_type('AC CHAIR CAR')

Enter value for days:


day_type('MONDAY','TUESDAY','WEDNESDAY','THURSDAY','FRIDAY','SATURDAY','SUNDAY')

Enter value for type: 'shatabdi'

old 2:
&train_number,&name,&source,&destination,&start_time,&reach_time,&traveltime_hours,&distance_
km,&class,&days,&type)

new 2: 12015,'AJMER SHTBDI','NEW DELHI','DAURAI','06:05','13:10',4.58,450,class_type('AC CHAIR


CAR'),day_type('MONDAY','TUESDAY','WEDNESDAY','THURSDAY','FRIDAY','SATURDAY','SUNDAY'),'shata
bdi')

1 row created.

SQL> insert into Train VALUES(

2
&train_number,&name,&source,&destination,&start_time,&reach_time,&traveltime_hours,&distance_
km,&class,&days,&type);

Enter value for train_number: 12621

Enter value for name: 'TAMIL NADU EXPRESS'

Enter value for source: 'NEW DELHI'

Enter value for destination: 'CHENNAI'

Enter value for start_time: '22:30'


Enter value for reach_time: '07:10'

Enter value for traveltime_hours: 32.66

Enter value for distance_km: 2182

Enter value for class: class_type('SLEEPER')

Enter value for days:


day_type('MONDAY,'TUESDAY','WEDNESDAY','THURSDAY','FRIDAY','SATURDAY','SUNDAY')

Enter value for type: 'superfast'

old 2:
&train_number,&name,&source,&destination,&start_time,&reach_time,&traveltime_hours,&distance_
km,&class,&days,&type)

new 2: 12621,'TAMIL NADU EXPRESS','NEW


DELHI','CHENNAI','22:30','07:10',32.66,2182,class_type('SLEEPER'),day_type('MONDAY,'TUESDAY','WED
NESDAY','THURSDAY','FRIDAY','SATURDAY','SUNDAY'),'superfast')

1 row created.

SQL> insert into Train VALUES(

2
&train_number,&name,&source,&destination,&start_time,&reach_time,&traveltime_hours,&distance_
km,&class,&days,&type);

Enter value for train_number: 12050

Enter value for name: 'GATIMAAN EXPRESS'

Enter value for source: 'H NIZAMUDDIN'

Enter value for destination: 'AGRA CANTT'

Enter value for start_time: '08:10'

Enter value for reach_time: '09:50'

Enter value for traveltime_hours: 1.66

Enter value for distance_km: 403

Enter value for class: class_type('AC CHAIR CAR')

Enter value for days: day_type('MONDAY','TUESDAY','WEDNESDAY','THURSDAY','SATURDAY','SUNDAY')


Enter value for type: 'shatabdi'

old 2:
&train_number,&name,&source,&destination,&start_time,&reach_time,&traveltime_hours,&distance_
km,&class,&days,&type)

new 2: 12050,'GATIMAAN EXPRESS','H NIZAMUDDIN','AGRA


CANTT','08:10','09:50',1.66,403,class_type('AC CHAIR
CAR'),day_type('MONDAY','TUESDAY','WEDNESDAY','THURSDAY','SATURDAY','SUNDAY'),'shatabdi')

1 row created.

SQL> insert into Ticket values(

2
&PNRNO,&Transactionid,&from_station,&to_station,&date_of_journey,&class,&date_of_booking,&tota
l_ticket_fare,&train_number);

Enter value for pnrno: 2334567891

Enter value for transactionid: 'VADE0B248931'

Enter value for from_station: 'NEW DELHI'

Enter value for to_station: 'CHENNAI CENTRAL'

Enter value for date_of_journey: '06/08/2018'

Enter value for class: 'sleeper'

Enter value for date_of_booking: '04/08/2018'

Enter value for total_ticket_fare: 780

Enter value for train_number: 12621

old 2:
&PNRNO,&Transactionid,&from_station,&to_station,&date_of_journey,&class,&date_of_booking,&tota
l_ticket_fare,&train_number)

new 2: 2334567891, 'VADE0B248931','NEW DELHI','CHENNAI


CENTRAL','06/08/2018','sleeper','04/08/2018',780,12621)
1 row created.

SQL> insert into Ticket values(

2
&PNRNO,&Transactionid,&from_station,&to_station,&date_of_journey,&class,&date_of_booking,&tota
l_ticket_fare,&train_number);

Enter value for pnrno: 2334567892

Enter value for transactionid: 'VADE0B248910'

Enter value for from_station: 'NEW DELHI'

Enter value for to_station: 'CHENNAI CENTRAL'

Enter value for date_of_journey: '06/08/2018'

Enter value for class: 'sleeper'

Enter value for date_of_booking: '04/08/2018'

Enter value for total_ticket_fare: 780

Enter value for train_number: 12616

old 2:
&PNRNO,&Transactionid,&from_station,&to_station,&date_of_journey,&class,&date_of_booking,&tota
l_ticket_fare,&train_number)

new 2: 2334567892, 'VADE0B248910','NEW DELHI','CHENNAI


CENTRAL','06/08/2018','sleeper','04/08/2018',780,12616)

1 row created.

SQL> insert into Ticket values(

2
&PNRNO,&Transactionid,&from_station,&to_station,&date_of_journey,&class,&date_of_booking,&tota
l_ticket_fare,&train_number);

Enter value for pnrno: 2334567880

Enter value for transactionid: 'VADE0B248920'

Enter value for from_station: 'H NIZAMUDDIN'


Enter value for to_station: 'AGRA CANTT'

Enter value for date_of_journey: '10/08/2018'

Enter value for class: 'AC CHAIR CAR'

Enter value for date_of_booking: '03/08/2018'

Enter value for total_ticket_fare: 665

Enter value for train_number: 12050

old 2:
&PNRNO,&Transactionid,&from_station,&to_station,&date_of_journey,&class,&date_of_booking,&tota
l_ticket_fare,&train_number)

new 2: 2334567880, 'VADE0B248920','H NIZAMUDDIN','AGRA CANTT','10/08/2018','AC CHAIR


CAR','03/08/2018',665,12050)

1 row created.

SQL> insert into Ticket values(

2
&PNRNO,&Transactionid,&from_station,&to_station,&date_of_journey,&class,&date_of_booking,&tota
l_ticket_fare,&train_number);

Enter value for pnrno: 2334567850

Enter value for transactionid: 'VADE0B248910'

Enter value for from_station: 'DELHI'

Enter value for to_station: 'JODHPUR JN'

Enter value for date_of_journey: '11/08/2018'

Enter value for class: 'sleeper'

Enter value for date_of_booking: '03/08/2018'

Enter value for total_ticket_fare: 370

Enter value for train_number: 12461


old 2:
&PNRNO,&Transactionid,&from_station,&to_station,&date_of_journey,&class,&date_of_booking,&tota
l_ticket_fare,&train_number)

new 2: 2334567850, 'VADE0B248910','DELHI','JODHPUR


JN','11/08/2018','sleeper','03/08/2018',370,12461)

1 row created.

SQL> insert into Ticket values(

2
&PNRNO,&Transactionid,&from_station,&to_station,&date_of_journey,&class,&date_of_booking,&tota
l_ticket_fare,&train_number);

Enter value for pnrno: 2334567890

Enter value for transactionid: 'VADE0B248930'

Enter value for from_station: 'NEW DELHI'

Enter value for to_station: 'DAURAI'

Enter value for date_of_journey: '09/08/2018'

Enter value for class: 'AC CHAIR CAR'

Enter value for date_of_booking: '04/08/2018'

Enter value for total_ticket_fare: 700

Enter value for train_number: 12015

old 2:
&PNRNO,&Transactionid,&from_station,&to_station,&date_of_journey,&class,&date_of_booking,&tota
l_ticket_fare,&train_number)

new 2: 2334567890,'VADE0B248930','NEW DELHI','DAURAI','09/08/2018','AC CHAIR


CAR','04/08/2018',700,12015)

1 row created.
SQL> insert into Passenger_details VALUES(

2 &PNRNO,&serialno,&name,&Age,&reservation_status);

Enter value for pnrno: 2334567850

Enter value for serialno: 1

Enter value for name: 'KUSHAGRA GARG'

Enter value for age: 18

Enter value for reservation_status: 'CONFIRM'

old 2: &PNRNO,&serialno,&name,&Age,&reservation_status)

new 2: 2334567850,1,'KUSHAGRA GARG',18,'CONFIRM')

1 row created.

SQL> insert into Passenger_details VALUES(

2 &PNRNO,&serialno,&name,&Age,&reservation_status);

Enter value for pnrno: 2334567880

Enter value for serialno: 2

Enter value for name: 'KAMAL GUPTA'

Enter value for age: 20

Enter value for reservation_status: 'WAITING'

old 2: &PNRNO,&serialno,&name,&Age,&reservation_status)

new 2: 2334567880,2,'KAMAL GUPTA',20,'WAITING')

1 row created.

SQL> insert into Passenger_details VALUES(


2 &PNRNO,&serialno,&name,&Age,&reservation_status);

Enter value for pnrno: 2334567890

Enter value for serialno: 3

Enter value for name: 'MANUL GUPTA'

Enter value for age: 21

Enter value for reservation_status: 'CONFIRM'

old 2: &PNRNO,&serialno,&name,&Age,&reservation_status)

new 2: 2334567890,3,'MANUL GUPTA',21,'CONFIRM')

1 row created.

SQL> insert into Passenger_details VALUES(

2 &PNRNO,&serialno,&name,&Age,&reservation_status);

Enter value for pnrno: 2334567891

Enter value for serialno: 4

Enter value for name: 'KAMAL GUPTA'

Enter value for age: 25

Enter value for reservation_status: 'WAITING'

old 2: &PNRNO,&serialno,&name,&Age,&reservation_status)

new 2: 2334567891,4,'KAMAL GUPTA',25,'WAITING')

1 row created.

SQL> insert into Passenger_details VALUES(

2 &PNRNO,&serialno,&name,&Age,&reservation_status);
Enter value for pnrno: 2334567892

Enter value for serialno: 5

Enter value for name: 'AKSHAY GUPTA'

Enter value for age: 26

Enter value for reservation_status: 'CONFIRM'

old 2: &PNRNO,&serialno,&name,&Age,&reservation_status)

new 2: 2334567892,5,'AKSHAY GUPTA',26,'CONFIRM')

1 row created.

SQL> insert into Train_Route values(

2 &Train_No,&route_no,&station_code,&name,&arrival_time,&depart_time,&distance,&day);

Enter value for train_no: 12461

Enter value for route_no: 5

Enter value for station_code: 'JP'

Enter value for name: 'JAIPUR'

Enter value for arrival_time: '02:35'

Enter value for depart_time: '02:45'

Enter value for distance: 308

Enter value for day: 2

old 2: &Train_No,&route_no,&station_code,&name,&arrival_time,&depart_time,&distance,&day)

new 2: 12461,5,'JP','JAIPUR','02:35','02:45',308,2)

1 row created.
SQL> insert into Train_Route values(

2 &Train_No,&route_no,&station_code,&name,&arrival_time,&depart_time,&distance,&day);

Enter value for train_no: 12015

Enter value for route_no: 4

Enter value for station_code: 'DEC'

Enter value for name: 'DELHI CANTT'

Enter value for arrival_time: '06:33'

Enter value for depart_time: '06:35'

Enter value for distance: 16

Enter value for day: 1

old 2: &Train_No,&route_no,&station_code,&name,&arrival_time,&depart_time,&distance,&day)

new 2: 12015,4,'DEC','DELHI CANTT','06:33','06:35',16,1)

1 row created.

SQL> insert into Train_Route values(

2 &Train_No,&route_no,&station_code,&name,&arrival_time,&depart_time,&distance,&day);

Enter value for train_no: 12461

Enter value for route_no: 5

Enter value for station_code: 'JP'

Enter value for name: 'JAIPUR'

Enter value for arrival_time: '02:35'

Enter value for depart_time: '02:45'


Enter value for distance: 308

Enter value for day: 2

old 2: &Train_No,&route_no,&station_code,&name,&arrival_time,&depart_time,&distance,&day)

new 2: 12461,5,'JP','JAIPUR','02:35','02:45',308,2)

1 row created.

SQL> insert into Train_Route values(

2 &Train_No,&route_no,&station_code,&name,&arrival_time,&depart_time,&distance,&day);

Enter value for train_no: 12015

Enter value for route_no: 4

Enter value for station_code: 'DEC'

Enter value for name: 'DELHI CANTT'

Enter value for arrival_time: '06:33'

Enter value for depart_time: '06:35'

Enter value for distance: 16

Enter value for day: 1

old 2: &Train_No,&route_no,&station_code,&name,&arrival_time,&depart_time,&distance,&day)

new 2: 12015,4,'DEC','DELHI CANTT','06:33','06:35',16,1)

1 row created.

SQL> insert into Train_Ticket_fare Values(

2
&train_number,&class,&base_fare,&reservation_charge,&superfast_charge,&other_charge,&tatkal_cha
rge,&service_tax);
Enter value for train_number: 12050

Enter value for class: 'AC CHAIR CAR'

Enter value for base_fare: 462

Enter value for reservation_charge: 40

Enter value for superfast_charge: 45

Enter value for other_charge: 175

Enter value for tatkal_charge: 146

Enter value for service_tax: 27

old 2:
&train_number,&class,&base_fare,&reservation_charge,&superfast_charge,&other_charge,&tatkal_cha
rge,&service_tax)

new 2: 12050,'AC CHAIR CAR',462,40,45,175,146,27)

1 row created.

SQL> insert into Train_Ticket_fare Values(

2
&train_number,&class,&base_fare,&reservation_charge,&superfast_charge,&other_charge,&tatkal_cha
rge,&service_tax);

Enter value for train_number: 12461

Enter value for class: 'sleeper'

Enter value for base_fare: 320

Enter value for reservation_charge: 20

Enter value for superfast_charge: 30

Enter value for other_charge: NULL

Enter value for tatkal_charge: 101

Enter value for service_tax: NULL


old 2:
&train_number,&class,&base_fare,&reservation_charge,&superfast_charge,&other_charge,&tatkal_cha
rge,&service_tax)

new 2: 12461,'sleeper',320,20,30,NULL,101,NULL)

1 row created.

SQL>

SQL> insert into Train_Ticket_fare Values(

2
&train_number,&class,&base_fare,&reservation_charge,&superfast_charge,&other_charge,&tatkal_cha
rge,&service_tax);

Enter value for train_number: 12616

Enter value for class: 'sleeper'

Enter value for base_fare: 730

Enter value for reservation_charge: 20

Enter value for superfast_charge: 30

Enter value for other_charge: NULL

Enter value for tatkal_charge: 230

Enter value for service_tax: NULL

old 2:
&train_number,&class,&base_fare,&reservation_charge,&superfast_charge,&other_charge,&tatkal_cha
rge,&service_tax)

new 2: 12616,'sleeper',730,20,30,NULL,230,NULL)

1 row created.

SQL> insert into Train_Ticket_fare Values(


2
&train_number,&class,&base_fare,&reservation_charge,&superfast_charge,&other_charge,&tatkal_cha
rge,&service_tax);

Enter value for train_number: 12621

Enter value for class: 'sleeper'

Enter value for base_fare: 730

Enter value for reservation_charge: 20

Enter value for superfast_charge: 30

Enter value for other_charge: NULL

Enter value for tatkal_charge: 230

Enter value for service_tax: NULL

old 2:
&train_number,&class,&base_fare,&reservation_charge,&superfast_charge,&other_charge,&tatkal_cha
rge,&service_tax)

new 2: 12621,'sleeper',730,20,30,NULL,230,NULL)

1 row created.

SQL> insert into Train_Ticket_fare Values(

2
&train_number,&class,&base_fare,&reservation_charge,&superfast_charge,&other_charge,&tatkal_cha
rge,&service_tax);

Enter value for train_number: 12015

Enter value for class: 'AC CHAIR CAR'

Enter value for base_fare: 582

Enter value for reservation_charge: 40

Enter value for superfast_charge: 45

Enter value for other_charge: 80


Enter value for tatkal_charge: 184

Enter value for service_tax: NULL

old 2:
&train_number,&class,&base_fare,&reservation_charge,&superfast_charge,&other_charge,&tatkal_cha
rge,&service_tax)

new 2: 12015,'AC CHAIR CAR',582,40,45,80,184,NULL)

1 row created.

2. Write simple DDL/DML Queries to (Low Level)

1. Remove all the rows from Passenger table permanently.

SQL> DELETE FROM Passenger;

5 rows deleted.

2. Change the name of the Passenger table to Passenger_Details.


SQL> RENAME Passenger to Passenger_details;

Table renamed.
3. List all train details.
SQL>select *from Train;
TRAIN_NUMBER NAME SOURCE
------------ ------------------------------ ------------------------------
DESTINATION START_T REACH_T TRAVELTIME_HOURS DISTANCE_KM
------------------------------ ------- ------- ---------------- -----------
CLASS
--------------------------------------------------------------------------------
DAYS
--------------------------------------------------------------------------------
TYPE
------------------------------
12050 GATIMAAN EXPRESS H NIZAMUDDIN
AGRA CANTT 08:10 09:50 2 403
CLASS_TYPE('AC CHAIR CAR')

TRAIN_NUMBER NAME SOURCE


------------ ------------------------------ ------------------------------
DESTINATION START_T REACH_T TRAVELTIME_HOURS DISTANCE_KM
------------------------------ ------- ------- ---------------- -----------
CLASS
--------------------------------------------------------------------------------
DAYS
--------------------------------------------------------------------------------
TYPE
------------------------------
DAY_TYPE('MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'SATURDAY',
'SUNDAY')
shatabdi

TRAIN_NUMBER NAME SOURCE


------------ ------------------------------ ------------------------------
DESTINATION START_T REACH_T TRAVELTIME_HOURS DISTANCE_KM
------------------------------ ------- ------- ---------------- -----------
CLASS
--------------------------------------------------------------------------------
DAYS
--------------------------------------------------------------------------------
TYPE
------------------------------
12621 TAMIL NADU EXPRESS NEW DELHI
CHENNAI CENTRAL 18:40 06:20 36 2181
CLASS_TYPE('sleeper')

TRAIN_NUMBER NAME SOURCE


------------ ------------------------------ ------------------------------
DESTINATION START_T REACH_T TRAVELTIME_HOURS DISTANCE_KM
------------------------------ ------- ------- ---------------- -----------
CLASS
--------------------------------------------------------------------------------
DAYS
--------------------------------------------------------------------------------
TYPE
------------------------------
DAY_TYPE('MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY',
'SATURDAY', 'SU
NDAY')
superfast

TRAIN_NUMBER NAME SOURCE


------------ ------------------------------ ------------------------------
DESTINATION START_T REACH_T TRAVELTIME_HOURS DISTANCE_KM
------------------------------ ------- ------- ---------------- -----------
CLASS
--------------------------------------------------------------------------------
DAYS
--------------------------------------------------------------------------------
TYPE
------------------------------

12616 G T EXPRESS NEW DELHI


CHENNAI CENTRAL 18:40 06:20 36 2181

TRAIN_NUMBER NAME SOURCE


------------ ------------------------------ ------------------------------
DESTINATION START_T REACH_T TRAVELTIME_HOURS DISTANCE_KM
------------------------------ ------- ------- ---------------- -----------
CLASS
--------------------------------------------------------------------------------
DAYS
--------------------------------------------------------------------------------
TYPE
------------------------------
CLASS_TYPE('sleeper')
DAY_TYPE('MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY',
'SATURDAY', 'SU
NDAY')

TRAIN_NUMBER NAME SOURCE


------------ ------------------------------ ------------------------------
DESTINATION START_T REACH_T TRAVELTIME_HOURS DISTANCE_KM
------------------------------ ------- ------- ---------------- -----------
CLASS
--------------------------------------------------------------------------------
DAYS
--------------------------------------------------------------------------------
TYPE
------------------------------
superfast

12015 AJMER SHTBDI NEW DELHI

TRAIN_NUMBER NAME SOURCE


------------ ------------------------------ ------------------------------
DESTINATION START_T REACH_T TRAVELTIME_HOURS DISTANCE_KM
------------------------------ ------- ------- ---------------- -----------
CLASS
--------------------------------------------------------------------------------
DAYS
--------------------------------------------------------------------------------
TYPE
------------------------------
DAURAI 06:05 13:10 5 450
CLASS_TYPE('AC CHAIR CAR')
DAY_TYPE('MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY',
'SATURDAY', 'SU

TRAIN_NUMBER NAME SOURCE


------------ ------------------------------ ------------------------------
DESTINATION START_T REACH_T TRAVELTIME_HOURS DISTANCE_KM
------------------------------ ------- ------- ---------------- -----------
CLASS
--------------------------------------------------------------------------------
DAYS
--------------------------------------------------------------------------------
TYPE
------------------------------
NDAY')
shatabdi

TRAIN_NUMBER NAME SOURCE


------------ ------------------------------ ------------------------------
DESTINATION START_T REACH_T TRAVELTIME_HOURS DISTANCE_KM
------------------------------ ------- ------- ---------------- -----------
CLASS
--------------------------------------------------------------------------------
DAYS
--------------------------------------------------------------------------------
TYPE
------------------------------
12461 MANDOR EXPRESS DELHI
JODHPUR JN 21:15 8:00 5 620
CLASS_TYPE('sleeper')

TRAIN_NUMBER NAME SOURCE


------------ ------------------------------ ------------------------------
DESTINATION START_T REACH_T TRAVELTIME_HOURS DISTANCE_KM
------------------------------ ------- ------- ---------------- -----------
CLASS
--------------------------------------------------------------------------------
DAYS
--------------------------------------------------------------------------------
TYPE
------------------------------
DAY_TYPE('MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY',
'SATURDAY', 'SU
NDAY')
superfast

TRAIN_NUMBER NAME SOURCE


------------ ------------------------------ ------------------------------
DESTINATION START_T REACH_T TRAVELTIME_HOURS DISTANCE_KM
------------------------------ ------- ------- ---------------- -----------
CLASS
--------------------------------------------------------------------------------
DAYS
--------------------------------------------------------------------------------
TYPE
------------------------------

4. List all passenger details.

PNRNO SERIALNO NAME AGE RESERVATIO


---------- ---------- ------------------------------ ---------- ----------
2334567850 1 KUSHAGRA GARG 18 CONFIRM
2334567880 2 KAMAL GUPTA 20 WAITING
2334567890 3 MANUL GUPTA 21 CONFIRM
2334567891 4 KAMAL GUPTA 25 WAITING
2334567892 5 AKSHAY GUPTA 26 CONFIRM
5. Give a list of trains in ascending order of number.
SQL> select train_number,name from Train ORDER BY Train_number ASC;

TRAIN_NUMBER NAME
------------ ------------------------------
12015 AJMER SHTBDI
12050 GATIMAAN EXPRESS
12461 MANDOR EXPRESS
12616 G T EXPRESS
12621 TAMIL NADU EXPRESS
6. List the senior citizen passengers details.
SQL> select *from Passenger_details where AGE>=60;

no rows selected
7. List the station names where code starts with 'M'.
SQL> select name from Train_Route where station_code='M*';

no rows selected
8. List the trains details within a range of numbers.
SQL> select *from Train where Train_number>=12000 and Train_number<=12500;
TRAIN_NUMBER NAME SOURCE
------------ ------------------------------ ------------------------------
DESTINATION START_T REACH_T TRAVELTIME_HOURS DISTANCE_KM
------------------------------ ------- ------- ---------------- -----------
CLASS
--------------------------------------------------------------------------------
DAYS
--------------------------------------------------------------------------------
TYPE
------------------------------
12050 GATIMAAN EXPRESS H NIZAMUDDIN
AGRA CANTT 08:10 09:50 2 403
CLASS_TYPE('AC CHAIR CAR')

TRAIN_NUMBER NAME SOURCE


------------ ------------------------------ ------------------------------
DESTINATION START_T REACH_T TRAVELTIME_HOURS DISTANCE_KM
------------------------------ ------- ------- ---------------- -----------
CLASS
--------------------------------------------------------------------------------
DAYS
--------------------------------------------------------------------------------
TYPE
------------------------------
DAY_TYPE('MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'SATURDAY',
'SUNDAY')
shatabdi

TRAIN_NUMBER NAME SOURCE


------------ ------------------------------ ------------------------------
DESTINATION START_T REACH_T TRAVELTIME_HOURS DISTANCE_KM
------------------------------ ------- ------- ---------------- -----------
CLASS
--------------------------------------------------------------------------------
DAYS
--------------------------------------------------------------------------------
TYPE
------------------------------
12015 AJMER SHTBDI NEW DELHI
DAURAI 06:05 13:10 5 450
CLASS_TYPE('AC CHAIR CAR')

TRAIN_NUMBER NAME SOURCE


------------ ------------------------------ ------------------------------
DESTINATION START_T REACH_T TRAVELTIME_HOURS DISTANCE_KM
------------------------------ ------- ------- ---------------- -----------
CLASS
--------------------------------------------------------------------------------
DAYS
--------------------------------------------------------------------------------
TYPE
------------------------------
DAY_TYPE('MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY',
'SATURDAY', 'SU
NDAY')
shatabdi

TRAIN_NUMBER NAME SOURCE


------------ ------------------------------ ------------------------------
DESTINATION START_T REACH_T TRAVELTIME_HOURS DISTANCE_KM
------------------------------ ------- ------- ---------------- -----------
CLASS
--------------------------------------------------------------------------------
DAYS
--------------------------------------------------------------------------------
TYPE
------------------------------

12461 MANDOR EXPRESS DELHI


JODHPUR JN 21:15 8:00 5 620

TRAIN_NUMBER NAME SOURCE


------------ ------------------------------ ------------------------------
DESTINATION START_T REACH_T TRAVELTIME_HOURS DISTANCE_KM
------------------------------ ------- ------- ---------------- -----------
CLASS
--------------------------------------------------------------------------------
DAYS
--------------------------------------------------------------------------------
TYPE
------------------------------
CLASS_TYPE('sleeper')
DAY_TYPE('MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY',
'SATURDAY', 'SU
NDAY')

TRAIN_NUMBER NAME SOURCE


------------ ------------------------------ ------------------------------
DESTINATION START_T REACH_T TRAVELTIME_HOURS DISTANCE_KM
------------------------------ ------- ------- ---------------- -----------
CLASS
--------------------------------------------------------------------------------
DAYS
--------------------------------------------------------------------------------
TYPE
------------------------------
superfast

9. Change the super fast charge value in train fare as zero , if it is null.
SQL> update Train_Ticket_fare set superfast_charge=0 where superfast_charge IS NULL;

0 rows updated.
10. List the passenger names whose tickets are not confirmed.
SQL> select *from Passenger_details where Reservation_status!='CONFIRM';

PNRNO SERIALNO NAME AGE RESERVATIO


---------- ---------- ------------------------------ ---------- ----------
2334567880 2 KAMAL GUPTA 20 WAITING
2334567891 4 KAMAL GUPTA 25 WAITING
11. List the base_fare of all AC coaches available in each train.
SQL> select BASE_FARE from Train_Ticket_fare where CLASS='AC CHAIR CAR';

BASE_FARE
----------
582
462
Find the ticket details where transaction id is not known.
SQL> select *from Ticket where Transactionid='NULL';

no rows selected
1. Use Interactive updation for updating the serial no for particular PNR NO.
SQL> update Passenger_details set serialno=&serialno where pnrno=&pnrno;
Enter value for serialno: 6
Enter value for pnrno: 2334567850
old 1: update Passenger_details set serialno=&serialno where pnrno=&pnrno
new 1: update Passenger_details set serialno=6 where pnrno=2334567850

1 row updated.

2. Find the train names that are from Chennai to Mumbai, but do not have the source or
destination in its name.
SQL> select name from Train where SOURCE='CHENNAI' AND DESTINATION='MUMBAI'
AND NAME!='*CHENNAI*MUMBAI*' AND NAME!='*MUMBAI*CHENNAI*';
no rows selected
3 Find the train details that are on Thursday(Use the ADT column created).

Day is of data type varray.

3. Create (Alter table to add constraint) the necessary foreign keys by identifying the
relationships in the table. (Middle Level)
1. Add a suitable constraint to train table to always have train no in the range 10001 to
99999.

SQL> alter table Train ADD constraint Train_Train_number_check check(TRAIN_NUMBER


BETWEEN 10001 AND 99999);

Table altered.

2. Add a suitable constraint for the column of station name, so that does not take
duplicates.
SQL> alter table Train_Route add constraint Train_Route_name_unique unique(name);

Table altered.

3. Change the data type of arrival time, depart time (date -> timestamp or timestamp to
date),
and do the necessary process for updating the table with new values.
SQL> alter table Train drop column start_time;

Table altered.

SQL> alter table train drop column reach_time;

Table altered.

SQL> alter table Train add start_time timestamp(0);

Table altered.

SQL> alter table Train add reach_time timestamp(0);

Table altered.

SQL> update Train set start_time=to_timestamp('08-08-2018 18:40:00','dd-mm-yyyy


HH24:MI:SS'),reach_time=to_timestamp('10-08-2018 06:20:00','dd-mm-yyyy HH24:MI:SS')
where train_number=12616;

1 row updated.
SQL> update Train set start_time=to_timestamp('08-08-2018 06:05:00','dd-mm-yyyy
HH24:MI:SS'),reach_time=to_timestamp('08-08-2018 13:10:00','dd-mm-yyyy HH24:MI:SS')
where train_number=12015;

1 row updated.

SQL> update Train set start_time=to_timestamp('08-08-2018 22:30:00','dd-mm-yyyy


HH24:MI:SS'),reach_time=to_timestamp('10-08-2018 07:10:00','dd-mm-yyyy HH24:MI:SS')
where train_number=12621;

1 row updated.

SQL> update Train set start_time=to_timestamp('08-08-2018 08:10:00','dd-mm-yyyy


HH24:MI:SS'),reach_time=to_timestamp('08-08-2018 09:50:00','dd-mm-yyyy HH24:MI:SS')
where train_number=12050;

1 row updated.

SQL> update Train set start_time=to_timestamp('08-08-2018 21:15:00','dd-mm-yyyy


HH24:MI:SS'),reach_time=to_timestamp('09-08-2018 8:00:00','dd-mm-yyyy HH24:MI:SS')
where train_number=12461;

1 row updated.

4. Add a suitable constraint for the class column that it should take values only as 1A, 2A,
3A, SL, C.
Class is of constraint varray
5. Add a not null constraint for the column distance in train_route.

SQL> alter table Train_route modify distance_km number constraint


2 train_route_distance_notnull NOT NULL;

Table altered.

4. Use SQL PLUS functions to. (Low Level)


1. Find the passengers whose date of journey is one month from today.
SQL> select PNRNO from Ticket where
to_date(date_of_journey,'dd/mm/yyyy')=add_months('08-AUG-2018',1);

no rows selected
2 Print the train names in upper case.
SQL> select upper(name) from Train;
UPPER(NAME)
------------------------------
GATIMAAN EXPRESS
TAMIL NADU EXPRESS
G T EXPRESS
AJMER SHTBDI
MANDOR EXPRESS
3 Print the passenger names with left padding character.
SQL> select lpad(name,20,'@') from Passenger_details;

LPAD(NAME,20,'@')
--------------------
@@@@@@@KUSHAGRA GARG
@@@@@@@@@KAMAL GUPTA
@@@@@@@@@MANUL GUPTA
@@@@@@@@@KAMAL GUPTA
@@@@@@@@AKSHAY GUPTA
4 Print the station codes replacing K with M.
SQL> select replace(station_code,'K','M') from Train_Route;

REPLACE(STATION_CODE
--------------------
JP
DEC
AGC
GWL
BPL
5. Translate all the LC in class column (Train_fare) to POT and display.
SQL> select translate(class,'LC','POT') from Train_ticket_fare;

TRANSLATE(CLASS,'LC','POT')
------------------------------
AO OHAIR OAR
AO OHAIR OAR
sleeper
sleeper
sleeper
6. Display the fare details of all trains, if any value is ZERO, print as NULL value.
SQL> select *from Train_Ticket_fare;

TRAIN_NUMBER CLASS BASE_FARE RESERVATION_CHARGE


------------ ------------------------------ ---------- ------------------
SUPERFAST_CHARGE OTHER_CHARGE TATKAL_CHARGE SERVICE_TAX
---------------- ------------ ------------- -----------
12621 sleeper 730 20
30 230

12015 AC CHAIR CAR 582 40


45 80 184

12050 AC CHAIR CAR 462 40


45 175 146 27

TRAIN_NUMBER CLASS BASE_FARE RESERVATION_CHARGE


------------ ------------------------------ ---------- ------------------
SUPERFAST_CHARGE OTHER_CHARGE TATKAL_CHARGE SERVICE_TAX
---------------- ------------ ------------- -----------
12461 sleeper 320 20
30 101

12616 sleeper 730 20


30 230
7. Display the pnrno and transaction id, if transaction id is null, print 'not generated'.
SQL> select pnrno,nvl(transactionid,'not generated') from ticket;

PNRNO NVL(TRANSACTIONID,'NOTGENERATE
---------- ------------------------------
2334567890 VADE0B248930
2334567880 VADE0B248920
2334567850 VADE0B248910
2334567892 VADE0B248910
2334567891 VADE0B248931
8. Print the date_of_journey in the format '27th November 2010'.
SQL> select to_char(to_date(date_of_journey,'DD/mm/yyyy'),'ddth month yyyy') from Ticket;

TO_CHAR(TO_DATE(DAT
-------------------
09th august 2018
10th august 2018
11th august 2018
06th august 2018
06th august 2018
9. Find the maximum fare (total fare).
SQL> select max(TOTAL_TICKET_FARE) from ticket;

MAX(TOTAL_TICKET_FARE)
----------------------
780
10. Find the average age of passengers in one ticket.
SQL> select avg(age) from Passenger_details;
AVG(AGE)
----------
22
11. Find the maximum length of station name available in the database.
SQL> select max(length(name)) from Train_route;

MAX(LENGTH(NAME))
-----------------
11
12. Print the fare amount of the passengers as rounded value.
SQL> select round(total_ticket_fare) from ticket;

ROUND(TOTAL_TICKET_FARE)
------------------------
700
665
370
780
780
13. Add the column halt time to train route.
SQL> alter table Train_route add Halt_time_min number(3);

Table altered.
14. Update values to it from arrival time and depart time.
SQL> update Train_route set Halt_time_min=10 where train_number=12461;

1 row updated.

SQL> update Train_route set Halt_time_min=2 where train_number=12015;

1 row updated.

SQL> update Train_route set Halt_time_min=3 where train_number=12621;

1 row updated.

SQL> update Train_route set Halt_time_min=2 where train_number=12050;

1 row updated.

SQL> update Train_route set Halt_time_min=5 where train_number=12616;

1 row updated.
High Level:
15. Update values to arrival time and depart time using conversion functions.
16. Display the arrival time, depart time in the format HH:MI (24 hours and minutes).

SQL> select arrival_time,depart_time from Train_route;

ARRIVAL_TI DEPART_TIM

---------- ----------

02:35 02:45

06:33 06:35

01:00 01:03

11:16 11:18

05:20 05:25

You might also like