WikiStart/RelationalDesign: kreiranje_old1.sql

File kreiranje_old1.sql, 15.5 KB (added by 121232, 10 months ago)
Line 
1DROP TABLE IF EXISTS student CASCADE;
2DROP TABLE IF EXISTS task CASCADE;
3DROP TABLE IF EXISTS feedback CASCADE;
4DROP TABLE IF EXISTS school_year CASCADE;
5DROP TABLE IF EXISTS term CASCADE;
6DROP TABLE IF EXISTS subject_at_term CASCADE;
7DROP TABLE IF EXISTS calendar_event CASCADE;
8DROP TABLE IF EXISTS repeating_event CASCADE;
9DROP TABLE IF EXISTS non_repeating_event CASCADE;
10DROP TABLE IF EXISTS e_mail CASCADE;
11DROP TABLE IF EXISTS reminder CASCADE;
12DROP TABLE IF EXISTS place CASCADE;
13DROP TABLE IF EXISTS dashboard CASCADE;
14DROP TABLE IF EXISTS notes CASCADE;
15DROP TABLE IF EXISTS professor CASCADE;
16DROP TABLE IF EXISTS subject_book CASCADE;
17DROP TABLE IF EXISTS author CASCADE;
18DROP TABLE IF EXISTS chapter CASCADE;
19DROP TABLE IF EXISTS has_taken_a_subject CASCADE;
20DROP TABLE IF EXISTS holds_subject CASCADE;
21DROP TABLE IF EXISTS holds_event CASCADE;
22DROP TABLE IF EXISTS happens_at_place CASCADE;
23DROP TABLE IF EXISTS term_has_subject CASCADE;
24DROP TABLE IF EXISTS calendar_task CASCADE;
25DROP TABLE IF EXISTS recommended_book CASCADE;
26DROP TABLE IF EXISTS recommended_chapter CASCADE;
27DROP TABLE IF EXISTS has_consultations CASCADE;
28DROP TABLE IF EXISTS year_has_subject CASCADE;
29DROP TABLE IF EXISTS student_dashboard CASCADE;
30DROP TABLE IF EXISTS studied_in_school_year CASCADE;
31DROP TABLE IF EXISTS dashboard_notes CASCADE;
32DROP TABLE IF EXISTS book_modules CASCADE;
33DROP TABLE IF EXISTS contains_terms CASCADE;
34DROP TABLE IF EXISTS has_e_mail CASCADE;
35DROP TABLE IF EXISTS dashboard_event CASCADE;
36DROP TABLE IF EXISTS subject_event CASCADE;
37DROP TABLE IF EXISTS creates_task CASCADE;
38DROP TABLE IF EXISTS student_event CASCADE;
39DROP TABLE IF EXISTS student_task CASCADE;
40DROP TABLE IF EXISTS book_author CASCADE;
41DROP TABLE IF EXISTS gives_feedback CASCADE;
42DROP TABLE IF EXISTS event_reminder CASCADE;
43DROP TABLE IF EXISTS dashboard_task CASCADE;
44
45CREATE TABLE student (
46 student_id INT PRIMARY KEY,
47 username VARCHAR(255),
48 s_first_name VARCHAR(255),
49 s_last_name VARCHAR(255),
50 s_password VARCHAR(255)
51);
52
53CREATE TABLE task (
54 task_id INT PRIMARY KEY,
55 task_name VARCHAR(255),
56 task_description TEXT,
57 task_priority INT,
58 is_done BOOLEAN
59);
60
61CREATE TABLE feedback (
62 fb_id INT PRIMARY KEY,
63 fb_date DATE,
64 fb_title VARCHAR(255),
65 fb_description TEXT,
66 fb_rating INT
67);
68
69CREATE TABLE school_year (
70 student_id INT,
71 starting_year INT,
72 finishing_year INT,
73 PRIMARY KEY (student_id, starting_year, finishing_year),
74 CONSTRAINT fk_school_year_student FOREIGN KEY (student_id) REFERENCES student(student_id)
75);
76
77CREATE TABLE term (
78 student_id INT,
79 starting_year INT,
80 finishing_year INT,
81 term_type VARCHAR(50),
82 term_start_date DATE,
83 term_end_date DATE,
84 PRIMARY KEY (student_id, starting_year, finishing_year, term_type),
85 CONSTRAINT fk_term_school_year FOREIGN KEY (student_id, starting_year, finishing_year) REFERENCES school_year(student_id, starting_year, finishing_year)
86);
87
88CREATE TABLE subject_at_term (
89 student_id INT,
90 starting_year INT,
91 finishing_year INT,
92 term_type VARCHAR(50),
93 term_subject_id INT,
94 subject_name VARCHAR(255),
95 PRIMARY KEY (student_id, starting_year, finishing_year, term_type, term_subject_id),
96 CONSTRAINT fk_subject_at_term_term FOREIGN KEY (student_id, starting_year, finishing_year, term_type) REFERENCES term(student_id, starting_year, finishing_year, term_type)
97);
98
99CREATE TABLE calendar_event (
100 calendar_event_id INT PRIMARY KEY,
101 event_name VARCHAR(255),
102 event_type VARCHAR(50),
103 event_description TEXT,
104 event_start_time TIME,
105 event_end_time TIME
106);
107
108CREATE TABLE repeating_event (
109 calendar_event_id INT,
110 event_week_day VARCHAR(20),
111 repeats_every_n_weeks INT,
112 starting_date DATE,
113 ending_date DATE,
114 PRIMARY KEY (calendar_event_id),
115 CONSTRAINT fk_repeating_event_calendar_event FOREIGN KEY (calendar_event_id) REFERENCES calendar_event(calendar_event_id)
116);
117
118CREATE TABLE non_repeating_event (
119 calendar_event_id INT,
120 event_date DATE,
121 PRIMARY KEY (calendar_event_id),
122 CONSTRAINT fk_non_repeating_event_calendar_event FOREIGN KEY (calendar_event_id) REFERENCES calendar_event(calendar_event_id)
123);
124
125CREATE TABLE e_mail (
126 student_id INT,
127 e_mail_id INT PRIMARY KEY,
128 e_mail_address VARCHAR(255),
129 CONSTRAINT fk_email_student FOREIGN KEY (student_id) REFERENCES student(student_id)
130);
131
132CREATE TABLE reminder (
133 calendar_event_id INT,
134 r_id INT PRIMARY KEY,
135 minutes_before_event INT,
136 r_flag BOOLEAN,
137 CONSTRAINT fk_reminder_calendar_event FOREIGN KEY (calendar_event_id) REFERENCES calendar_event(calendar_event_id)
138);
139
140CREATE TABLE place (
141 place_id INT PRIMARY KEY,
142 building_name VARCHAR(255),
143 room_name VARCHAR(255)
144);
145
146
147CREATE TABLE dashboard (
148 student_id INT,
149 dashboard_id INT,
150 dashboard_date DATE,
151 PRIMARY KEY (dashboard_id),
152 UNIQUE (student_id, dashboard_id),
153 CONSTRAINT fk_dashboard_student FOREIGN KEY (student_id) REFERENCES student(student_id)
154);
155
156CREATE TABLE notes (
157 student_id INT,
158 dashboard_id INT,
159 note_id INT PRIMARY KEY,
160 notes_date DATE,
161 note_text TEXT,
162 CONSTRAINT fk_notes_dashboard FOREIGN KEY (student_id, dashboard_id) REFERENCES dashboard(student_id, dashboard_id)
163);
164
165CREATE TABLE professor (
166 professor_id INT PRIMARY KEY,
167 p_first_name VARCHAR(255),
168 p_last_name VARCHAR(255),
169 p_role VARCHAR(50),
170 p_email VARCHAR(255)
171);
172
173CREATE TABLE subject_book (
174 book_id INT PRIMARY KEY,
175 book_title VARCHAR(255),
176 b_year_of_publishment INT,
177 book_edition VARCHAR(50)
178);
179
180CREATE TABLE author (
181 author_id INT PRIMARY KEY,
182 author_name VARCHAR(255),
183 a_short_biography TEXT
184);
185
186CREATE TABLE chapter (
187 book_id INT,
188 chapter_number INT,
189 chapter_title VARCHAR(255),
190 PRIMARY KEY (book_id, chapter_number),
191 CONSTRAINT fk_chapter_subject_book FOREIGN KEY (book_id) REFERENCES subject_book(book_id)
192);
193
194CREATE TABLE has_taken_a_subject (
195 student_id INT,
196 starting_year INT,
197 finishing_year INT,
198 term_type VARCHAR(50),
199 term_subject_id INT,
200 student_id_ref INT,
201 PRIMARY KEY (student_id, starting_year, finishing_year, term_type, term_subject_id),
202 CONSTRAINT fk_taken_subject_at_term FOREIGN KEY (student_id, starting_year, finishing_year, term_type, term_subject_id) REFERENCES subject_at_term(student_id, starting_year, finishing_year, term_type, term_subject_id),
203 CONSTRAINT fk_taken_student FOREIGN KEY (student_id_ref) REFERENCES student(student_id)
204);
205
206CREATE TABLE holds_subject (
207 student_id INT,
208 starting_year INT,
209 finishing_year INT,
210 term_type VARCHAR(50),
211 term_subject_id INT,
212 professor_id INT,
213 PRIMARY KEY (student_id, starting_year, finishing_year, term_type, term_subject_id),
214 CONSTRAINT fk_holds_subject_at_term FOREIGN KEY (student_id, starting_year, finishing_year, term_type, term_subject_id) REFERENCES subject_at_term(student_id, starting_year, finishing_year, term_type, term_subject_id),
215 CONSTRAINT fk_holds_professor FOREIGN KEY (professor_id) REFERENCES professor(professor_id)
216);
217
218CREATE TABLE holds_event (
219 calendar_event_id INT,
220 professor_id INT,
221 PRIMARY KEY (calendar_event_id),
222 CONSTRAINT fk_holds_event_calendar_event FOREIGN KEY (calendar_event_id) REFERENCES calendar_event(calendar_event_id),
223 CONSTRAINT fk_holds_event_professor FOREIGN KEY (professor_id) REFERENCES professor(professor_id)
224);
225
226CREATE TABLE happens_at_place (
227 calendar_event_id INT,
228 place_id INT,
229 PRIMARY KEY (calendar_event_id),
230 CONSTRAINT fk_happens_at_place_calendar_event FOREIGN KEY (calendar_event_id) REFERENCES calendar_event(calendar_event_id),
231 CONSTRAINT fk_happens_at_place_place FOREIGN KEY (place_id) REFERENCES place(place_id)
232);
233
234CREATE TABLE term_has_subject (
235 student_id INT,
236 starting_year INT,
237 finishing_year INT,
238 term_type VARCHAR(50),
239 term_subject_id INT,
240 PRIMARY KEY (student_id, starting_year, finishing_year, term_type, term_subject_id),
241 CONSTRAINT fk_term_has_subject_subject_at_term FOREIGN KEY (student_id, starting_year, finishing_year, term_type, term_subject_id) REFERENCES subject_at_term(student_id, starting_year, finishing_year, term_type, term_subject_id)
242);
243
244CREATE TABLE dashboard_task (
245 task_id INT,
246 student_id INT,
247 dashboard_id INT,
248 PRIMARY KEY (task_id),
249 CONSTRAINT fk_dashboard_task_task FOREIGN KEY (task_id) REFERENCES task(task_id),
250 CONSTRAINT fk_dashboard_task_dashboard FOREIGN KEY (student_id, dashboard_id) REFERENCES dashboard(student_id, dashboard_id)
251);
252
253CREATE TABLE gives_feedback (
254 fb_id INT,
255 student_id INT,
256 PRIMARY KEY (fb_id),
257 CONSTRAINT fk_gives_feedback_feedback FOREIGN KEY (fb_id) REFERENCES feedback(fb_id),
258 CONSTRAINT fk_gives_feedback_student FOREIGN KEY (student_id) REFERENCES student(student_id)
259);
260
261CREATE TABLE recommended_book(
262 student_id INT,
263 starting_year INT,
264 finishing_year INT,
265 term_type VARCHAR(50),
266 term_subject_id INT,
267 book_id INT,
268 fb_id INT, -- Added fb_id here
269 PRIMARY KEY (student_id, starting_year, finishing_year, term_type, term_subject_id),
270 CONSTRAINT fk_recommended_book_subject_at_term FOREIGN KEY (student_id, starting_year, finishing_year, term_type, term_subject_id) REFERENCES subject_at_term(student_id, starting_year, finishing_year, term_type, term_subject_id),
271 CONSTRAINT fk_recommended_book_subject_book FOREIGN KEY (book_id) REFERENCES subject_book(book_id),
272 CONSTRAINT fk_recommended_book_feedback FOREIGN KEY (fb_id) REFERENCES feedback(fb_id) -- Corrected the reference here
273);
274
275CREATE TABLE recommended_chapter (
276 calendar_event_id INT,
277 book_id INT,
278 chapter_number INT,
279 PRIMARY KEY (calendar_event_id),
280 CONSTRAINT fk_recommended_chapter_calendar_event FOREIGN KEY (calendar_event_id) REFERENCES calendar_event(calendar_event_id),
281 CONSTRAINT fk_recommended_chapter_chapter FOREIGN KEY (book_id, chapter_number) REFERENCES chapter(book_id, chapter_number)
282);
283
284CREATE TABLE has_consultations (
285 calendar_event_id INT,
286 professor_id INT,
287 PRIMARY KEY (calendar_event_id),
288 CONSTRAINT fk_has_consultations_calendar_event FOREIGN KEY (calendar_event_id) REFERENCES calendar_event(calendar_event_id),
289 CONSTRAINT fk_has_consultations_professor FOREIGN KEY (professor_id) REFERENCES professor(professor_id)
290);
291
292CREATE TABLE year_has_subject (
293 student_id INT,
294 starting_year INT,
295 finishing_year INT,
296 term_type VARCHAR(50),
297 term_subject_id INT,
298 student_id_ref INT,
299 starting_year_ref INT,
300 finishing_year_ref INT,
301 PRIMARY KEY (student_id, starting_year, finishing_year, term_type, term_subject_id),
302 CONSTRAINT fk_year_has_subject_subject_at_term FOREIGN KEY (student_id, starting_year, finishing_year, term_type, term_subject_id) REFERENCES subject_at_term(student_id, starting_year, finishing_year, term_type, term_subject_id),
303 CONSTRAINT fk_year_has_subject_school_year FOREIGN KEY (student_id_ref, starting_year_ref, finishing_year_ref) REFERENCES school_year(student_id, starting_year, finishing_year)
304);
305
306CREATE TABLE student_dashboard (
307 student_id INT,
308 dashboard_id INT,
309 PRIMARY KEY (student_id, dashboard_id),
310 CONSTRAINT fk_student_dashboard_dashboard FOREIGN KEY (student_id, dashboard_id) REFERENCES dashboard(student_id, dashboard_id)
311);
312
313CREATE TABLE studied_in_school_year (
314 student_id INT,
315 starting_year INT,
316 finishing_year INT,
317 term_type VARCHAR(50),
318 term_subject_id INT,
319 PRIMARY KEY (student_id, starting_year, finishing_year, term_type, term_subject_id),
320 CONSTRAINT fk_studied_in_school_year_subject_at_term FOREIGN KEY (student_id, starting_year, finishing_year, term_type, term_subject_id) REFERENCES subject_at_term(student_id, starting_year, finishing_year, term_type, term_subject_id)
321);
322
323CREATE TABLE dashboard_notes (
324 student_id INT,
325 dashboard_id INT,
326 PRIMARY KEY (student_id, dashboard_id),
327 CONSTRAINT fk_dashboard_notes_dashboard FOREIGN KEY (student_id, dashboard_id) REFERENCES dashboard(student_id, dashboard_id)
328);
329
330CREATE TABLE book_modules (
331 book_id INT,
332 chapter_number INT,
333 PRIMARY KEY (book_id, chapter_number),
334 CONSTRAINT fk_book_modules_subject_book FOREIGN KEY (book_id) REFERENCES subject_book(book_id)
335);
336
337CREATE TABLE contains_terms (
338 student_id INT,
339 starting_year INT,
340 finishing_year INT,
341 term_type VARCHAR(50),
342 term_subject_id INT,
343 PRIMARY KEY (student_id, starting_year, finishing_year, term_type, term_subject_id),
344 CONSTRAINT fk_contains_terms_subject_at_term FOREIGN KEY (student_id, starting_year, finishing_year, term_type, term_subject_id) REFERENCES subject_at_term(student_id, starting_year, finishing_year, term_type, term_subject_id)
345);
346
347CREATE TABLE has_e_mail (
348 student_id INT,
349 e_mail_id INT,
350 PRIMARY KEY (student_id, e_mail_id),
351 CONSTRAINT fk_has_e_mail_student FOREIGN KEY (student_id) REFERENCES student(student_id)
352);
353
354CREATE TABLE dashboard_event (
355 student_id INT,
356 dashboard_id INT,
357 calendar_event_id INT,
358 PRIMARY KEY (student_id, dashboard_id, calendar_event_id),
359 CONSTRAINT fk_dashboard_event_dashboard FOREIGN KEY (student_id, dashboard_id) REFERENCES dashboard(student_id, dashboard_id),
360 CONSTRAINT fk_dashboard_event_calendar_event FOREIGN KEY (calendar_event_id) REFERENCES calendar_event(calendar_event_id)
361);
362
363CREATE TABLE subject_event (
364 calendar_event_id INT,
365 student_id INT,
366 starting_year INT,
367 finishing_year INT,
368 term_type VARCHAR(50),
369 term_subject_id INT,
370 PRIMARY KEY (calendar_event_id),
371 CONSTRAINT fk_subject_event_calendar_event FOREIGN KEY (calendar_event_id) REFERENCES calendar_event(calendar_event_id),
372 CONSTRAINT fk_subject_event_subject_at_term FOREIGN KEY (student_id, starting_year, finishing_year, term_type, term_subject_id) REFERENCES subject_at_term(student_id, starting_year, finishing_year, term_type, term_subject_id)
373);
374
375CREATE TABLE creates_task (
376 task_id INT,
377 student_id INT,
378 starting_year INT,
379 finishing_year INT,
380 term_type VARCHAR(50),
381 term_subject_id INT,
382 PRIMARY KEY (task_id),
383 CONSTRAINT fk_creates_task_task FOREIGN KEY (task_id) REFERENCES task(task_id),
384 CONSTRAINT fk_creates_task_subject_at_term FOREIGN KEY (student_id, starting_year, finishing_year, term_type, term_subject_id) REFERENCES subject_at_term(student_id, starting_year, finishing_year, term_type, term_subject_id)
385);
386CREATE TABLE student_event (
387 calendar_event_id INT,
388 student_id INT,
389 PRIMARY KEY (calendar_event_id, student_id),
390 CONSTRAINT fk_student_event_calendar_event
391 FOREIGN KEY (calendar_event_id)
392 REFERENCES calendar_event(calendar_event_id),
393 CONSTRAINT fk_student_event_student
394 FOREIGN KEY (student_id)
395 REFERENCES student(student_id)
396);
397
398CREATE TABLE student_task (
399 task_id INT,
400 student_id INT,
401 PRIMARY KEY (task_id, student_id),
402 CONSTRAINT fk_student_task_task
403 FOREIGN KEY (task_id)
404 REFERENCES task(task_id),
405 CONSTRAINT fk_student_task_student
406 FOREIGN KEY (student_id)
407 REFERENCES student(student_id)
408);
409
410CREATE TABLE book_author (
411 author_id INT,
412 book_id INT,
413 PRIMARY KEY (author_id, book_id),
414 CONSTRAINT fk_book_author_author
415 FOREIGN KEY (author_id)
416 REFERENCES author(author_id),
417 CONSTRAINT fk_book_author_book
418 FOREIGN KEY (book_id)
419 REFERENCES subject_book(book_id)
420);
421
422CREATE TABLE event_reminder (
423 calendar_event_id INT,
424 r_id INT,
425 PRIMARY KEY (calendar_event_id, r_id),
426 CONSTRAINT fk_event_reminder_calendar_event
427 FOREIGN KEY (calendar_event_id)
428 REFERENCES calendar_event(calendar_event_id)
429);
430
431