Skip to content

Commit 82e4651

Browse files
Small fixes on the last procedure
1 parent 243b09b commit 82e4651

File tree

5 files changed

+72
-43
lines changed

5 files changed

+72
-43
lines changed

src/AltersAndTriggersOfFlightsDatabase/TriggersForFlightsDatabase.sql

+8-6
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ AS
88
BEGIN
99
DECLARE @flight_date date;
1010
DECLARE @order_date date;
11-
SET @flight_date = (SELECT f.flight_date FROM Flights f
11+
SET @flight_date = (SELECT f.flight_date
12+
FROM Flights f
1213
JOIN INSERTED i
13-
ON i.flight_number = f.flight_number);
14+
ON i.flight_number = f.flight_number);
1415
SELECT @order_date = i.order_date FROM INSERTED i;
1516

1617
IF @order_date > @flight_date
@@ -23,7 +24,7 @@ ON Bookings
2324
FOR INSERT
2425
AS
2526
BEGIN
26-
DECLARE @fclass integer;
27+
DECLARE @fclass integer;
2728
DECLARE @flight_number integer;
2829
DECLARE @price decimal(12, 2);
2930
DECLARE @occ_econ_cap integer;
@@ -57,7 +58,7 @@ ON Bookings
5758
FOR DELETE
5859
AS
5960
BEGIN
60-
DECLARE @fclass integer;
61+
DECLARE @fclass integer;
6162
DECLARE @flight_number integer;
6263
DECLARE @price decimal(12, 2);
6364
DECLARE @occ_econ_cap integer;
@@ -85,6 +86,7 @@ BEGIN
8586
WHERE flight_number = @flight_number;
8687
END
8788
GO
89+
8890
-- Tests for the first trigger that verifies order_date is before flight_date
8991
INSERT INTO Bookings
9092
VALUES ('AA', 1000, 201, 4020, '2012-05-20', 'Petar Petkov', 1);
@@ -129,7 +131,7 @@ GO
129131
-- Views
130132
CREATE VIEW v_Flights_Schedule AS
131133
SELECT
132-
a.name,
134+
a.name,
133135
f.flight_number ,
134136
f.flight_date,
135137
f.price,
@@ -145,7 +147,7 @@ GO
145147

146148
CREATE VIEW v_Company_Paymentsum AS
147149
SELECT
148-
a.name,
150+
a.name,
149151
COUNT(DISTINCT f.flight_number) as flights_which_have_reservations,
150152
SUM(f.paymentsum) as sum_of_all_paymentsums
151153
FROM

src/CreationOfFlightsDatabase/CreateFlightsDatabase.sql

