20mis0415 DBMS Lab Da 1
20mis0415 DBMS Lab Da 1
SYSTEMS
LAB ASSIGNMENT-I
NAME: THULASI MADHAN R
REGNO: 20MIS0415
Faculty: JAYARAM REDDY A
Slot: L3+L4
TICKET:
Create Table Ticket(PNR_NO NUMBER(10), constraint
Ticket_PNR_NO_pk primary key(PNR_NO), Transaction_Id NUMBER(15),
From_Station VARCHAR(20), To_Station VARCHAR(20), Date_Of_Journey
DATE, Class VARCHAR(20), Date_Of_Booking DATE, Total_Fare
NUMBER(6), Train_Number Number(7) constraint Ticket_Train_Number_fk REFERENCES
Train(Train_Number));
PASSANGER:
Create Table Passenger(PNR_NO NUMBER(10), Serial_No
NUMBER(20), Name VARCHAR(30), Age NUMBER(2), Reservation_Status
VARCHAR(10), constraint Passenger_PNR_NO_fk foreign key(PNR_NO) REFERENCES Ticket(PNR_NO),
constraint Passenger_pk primary key(PNR_NO,Serial_No));
TRAIN ROUTE:
Create Table Train_Route(Train_Number NUMBER(7), Route_No
NUMBER(20), Station_Code VARCHAR(20), Train_Name VARCHAR(30),
Arrival_Time Timestamp, Depart_Time Timestamp, Distance_Kms NUMBER(10), Days VARCHAR(10),
constraint Train_Route_pk primary key(Train_Number,Route_No), constraint
Train_Route_train_Number foreign key(Train_Number) REFERENCES Train(Train_Number));
new 2: 'Kerala', 'Vellore', '02-sep-22 07:15:00 am', '03-sep-22 04:00:00 am', 600,
1 row created.
new 2: 'AP', 'Knniyakumari', '05-sep-22 07:15:00 am', '07-sep-22 07:15:00 am', 30,
1 row created.
new 2: 'Kanniyakumari', 'Madras', '05-oct-22 07:15:00 am', '08-oct-22 07:15:00 am', 40,
1 row created.
new 2: 'Delhi', 'Vellore', '05-aug-22 07:15:00 am', '08-aug-22 07:15:00 am', 45,
1 row created.
new 2: 'mumbai', 'Vellore', '11-aug-22 07:15:00 am', '16-aug-22 07:15:00 am', 56,
1 row created.
START_TIME
---------------------------------------------------------------------------
REACH_TIME
---------------------------------------------------------------------------
02-SEP-22 07.15.00.000000 AM
03-SEP-22 04.00.00.000000 AM
START_TIME
---------------------------------------------------------------------------
REACH_TIME
---------------------------------------------------------------------------
05-SEP-22 07.15.00.000000 AM
07-SEP-22 07.15.00.000000 AM
START_TIME
---------------------------------------------------------------------------
REACH_TIME
---------------------------------------------------------------------------
05-OCT-22 07.15.00.000000 AM
08-OCT-22 07.15.00.000000 AM
START_TIME
---------------------------------------------------------------------------
REACH_TIME
---------------------------------------------------------------------------
05-AUG-22 07.15.00.000000 AM
08-AUG-22 07.15.00.000000 AM
START_TIME
---------------------------------------------------------------------------
REACH_TIME
---------------------------------------------------------------------------
11-AUG-22 07.15.00.000000 AM
16-AUG-22 07.15.00.000000 AM
TICKET:
1 row created.
1 row created.
SQL> Insert Into Ticket Values(&PNR_NO, &Transaction_Id,
2 &From_Station, &To_Station, &Date_Of_Journey, &Class,
3 &Date_Of_Booking, &Total_Ticket_Fare, &Train_Number);
Enter value for pnr_no: 98765432
Enter value for transaction_id: 10003445679
old 1: Insert Into Ticket Values(&PNR_NO, &Transaction_Id,
new 1: Insert Into Ticket Values(98765432, 10003445679,
Enter value for from_station: 'kerala'
Enter value for to_station: 'Vellore'
Enter value for date_of_journey: '02-SEP-22'
Enter value for class: 'Normal'
old 2: &From_Station, &To_Station, &Date_Of_Journey, &Class,
new 2: 'kerala', 'Vellore', '02-SEP-22', 'Normal',
Enter value for date_of_booking: '02-AUG-22'
Enter value for total_ticket_fare: 900
Enter value for train_number: 124536
old 3: &Date_Of_Booking, &Total_Ticket_Fare, &Train_Number)
new 3: '02-AUG-22', 900, 124536)
1 row created.
1 row created.
PASSANGER:
SQL> Insert Into Passenger Values(&PNR_NO, &Serial_No,
2 &Name, &Age, &Reservation_Status);
Enter value for pnr_no: 65748943
Enter value for serial_no: 23
old 1: Insert Into Passenger Values(&PNR_NO, &Serial_No,
new 1: Insert Into Passenger Values(65748943, 23,
Enter value for name: 'MAHA'
Enter value for age: 21
Enter value for reservation_status: 'R'
old 2: &Name, &Age, &Reservation_Status)
new 2: 'MAHA', 21, 'R')
1 row created.
1 row created.
1 row created.
1 row created.
TRAIN ROUTE:
Insert Into Train_Route Values(&Train_NUmber,
2 &Route_No, &Station_Code, &Train_Name, &Arrival_Time, &Depart_Time,
3 &Distance_Kms, &Days);
Enter value for train_number: 24563
old 1: Insert Into Train_Route Values(&Train_NUmber,
new 1: Insert Into Train_Route Values(24563,
Enter value for route_no: 15
Enter value for station_code: 'DL'
Enter value for train_name: 'dEVIL EXPRESS'
Enter value for arrival_time: '05-AUG-22 07:00:00'
Enter value for depart_time: '05-AUG-22 07:15:00'
old 2: &Route_No, &Station_Code, &Train_Name, &Arrival_Time, &Depart_Time,
new 2: 15, 'DL', 'dEVIL EXPRESS', '05-AUG-22 07:00:00', '05-AUG-22 07:15:00',
Enter value for distance_kms: 1500
Enter value for days: 'MONDAY'
old 3: &Distance_Kms, &Days)
new 3: 1500, 'MONDAY')
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
For DAYS
CODE:
CREATE Or REPLACE TYPE Days_type AS VARRAY(7) OF VARCHAR(20);
2 /
Type created.
2. Write simple DDL/DML Queries
a) Remove all the rows from Passenger table permanently.
65748943 23 MAHA 21 R
98765432 34 TARIK 27 NR
12345678 75 TENZ 28 R
23456789 45 SHROUD 32 R
2 Train_number ASC;
TRAIN_NUMBER TRAIN_NAME
------------ --------------------
no rows selected
no rows selected
23456789 45 SHROUD 32 R
no rows selected
2 Train_Number<=35000;
START_TIME
---------------------------------------------------------------------------
REACH_TIME
---------------------------------------------------------------------------
05-AUG-22 07.15.00.000000 AM
08-AUG-22 07.15.00.000000 AM
2 Superfast_Charge IS NULL;
0 rows updated.
98765432 34 TARIK 27 NR
BASE_FARE
----------
800
500
no rows selected
i) Use interactive update for updating the seat no for a particular PNR_NO.
SQL> Update Passenger_details SET Serial_No=&Serial_No WHERE
2 Pnr_No=&Pnr_No;
old 2: Pnr_No=&Pnr_No
new 2: Pnr_No=23456789
1 row updated.
65748943 23 MAHA 21 R
98765432 34 TARIK 27 NR
12345678 75 TENZ 28 R
23456789 45 SHROUD 32 R
ii) Find the train names that are from Chennai to Mumbai, but do not have
the source or destination in its name.
SQL> Select Train_Name From Train WHERE Source='Chennai' AND Destination='Mumbai' AND
Train_Name!='*Chennai*Mumbai*' AND Train_Name!='*Mumbai*Chennai*';
no rows selected
no rows selected
ii) 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(Train_Name);
Table altered.
iii) 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.
Alter Table Train_Route modify(Arrival_Time date);
Table altered.
Table altered.
1 row updated.
1 row updated.
1 row updated.
1 row updated.
1 row updated.
1 row updated.
For Train Number 67890
SQL> Update Train_Route SET Arrival_Time='05-JUL-22' WHERE Train_Number=67890;
1 row updated.
2 Train_Number=67890;
1 row updated.
ARRIVAL_T DEPART_TI
--------- ---------
02-AUG-22 02-AUG-22
05-JUL-22 30-JUN-22
15-MAY-23 15-MAY-23
05-AUG-22 30-AUG-22
iv) Add a suitable constraint for the class column that it should take values only as 1A, 2A,
3A, SL, C.
Alter Table Train ADD constraint Train_Class_cK1 check(CLASS_TYPE in ('1A','2A','3A','SL','C'));
no rows selected
UPPER(TRAIN_NAME)
--------------------
DEVIL EXPRESS
VIT EXPRESS
KERALA EXPRESS
VIKRAM EXPRESS
LPAD(NAME,20,'&')
--------------------------------------------------------------------------------
&&&&&&&&&&&&&&&&MAHA
&&&&&&&&&&&&&&&TARIK
&&&&&&&&&&&&&&&&TENZ
&&&&&&&&&&&&&&SHROUD
REPLACE(STATION_CODE
--------------------
DL
AP
MR
MV
TRANSLATE(CLASS,'LC','POT')
--------------------------------------------------------------------------------
SPEEPER
SPEEPERS
SPEEPER
NORMAP
vi) Display the fare details of all trains, if any value is ZERO, print as
NULL value.
SQL> update Train_Ticket_Fare set base_fare=NULL where base_fare=0;
0 rows updated.
0 rows updated.
0 rows updated.
0 rows updated.
0 rows updated.
SQL> Select
TO_CHAR(TO_DATE(DATE_OF_JOURNEY,'DD/MM/YYYY'),
----------------------------------------------
MAX(TOTAL_FARE)
---------------
1500
AVG(AGE)
----------
27
MAX(LENGTH(TRAIN_NAME))
-----------------------
14
-----------------
400
700
900
1500
Table altered.
Table altered.
4 rows updated.
High Level:
xv) Update values to arrival time and depart time using conversion
functions.
SQL> Update Train_Route set Arrival_time=CURRENT_TIMESTAMP;
4 rows updated.
xvi) Display the arrival time, depart time in the format HH:MI (24 hours
and minutes).
SQL> select to_char(Arrival_time,'HH:MM') from Train_Route;
TO_CH
-----
01:08
01:08
01:08
01:08
TO_CH
-----
12:08
12:06
12:05
12:08