-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgfantacalcio--1.0.sql
105 lines (90 loc) · 2.67 KB
/
pgfantacalcio--1.0.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
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
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION pgfantacalcio" to load this file. \quit
CREATE TABLE leagues (
id INTEGER PRIMARY KEY,
code VARCHAR(6) NOT NULL UNIQUE,
name VARCHAR(256) NOT NULL,
url VARCHAR(256)
);
CREATE TABLE teams (
id INTEGER PRIMARY KEY,
country CHAR(2) NOT NULL,
code VARCHAR(6) NOT NULL,
name VARCHAR(256) NOT NULL,
city VARCHAR(64),
url VARCHAR(256),
UNIQUE (country, code)
);
CREATE TABLE players (
id INTEGER PRIMARY KEY,
name VARCHAR(256) NOT NULL,
dob DATE,
country CHAR(2)
);
CREATE TABLE roles (
id INTEGER PRIMARY KEY,
code VARCHAR(3) NOT NULL UNIQUE
);
CREATE TABLE matches (
league_id INTEGER NOT NULL REFERENCES leagues,
season SMALLINT NOT NULL, -- 2012
round SMALLINT NOT NULL,
seq SMALLINT NOT NULL,
home_team_id INTEGER NOT NULL REFERENCES teams,
away_team_id INTEGER NOT NULL REFERENCES teams,
kick_off_time TIMESTAMP,
is_played BOOLEAN NOT NULL DEFAULT FALSE,
home_goals SMALLINT,
away_goals SMALLINT,
PRIMARY KEY (league_id, season, round, seq),
-- Home team must be different from away team
CHECK (home_team_id <> away_team_id),
-- Check for results to be filled only if the
-- match has been played
CHECK ( ( is_played = TRUE AND
home_goals IS NOT NULL AND home_goals >= 0 AND
away_goals IS NOT NULL AND away_goals >= 0
) OR (
is_played = FALSE AND
home_goals IS NULL AND
away_goals IS NULL
) ),
-- Season > 2000
CHECK (season > 2000),
-- Round > 0
CHECK (round > 0),
-- Seq > 0
CHECK (seq > 0)
);
CREATE TABLE players_teams (
league_id INTEGER NOT NULL,
season SMALLINT NOT NULL, -- 2012
round SMALLINT NOT NULL,
player_id INTEGER NOT NULL REFERENCES players,
role_id INTEGER NOT NULL REFERENCES roles,
team_id INTEGER NOT NULL REFERENCES teams,
rating SMALLINT,
PRIMARY KEY (league_id, season, round, player_id)
);
CREATE VIEW v_players_teams AS
SELECT l.code AS league, season, round,
p.name AS player, r.code AS role, t.name AS team, rating
FROM players_teams pt JOIN leagues l ON (pt.league_id = l.id)
JOIN players p ON (pt.player_id = p.id)
JOIN roles r ON (pt.role_id = r.id)
JOIN teams t ON (pt.team_id = t.id);
CREATE VIEW v_matches AS
SELECT l.code AS league, season, round, seq,
th.name AS home_team,
ta.name AS away_team,
kick_off_time,
is_played,
home_goals,
away_goals
FROM matches m JOIN leagues l ON (m.league_id = l.id)
JOIN teams th ON (m.home_team_id = th.id)
JOIN teams ta ON (m.away_team_id = ta.id);
-- Objects for dump
SELECT pg_catalog.pg_extension_config_dump('roles', '');
SELECT pg_catalog.pg_extension_config_dump('leagues', '');
SELECT pg_catalog.pg_extension_config_dump('teams', '');