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