10111d0003_dd11_w2_m7_m8

17
Tables (15) 1. Airlines_Master Fields Aircode : Char (2) : PK Airline_Name: Varchar(15) To Create Table CREATE TABLE Airlines_Master(Aircode CHAR(2),Airline_Name Varchar(15), PRIMARY KEY (Aircode)); To Insert Data into Table INSERT INTO Airlines_Master VALUES ('in','indigo'), ('jt','jet'), ('kf','kingfisher'), ('sj','spicejet'); 2. Category_Master Fields Category_Code : Char(2) : PK Description : Varchar(15) To Create Table CREATE TABLE Category_Master(Category_Code char(2),Description varchar(15), primary key(Category_Code)); To Insert Data into Table INSERT INTO Category_Master VALUES ('gd','gold'), ('pl','paltinum'),('sl','silver'); 3. Class_Master Fields Class_code : Char(3) : PK Class_name : varchar(15) B Mahesh Kumar Yadav Page 1

Upload: mahesh-yadav

Post on 28-Nov-2014

185 views

Category:

Documents


17 download

TRANSCRIPT

Page 1: 10111D0003_DD11_W2_M7_M8

Tables (15)

1. Airlines_Master

FieldsAircode : Char (2) : PKAirline_Name: Varchar(15)

To Create Table

CREATE TABLE Airlines_Master(Aircode CHAR(2),Airline_Name Varchar(15), PRIMARY KEY (Aircode));

To Insert Data into Table

INSERT INTO Airlines_Master VALUES ('in','indigo'),('jt','jet'), ('kf','kingfisher'), ('sj','spicejet');

2. Category_Master

FieldsCategory_Code : Char(2) : PKDescription : Varchar(15)

To Create Table

CREATE TABLE Category_Master(Category_Code char(2),Description varchar(15), primary key(Category_Code));

To Insert Data into Table

INSERT INTO Category_Master VALUES ('gd','gold'),('pl','paltinum'),('sl','silver');

3. Class_Master

FieldsClass_code : Char(3) : PKClass_name : varchar(15)

To Create Table

CREATE TABLE Class_Master(class_code char(3) ,class_name varchar(15), primary key(class_code));

To Insert Data into Table

INSERT INTO Class_Master VALUES ('ec','economy'),('ex','executive'),('f','first');

B Mahesh Kumar Yadav Page 1

Page 2: 10111D0003_DD11_W2_M7_M8

4. City_Master

FieldsCity_Code : Varchar(5) : PKCity_Name : Varchar(15)Country : Varchar (15)

To Create Table

CREATE TABLE City_Master(city_code varchar(5),city_name varchar(15),country varchar(15),primary key(city_code));

To Insert Data into Table

INSERT INTO City_Master VALUES ('hyd','hyderabad','india'),('lrd','lords','england'), ('syd','sydney','australia'),('wash','washington','america');

5. Day_Master

FieldsDay_Code : Int (4) : PKDay_Name : Varchar(12)

To Create Table

CREATE TABLE Day_Master(day_code int(4),day_name varchar(12),primary key(day_code));

To Insert Data into Table

INSERT INTO Day_Master VALUES (1,'sunday'),(2,'monday'),(3,'tuesday'), (4,'wednesday'),(5,'thursday'),(6,'friday'),(7,'saturday');

6. Meal

FieldsMeal_Code : Varchar(5) : PKMeal_Name : Varchar

To Create Table

CREATE TABLE Meal(meal_code varchar(5), meal_name varchar(20), primary key(meal_code));

To Insert Data into Table

INSERT INTO Meal VALUES (’piz’,’pizza’),('chbur','chicken burger'), ('hmbur','hamburger'),('hd','hotdog');

B Mahesh Kumar Yadav Page 2

Page 3: 10111D0003_DD11_W2_M7_M8

7. Service

FieldsService_Code : Varchar(3) : PKService_Name : Varchar

To Create Table

