Add CT3531 Database Systems II

This commit is contained in:
2023-12-07 01:44:01 +00:00
parent 7b2a29c42d
commit cf8beab504
95 changed files with 2485 additions and 0 deletions

View File

@ -0,0 +1,82 @@
CREATE TABLE teams (
team_name VARCHAR(255) NOT NULL, -- assuming team name is unique
home_venue VARCHAR(255) NOT NULL, -- assuming that two teams could share a home venue (e.g, A teams and B teams)
manager VARCHAR(255), -- assuming no other information is known/required for managers other than name
PRIMARY KEY (team_name)
);
CREATE TABLE players (
player_id INT NOT NULL AUTO_INCREMENT,
team_name VARCHAR(255) NOT NULL,
squad_number INT NOT NULL, -- assuming squad number is unique within squads
player_name VARCHAR(255) NOT NULL,
PRIMARY KEY (player_id),
FOREIGN KEY (team_name) REFERENCES teams(team_name)
);
CREATE TABLE games (
game_id INT NOT NULL AUTO_INCREMENT, -- no other uniquely identifying information about a game (there could be two otherwise identical games)
home_team VARCHAR(255) NOT NULL, -- venue can be inferred from this
away_team VARCHAR(255) NOT NULL,
path_to_heap_file VARCHAR(255),
PRIMARY KEY (game_id),
FOREIGN KEY (home_team) REFERENCES teams(team_name),
FOREIGN KEY (away_team) REFERENCES teams(team_name)
);
-- this table should only be inserted into upon the completion of a game
CREATE TABLE results (
game_id INT NOT NULL,
winner VARCHAR(255), -- NULL value indicates a draw
PRIMARY KEY (game_id),
FOREIGN KEY (winner) REFERENCES teams(team_name)
);
-- data redundancy here: players rarely change during match
CREATE TABLE active_players (
active_time TIMESTAMP NOT NULL,
game_id INT NOT NULL,
player_id INT NOT NULL,
PRIMARY KEY (active_time, player_id),
FOREIGN KEY (game_id) REFERENCES games(game_id),
FOREIGN KEY (player_id) REFERENCES players(player_id)
);
CREATE TABLE substitutions (
substitution_time TIMESTAMP NOT NULL,
game_id INT NOT NULL,
off_player_id INT NOT NULL,
on_player_id INT NOT NULL,
PRIMARY KEY (substitution_time, off_player_id), -- assuming two substitutions could be done at the same time
FOREIGN KEY (game_id) REFERENCES games(game_id),
FOREIGN KEY (off_player_id) REFERENCES players(player_id),
FOREIGN KEY (on_player_id) REFERENCES players(player_id)
);
CREATE TABLE sendoffs (
sendoff_time TIMESTAMP NOT NULL,
game_id INT NOT NULL,
player_id INT NOT NULL,
PRIMARY KEY (sendoff_time, player_id), -- assuming two sendoffs could be done at the same time - otherwise could just use time
FOREIGN KEY (game_id) REFERENCES games(game_id),
FOREIGN KEY (player_id) REFERENCES players(player_id)
);
CREATE TABLE goals (
goal_time TIMESTAMP NOT NULL, -- assuming two goals can't be scored at the same time
game_id INT NOT NULL,
player_id INT NOT NULL, -- cam infer squad number from this
benefitting_team VARCHAR(255) NOT NULL, -- the team to which the points are awarded for this goal
PRIMARY KEY (goal_time, game_id),
FOREIGN KEY (game_id) REFERENCES games(game_id),
FOREIGN KEY (player_id) REFERENCES players(player_id),
FOREIGN KEY (benefitting_team) REFERENCES teams(team_name)
);

View File

@ -0,0 +1,36 @@
-- 1. List all players playing for a given team
-- SELECT * FROM active_players JOIN players ON active_players.player_id = players.player_id WHERE team_name = "<insert team name here>" AND time = CURRENT_TIMESTAMP;
SELECT player_name FROM players WHERE team_name = "man u";
-- 2. List all players who have scored in a given game
SELECT player_id FROM goals WHERE game_id = "<insert game ID here>";
SELECT player_name FROM goals JOIN players ON goals.player_id = players.player_id WHERE game_id = 1;
-- 3. List the top five goal scorers in the league
SELECT player_id, COUNT(*) as goals_scored FROM goals GROUP BY player_id ORDER BY goals_scored DESC LIMIT 5;
SELECT player_id, COUNT(*) as goals_scored FROM goals GROUP BY player_id ORDER BY goals_scored DESC LIMIT 5;
SELECT
player_name, COUNT(*) as goals_scored
FROM
goals INNER JOIN players ON goals.player_id = players.player_id
GROUP BY players.player_id
ORDER BY goals_scored DESC
LIMIT 5;
-- 4. List all teams and the amounts of points that they have so far
-- drawback: adding up each time, costly if this is a common query
SELECT
teams.team_name,
SUM(
CASE
WHEN results.winner = teams.team_name THEN 3
WHEN results.winner IS NULL THEN 1
ELSE 0
END
) AS total_points
FROM
teams
LEFT JOIN games ON teams.team_name = games.home_team OR teams.team_name = games.away_team
LEFT JOIN results ON games.game_id = results.game_id
GROUP BY
teams.team_name;