RelationalDesign: schema_creation.sql

File schema_creation.sql, 10.1 KB (added by 236024, 4 days ago)
Line 
1BEGIN;
2
3DROP SCHEMA IF EXISTS project CASCADE;
4CREATE SCHEMA project;
5SET search_path TO project;
6
7CREATE TABLE students (
8 student_index integer PRIMARY KEY,
9 first_name text NOT NULL CHECK (length(trim(first_name)) > 0),
10 last_name text NOT NULL CHECK (length(trim(last_name)) > 0),
11 faculty text NOT NULL CHECK (length(trim(faculty)) > 0),
12 field_of_studies text NOT NULL CHECK (length(trim(field_of_studies)) > 0),
13 year_of_studies integer NOT NULL CHECK (year_of_studies >= 0),
14 birthday date NOT NULL CHECK (birthday < CURRENT_DATE),
15 email text NOT NULL UNIQUE CHECK (email ~* '^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$'),
16 phone_number text NOT NULL CHECK (length(trim(phone_number)) >= 6)
17);
18
19CREATE TABLE members (
20 student_index integer PRIMARY KEY REFERENCES students(student_index)
21 ON UPDATE CASCADE ON DELETE CASCADE
22);
23
24CREATE TABLE observers (
25 student_index integer PRIMARY KEY REFERENCES members(student_index)
26 ON UPDATE CASCADE ON DELETE CASCADE
27);
28
29CREATE TABLE full_members (
30 student_index integer PRIMARY KEY REFERENCES members(student_index)
31 ON UPDATE CASCADE ON DELETE CASCADE
32);
33
34CREATE TABLE young_members (
35 student_index integer PRIMARY KEY REFERENCES members(student_index)
36 ON UPDATE CASCADE ON DELETE CASCADE,
37 mentor_index integer NOT NULL REFERENCES full_members(student_index)
38 ON UPDATE CASCADE ON DELETE RESTRICT,
39 CHECK (student_index <> mentor_index)
40);
41
42CREATE TABLE alumni (
43 student_index integer PRIMARY KEY REFERENCES members(student_index)
44 ON UPDATE CASCADE ON DELETE CASCADE
45);
46
47CREATE TABLE membership_stages (
48 ms_id integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
49 ms_name text NOT NULL UNIQUE CHECK (length(trim(ms_name)) > 0)
50);
51
52CREATE TABLE has_stage (
53 student_index integer NOT NULL REFERENCES members(student_index)
54 ON UPDATE CASCADE ON DELETE CASCADE,
55 ms_id integer NOT NULL REFERENCES membership_stages(ms_id)
56 ON UPDATE CASCADE ON DELETE RESTRICT,
57 valid_from date NOT NULL,
58 valid_to date,
59 PRIMARY KEY (student_index, ms_id, valid_from),
60 CHECK (valid_to IS NULL OR valid_to >= valid_from)
61);
62
63CREATE TABLE organizational_functions (
64 of_id integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
65 of_title text NOT NULL UNIQUE CHECK (length(trim(of_title)) > 0)
66);
67
68CREATE TABLE board (
69 of_id integer PRIMARY KEY REFERENCES organizational_functions(of_id)
70 ON UPDATE CASCADE ON DELETE CASCADE
71);
72
73CREATE TABLE non_board (
74 of_id integer PRIMARY KEY REFERENCES organizational_functions(of_id)
75 ON UPDATE CASCADE ON DELETE CASCADE
76);
77
78CREATE TABLE function_mandates (
79 student_index integer NOT NULL REFERENCES full_members(student_index)
80 ON UPDATE CASCADE ON DELETE CASCADE,
81 of_id integer NOT NULL REFERENCES organizational_functions(of_id)
82 ON UPDATE CASCADE ON DELETE RESTRICT,
83 mandate_year integer NOT NULL CHECK (mandate_year >= 2002),
84 PRIMARY KEY (student_index, of_id, mandate_year)
85);
86
87CREATE TABLE activity_types (
88 at_id integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
89 at_name text NOT NULL UNIQUE CHECK (length(trim(at_name)) > 0),
90 at_description text NOT NULL CHECK (length(trim(at_description)) > 0)
91);
92
93CREATE TABLE event_types (
94 et_id integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
95 et_name text NOT NULL UNIQUE CHECK (length(trim(et_name)) > 0),
96 et_description text NOT NULL CHECK (length(trim(et_description)) > 0)
97);
98
99CREATE TABLE event_session_types (
100 est_id integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
101 est_name text NOT NULL UNIQUE CHECK (length(trim(est_name)) > 0),
102 est_description text NOT NULL CHECK (length(trim(est_description)) > 0)
103);
104
105CREATE TABLE event_function_types (
106 eft_id integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
107 eft_name text NOT NULL UNIQUE CHECK (length(trim(eft_name)) > 0)
108);
109
110CREATE TABLE locations (
111 l_id integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
112 l_name text NOT NULL UNIQUE CHECK (length(trim(l_name)) > 0)
113);
114
115CREATE TABLE event_editions (
116 ee_id integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
117 et_id integer NOT NULL REFERENCES event_types(et_id)
118 ON UPDATE CASCADE ON DELETE RESTRICT,
119 ee_title text NOT NULL CHECK (length(trim(ee_title)) > 0),
120 ee_start_time timestamp NOT NULL,
121 ee_end_time timestamp NOT NULL,
122 CHECK (ee_end_time >= ee_start_time)
123);
124
125CREATE TABLE event_edition_locations (
126 ee_id integer NOT NULL REFERENCES event_editions(ee_id)
127 ON UPDATE CASCADE ON DELETE CASCADE,
128 l_id integer NOT NULL REFERENCES locations(l_id)
129 ON UPDATE CASCADE ON DELETE RESTRICT,
130 PRIMARY KEY (ee_id, l_id)
131);
132
133CREATE TABLE event_functions (
134 ee_id integer NOT NULL REFERENCES event_editions(ee_id)
135 ON UPDATE CASCADE ON DELETE CASCADE,
136 ef_no integer NOT NULL CHECK (ef_no > 0),
137 eft_id integer NOT NULL REFERENCES event_function_types(eft_id)
138 ON UPDATE CASCADE ON DELETE RESTRICT,
139 PRIMARY KEY (ee_id, ef_no)
140);
141
142CREATE TABLE event_sessions (
143 ee_id integer NOT NULL REFERENCES event_editions(ee_id)
144 ON UPDATE CASCADE ON DELETE CASCADE,
145 es_no integer NOT NULL CHECK (es_no > 0),
146 est_id integer NOT NULL REFERENCES event_session_types(est_id)
147 ON UPDATE CASCADE ON DELETE RESTRICT,
148 l_id integer NOT NULL REFERENCES locations(l_id)
149 ON UPDATE CASCADE ON DELETE RESTRICT,
150 es_start_time timestamp NOT NULL,
151 es_title text NOT NULL CHECK (length(trim(es_title)) > 0),
152 needed_no_volunteers integer NOT NULL CHECK (needed_no_volunteers >= 0),
153 PRIMARY KEY (ee_id, es_no)
154);
155
156CREATE TABLE activity_sessions (
157 as_id integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
158 at_id integer NOT NULL REFERENCES activity_types(at_id)
159 ON UPDATE CASCADE ON DELETE RESTRICT,
160 l_id integer NOT NULL REFERENCES locations(l_id)
161 ON UPDATE CASCADE ON DELETE RESTRICT,
162 as_start_time timestamp NOT NULL
163);
164
165CREATE TABLE related_to (
166 as_id integer PRIMARY KEY REFERENCES activity_sessions(as_id)
167 ON UPDATE CASCADE ON DELETE CASCADE,
168 ee_id integer NOT NULL REFERENCES event_editions(ee_id)
169 ON UPDATE CASCADE ON DELETE CASCADE
170);
171
172CREATE TABLE applications (
173 application_id integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
174 student_index integer NOT NULL REFERENCES students(student_index)
175 ON UPDATE CASCADE ON DELETE CASCADE,
176 ee_id integer NOT NULL REFERENCES event_editions(ee_id)
177 ON UPDATE CASCADE ON DELETE CASCADE,
178 ef_no integer,
179 motivational_letter text NOT NULL CHECK (length(trim(motivational_letter)) > 0),
180 application_type text NOT NULL CHECK (
181 application_type IN (
182 'event_function',
183 'event_participation',
184 'young_member',
185 'mentor'
186 )
187 ),
188 application_status text NOT NULL CHECK (
189 application_status IN ('submitted', 'accepted', 'rejected', 'withdrawn')
190 ),
191 FOREIGN KEY (ee_id, ef_no)
192 REFERENCES event_functions(ee_id, ef_no)
193 ON UPDATE CASCADE ON DELETE CASCADE
194);
195
196CREATE TABLE holds_event_function (
197 student_index integer NOT NULL REFERENCES members(student_index)
198 ON UPDATE CASCADE ON DELETE CASCADE,
199 ee_id integer NOT NULL,
200 ef_no integer NOT NULL,
201 PRIMARY KEY (student_index, ee_id, ef_no),
202 FOREIGN KEY (ee_id, ef_no)
203 REFERENCES event_functions(ee_id, ef_no)
204 ON UPDATE CASCADE ON DELETE CASCADE
205);
206
207CREATE TABLE participation (
208 student_index integer NOT NULL REFERENCES students(student_index)
209 ON UPDATE CASCADE ON DELETE CASCADE,
210 as_id integer NOT NULL REFERENCES activity_sessions(as_id)
211 ON UPDATE CASCADE ON DELETE CASCADE,
212 rsvp_status text NOT NULL DEFAULT 'no_response' CHECK (
213 rsvp_status IN ('going', 'not_going', 'maybe', 'no_response')
214 ),
215 rsvp_at timestamp,
216 attendance_status text NOT NULL DEFAULT 'not_checked' CHECK (
217 attendance_status IN ('present', 'absent', 'excused', 'late', 'online', 'left_early', 'not_checked')
218 ),
219 PRIMARY KEY (student_index, as_id),
220 CHECK (
221 (rsvp_status = 'no_response' AND rsvp_at IS NULL)
222 OR (rsvp_status <> 'no_response' AND rsvp_at IS NOT NULL)
223 )
224);
225
226CREATE TABLE required_attendance (
227 student_index integer NOT NULL REFERENCES members(student_index)
228 ON UPDATE CASCADE ON DELETE CASCADE,
229 as_id integer NOT NULL REFERENCES activity_sessions(as_id)
230 ON UPDATE CASCADE ON DELETE CASCADE,
231 PRIMARY KEY (student_index, as_id)
232);
233
234CREATE TABLE announces_absence (
235 student_index integer NOT NULL,
236 as_id integer NOT NULL,
237 reason text NOT NULL CHECK (length(trim(reason)) > 0),
238 announced_at timestamp NOT NULL,
239 PRIMARY KEY (student_index, as_id),
240 FOREIGN KEY (student_index, as_id)
241 REFERENCES required_attendance(student_index, as_id)
242 ON UPDATE CASCADE ON DELETE CASCADE
243);
244
245CREATE TABLE responsible_for (
246 student_index integer NOT NULL REFERENCES members(student_index)
247 ON UPDATE CASCADE ON DELETE CASCADE,
248 as_id integer NOT NULL REFERENCES activity_sessions(as_id)
249 ON UPDATE CASCADE ON DELETE CASCADE,
250 PRIMARY KEY (student_index, as_id)
251);
252
253CREATE TABLE volunteers (
254 student_index integer NOT NULL REFERENCES members(student_index)
255 ON UPDATE CASCADE ON DELETE CASCADE,
256 ee_id integer NOT NULL,
257 es_no integer NOT NULL,
258 volunteer_role text NOT NULL CHECK (length(trim(volunteer_role)) > 0),
259 PRIMARY KEY (student_index, ee_id, es_no),
260 FOREIGN KEY (ee_id, es_no)
261 REFERENCES event_sessions(ee_id, es_no)
262 ON UPDATE CASCADE ON DELETE CASCADE
263);
264
265CREATE TABLE companies (
266 c_id integer PRIMARY KEY,
267 c_name text NOT NULL CHECK (length(trim(c_name)) > 0),
268 naframa_reference text UNIQUE
269);
270
271CREATE TABLE cooperation (
272 c_id integer NOT NULL REFERENCES companies(c_id)
273 ON UPDATE CASCADE ON DELETE CASCADE,
274 ee_id integer NOT NULL REFERENCES event_editions(ee_id)
275 ON UPDATE CASCADE ON DELETE CASCADE,
276 cooperation_type text NOT NULL CHECK (
277 cooperation_type IN ('sponsor', 'partner', 'participant', 'challenge_provider', 'supporter')
278 ),
279 PRIMARY KEY (c_id, ee_id)
280);
281
282COMMIT;