CREATE TABLE Service(service_code varchar(3),service_name varchar(15),primary key(service_code));

To Insert Data into Table

INSERT INTO Service VALUES ('cc','chlid care'),('nur','nusre'),('wc','wheel chair');

8. Status_Master

FieldsStatus_Code : Varchar : PKDescription : Varchar

To Create Table

CREATE TABLE Status_Master(status_code varchar(5),description varchar(20),primary key(status_code));

To Insert Data into Table

INSERT INTO Status_Master VALUES ('canc','cancelled'),('wait','waiting'), ('res','reserved');

9. Airlines_Service

FieldsAircode : Char (2) : FKService_Code : Varchar (3) : FK

To Create Table

CREATE TABLE Airlines_Service(aircode char(2),service_code varchar(3),foreign key(aircode) references airlines_master(aircode) on delete cascade on update no action,foreign key(service_code) references service(service_code) on delete cascade on update no action);

To Insert Data into Table

INSERT INTO Airlines_Service VALUES ('jt','wc'),('kf','cc'),('sj','nur'), ('in','cc');

B Mahesh Kumar Yadav Page 3

Page 4: 10111D0003_DD11_W2_M7_M8

10. Airline_Meals

FieldsAircode : Char (2) : FKMeal_Code : Varchar (5) : FK

To Create Table

CREATE TABLE Airline_Meals(aircode char(2),meal_code varchar(5),foreign key(aircode) references airlines_master(aircode) on delete cascade on update no action,foreign key(meal_code) references meal(meal_code) on delete cascade on update no action);

To Insert Data into Table

INSERT INTO Airline_Meals VALUES('in','piz'),('jt','chbur'), ('kf','hmbur'),('sj','hd'), ('kf','piz');

11. Flight

FieldsAircraft_code: Varchar : PkAircode : char : FKType : VarcharSource : VarcharDestination : VarcharCategory : VarcharDep_time : VarcharJourney_Hrs : Int

To Create Table

CREATE TABLE Flight(Aircraft_code Varchar(10),Aircode char(2),Type Varchar(5),Source Varchar(15),Destination Varchar(15),Category Varchar(10),Dep_time Varchar(10),Journey_Hrs Int(5),primary key(aircraft_code),foreign key(aircode) references airlines_master(aircode) on delete cascade on update no action);

To Insert Data into Table

INSERT INTO Flight VALUES ('in1','in','daily','hyderabad','washington','silver','2:00 pm',4),('kf1','kf','week','delhi','lords','gold','3:00am',8),('jt1','jt','daily','chennai','sydney','platinum','9:00 am',11),('sj1','sj','week','mumbai','south africa','gold','12:00am',13), ('ICOF2','jt','daily','delhi','hyd','silver','2:00pm',10);

12. Flight_Days

FieldsAircraft_code : Varchar : FKDay_Code: Int : FK

B Mahesh Kumar Yadav Page 4

Page 5: 10111D0003_DD11_W2_M7_M8

To Create Table

CREATE TABLE Flight_Days(aircraft_code varchar(10),day_code int(4),foreign key(aircraft_code) references flight(aircraft_code) on delete cascade on update no action,foreign key(day_code) references day_master(day_code) on delete cascade on update no action);

To Insert Data into Table

INSERT INTO Flight_Days VALUES ('in1',1),('jt1',2),('kf1',3),('sj1',4),('jt1',5),('kf1',6), ('in1',7),(‘jt1’,1);

13. Flight_Details

FieldsAircraft_Code : Varchar : FKClass_Code: Char : FKFare : NumericSeats : Numeric

To Create Table

CREATE TABLE Flight_Details(aircraft_code varchar(10),class_code char(3),fare int(10),seats int(4),foreign key(aircraft_code) references flight(aircraft_code) on delete cascade on update no action,foreign key(class_code) references class_master(class_code) on delete cascade on update no action);

To Insert Data into Table

