-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinitial tables creation.sql
More file actions
50 lines (36 loc) · 1.06 KB
/
initial tables creation.sql
File metadata and controls
50 lines (36 loc) · 1.06 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
47
48
49
50
/* create database */
CREATE DATABASE worldcup
/* connect to database */
USE worldcup
/* create table 'game' to store the games information */
CREATE TABLE games (
game_id integer NOT NULL identity(1,1),
year integer NOT NULL,
round character varying(40) NOT NULL,
winner_id integer NOT NULL,
opponent_id integer NOT NULL,
winner_goals integer NOT NULL,
opponent_goals integer NOT NULL
);
/* create table 'teams' to store the teams information
includes winners and opponents*/
CREATE TABLE teams (
team_id integer NOT NULL identity(1,1),
name character varying(40) NOT NULL
);
/* add unique constraint on team names */
ALTER TABLE teams
ADD UNIQUE (name);
/* add primary key constraints */
ALTER TABLE games
ADD PRIMARY KEY (game_id);
ALTER TABLE teams
ADD PRIMARY KEY (team_id);
/* add foreign keys */
ALTER TABLE games
ADD FOREIGN KEY (opponent_id)
REFERENCES teams(team_id);
ALTER TABLE games
ADD FOREIGN KEY (winner_id)
REFERENCES teams(team_id);
/* data will be inserted by running the script insert_data.sh */