-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_football.sql
136 lines (120 loc) · 3.41 KB
/
create_football.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
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
create schema football;
create table football.nationality
(
id bigint not null auto_increment,
name varchar(255) not null,
primary key (id)
);
create table football.country
(
id bigint not null auto_increment,
name varchar(255) not null,
primary key (id)
);
create table football.city
(
id bigint not null auto_increment,
name varchar(255) not null,
country_id bigint,
primary key (id),
foreign key (country_id) references football.country(id)
);
create table football.position
(
id bigint not null auto_increment,
name varchar(255) not null,
primary key (id)
);
create table football.league
(
id bigint not null auto_increment,
name varchar(255) not null,
primary key (id)
);
create table football.team
(
id bigint not null auto_increment,
name varchar(255) not null,
short_name varchar(255) not null,
arena_name varchar(255) not null,
coach_name varchar(255) not null,
city_id bigint,
league_id bigint,
primary key (id),
foreign key (city_id) references football.city(id)
);
create table football.player
(
id bigint not null auto_increment,
firstname varchar(255) not null,
lastname varchar(255) not null,
date_of_birth date,
place_of_birth_id bigint,
nationality_id bigint,
team_id bigint,
position_id bigint,
primary key (id),
foreign key (place_of_birth_id) references football.city(id),
foreign key (nationality_id) references football.nationality(id),
foreign key (team_id) references football.team(id),
foreign key (position_id) references football.position(id)
);
create table football.season
(
id bigint not null auto_increment,
name varchar(255) not null,
primary key (id)
);
create table football.game
(
id bigint not null auto_increment,
home_team_id bigint not null references football.team(id),
away_team_id bigint not null references football.team(id),
season_id bigint not null references football.season(id),
league_id bigint not null references football.league(id),
game_date date,
game_day tinyint,
primary key (id)
);
create table football.scores
(
id bigint not null auto_increment,
game_id bigint not null references football.game(id),
player_id bigint not null references football.player(id),
team_id bigint not null references football.team(id),
own_goal tinyint default 0,
minute tinyint,
primary key (id)
);
create table football.team_league_season
(
id bigint not null auto_increment,
team_id bigint not null references football.team(id),
league_id bigint not null references football.league(id),
season_id bigint not null references football.season(id),
primary key (id)
);
-- ######################## VIEWS ########################
create view football.scorerrank as
select s.player_id,
sum(case when s.own_goal=0 then 1 else 0 end) as scored_goals,
g.season_id,
g.league_id
from football.scores as s
inner join football.game as g
on s.game_id = g.id
group by s.player_id, g.season_id
order by scored_goals desc;
create view football.scores_for_game as
select
g.id as game_id,
g.season_id,
g.league_id,
g.home_team_id,
g.away_team_id,
sum(case when g.home_team_id=s.team_id then 1 else 0 end) as home_team_score,
sum(case when g.away_team_id = s.team_id then 1 else 0 end) as away_team_score
from football.game as g
inner join football.scores s
on s.game_id = g.id
group by g.id;