INSERT INTO Flight_Details VALUES ('jt1','ec',1000,75),('kf1','f',9000,60), ('jt1','ex',3000,40), ('sj1','ex',2000,50), ('IC0F2','F',5000,70);

14. Passenger

FieldsPNR_No : Numeric(9) : PKTicket_No: Numeric (9)Name: Varchar(15)Age : int (4)Sex:Char(10) : Male / FemalePPNO: Varchar(15)Meal Pref: Varchar(20)

To Create Table

CREATE TABLE Passenger( pnr_no Numeric(9), ticket_no numeric(9),name varchar(15), age int(4) unsigned, sex set ('male','female') not null , ppno varchar(15) not null, meal_pref varchar(20),primary key(pnr_no));

B Mahesh Kumar Yadav Page 5

Page 6: 10111D0003_DD11_W2_M7_M8

To Insert Data into Table

INSERT INTO Passenger VALUES (10111001,'0128701','prince mahesh',33,'male', 'Ppm0101','chicken burger'), (10111002,'0128702','ntr',29,'male','Pn0202','hamburger'), (10111003,'0128703','ram charan',55,'male','Prc0303','hotdog'), (10111004,'0128704', 'samantah',23,'female','Ps0404','pizza'), (10111005,'0128705','trisha',29,'female', 'Pt0505','hotdog'), (10111006,'0128706','XYZ',44,'male', 'F14032','chicken burger');

15. Reservation

FieldsPNR_No: Numeric(9) : FKAircraft_Code : Varchar(5) : FKJourney_date : datetime(8)Class_code : Char(3)No_of_seats : int (8)Address : Varchar (50)Contact_No: Numeric (9) --> Should not be less than 9 and Should not accept any other character other than IntegerStatus: Char (2) : Yes / No

To Create Table

CREATE TABLE Reservation(pnr_no numeric(9),aircraft_code varchar(5),journey_date date,class_code char(3),no_of_seats int(8),address varchar(50),contact_no int(20),status set('yes','no') not null,foreign key(pnr_no) references passenger(pnr_no) on delete cascade on update no action,foreign key(aircraft_code) references flight(aircraft_code)on delete cascade on update no action);

To Modify

ALTER TABLE Reservation MODIFY journey_date date;

To Insert Data into Table

INSERT INTO Reservation VALUES (10111001,'jt1', '2012-01-05','f',14, 'hyderabad', 916023854,'yes'),( 10111002,'kf1','2011-03-16','ec',9,'delhi',994881493, 'no'),( 10111003,'sj1','2010-09-28','ex',12,'mumbai',994851548,'yes'),( 10111004,'in1','2012-03-03','ec',13,'warangal',964278077,'no'), (10111006,'jt1','2006-02-12','f',16, 'hyderabad', 994256321,'yes'), (10111003,'IC0F2','2012-01-05','M',18, 'chennai', 994456821,'yes');

B Mahesh Kumar Yadav Page 6

Page 7: 10111D0003_DD11_W2_M7_M8

SQL Queries

1. Find the complete details of the passengers whose passport number is “F14032”

Ans) SELECT * from Passenger WHERE ppno=’F14032’;

+----------+-----------+-------+------+------+--------+----------------+

| pnr_no | ticket_no | name | age | sex | ppno | meal_pref |

+----------+-----------+-------+------+------+--------+----------------+

| 10111006 | 128706 | XYZ| 44 | male | F14032 | chicken burger |

+----------+-----------+-------+------+------+--------+----------------+

2. Find the passport number of the passenger whose name is “XYZ” and traveled on 12th February 2006

Ans) SELECT ppno from Passenger WHERE name=’XYZ’ AND pnr_no in (SELECT pnr_no from Reservation WHERE journey_date='2006-02-12');

(or)

SELECT ppno from Passenger JOIN Reservation on (passenger.pnr_no= reservation.pnr_no) WHERE journey_date='2006-02-12' AND name='XYZ';

(or)

