WikiStart/RelationalDesign: kreiranje.sql

File kreiranje.sql, 14.6 KB (added by 121232, 10 months ago)
Line 
1-- Базирано на стабилна трансформација
2
3-- поставување на патека односно шема во која ќе се извршуваат командите
4set search_path = project;
5
6DROP TABLE IF EXISTS student CASCADE;
7DROP TABLE IF EXISTS task CASCADE;
8DROP TABLE IF EXISTS feedback CASCADE;
9DROP TABLE IF EXISTS school_year CASCADE;
10DROP TABLE IF EXISTS term CASCADE;
11DROP TABLE IF EXISTS subject_at_term CASCADE;
12DROP TABLE IF EXISTS calendar_event CASCADE;
13DROP TABLE IF EXISTS repeating_event CASCADE;
14DROP TABLE IF EXISTS non_repeating_event CASCADE;
15DROP TABLE IF EXISTS e_mail CASCADE;
16DROP TABLE IF EXISTS reminder CASCADE;
17DROP TABLE IF EXISTS place CASCADE;
18DROP TABLE IF EXISTS dashboard CASCADE;
19DROP TABLE IF EXISTS notes CASCADE;
20DROP TABLE IF EXISTS professor CASCADE;
21DROP TABLE IF EXISTS subject_book CASCADE;
22DROP TABLE IF EXISTS author CASCADE;
23DROP TABLE IF EXISTS chapter CASCADE;
24
25DROP TABLE IF EXISTS has_taken_a_subject CASCADE;
26DROP TABLE IF EXISTS holds_subject CASCADE;
27DROP TABLE IF EXISTS holds_event CASCADE;
28DROP TABLE IF EXISTS happens_at_place CASCADE;
29DROP TABLE IF EXISTS recommended_book CASCADE;
30DROP TABLE IF EXISTS recommended_chapter CASCADE;
31DROP TABLE IF EXISTS has_consultations CASCADE;
32DROP TABLE IF EXISTS year_has_subject CASCADE;
33DROP TABLE IF EXISTS dashboard_event CASCADE;
34DROP TABLE IF EXISTS subject_event CASCADE;
35DROP TABLE IF EXISTS creates_task CASCADE;
36DROP TABLE IF EXISTS student_event CASCADE;
37DROP TABLE IF EXISTS student_task CASCADE;
38DROP TABLE IF EXISTS gives_feedback CASCADE;
39DROP TABLE IF EXISTS dashboard_task CASCADE;
40DROP TABLE IF EXISTS book_author CASCADE;
41
42-- отстранување на постојните домени
43
44drop domain if exists string_long;
45drop domain if exists string_short;
46drop domain if exists string_real_short;
47
48-- отстранување на шемата
49
50drop schema if exists project;
51
52-- креирање на шемата
53
54create schema project;
55
56-- поставување на патека односно шема во која ќе се извршуваат командите
57
58set search_path = project;
59
60-- креирање потребните домени - нови типови податоци
61
62create domain string_long AS character varying(1000);
63create domain string_short AS character varying(255);
64create domain string_real_short AS character varying(20);
65
66-- трансформација на множествата ентитети во табели
67
68CREATE 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
77CREATE 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
85CREATE 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
93CREATE 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
103CREATE 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
114CREATE 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
125CREATE 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
135CREATE 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
145CREATE 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
152CREATE 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
160CREATE 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
169CREATE TABLE place (
170 place_id SERIAL PRIMARY KEY,
171 building_name string_short,
172 room_name string_short not null
173);
174
175
176CREATE 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
185CREATE 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
195CREATE 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
204CREATE 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
212CREATE 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
220CREATE 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
230CREATE 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
242CREATE 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
254CREATE 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
262CREATE 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
272CREATE 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
281CREATE 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
289CREATE 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
302CREATE 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
311CREATE 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
322CREATE 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
338CREATE 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
347CREATE 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
359CREATE 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);
370CREATE 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
380CREATE 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
390CREATE 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