0% found this document useful (0 votes)
88 views

Lab 8 Complex SQL Queries: Code

The document describes creating database schemas, tables, views, and triggers to model hotel bookings, with sample data inserted and a series of complex SQL queries demonstrated including aggregating revenues, counts of guests, room details, and unoccupied rooms. Triggers are implemented to log changes to an employee table to an audit table for auditing purposes.

Uploaded by

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

Lab 8 Complex SQL Queries: Code

The document describes creating database schemas, tables, views, and triggers to model hotel bookings, with sample data inserted and a series of complex SQL queries demonstrated including aggregating revenues, counts of guests, room details, and unoccupied rooms. Triggers are implemented to log changes to an employee table to an audit table for auditing purposes.

Uploaded by

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

Lab 8

Complex SQL Queries


Code:
-- Drop Previous Schema
drop schema hotelBooking1;

-- Create New Schema


create schema hotelBooking1;

use hotelBooking1;

-- Hotel Table
create Table hotel
(hotelno varchar(10) primary key,
hotelname varchar(20),
city varchar(20));

-- Room Table
create table room(
roomno numeric(5),
hotelno varchar(10),
type varchar(10),
price decimal(5,2),
primary key (roomno, hotelno),
foreign key (hotelno) references hotel(hotelno) on delete restrict on update cascade);

-- Guest Table
create table guest(
guestno numeric(5) primary key,
guestname varchar(20),
guestaddress varchar(50));

-- Booking Table
create table booking(
hotelno varchar(10),
guestno numeric(5),
datefrom datetime,
dateto datetime,
roomno numeric(5),
primary key (hotelno, guestno, datefrom),
foreign key (roomno, hotelno) references room(roomno, hotelno) on delete restrict on update cascade,
foreign key (guestno) references guest(guestno) on delete restrict on update cascade);

-- Populating Hotel Table


insert into hotel
values
('fb01', 'Avari', 'Lahore'),
('ch01', 'Serena', 'Islamabad'),
('dc01', 'Hotel Inn', 'Lahore'),
('fb02', 'Nishat', 'Lahore'),
('ch02', 'Marriott', 'Islamabad'),
('dc02', 'Shangrila Resort', 'Murree');

-- Populating Room Table


insert into room
values
(501, 'fb01', 'single', 19),
(601, 'fb01', 'double', 29),
(701, 'fb01', 'family', 39),
(1001, 'fb02', 'single', 58),
(1101, 'fb02', 'double', 86),
(1001, 'ch01', 'double', 29.99),
(1101, 'ch01', 'family', 59.99),
(401, 'ch02', 'double', 15),
(701, 'ch02', 'single', 10),
(801, 'ch02', 'double', 15),
(901, 'dc02', 'single', 18),
(1001, 'dc01', 'double', 30),
(1101, 'dc01', 'family', 35),
(1102, 'dc01', 'family', 25),
(1103, 'dc01', 'single', 15),
(1104, 'dc01', 'double', 40);

-- Populating Guest Table


insert into guest
values
(10001, 'John Kay', '56 High St, London'),
(10002, 'Mike Ritchie', '18 Tain St, London'),
(10003, 'Mary Tregear', '5 Tarbot Rd, Aberdeen'),
(10004, 'Joe Keogh', '2 Fergus Dr, Aberdeen'),
(10005, 'Carol Farrel', '6 Achray St, Glasgow'),
(10006, 'Tina Murphy', '63 Well St, Glasgow'),
(10007, 'Tony Shaw', '12 Park Pl, Glasgow');

-- Populating Booking Table


insert into booking
values
('fb01', 10001, '12-04-01', '12-04-08', 501),
('fb01', 10004, '14-08-01', '14-08-15', 601),
('fb01', 10005, '13-05-02', '13-05-07', 701),
('fb02', 10001, '14-08-01', null, 1101),
('fb02', 10003, '19-08-05', '19-08-10', 1001),
('ch01', 10006, '14-04-21', null, 1101),
('ch02', 10002, '20-04-10', '20-05-30', 801),
('ch02', 10007, '20-05-08', '20-08-30', 701),
('ch02', 10004, '20-05-04', '20-05-10', 401),
('dc02', 10001, '12-08-20', null, 901),
('dc01', 10004, '20-05-08', null, 1001),
('dc01', 10005, '20-05-08', null, 1101),
('dc01', 10006, '20-05-05', '20-05-06', 1102);

-- Printing the Four Tables


select 'Hotel Table' as '';
select * from hotel;
select 'Room Table' as '';
select * from room;
select 'Guest Table' as '';
select * from guest;
select 'Booking Table' as '';
select * from booking;

