RelationalDesign: KreiranjeTabeli.sql

File KreiranjeTabeli.sql, 7.7 KB (added by 201100, 2 years ago)
Line 
1drop table administrator cascade;
2drop table attempt cascade;
3drop table badge cascade;
4drop table badge_is_awarded_to_quiztaker cascade;
5drop table category cascade;
6drop table choice cascade;
7drop table hint cascade;
8drop table medal cascade;
9drop table medal_is_awarded_to_quiztaker cascade;
10drop table moderator cascade;
11drop table question cascade;
12drop table question_is_part_of_quiz cascade;
13drop table quiz cascade;
14drop table quiz_is_part_of_tournament cascade;
15drop table quiz_is_part_of_tournamentphase cascade;
16drop table quiztaker cascade;
17drop table quiztaker_participates_in_tournament cascade;
18drop table quiztaker_participates_in_tournamentphase cascade;
19drop table result_table cascade;
20drop table selectedchoice cascade;
21drop table selectionquestion cascade;
22drop table subject cascade;
23drop table textquestion cascade;
24drop table tournament cascade;
25drop table tournamentorganizer cascade;
26drop table tournamentorganizer_hosts_tournament cascade;
27drop table tournamentphase cascade;
28drop table user_table cascade;
29
30
31create table user_table(
32 userID serial primary key,
33 fullName varchar(100),
34 username varchar(50) not null unique,
35 password_attr varchar(50) not null
36);
37
38create table administrator(
39 userID integer primary key,
40
41 constraint userID_fk foreign key(userID)
42 references user_table(userID)
43);
44
45create table moderator(
46 userID integer primary key,
47
48 constraint userID_fk foreign key(userID)
49 references user_table(userID)
50);
51
52create table tournament(
53 tournamentID serial primary key,
54 tournamentName varchar(50) not null
55);
56
57create table tournamentPhase(
58 tournamentPhaseID serial primary key,
59 tournamentID integer not null,
60 tournamentPhaseName varchar(50) not null,
61
62 constraint tournamentID_fk foreign key (tournamentID)
63 references tournament(tournamentID)
64);
65
66
67create table quizTaker(
68 userID integer primary key,
69
70 constraint userID_fk foreign key(userID)
71 references user_table(userID)
72
73);
74
75create table tournamentOrganizer(
76 userID integer not null,
77 tournamentOrganizerID serial not null unique,
78
79 constraint userID_fk foreign key(userID)
80 references moderator(userID),
81
82 constraint tournamentOrganizer_pk primary key(tournamentOrganizerID, userID)
83
84
85
86);
87
88create table subject(
89 subjectID serial primary key,
90 subjectName varchar(100) not null
91
92);
93
94create table category(
95 categoryID serial primary key,
96 subjectID integer not null,
97 categoryName varchar(100) not null,
98
99 constraint subjectID_fk foreign key(subjectID)
100 references subject(subjectID)
101
102);
103
104create table quiz(
105 quizID serial primary key,
106 categoryID integer not null,
107 quizName varchar(100) not null,
108
109 constraint categoryID_fk foreign key(categoryID)
110 references category(categoryID)
111
112);
113
114create table medal(
115 medalID serial primary key,
116 tournamentID integer not null,
117 medalName varchar(50) not null,
118 description varchar(500) not null,
119
120 constraint tournamentID_fk foreign key(tournamentID)
121 references tournament(tournamentID)
122
123);
124
125
126create table question(
127
128 questionID serial primary key,
129 questionText varchar(500) not null
130
131);
132
133create table hint(
134 hintID serial primary key,
135 hintText varchar(500) not null,
136 questionID integer not null,
137
138 constraint questionID_fk foreign key(questionID)
139 references question(questionID)
140);
141
142create table textQuestion(
143 questionID integer primary key,
144
145 constraint questionID_fk foreign key(questionID)
146 references question(questionID)
147);
148
149create table selectionQuestion(
150 questionID integer primary key,
151
152 constraint questionID_fk foreign key(questionID)
153 references question(questionID)
154);
155
156create table choice(
157 choiceID serial primary key,
158 questionID integer not null,
159 isCorrect boolean not null,
160 choiceText varchar(500) not null,
161
162 constraint questionID_fk foreign key(questionID)
163 references selectionQuestion(questionID)
164);
165
166create table attempt(
167 attemptID serial primary key,
168 userID integer not null,
169 attemptDate Date not null,
170 quizID integer not null,
171
172 constraint userID_fk foreign key(userID)
173 references quizTaker(userID),
174
175 constraint quizID_fk foreign key(quizID)
176 references quiz(quizID)
177);
178
179create table selectedChoice (
180
181 selectedChoiceID serial not null,
182
183 choiceID integer not null,
184
185 attemptID integer not null,
186
187 constraint choiceID_fk foreign key(choiceID)
188 references choice(choiceID),
189
190 constraint attemptID_fk foreign key(attemptID)
191 references attempt(attemptID),
192
193 constraint selectedChoice_pk primary key(selectedChoiceID, choiceID)
194
195);
196
197
198create table result_table(
199 resultID serial primary key,
200 attemptID integer not null,
201 points integer not null,
202
203 constraint attemptID_fk foreign key(attemptID)
204 references attempt(attemptID)
205);
206
207create table badge(
208 badgeID serial primary key,
209 badgeName varchar(100) not null,
210 description varchar(500) not null,
211 resultID integer not null,
212
213 constraint resultID_fk foreign key(resultID)
214 references result_table(resultID)
215);
216
217create table quiz_is_part_of_tournament(
218quizID integer not null,
219tournamentID integer not null,
220
221constraint quizID_fk foreign key(quizID)
222 references quiz(quizID),
223
224constraint tournamentID_fk foreign key(tournamentID)
225 references tournament(tournamentID),
226
227 constraint pom1_pk primary key(quizID, tournamentID)
228);
229
230create table quiz_is_part_of_tournamentPhase(
231quizID integer not null,
232tournamentPhaseID integer not null,
233
234constraint quizID_fk foreign key(quizID)
235 references quiz(quizID),
236
237constraint tournamentPhaseID_fk foreign key(tournamentPhaseID)
238 references tournamentPhase(tournamentPhaseID),
239
240 constraint pom8_pk primary key(quizID, tournamentPhaseID)
241);
242
243create table tournamentOrganizer_hosts_tournament(
244userID integer not null,
245tournamentID integer not null,
246
247constraint tournamentOrganizerID_fk foreign key(userID)
248 references tournamentOrganizer(tournamentorganizerid),
249
250constraint tournamentID_fk foreign key(tournamentID)
251 references tournament(tournamentID),
252
253 constraint pom2_pk primary key(userID, tournamentID)
254);
255
256create table question_is_part_of_quiz(
257 questionID integer not null,
258 quizID integer not null,
259
260 constraint questionID_fk foreign key(questionID)
261 references question(questionID),
262
263 constraint quizID_fk foreign key(quizID)
264 references quiz(quizID),
265
266 constraint pom3_pk primary key(questionID, quizID)
267);
268
269create table quizTaker_participates_in_tournament(
270 userID integer not null,
271 tournamentID integer not null,
272
273 constraint userID_fk foreign key(userID)
274 references quizTaker(userID),
275
276 constraint tournamentID_fk foreign key(tournamentID)
277 references tournament(tournamentID),
278
279 constraint pom4_pk primary key(userID, tournamentID)
280
281);
282
283create table quizTaker_participates_in_tournamentPhase(
284 userID integer not null,
285 tournamentPhaseID integer not null,
286
287 constraint userID_fk foreign key(userID)
288 references quizTaker(userID),
289
290 constraint tournamentPhaseID_fk foreign key(tournamentPhaseID)
291 references tournamentPhase(tournamentPhaseID),
292
293 constraint pom5_pk primary key(userID, tournamentPhaseID)
294
295);
296
297create table medal_is_awarded_to_quizTaker(
298 medalID integer not null,
299 userID integer not null,
300
301 constraint medalID_fk foreign key(medalID)
302 references medal(medalID),
303
304 constraint userID_fk foreign key(userID)
305 references quizTaker(userID),
306
307 constraint pom6_pk primary key(medalID, userID)
308);
309
310create table badge_is_awarded_to_quizTaker(
311 badgeID integer not null,
312 userID integer not null,
313
314 constraint badgeID_fk foreign key(badgeID)
315 references badge(badgeID),
316
317 constraint userID_fk foreign key(userID)
318 references quizTaker(userID),
319
320 constraint pom7_pk primary key(badgeID, userID)
321);