-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpart4_procedures_triggers.sql
More file actions
191 lines (150 loc) · 6.18 KB
/
Copy pathpart4_procedures_triggers.sql
File metadata and controls
191 lines (150 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
-- ---------------------- SUMMARY ---------------------------------------------------
-- 2 procedures
-- GetTeamSummary(2); -- returns summary for team India
-- GetTopScoringInnings('T20', 3); -- returns top 3 T20 innings
--
-- 2 triggers
-- after_wins_insert -> creates and stores a win summary
-- before_participates_insert -> makes sure no more than 2 teams for each match
-- ----------------------------------------------------------------------------------
-- STORED PROCEDURE 1: GetTeamSummary
-- Takes a team_id, returns matches played, wins, losses, draws
-- Example usage: CALL GetTeamSummary(2); -- returns summary for India
DELIMITER $$
CREATE PROCEDURE GetTeamSummary(IN p_team_id INT)
BEGIN
-- variables to hold count
DECLARE v_team_name VARCHAR(100);
DECLARE v_played INT DEFAULT 0;
DECLARE v_wins INT DEFAULT 0;
DECLARE v_losses INT DEFAULT 0;
DECLARE v_draws INT DEFAULT 0;
-- display team
SELECT team_name INTO v_team_name
FROM Team
WHERE team_id = p_team_id;
-- if team doesnt exist, then dont do anything else
IF v_team_name IS NULL THEN
SELECT 'Error: Team not found' AS message;
ELSE
-- total matches the team participated in
SELECT COUNT(*) INTO v_played
FROM Participates_In
WHERE team_id = p_team_id;
-- matches the team won
SELECT COUNT(*) INTO v_wins
FROM Wins
WHERE team_id = p_team_id;
-- draws -> matches team played in but no result in Wins table
-- A draw means no team won, so match_id not in Wins at all
SELECT COUNT(*) INTO v_draws
FROM Participates_In pi
WHERE pi.team_id = p_team_id
AND pi.match_id NOT IN (
SELECT match_id FROM Wins
);
-- losses -> totalMatches - total_wins - total_draws
SET v_losses = v_played - v_wins - v_draws;
-- display summary
SELECT
v_team_name AS team_name,
v_played AS matches_played,
v_wins AS wins,
v_losses AS losses,
v_draws AS draws,
ROUND((v_wins / v_played) * 100, 2) AS win_percentage;
END IF;
END$$
DELIMITER ;
-- -----------------------------------------------------------------------------------------------------------------------------------------
-- STORED PROCEDURE 2: GetTopScoringInnings
-- Takes a format (T20/ODI/Test) and a number N,
-- returns the top N highest scoring innings for that format
-- Example usage:
-- CALL GetTopScoringInnings('ODI', 5); -- top 5 ODI innings
-- CALL GetTopScoringInnings('T20', 3); -- top 3 T20 innings
DELIMITER $$
CREATE PROCEDURE GetTopScoringInnings(
IN p_format VARCHAR(10),
IN p_n INT
)
BEGIN
-- Validate format input using IF-ELSE
IF p_format NOT IN ('T20', 'ODI', 'Test') THEN
SELECT 'Error: format must be T20, ODI or Test' AS message;
ELSEIF p_n <= 0 THEN
SELECT 'Error: N must be a positive integer' AS message;
ELSE
SELECT
tr.name AS tournament,
m.match_date AS match_date,
t.team_name AS team_name,
i.total_runs AS total_runs,
i.total_wickets AS total_wickets,
i.total_overs AS total_overs,
i.total_sixes AS total_sixes
FROM Innings i
JOIN `Match` m ON i.match_id = m.match_id
JOIN Tournament tr ON m.tournament_id = tr.tournament_id
JOIN Team t ON i.team_id = t.team_id
WHERE tr.format = p_format
ORDER BY i.total_runs DESC -- enables ranking
LIMIT p_n; -- prints only the top specified rank
END IF;
END$$
DELIMITER ;
-- --------------------------------------------------------------------------------------------------------------------------------------
-- TRIGGER 1: AFTER INSERT on Wins
-- Automatically updates result_summary in Match to reflect winner
-- Example: inserting a new win will auto-update Match
-- INSERT INTO Wins VALUES (11, 3, 4, 'Wickets');
-- Then check: SELECT result_summary FROM `Match` WHERE match_id = 11;
-- Should now read: 'England won by 4 Wickets'
DELIMITER $$
CREATE TRIGGER after_wins_insert
AFTER INSERT ON Wins
FOR EACH ROW
BEGIN
-- Declare a variable to hold the winning team name
DECLARE v_winner_name VARCHAR(100);
DECLARE v_summary VARCHAR(255);
-- Look up the winning team name using the inserted team_id
SELECT team_name INTO v_winner_name
FROM Team
WHERE team_id = NEW.team_id;
-- Build the result summary string
SET v_summary = CONCAT(
v_winner_name, ' won by ',
NEW.win_margin, ' ',
NEW.win_type
);
-- Update the result_summary in the Match table
UPDATE `Match`
SET result_summary = v_summary
WHERE match_id = NEW.match_id;
END$$
DELIMITER ;
-- --------------------------------------------------------------------------------------------------------------------------------------
-- TRIGGER 2: BEFORE INSERT on Participates_In
-- Prevents more than 2 teams being added to the same match
-- Example: this should fail since match 1 already has 2 teams
-- INSERT INTO Participates_In VALUES (3, 1, 'Neutral');
-- Should return: Error: A match cannot have more than 2 teams
DELIMITER $$
CREATE TRIGGER before_participates_insert
BEFORE INSERT ON Participates_In
FOR EACH ROW
BEGIN
-- Variable to hold current team count for this match
DECLARE v_team_count INT DEFAULT 0;
-- Count how many teams are already registered for this match
SELECT COUNT(*) INTO v_team_count
FROM Participates_In
WHERE match_id = NEW.match_id;
-- If 2 teams already exist, block the insert with an error
IF v_team_count >= 2 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Error: A match cannot have more than 2 teams';
END IF;
END$$
DELIMITER ;