-- Query 1
select 'Query 1' as '';
select B.datefrom, sum(price) as total_revenue_per_night
from booking B natural join room R
where R.type = 'double'
group by B.datefrom;
-- Query 2
select 'Query 2' as '';
select count(distinct guestno) as no_of_Aug_guests
from booking B natural join guest G
where month(dateto) = 8 or month(datefrom) = 8;

-- Query 3
select 'Query 3' as '';
select R.type, R.price
from hotel H natural join room R
where H.hotelname = 'Avari';

-- Query 4
select 'Query 4' as '';
select G.guestno, G.guestname
from booking B natural join guest G natural join hotel H
where H.hotelname = 'Marriott' and (B.dateto >= date(sysdate()) or B.datefrom = date(sysdate()));
-- B.datefrom = sysdate() is written for those entries who did not confirm the last date of their
-- booking. This query is written with the assumption that no client can make any reservation for future
-- without staying at the hotel.

-- Query 5
select 'Query 5' as '';
select sum(R.price) as 'today\'s_earning_of_Hotel_Inn'
from booking B natural join hotel H natural join room R
where H.hotelname = 'Hotel Inn' and B.datefrom = date(sysdate());

-- Query 6
select 'Query 6' as '';
select R.roomno as 'unoccupied_rooms_in_Hotel_Inn'
from room R natural left join booking B natural join hotel H
where H.hotelname = 'Hotel Inn' and B.guestno is null;

-- Query 7
select 'Query 7' as '';
select sum(R.price) as 'lost_income_of_Hotel_Inn'
from room R natural left join booking B natural join hotel H
where H.hotelname = 'Hotel Inn' and B.guestno is null;

-- Query 8
select 'Query 8' as '';
select sum(price) as 'today\'s_lost_income_of_all_hotels'
from room R natural left join booking B
where
B.dateto <= date(sysdate()) -- bbokings with null dateto are considered to be still occupied
or (roomno, hotelno) IN
(select roomno, hotelno
from room R natural left join booking B natural join hotel H
where B.guestno is null);

-- Query 9
select 'Query 9' as '';
select sum(price) as 'lost_income_of_hotels_having_two_different_types_of_rooms'
from room R natural left join booking B
where B.guestno is null
and hotelno IN
(select hotelno
from room
group by hotelno
having count(distinct type) > 2);
-- Query 10
select 'Query 10' as '';
update room
set price = price * 0.02;
select *
from room;

Output:
Lab 9
Views and Triggers
Code:
select @@sql_mode;
set sql_mode = '';

use hotelbooking1;

-- Task 1
create view view1
as select R.roomno, R.type, R.price, G.guestname
from room R natural join booking B natural join hotel H natural join guest G
where H.hotelname = 'Marriott';

select 'Task 1' as '';


select *
from view1;

-- Task 2
create view view2
as select G.guestname, G.guestaddress
from guest G
where G.guestaddress like '%London'
order by G.guestname;

select 'Task 2' as'';


select *
from view2;
-- Task 3
create view view3
as select *
from booking B
where B.dateto is null;

select 'Task 3' as '';


select *
from view3;

-- Task 4
insert into view2
values('Taylor Swift', 'Kemp House, 152 City Road, London');

select 'Task 4' as '';


select *
from view2;

select 'Yes, New Data has updated the original table as well.' as '';

-- Task 5
update view1
set price = price * 0.02;

select 'Task 5' as '';


select *
from view1;

select 'Yes, update operation is performed on the original database as well.' as '';
-- Task 6
create table employee(
ssn varchar(9) primary key,
fname varchar(10),
lname varchar(10),
bdate date,
address varchar(50),
gender char(1),
salary int(6));

insert into employee


values
('123456789', 'John', 'Smith', '1965-01-09', '731 Fondren, Houston, TX', 'M', '30000'),
('999887777', 'Alicia', 'Zelaya', '1968-01-19', '3321 Castle, Spring, TX', 'F', '25000');

select 'Task 6' as '';


select 'Employee Table' as '';
select *
from employee;

create table employeeaudit(


id int auto_increment primary key,
ssn varchar(9),
fname varchar(10),
lname varchar(10),
bdate date,
address varchar(50),
gender char(1),
salary int(6),
changedate datetime,
action varchar(50));

create trigger before_emp_update


before update on employee
for each row
insert into employeeaudit
values(1, old.ssn, old.fname, old.lname, old.bdate, old.address, old.gender, old.salary,
sysdate(), 'update');

update employee
set lname = 'Cena'
where ssn = '123456789';

select 'Employee Table After Update' as '';


select *
from employee;

select 'EmployeeAudit Table After the Update of Employee Table' as '';


select *
from employeeaudit;
Output:

You might also like