+3-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ CREATE TABLE Bookings (
6363
booking_number integer check (booking_number BETWEEN 10000000 AND 99999999)
6464
PRIMARY KEY,
6565
customer_number integer check (customer_number BETWEEN 10000000 AND 99999999)
66-
NOT NULL,
66+
NOT NULL,
6767
order_date date NOT NULL
6868
);
6969
GO
@@ -75,7 +75,8 @@ AS
7575
BEGIN
7676
DECLARE @flight_date date;
7777
DECLARE @order_date date;
78-
SET @flight_date = (SELECT f.flight_date FROM Flights f
78+
SET @flight_date = (SELECT f.flight_date
79+
FROM Flights f
7980
JOIN INSERTED i
8081
ON i.flight_number = f.flight_number);
8182
SELECT @order_date = i.order_date FROM INSERTED i;

src/CreationOfFlightsDatabase/CreationOfTheTables/CreateBookingsTable.sql

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ CREATE TABLE Bookings (
88
booking_number integer check (booking_number BETWEEN 10000000 AND 99999999)
99
PRIMARY KEY,
1010
customer_number integer check (customer_number BETWEEN 10000000 AND 99999999)
11-
NOT NULL,
11+
NOT NULL,
1212
order_date date NOT NULL
1313
);
1414
GO
@@ -20,7 +20,8 @@ AS
2020
BEGIN
2121
DECLARE @flight_date date;
2222
DECLARE @order_date date;
23-
SET @flight_date = (SELECT f.flight_date FROM Flights f
23+
SET @flight_date = (SELECT f.flight_date
24+
FROM Flights f
2425
JOIN INSERTED i
2526
ON i.flight_number = f.flight_number);
2627
SELECT @order_date = i.order_date FROM INSERTED i;

src/FunctionsAndViewsForFlightsDatabase/FunctionsAndViews.sql

+9-4
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ RETURN
1515
s.arrv_time
1616
FROM
1717
Flights f
18-
INNER JOIN Schedules s ON s.flight_number = f.flight_number
18+
INNER JOIN
19+
Schedules s ON s.flight_number = f.flight_number
1920
WHERE f.flight_date = @flight_date
2021
);
2122
GO
2223

24+
SELECT * FROM dbo.ScheduleForAGivenFlight('2012-06-20')
25+
2326
DROP FUNCTION dbo.ScheduleForAGivenFlight
2427
GO
2528

@@ -74,7 +77,7 @@ CREATE VIEW v_info_about_airlines_company AS
7477
FROM
7578
Airlines a
7679
INNER JOIN
77-
Flights f ON (a.code = f.airline_code)
80+
Flights f ON (a.code = f.airline_code)
7881
INNER JOIN
7982
(SELECT * FROM ScheduleForAGivenFlight(CAST(GETDATE() AS date))) AS s
8083
ON (f.flight_number = s.flight_number)
@@ -83,10 +86,12 @@ GO
8386
DROP VIEW v_info_about_airlines_company
8487
GO
8588

89+
SELECT * FROM dbov_info_about_airlines_company
90+
8691
-- TESTING THE VIEW
8792

8893
INSERT INTO FLIGHTS
89-
VALUES('DL', 1210, '2017-12-26', 427, 'USD', 385, 270, 52, 50, ((50*1.5 + 270)*427), 17);
94+
VALUES('DL', 1215, '2018-01-15', 427, 'USD', 385, 270, 52, 50, ((50*1.5 + 270)*427), 17);
9095

9196
INSERT INTO FLIGHTS
9297
VALUES('DL', 1211, '2017-12-30', 668, 'USD', 385, 300, 52, 0, (300*668), 17);
@@ -98,7 +103,7 @@ INSERT INTO FLIGHTS
98103
VALUES('DL', 1213, '2017-12-03', 669.94, 'USD', 385, 200, 52, 50, ((50*1.5 + 200)*669.94), 17);
99104

100105
INSERT INTO SCHEDULES
101-
VALUES('DL', 1210, 'US', 'NEW YORK', 'JFK', '12:00:00', 'US', 'SAN FRANSISCO', 'SFO', '15:03:00', 182, 2679);
106+
VALUES('DL', 1215, 'US', 'NEW YORK', 'JFK', '12:00:00', 'US', 'SAN FRANSISCO', 'SFO', '15:03:00', 182, 2679);
102107

103108
INSERT INTO SCHEDULES
104109
VALUES('DL', 1211, 'IT', 'ROME', 'FCO', '19:05:00', 'DE', 'FRANKFURT', 'FRA', '21:05:00', 125, 845);

src/ProceduresForFlightsDatabase/Procedures.sql

+49-29
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ GO
44
CREATE PROC Info_About_Flights_Of_AirlinesCompany @code char(2)
55
AS
66
SELECT
7-
a.name,
8-
f.flight_number,
7+
a.name,
8+
f.flight_number,
99
f.flight_date,
1010
s.dept_city,
1111
s.dept_time,
@@ -27,8 +27,10 @@ AS
2727
s.flight_time,
2828
s.distance
2929
FROM Airlines a
30-
INNER JOIN Flights f ON a.code = f.airline_code
31-
INNER JOIN Schedules s ON a.code = s.airline_code
30+
INNER JOIN
31+
Flights f ON a.code = f.airline_code
32+
INNER JOIN
33+
Schedules s ON a.code = s.airline_code
3234
WHERE a.code = @code
3335
GO
3436

@@ -38,51 +40,69 @@ GO
3840
CREATE PROC InfoForAirlineCompanyFlights @flight_date date, @dept_country char(2)
3941
AS
4042
SELECT
41-
a.name,
43+
a.name,
4244
f.*
4345
FROM Schedules s
44-
INNER JOIN Flights f ON f.flight_number = s.flight_number
45-
INNER JOIN Airlines a ON a.code = s.airline_code
46+
INNER JOIN
47+
Flights f ON f.flight_number = s.flight_number
48+
INNER JOIN
49+
Airlines a ON a.code = s.airline_code
4650
WHERE f.flight_date = @flight_date AND s.dept_country = @dept_country
4751
GO
4852

4953
EXEC InfoForAirlineCompanyFlights '2012-05-23', 'US'
5054
GO
5155

52-
CREATE FUNCTION GetCountOfReservations(@passenger_name varchar(100), @code char(2))
56+
CREATE FUNCTION GetCountOfReservations(@customer_name varchar(100), @code char(2))
5357
RETURNS integer
5458
AS
5559
BEGIN
5660
DECLARE @countOfReservations integer;
57-
SET @countOfReservations = (SELECT COUNT(b.customer_number)
58-
FROM Bookings b
59-
INNER JOIN Flights f ON b.airline_code = f.airline_code
61+
SET @countOfReservations = (SELECT COUNT(b.booking_number)
62+
FROM Bookings b , Flights f
6063
WHERE b.airline_code = @code
61-
AND b.customer_name = @passenger_name
62-
AND YEAR(f.flight_date) = 2012)
64+
AND b.customer_name = @customer_name
65+
AND YEAR(f.flight_date) = 2012
66+
AND b.flight_number = f.flight_number)
6367
RETURN @countOfReservations
6468
END;
6569
GO
6670

71+
DROP FUNCTION dbo.GetCountOfReservations
72+
GO
73+
74+
DROP PROCEDURE ResultSetProc
75+
GO
76+
77+
EXEC ResultSetProc 1000
78+
GO
79+
6780
CREATE PROC ResultSetProc @flight_number integer
6881
AS
69-
DECLARE @first_set table(passenger_name varchar(100));
70-
INSERT INTO @first_set SELECT b.customer_name
71-
FROM Bookings b
72-
WHERE b.flight_number = @flight_number
73-
DECLARE @second_set table(passenger_name varchar(100), airline_name varchar(20),
74-
bookings_count integer);
75-
INSERT INTO @second_set SELECT
76-
b.customer_name,
77-
a.name,
78-
dbo.GetCountOfReservations((SELECT fs.passenger_name FROM @first_set fs), a.code)
79-
FROM Bookings b
80-
INNER JOIN Airlines a ON b.airline_code = a.code
81-
WHERE b.flight_number = @flight_number
82-
AND dbo.GetCountOfReservations((SELECT fs.passenger_name FROM @first_set fs), a.code) >= 3
82+
SELECT DISTINCT(b.customer_name)
83+
FROM Bookings b
84+
WHERE b.flight_number = @flight_number
8385

84-
SELECT * FROM @second_set
86+
SELECT DISTINCT(b.customer_name), a.name, dbo.GetCountOfReservations(b.customer_name, a.code)
87+
FROM Bookings b, Airlines a
88+
WHERE b.flight_number = @flight_number AND b.airline_code = a.code
89+
--AND dbo.GetCountOfReservations(b.customer_number, a.code) >=3
8590
GO
8691

87-
EXEC ResultSetProc 1000
92+
DROP PROCEDURE ResultSetProc
8893
GO
94+
95+
EXEC ResultSetProc 1000 WITH RESULT SETS
96+
(
97+
(
98+
customer_name varchar(100)
99+
),
100+
(
101+
customer_name varchar(100),
102+
airline_name varchar(20),
103+
bookings_count integer
104+
)
105+
);
106+
107+
INSERT INTO dbo.Bookings VALUES ('AA', 1000, 203, 2873, '2012-03-20', 'Irene Barth', 1);
108+
INSERT INTO dbo.Bookings VALUES ('AA', 1000, 205, 2873, '2012-03-22', 'Irene Barth', 0);

0 commit comments

Comments
 (0)