-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTournamentTracker_DML.sql
More file actions
200 lines (165 loc) · 5.84 KB
/
Copy pathTournamentTracker_DML.sql
File metadata and controls
200 lines (165 loc) · 5.84 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
192
193
194
195
196
197
198
199
200
/* ===========================================================
TournamentTracker_DML.sql
Group 59 - Step 3 FINAL
Note: Variables are written as :varName
=========================================================== */
-- =========================================
-- PLAYERS
-- =========================================
-- READ all players
SELECT playerID, username, email, joinDate, rank
FROM Players
ORDER BY playerID ASC;
-- INSERT a new player
INSERT INTO Players (username, email, rank)
VALUES (:usernameInput, :emailInput, :rankInput);
-- UPDATE a player (blank values keep existing)
UPDATE Players
SET
username = IFNULL(NULLIF(:usernameInput, ''), username),
email = IFNULL(NULLIF(:emailInput, ''), email),
rank = IFNULL(NULLIF(:rankInput, ''), rank)
WHERE playerID = :playerID;
-- DELETE a player
DELETE FROM Players
WHERE playerID = :playerID;
-- =========================================
-- TOURNAMENTS
-- =========================================
-- READ all tournaments
SELECT tournamentID, title, game, startDate, endDate, maxPlayers
FROM Tournaments
ORDER BY tournamentID ASC;
-- INSERT a new tournament
INSERT INTO Tournaments (title, game, startDate, endDate, maxPlayers)
VALUES (:titleInput, :gameInput, :startDateInput, :endDateInput, :maxPlayersInput);
-- UPDATE a tournament
UPDATE Tournaments
SET
title = IFNULL(NULLIF(:titleInput, ''), title),
game = IFNULL(NULLIF(:gameInput, ''), game),
startDate = IFNULL(NULLIF(:startDateInput, ''), startDate),
endDate = IFNULL(NULLIF(:endDateInput, ''), endDate),
maxPlayers = IFNULL(NULLIF(:maxPlayersInput, ''), maxPlayers)
WHERE tournamentID = :tournamentID;
-- DELETE a tournament
DELETE FROM Tournaments
WHERE tournamentID = :tournamentID;
-- =========================================
-- REGISTRATIONS (M:M Players <-> Tournaments)
-- =========================================
-- READ with JOINs for user-friendly FK display
SELECT
r.registrationID,
r.registrationDate,
r.playerID,
p.username AS playerName,
r.tournamentID,
t.title AS tournamentTitle
FROM Registrations r
INNER JOIN Players p ON r.playerID = p.playerID
INNER JOIN Tournaments t ON r.tournamentID = t.tournamentID
ORDER BY r.registrationID ASC;
-- INSERT new registration
INSERT INTO Registrations (playerID, tournamentID)
VALUES (:playerID, :tournamentID);
-- UPDATE registration (change which player/tournament are linked)
UPDATE Registrations
SET
playerID = IFNULL(NULLIF(:playerID, ''), playerID),
tournamentID = IFNULL(NULLIF(:tournamentID, ''), tournamentID)
WHERE registrationID = :registrationID;
-- DELETE a registration (removes only the link in the M:M table)
DELETE FROM Registrations
WHERE registrationID = :registrationID;
-- =========================================
-- MATCHES
-- =========================================
-- READ matches with tournament info
SELECT
m.matchID,
m.tournamentID,
t.title AS tournamentTitle,
m.round,
m.matchDate,
m.status
FROM Matches m
INNER JOIN Tournaments t ON m.tournamentID = t.tournamentID
ORDER BY m.matchID ASC;
-- INSERT a new match
INSERT INTO Matches (tournamentID, round, matchDate, status)
VALUES (:tournamentID, :roundInput, :matchDateInput, :statusInput);
-- UPDATE a match
UPDATE Matches
SET
tournamentID = IFNULL(NULLIF(:tournamentID, ''), tournamentID),
round = IFNULL(NULLIF(:roundInput, ''), round),
matchDate = IFNULL(NULLIF(:matchDateInput, ''), matchDate),
status = IFNULL(NULLIF(:statusInput, ''), status)
WHERE matchID = :matchID;
-- DELETE a match
DELETE FROM Matches
WHERE matchID = :matchID;
-- =========================================
-- MATCH RESULTS
-- =========================================
-- READ results with player + match + tournament info
SELECT
r.resultID,
r.matchID,
r.playerID,
r.score,
r.result,
m.round,
m.matchDate,
t.title AS tournamentTitle,
p.username AS playerName
FROM MatchResults r
INNER JOIN Matches m ON r.matchID = m.matchID
INNER JOIN Tournaments t ON m.tournamentID = t.tournamentID
INNER JOIN Players p ON r.playerID = p.playerID
ORDER BY r.resultID ASC;
-- INSERT a match result
INSERT INTO MatchResults (matchID, playerID, score, result)
VALUES (:matchID, :playerID, :scoreInput, :resultInput);
-- UPDATE a match result
UPDATE MatchResults
SET
matchID = IFNULL(NULLIF(:matchID, ''), matchID),
playerID = IFNULL(NULLIF(:playerID, ''), playerID),
score = IFNULL(NULLIF(:scoreInput, ''), score),
result = IFNULL(NULLIF(:resultInput, ''), result)
WHERE resultID = :resultID;
-- DELETE a match result
DELETE FROM MatchResults
WHERE resultID = :resultID;
-- =========================================
-- LEADERBOARDS
-- =========================================
-- READ leaderboard entries with player + tournament info
SELECT
l.leaderboardID,
l.tournamentID,
l.playerID,
l.points,
l.placement,
t.title AS tournamentTitle,
p.username AS playerName
FROM Leaderboards l
INNER JOIN Tournaments t ON l.tournamentID = t.tournamentID
INNER JOIN Players p ON l.playerID = p.playerID
ORDER BY l.leaderboardID ASC;
-- INSERT leaderboard entry
INSERT INTO Leaderboards (tournamentID, playerID, points, placement)
VALUES (:tournamentID, :playerID, :pointsInput, :placementInput);
-- UPDATE leaderboard entry
UPDATE Leaderboards
SET
tournamentID = IFNULL(NULLIF(:tournamentID, ''), tournamentID),
playerID = IFNULL(NULLIF(:playerID, ''), playerID),
points = IFNULL(NULLIF(:pointsInput, ''), points),
placement = IFNULL(NULLIF(:placementInput, ''), placement)
WHERE leaderboardID = :leaderboardID;
-- DELETE leaderboard entry
DELETE FROM Leaderboards
WHERE leaderboardID = :leaderboardID;