-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQL
54 lines (45 loc) · 1.76 KB
/
SQL
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
1. Table Creation
CREATE TABLE Tournament (
Tournament_ID Float(10) Not Null,
Tournament_name Varchar(20) Not Null,
Start_date Date Not NUll,
End_date Date Not Null,
Primary key (Tournament_ID)
);
2. Data Insertion
INSERT ALL
INTO TournamentSport (Tournament_IDSport_ID) VALUES ('0001''01')
INTO TournamentSport (Tournament_IDSport_ID) VALUES ('0001''02')
INTO TournamentSport (Tournament_IDSport_ID) VALUES ('0001''03')
SELECT * FROM dual;
3. Views
CREATE VIEW TournamentScheduleView AS
SELECT t.Tournament_Name, s.Sport_Name, sch.Venue, sch.Date_Time
FROM Tournament t
JOIN Schedule sch ON t.Tournament_ID = sch.Tournament_ID
JOIN Sport s ON s.Sport_ID = sch.Sport_ID;
4. Stored Procedures
CREATE OR REPLACE PROCEDURE CalculatePlayerStatistics(
p_player_id IN PlayerStatistics.Player_ID%TYPE,
p_sport_id IN PlayerStatistics.Sport_ID%TYPE
) AS
BEGIN
UPDATE PlayerStatistics
SET Goal_Scored = (SELECT SUM(Score) FROM Game WHERE Team_ID IN (SELECT Team_ID FROM Team WHERE Sport_ID = p_sport_id) AND Player_ID = p_player_id),
Points_Scored = (SELECT SUM(Score) FROM Game WHERE Team_ID IN (SELECT Team_ID FROM Team WHERE Sport_ID = p_sport_id) AND Player_ID = p_player_id),
Assist = (SELECT COUNT(*) FROM Game WHERE Team_ID IN (SELECT Team_ID FROM Team WHERE Sport_ID = p_sport_id) AND Player_ID = p_player_id)
WHERE Player_ID = p_player_id AND Sport_ID = p_sport_id;
COMMIT;
END;
5. Triggers
CREATE OR REPLACE TRIGGER UpdatePlayerStatisticsTrigger
AFTER INSERT ON Game
FOR EACH ROW
BEGIN
UPDATE PlayerStatistics
SET Goal_Scored = Goal_Scored + :NEW.Score,
Points_Scored = Points_Scored + :NEW.Score,
Sets_Won = Sets_Won + :NEW.Score,
Assist = Assist + :NEW.Assist
WHERE Player_ID = :NEW.Player_ID AND Sport_ID = :NEW.Sport_ID;
END;