-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDDLscript.txt
More file actions
190 lines (166 loc) · 4.84 KB
/
Copy pathDDLscript.txt
File metadata and controls
190 lines (166 loc) · 4.84 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
CREATE SCHEMA CodingPlatform;
SET SEARCH_PATH TO CodingPlatform;
CREATE TABLE users(
userid int PRIMARY KEY,
pwd varchar(20) NOT NULL,
name varchar(18) NOT NULL,
email varchar(30),
phone_no varchar(16),
rating int,
reg_date timestamp NOT NULL,
country varchar(20) NOT NULL
);
CREATE TABLE friend_of(
userid int,
friendid int,
PRIMARY KEY (userid, friendid),
FOREIGN KEY (userid) REFERENCES users
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (friendid) REFERENCES users
ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE problems(
probid int PRIMARY KEY,
prob_name varchar(18) NOT NULL,
statement text NOT NULL,
max_memory_mb int NOT NULL,
max_runtime_s int NOT NULL,
difficulty int,
creator_id int,
contestid int,
FOREIGN KEY (creator_id) REFERENCES users
ON DELETE SET NULL ON UPDATE CASCADE
);
CREATE TABLE tags(
probid int,
tagname varchar(40),
PRIMARY KEY (probid, tagname),
FOREIGN KEY (probid) REFERENCES problems
ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE favorites(
userid int,
probid int,
PRIMARY KEY (userid, probid),
FOREIGN KEY (userid) REFERENCES users
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (probid) REFERENCES problems
ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE test_cases(
probid int,
testcaseno int,
input text,
PRIMARY KEY (probid, testcaseno),
FOREIGN KEY (probid) REFERENCES problems
ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE output(
probid int,
testcaseno int,
output text,
PRIMARY KEY (probid, testcaseno, output),
FOREIGN KEY (probid) REFERENCES problems
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (probid, testcaseno) REFERENCES test_cases
ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE discussions(
discid int PRIMARY KEY,
topic varchar(50) NOT NULL,
started_by int,
FOREIGN KEY (started_by) REFERENCES users
ON DELETE SET NULL ON UPDATE CASCADE
);
CREATE TABLE about(
discid int,
probid int,
PRIMARY KEY (discid, probid),
FOREIGN KEY (discid) REFERENCES discussions
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (probid) REFERENCES problems
ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE posts(
postid int PRIMARY KEY,
comment text NOT NULL,
posted_at timestamp NOT NULL,
userid int NOT NULL,
discid int NOT NULL,
FOREIGN KEY (userid) REFERENCES users
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (discid) REFERENCES discussions
ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE contests(
contestid int PRIMARY KEY,
contest_name text NOT NULL,
contest_date_time timestamp NOT NULL,
duration_hrs int NOT NULL,
contest_div int
);
CREATE TABLE participates_in(
userid int,
contestid int,
rank int NOT NULL,
score int NOT NULL,
time_spent_hrs int NOT NULL,
PRIMARY KEY (userid, contestid),
FOREIGN KEY (userid) REFERENCES users
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (contestid) REFERENCES contests
ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE written_by(
userid int,
contestid int,
PRIMARY KEY (userid, contestid),
FOREIGN KEY (userid) REFERENCES users
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (contestid) REFERENCES contests
ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE submissions(
subid int PRIMARY KEY,
code text NOT NULL,
status varchar(8) NOT NULL CHECK(status in ('Accepted', 'Rejected')),
memory_mb int NOT NULL,
runtime_s int NOT NULL,
language varchar(20) NOT NULL,
submission_time timestamp NOT NULL,
userid int NOT NULL,
probid int NOT NULL,
FOREIGN KEY (userid) REFERENCES users
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (probid) REFERENCES problems
ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE solution(
solutionid int PRIMARY KEY,
code text NOT NULL,
sol_memory_mb int NOT NULL,
sol_runtime_s int NOT NULL,
language varchar(20) NOT NULL,
userid int NOT NULL,
probid int NOT NULL,
FOREIGN KEY (userid) REFERENCES users
ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (probid) REFERENCES problems
ON DELETE CASCADE ON UPDATE CASCADE
);
ALTER table problems add FOREIGN KEY (contestid) REFERENCES contests
ON DELETE SET NULL ON UPDATE CASCADE;
--modifications
ALTER TABLE submissions
DROP CONSTRAINT submissions_status_check;
ALTER TABLE submissions
ALTER COLUMN status TYPE varchar(20);
ALTER TABLE submissions
ADD CONSTRAINT submissions_status_check CHECK(status IN ('Accepted', 'Wrong_Answer', 'TLE', 'MLE', 'OutputLE', 'Runtime_Error', 'Compile_Error'));
ALTER TABLE submissions
ADD COLUMN wrong_testcaseno int,
ADD FOREIGN KEY (probid, wrong_testcaseno) REFERENCES test_cases(probid, testcaseno)
ON DELETE RESTRICT ON UPDATE CASCADE,
ADD CONSTRAINT wrong_testcaseno_check CHECK ((status = 'Wrong Answer' AND wrong_testcaseno IS NOT NULL) OR (status <> 'Wrong Answer' AND wrong_testcaseno IS NULL));
ALTER TABLE output
DROP CONSTRAINT output_probid_fkey;