forked from JoonKai1995/task-sourcing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_creation.sql
55 lines (47 loc) · 1.59 KB
/
table_creation.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
CREATE TABLE users (
username VARCHAR(64) NOT NULL,
user_id NUMERIC UNIQUE NOT NULL,
password VARCHAR(64) NOT NULL,
PRIMARY KEY (username)
);
CREATE TABLE tasks (
task_id NUMERIC PRIMARY KEY NOT NULL,
task_description VARCHAR(255),
location VARCHAR(64),
task_date DATE NOT NULL,
task_time TIME NOT NULL,
task_allocated BOOLEAN DEFAULT FALSE,
task_completion BOOLEAN DEFAULT FALSE,
CONSTRAINT check_date_time CHECK(task_date > current_date OR
(task_date = current_date AND task_time >= current_time))
);
CREATE TABLE administrators (
admin_id VARCHAR(64) PRIMARY KEY NOT NULL,
username VARCHAR(64) NOT NULL,
password VARCHAR(64) NOT NULL
);
CREATE TABLE submits (
suser_ID NUMERIC NOT NULL,
stask_ID NUMERIC NOT NULL,
FOREIGN KEY (suser_ID) REFERENCES users(user_ID) ON DELETE CASCADE,
FOREIGN KEY (stask_ID) REFERENCES tasks(task_ID) ON DELETE CASCADE,
PRIMARY KEY (suser_ID, stask_ID)
);
CREATE TABLE bids (
bid_value NUMERIC NOT NULL,
buser_ID NUMERIC NOT NULL,
btask_ID NUMERIC NOT NULL,
suser_ID NUMERIC NOT NULL,
PRIMARY KEY (buser_ID, btask_ID),
FOREIGN KEY (suser_ID, btask_ID) REFERENCES submits(suser_ID, stask_ID) ON DELETE CASCADE,
FOREIGN KEY (buser_ID) REFERENCES users(user_ID) ON DELETE CASCADE,
CONSTRAINT buser_ID CHECK (buser_ID <> suser_ID)
);
CREATE TABLE allocated (
auser_ID NUMERIC NOT NULL,
atask_ID NUMERIC NOT NULL,
suser_ID NUMERIC NOT NULL,
PRIMARY KEY (auser_ID, atask_ID),
FOREIGN KEY (suser_ID, atask_ID) REFERENCES submits(suser_ID, stask_ID) ON DELETE CASCADE,
FOREIGN KEY (auser_ID, atask_ID) REFERENCES bids(buser_ID, btask_ID) ON DELETE CASCADE
);