-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
52 lines (46 loc) · 1.35 KB
/
schema.sql
File metadata and controls
52 lines (46 loc) · 1.35 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
51
52
-- In this SQL file, write (and comment!) the schema of your database, including the CREATE TABLE, CREATE INDEX, CREATE VIEW, etc. statements that compose it
-- Drop existing tables if they exist
DROP TABLE IF EXISTS Results;
DROP TABLE IF EXISTS Predictions;
DROP TABLE IF EXISTS Drivers;
DROP TABLE IF EXISTS Teams;
DROP TABLE IF EXISTS Races;
-- Create Drivers table
CREATE TABLE Drivers (
driver_id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
nationality TEXT NOT NULL,
team_id INTEGER,
FOREIGN KEY (team_id) REFERENCES Teams(team_id)
);
-- Create Teams table
CREATE TABLE Teams (
team_id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
headquarters TEXT
);
-- Create Races table
CREATE TABLE Races (
race_id INTEGER PRIMARY KEY,
location TEXT NOT NULL,
date DATE NOT NULL
);
-- Create Results table
CREATE TABLE Results (
result_id INTEGER PRIMARY KEY,
race_id INTEGER,
driver_id INTEGER,
position INTEGER,
points INTEGER,
FOREIGN KEY (race_id) REFERENCES Races(race_id),
FOREIGN KEY (driver_id) REFERENCES Drivers(driver_id)
);
-- Create Predictions table
CREATE TABLE Predictions (
prediction_id INTEGER PRIMARY KEY,
race_id INTEGER,
driver_id INTEGER,
predicted_position INTEGER,
FOREIGN KEY (race_id) REFERENCES Races(race_id),
FOREIGN KEY (driver_id) REFERENCES Drivers(driver_id)
);