]> git.sesse.net Git - ccbs/blob - ccbs.sql
74fa4394c2f77028a0b940ad765f43f696674a5b
[ccbs] / ccbs.sql
1 -- vim:set tw=0:
2
3 CREATE TABLE songs (
4         song SERIAL PRIMARY KEY,
5         title VARCHAR NOT NULL,
6         artist VARCHAR NOT NULL
7 );
8
9 CREATE TABLE songratings (
10         song INTEGER NOT NULL REFERENCES songs,
11         playmode VARCHAR NOT NULL CHECK (playmode IN ('single','double')),
12         difficulty VARCHAR NOT NULL CHECK (difficulty IN ('beginner','standard','difficult','expert','challenge')),
13         feetrating INTEGER NOT NULL CHECK (feetrating >= 0 AND feetrating <= 10),
14
15         PRIMARY KEY (song, playmode, difficulty)
16 );
17
18 CREATE TABLE players (
19         player SERIAL PRIMARY KEY,
20         nick VARCHAR NOT NULL
21 );
22
23 CREATE TABLE seasons (
24         season SERIAL PRIMARY KEY,
25         name VARCHAR NOT NULL
26 );
27
28 CREATE TABLE tournaments (
29         tournament SERIAL PRIMARY KEY,
30         season INTEGER NOT NULL REFERENCES seasons,
31         name VARCHAR NOT NULL
32 );
33
34 CREATE TABLE rounds (
35         tournament INTEGER NOT NULL REFERENCES tournaments,
36         round INTEGER NOT NULL,
37         randomsongs INTEGER NOT NULL,
38         chosensongs INTEGER NOT NULL,
39
40         PRIMARY KEY (tournament, round)
41 );
42
43 CREATE TABLE groups (
44         tournament INTEGER NOT NULL REFERENCES tournaments,
45         round INTEGER NOT NULL,
46         parallel INTEGER NOT NULL,
47
48         FOREIGN KEY (tournament, round) REFERENCES rounds (tournament, round),
49         PRIMARY KEY (tournament, round, parallel)
50 );
51
52 CREATE TABLE roundrandomsongs (
53         tournament INTEGER NOT NULL,
54         round INTEGER NOT NULL,
55         parallel INTEGER NOT NULL,
56         song INTEGER NOT NULL REFERENCES songs,
57
58         FOREIGN KEY (tournament, round, parallel) REFERENCES groups (tournament, round, parallel),
59         PRIMARY KEY (tournament, round, parallel, song)
60 );
61
62 CREATE TABLE roundparticipation (
63         tournament INTEGER NOT NULL,
64         round INTEGER NOT NULL,
65         parallel INTEGER NOT NULL,
66         player INTEGER NOT NULL REFERENCES players,
67
68         UNIQUE (tournament, round, player),
69         FOREIGN KEY (tournament, round, parallel) REFERENCES groups (tournament, round, parallel),
70         PRIMARY KEY (tournament, round, parallel, player)
71 );
72
73 CREATE TABLE scores (
74         tournament INTEGER NOT NULL,
75         round INTEGER NOT NULL,
76         parallel INTEGER NOT NULL,
77         player INTEGER NOT NULL REFERENCES players,
78         
79         song INTEGER NOT NULL REFERENCES songs,
80         playmode VARCHAR CHECK (playmode IS NULL OR playmode IN ('single','double')),
81         difficulty VARCHAR CHECK (difficulty IS NULL OR difficulty IN ('beginner','standard','difficult','expert','challenge')),
82         
83         chosen BOOLEAN NOT NULL,
84         score INTEGER NOT NULL CHECK (score >= 0 AND score <= 10000),
85         
86         FOREIGN KEY (song) REFERENCES songs (song),
87         FOREIGN KEY (song, playmode, difficulty) REFERENCES songratings (song, playmode, difficulty),
88         FOREIGN KEY (tournament, round, parallel, player) REFERENCES roundparticipation (tournament, round, parallel, player),
89         PRIMARY KEY (tournament, round, parallel, player, song)
90 );