create database flight_management;
create database flight_management;
use flight_management;
create table flights ( flightid int primary key auto_increment, airline varchar
(50) not null,
source varchar(55) not null , destination varchar(55) not null,
status enum ('scheduled','departed','arrived','cancelled') default 'scheduled',
price decimal(10,2) check (price > 0) );
create table bookings( bookingid int auto_increment primary key , passengerid int
unique ,
flightid int , bookingdate timestamp default current_timestamp,
seatnumber varchar(10) not null, status enum('confirmed', 'cancelled') ,
foreign key (passengerid) references passengers ( passengerid),
foreign key (flightid) references flights (flightid));
create table crew( crewid int primary key auto_increment, fullname varchar(100) not
null,
role enum('pilot','co-pilot','flight attendant') not null, flightid int,
foreign key (flightid) references flights (flightid) );
create table payments( paymentid int primary key auto_increment, bookingid int,
amount decimal(10,2) not null check (amount>0),
paymentmethod enum('Credit card',' Debit card','UPI', 'Netbanking') not null,
paymentdate timestamp default current_timestamp,
status enum('successful','failed','pending') default 'pending',
foreign key (bookingid ) references bookings(bookingid));
select*from flights;
insert into bookings(seatnumber,status) values
('10A','confirmed'),('11B','confirmed'),('12C','confirmed'),('13A','cancelled');
select*from bookings;
select*from passengers;
select*from bookings;
update flights
set source = case
when flightid= 5 then 'chennai' when flightid= 6 then 'chennai'
when flightid= 7 then 'chennai' when flightid= 8 then 'chennai'
when flightid= 9 then 'chennai' when flightid= 10 then 'chennai'
when flightid= 11 then 'chennai' else source
end;
update flights
set departuretime = case
when flightid= 5 then'2025-03-23 09:30:00' when flightid= 6 then '2025-03-23
09:50:00'
when flightid= 7 then '2025-03-23 10:30:00' when flightid= 8 then '2025-03-23
11:30:00'
when flightid= 9 then '2025-03-23 12:05:00' when flightid= 10 then '2025-03-23
13:15:00'
when flightid= 11 then '2025-03-23 14:30:00' else departuretime
end;
insert into crew ( fullname , role, flightid) values
('maan singh','pilot',7),('hema bajwa','co-pilot',7),('saitha','flight
attendant',7),
('shivay rathor','pilot',8),('manbir singh','co-pilot',8),('keerthi','flight
attendant',8),
('shiva shankar','pilot',9),('harmanpreet','co-pilot',9),('mohan','flight
attendant',9),
('janani','pilot',10),('nithish gosh','co-pilot',10),('isha negi','flight
attendant',10),
('shan','pilot',11),('rithivik gosh','co-pilot',11),('josh','flight attendant',11);
update bookings
set passengerid = case
when bookingid=9 then 17 when bookingid=10 then 18
when bookingid=11 then 19 when bookingid=12 then 20
else passengerid end,
flightid = case
when bookingid=9 then 7 when bookingid=10 then 8
when bookingid=11 then 8 when bookingid=12 then 7
else flightid end;
DELIMITER //
CREATE PROCEDURE GetFlightsByStatus(IN flight_status
ENUM('scheduled','departed','arrived','cancelled'))
BEGIN
SELECT * FROM flights WHERE status = flight_status;
END //
DELIMITER ;
CALL GetFlightsByStatus('arrived');
DELIMITER //
CREATE TRIGGER update_payment_on_booking_cancel
AFTER UPDATE ON bookings
FOR EACH ROW
BEGIN
IF NEW.status = 'cancelled' THEN
UPDATE payments SET status = 'failed' WHERE bookingid = NEW.bookingid;
END IF;
END //
DELIMITER ;
UPDATE flights SET status = 'cancelled' WHERE flightid = 2;