-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhangman.postgres.sql
More file actions
46 lines (39 loc) · 1.24 KB
/
hangman.postgres.sql
File metadata and controls
46 lines (39 loc) · 1.24 KB
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
-- In this schema, when a player initiates a new game, the computer would
-- pick one of the entries from the `phrases` table as the phrase-to-guess.
--
-- Every turn or guess would result in a new entry in the `turns` table,
-- containing the letter the player guessed for that turn.
CREATE TABLE users (
id SERIAL PRIMARY KEY,
email TEXT NOT NULL,
password_digest TEXT NOT NULL,
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL
);
ALTER TABLE users ADD UNIQUE (email);
CREATE TABLE phrases (
id SERIAL PRIMARY KEY,
body TEXT NOT NULL
);
CREATE TABLE games (
id SERIAL PRIMARY KEY,
player_id INTEGER NOT NULL,
phrase_id INTEGER NOT NULL,
guess_limit INTEGER NOT NULL,
completed_at TIMESTAMP,
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL,
FOREIGN KEY (player_id) REFERENCES users(id),
FOREIGN KEY (phrase_id) REFERENCES phrases(id),
CHECK(guess_limit > 0)
);
CREATE INDEX ON games(player_id);
CREATE TABLE turns (
id SERIAL PRIMARY KEY,
game_id INTEGER NOT NULL,
letter_guessed CHAR(1) NOT NULL,
created_at TIMESTAMP NOT NULL,
FOREIGN KEY (game_id) REFERENCES games(id)
);
-- For a given game, a player can't guess the same letter twice.
ALTER TABLE turns ADD UNIQUE (game_id, letter_guessed);