SELECT ppno from Passenger,Reservation WHERE passenger.pnr_no= reservation.pnr_no AND journey_date='2006-02-12' AND name='XYZ';

(or)

SELECT ppno from Passenger P,Reservation R WHERE P.pnr_no= R.pnr_no AND journey_date='2006-02-12' AND name='XYZ';

+--------+

| ppno |

+--------+

| F14032 |

+--------+

B Mahesh Kumar Yadav Page 7

Page 8: 10111D0003_DD11_W2_M7_M8

3. Find the names of passengers whose age is between 30 and 45

Ans) SELECT name, age from Passenger WHERE age>30 AND age<45;

+---------------+------+

| name | age |

+---------------+------+

| prince mahesh | 33 |

| XYZ | 44 |

+---------------+------+

4. Display all the details of the flights which are traveling on “Sunday”

Ans) SELECT F.aircraft_code,F.class_code,F.fare,F.seats,FD.day_code, DM.day_name from Flight_Details F,Flight_days FD, Day_Master DM WHERE F.aircraft_code=FD.aircraft_code AND FD.day_code=DM.day_code AND day_name='sunday';

+---------------+------------+------+-------+----------+----------+

| aircraft_code | class_code | fare | seats | day_code | day_name |

+---------------+------------+------+-------+----------+----------+

| jt1 | eco | 1000 | 75 | 1 | sunday |

| jt1 | exe | 3000 | 40 | 1 | sunday |

+---------------+------------+------+-------+----------+----------+

5. Display the fares of all the classes whose source is “DELHI” and destination is “HYD”

Ans) SELECT CM.class_name ,FD.fare from Flight_Details FD, Class_Master CM, Flight F WHERE FD.class_code=CM.class_code AND FD.aircraft_code=F.aircraft_code AND F.source='delhi' AND F.destination='hyd';

+------------+------+

| class_name | fare |

+------------+------+

B Mahesh Kumar Yadav Page 8

Page 9: 10111D0003_DD11_W2_M7_M8

| first | 5000 |

+------------+------+

6. Display the services provided by the flight whose code is “IC0F2”

Ans) SELECT S.service_name from Service S, Airlines_Service A, Flight F WHERE S.service_code=A.service_code AND F.aircode=A.aircode AND F.aircraft_code='IC0F2';

(OR)

SELECT service_name from Service WHERE service_code in(SELECT service_code from Airlines_Service WHERE aircode in (SELECT aircode from Flight WHERE aircraft_code='IC0F2'));

+--------------+

| service_name |

+--------------+

| wheel chair |

+--------------+

7. Display total number of seats reserved for the class “F”

Ans) SELECT SUM(No_of_seats) from Reservation WHERE class_code=’F’;

+------------------+

| SUM(No_of_seats) |

+------------------+

| 30 |

+------------------+

8. Display the details of the flight having maximum number of seats

Ans) SELECT F.*,FD.fare,FD.seats from Flight_Details FD, Flight F WHERE F.Aircraft_code=FD.Aircraft_code HAVING MAX(FD.seats) ;

(OR)

B Mahesh Kumar Yadav Page 9

Page 10: 10111D0003_DD11_W2_M7_M8

SELECT F.*,FD.fare,FD.seats from Flight_Details FD, Flight F WHERE F.Aircraft_code=FD.Aircraft_code AND seats in(Select MAX(Seats) from Flight_Details);

+---------------+---------+-------+---------+-------------+----------+----------

+-------------+------+-------+

| Aircraft_code | Aircode | Type | Source | Destination | Category | Dep_time

| Journey_Hrs | fare | seats |

+---------------+---------+-------+---------+-------------+----------+----------

+-------------+------+-------+

| jt1 | jt | daily | chennai | sydney | platinum | 9:00 am

| 11 | 1000 | 75 |

+---------------+---------+-------+---------+-------------+----------+----------

+-------------+------+-------+

9. Display the type of meals provided by JET Airways

