This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.sql
386 lines (306 loc) · 12.3 KB
/
schema.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
USE u_bkozak
-- Drop all foreign keys
DECLARE @Sql NVARCHAR(MAX) = '';
SELECT @Sql += 'ALTER TABLE ' + table_name + ' DROP CONSTRAINT ' + constraint_name + ';'
FROM information_schema.table_constraints
WHERE constraint_type = 'FOREIGN KEY';
-- Execute the generated statements
EXEC sp_executesql @Sql;
-- Drop all tables
SET @Sql = '';
SELECT @Sql += 'DROP TABLE ' + table_name + ';'
FROM information_schema.tables
WHERE table_type = 'BASE TABLE';
-- Execute the generated DROP TABLE statements
EXEC sp_executesql @Sql;
CREATE TABLE Student
(
id int NOT NULL IDENTITY (1, 1),
first_name varchar(50) NOT NULL,
last_name varchar(50) NOT NULL,
address varchar(200) NOT NULL,
email varchar(50) NOT NULL UNIQUE,
phone_number varchar(20) NOT NULL UNIQUE,
PRIMARY KEY (id),
)
CREATE TABLE Teacher
(
id int NOT NULL IDENTITY (1, 1),
first_name varchar(50) NOT NULL,
last_name varchar(50) NOT NULL,
address varchar(200) NOT NULL,
email varchar(50) NOT NULL UNIQUE,
phone_number varchar(20) NOT NULL UNIQUE,
PRIMARY KEY (id),
)
CREATE TABLE Translator
(
id int NOT NULL IDENTITY (1, 1),
first_name varchar(50) NOT NULL,
last_name varchar(50) NOT NULL,
address varchar(200) NOT NULL,
email varchar(50) NOT NULL UNIQUE,
phone_number varchar(20) NOT NULL UNIQUE,
PRIMARY KEY (id),
)
CREATE TABLE Language
(
id int NOT NULL IDENTITY (1, 1),
name varchar(50) NOT NULL UNIQUE,
PRIMARY KEY (id),
)
CREATE TABLE TranslatorLanguage
(
translator_id int NOT NULL,
language_id int NOT NULL,
PRIMARY KEY (translator_id, language_id),
FOREIGN KEY (translator_id) REFERENCES Translator (id) ON DELETE CASCADE,
FOREIGN KEY (language_id) REFERENCES Language (id) ON DELETE CASCADE,
)
CREATE TABLE Webinar
(
id int NOT NULL IDENTITY (1, 1),
title varchar(100) NOT NULL,
price int DEFAULT 0 NOT NULL, --in Polish grosz
datetime datetime NOT NULL,
url varchar(200) NOT NULL UNIQUE,
language varchar(50) DEFAULT 'Polish' NOT NULL,
translator_id int,
teacher_id int NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (translator_id) REFERENCES Translator (id) ON DELETE SET NULL,
FOREIGN KEY (teacher_id) REFERENCES Teacher (id) ON DELETE NO ACTION,
CONSTRAINT not_negative_webinar_price CHECK (price >= 0),
)
CREATE TABLE StudentWebinar
(
student_id int NOT NULL,
webinar_id int NOT NULL,
payment_date date NOT NULL,
PRIMARY KEY (student_id, webinar_id),
FOREIGN KEY (student_id) REFERENCES Student (id) ON DELETE CASCADE,
FOREIGN KEY (webinar_id) REFERENCES Webinar (id) ON DELETE CASCADE,
)
CREATE TABLE Course
(
id int NOT NULL IDENTITY (1, 1),
title varchar(100) NOT NULL,
price int NOT NULL, --in Polish grosz
advance_price int NOT NULL, --in Polish grosz
subject varchar(100) NOT NULL,
language varchar(50) DEFAULT 'Polish' NOT NULL,
student_limit int NOT NULL,
PRIMARY KEY (id),
CONSTRAINT positive_course_price CHECK (price > 0),
CONSTRAINT not_negative_advance_price CHECK (advance_price >= 0),
CONSTRAINT positive_course_student_limit CHECK (student_limit > 0),
)
CREATE TABLE StudentCourse
(
student_id int NOT NULL,
course_id int NOT NULL,
advance_payment_date date NOT NULL,
full_payment_date date,
credit_date date,
certificate_post_date date,
PRIMARY KEY (student_id, course_id),
FOREIGN KEY (student_id) REFERENCES Student (id) ON DELETE CASCADE,
FOREIGN KEY (course_id) REFERENCES Course (id) ON DELETE CASCADE,
CONSTRAINT advance_payment_date_lowerEq_full_payment_date CHECK (advance_payment_date <= full_payment_date),
CONSTRAINT full_payment_date_lowerEq_credit_date CHECK (full_payment_date <= credit_date),
CONSTRAINT credit_date_lowerEq_certificate_post_date CHECK (credit_date <= certificate_post_date),
)
CREATE TABLE Room
(
id int NOT NULL IDENTITY (1, 1),
number varchar(10) NOT NULL,
building varchar(50) NOT NULL,
PRIMARY KEY (id),
)
CREATE TABLE Module
(
id int NOT NULL IDENTITY (1, 1),
course_id int NOT NULL,
type varchar(15) NOT NULL,
room_id int,
teacher_id int NOT NULL,
start_date date NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (course_id) REFERENCES Course (id) ON DELETE CASCADE,
FOREIGN KEY (room_id) REFERENCES Room (id) ON DELETE NO ACTION,
FOREIGN KEY (teacher_id) REFERENCES Teacher (id) ON DELETE NO ACTION,
CONSTRAINT type_and_room CHECK (
type = 'hybrid' OR
type = 'in_person' AND room_id IS NOT NULL OR
type IN ('online_sync', 'online_async') AND room_id IS NULL
),
)
CREATE TABLE Studies
(
id int NOT NULL IDENTITY (1, 1),
title varchar(100) NOT NULL,
syllabus varchar(5000),
registration_price int NOT NULL, --in Polish grosz
language varchar(50) DEFAULT 'Polish' NOT NULL,
student_limit int NOT NULL,
PRIMARY KEY (id),
CONSTRAINT non_negative_studies_registration_price CHECK (registration_price >= 0),
CONSTRAINT positive_studies_student_limit CHECK (student_limit > 0),
)
CREATE TABLE Semester
(
id int NOT NULL IDENTITY (1, 1),
number int NOT NULL,
studies_id int NOT NULL,
price int NOT NULL, --in Polish grosz
schedule_url varchar(200) NOT NULL,
start_date date NOT NULL,
end_date date NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (studies_id) REFERENCES Studies (id) ON DELETE NO ACTION,
CONSTRAINT number_between_1_and_12 CHECK (number > 0 AND number <= 12),
CONSTRAINT start_date_lower_end_date CHECK (start_date < end_date),
CONSTRAINT non_negative_semester_price CHECK (price >= 0),
UNIQUE (studies_id, number),
)
CREATE TABLE StudentSemester
(
student_id int NOT NULL,
semester_id int NOT NULL,
payment_date date NOT NULL,
PRIMARY KEY (student_id, semester_id),
FOREIGN KEY (student_id) REFERENCES Student (id) ON DELETE CASCADE,
FOREIGN KEY (semester_id) REFERENCES Semester (id) ON DELETE CASCADE,
)
CREATE TABLE StudentStudies
(
student_id int NOT NULL,
studies_id int NOT NULL,
registration_payment_date date NOT NULL, -- wpisowe
credit_date date,
certificate_post_date date,
PRIMARY KEY (student_id, studies_id),
FOREIGN KEY (student_id) REFERENCES Student (id) ON DELETE CASCADE,
FOREIGN KEY (studies_id) REFERENCES Studies (id) ON DELETE CASCADE,
CONSTRAINT registration_payment_date_lower_certificate_post_date CHECK (registration_payment_date < certificate_post_date),
)
CREATE TABLE Subject
(
id int NOT NULL IDENTITY (1, 1),
name varchar(200) NOT NULL,
semester_id int NOT NULL,
teacher_id int,
room_id int,
PRIMARY KEY (id),
FOREIGN KEY (semester_id) REFERENCES Semester (id) ON DELETE CASCADE,
FOREIGN KEY (teacher_id) REFERENCES Teacher (id) ON DELETE SET NULL,
FOREIGN KEY (room_id) REFERENCES Room (id) ON DELETE SET NULL,
)
CREATE TABLE Internship
(
id int NOT NULL IDENTITY (1, 1),
studies_id int NOT NULL,
date date NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (studies_id) REFERENCES Studies (id) ON DELETE CASCADE,
)
CREATE TABLE StudentInternship
(
student_id int NOT NULL,
internship_id int NOT NULL,
attended_days int DEFAULT 0 NOT NULL,
exam_result int,
PRIMARY KEY (student_id, internship_id),
FOREIGN KEY (student_id) REFERENCES Student (id) ON DELETE CASCADE,
FOREIGN KEY (internship_id) REFERENCES Internship (id) ON DELETE CASCADE,
CONSTRAINT not_negative_attended_days CHECK (attended_days >= 0),
CONSTRAINT exam_result_between_0_and_100 CHECK (exam_result >= 0 AND exam_result <= 100),
)
CREATE TABLE Meeting
(
id int NOT NULL IDENTITY (1, 1),
module_id int,
subject_id int,
url varchar(200),
datetime datetime NOT NULL,
length int DEFAULT 90 NOT NULL, --in minutes
type varchar(10) NOT NULL,
standalone_price int, --in Polish grosz
translator_id int,
substituting_teacher_id int DEFAULT NULL,
substituting_room_id int DEFAULT NULL,
student_limit int NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (module_id) REFERENCES Module (id) ON DELETE NO ACTION,
FOREIGN KEY (subject_id) REFERENCES Subject (id) ON DELETE NO ACTION,
FOREIGN KEY (translator_id) REFERENCES Translator (id) ON DELETE SET NULL,
FOREIGN KEY (substituting_teacher_id) REFERENCES Teacher (id) ON DELETE NO ACTION,
FOREIGN KEY (substituting_room_id) REFERENCES Room (id) ON DELETE NO ACTION,
CONSTRAINT polymorphic CHECK (
module_id IS NOT NULL AND subject_id IS NULL OR
module_id IS NULL AND subject_id IS NOT NULL OR
module_id IS NULL AND subject_id IS NULL
),
CONSTRAINT type_and_url CHECK (type = 'in_person' OR type IN ('online', 'video') AND url IS NOT NULL),
CONSTRAINT positive_student_limit CHECK (student_limit > 0),
CONSTRAINT positive_standalone_price CHECK (standalone_price is NULL OR standalone_price > 0),
)
CREATE TABLE StudentMeeting
(
student_id int NOT NULL,
meeting_id int NOT NULL,
payment_date date,
PRIMARY KEY (student_id, meeting_id),
FOREIGN KEY (student_id) REFERENCES Student (id) ON DELETE CASCADE,
FOREIGN KEY (meeting_id) REFERENCES Meeting (id) ON DELETE CASCADE,
)
CREATE TABLE StudentMeetingAttendance
(
student_id int NOT NULL,
meeting_id int NOT NULL,
PRIMARY KEY (student_id, meeting_id),
FOREIGN KEY (student_id) REFERENCES Student (id) ON DELETE CASCADE,
FOREIGN KEY (meeting_id) REFERENCES Meeting (id) ON DELETE CASCADE,
)
CREATE TABLE Basket
(
id int NOT NULL IDENTITY (1, 1),
student_id int NOT NULL,
payment_url varchar(200),
state varchar(15) NOT NULL,
create_date date NOT NULL,
payment_date date,
PRIMARY KEY (id),
FOREIGN KEY (student_id) REFERENCES Student (id) ON DELETE CASCADE,
CONSTRAINT state_enum CHECK (state IN ('open', 'pending_payment', 'success_payment', 'failed_payment')),
CONSTRAINT state_and_payment_url CHECK (state = 'open' OR payment_url IS NOT NULL ),
CONSTRAINT payment_date_greaterEq_create_date CHECK (payment_date IS NULL OR payment_date >= create_date),
)
CREATE TABLE BasketItem
(
basket_id int NOT NULL,
course_id int NULL,
meeting_id int NULL,
studies_id int NULL,
webinar_id int NULL,
UNIQUE (basket_id, course_id, meeting_id, studies_id, webinar_id),
FOREIGN KEY (basket_id) REFERENCES Basket (id) ON DELETE CASCADE,
FOREIGN KEY (course_id) REFERENCES Course (id),
FOREIGN KEY (meeting_id) REFERENCES Meeting (id),
FOREIGN KEY (studies_id) REFERENCES Studies (id),
FOREIGN KEY (webinar_id) REFERENCES Webinar (id),
CONSTRAINT polymorphism CHECK (
(course_id IS NULL AND meeting_id IS NULL AND studies_id IS NULL AND webinar_id IS NOT NULL) OR
(course_id IS NULL AND meeting_id IS NULL AND studies_id IS NOT NULL AND webinar_id IS NULL) OR
(course_id IS NULL AND meeting_id IS NOT NULL AND studies_id IS NULL AND webinar_id IS NULL) OR
(course_id IS NOT NULL AND meeting_id IS NULL AND studies_id IS NULL AND webinar_id IS NULL)
)
)
CREATE TABLE Parameter
(
name varchar(50) NOT NULL,
value varchar(50) NOT NULL,
date date NOT NULL,
PRIMARY KEY (name, date),
)
GO