Ans) SELECT M.Meal_Name from Meal M WHERE M.Meal_Code in(SELECT AM.Meal_Code from Airline_Meals AM WHERE AM.Aircode in(SELECT A.Aircode from Airlines_Master A WHERE A.Airline_Name='JET'));

(OR)

SELECT M.Meal_Name from Meal M, Airline_Meals AM, Airlines_Master A WHERE M.Meal_Code=AM.Meal_Code AND AM.Aircode=A.Aircode AND A.Airline_Name='JET';

+----------------+

| Meal_Name |

+----------------+

| chicken burger |

+----------------+

B Mahesh Kumar Yadav Page 10

Page 11: 10111D0003_DD11_W2_M7_M8

10. Display passenger details who traveled between 1st March to 30th March

Ans) SELECT * from Passenger P WHERE P.pnr_no in(SELECT R.pnr_no from Reservation R WHERE journey_date>'2011-03-1' AND journey_date<'2011-03-30');

(OR)

SELECT P.* from Passenger P,Reservation R WHERE P.pnr_no=R.pnr_no AND R.journey_date>'2011-03-1' AND R.journey_date<'2011-03-30';

+----------+-----------+------+------+------+--------+-----------+

| pnr_no | ticket_no | name | age | sex | ppno | meal_pref |

+----------+-----------+------+------+------+--------+-----------+

| 10111002 | 128702 | ntr | 29 | male | Pn0202 | hamburger |

+----------+-----------+------+------+------+--------+-----------+

11. Find the passenger names who travelled on 5th january,2012

Ans) SELECT name from Passenger WHERE pnr_no in(SELECT pnr_no from Reservation WHERE journey_date='2012-01-05');

(OR)

SELECT P.name from Passenger P, Reservation R WHERE P.pnr_no=R.pnr_no AND R.journey_date='2012-01-05';

+---------------+

| name |

+---------------+

| prince mahesh |

+---------------+

12. Find the total number of passengers travelled in "IC0F2"

Ans) SELECT COUNT(pnr_no) from Passenger WHERE pnr_no in(SELECT pnr_no from Reservation WHERE aircraft_code='IC0F2');

(OR)

B Mahesh Kumar Yadav Page 11

Page 12: 10111D0003_DD11_W2_M7_M8

SELECT COUNT(P.pnr_no) from Passenger P, Reservation R WHERE P.pnr_no=R.pnr_no AND R.aircraft_code='IC0F2';

+---------------+

| COUNT(pnr_no) |

+---------------+

| 1 |

+---------------+

13. Find the source and destination of flights whose journey hours is less than 5hrs.

Ans) SELECT source,destination from Flight WHERE journey_hrs<5;

+--------+-------------+

| source | destination |

+--------+-------------+

| delhi | hyd |

+--------+-------------+

14. Display the contact numbers of passengers whose reservation status is 'No'.

Ans) SELECT contact_no from Reservation WHERE Status='No';

+------------+

| contact_no |

+------------+

| 994881493 |

| 964278077 |

+------------+

15. Display the service names available in kingfisher airways.

Ans) SELECT S.service_name from Service S join Airlines_Service A on A.service_code=S.service_code join Airlines_Master AM on AM.aircode=A.aircode WHERE AM.airline_name='kingfisher';

B Mahesh Kumar Yadav Page 12

Page 13: 10111D0003_DD11_W2_M7_M8

(OR)

SELECT S.service_name from Service S,Airlines_Service A, Airlines_Master AM WHERE A.service_code=S.service_code AND AM.aircode=A.aircode AND AM.airline_name='kingfisher';

(OR)

SELECT service_name from Service WHERE service_code in( SELECT service_code from Airlines_Service WHERE aircode in( SELECT aircode from Airlines_Master WHERE airline_name='kingfisher'));

+--------------+

| service_name |

+--------------+

| chlid care |

+--------------+

B Mahesh Kumar Yadav Page 13