--Create database Lab1 --use Lab1 CREATE TABLE Faculty FacPK int PRIMA

  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
--Create database [Lab1.3]
--use [Lab1.3]
CREATE TABLE Faculty (
FacPK int PRIMARY KEY,
[Name] nvarchar(50) UNIQUE NOT NULL,
DekanFK int, -- Если удаляется преподаватель, являющийся деканом, то ссылка на него устанавливается в NULL.
Building char(2),
Fund decimal(9, 2),
CONSTRAINT CK_Faculty_Building_Number_Legit CHECK(Building IN ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10')),
CONSTRAINT CK_Fund_Sufficient CHECK(Fund > 10000.00)
)
CREATE TABLE Department (
DepPK int PRIMARY KEY,
FacFK int NOT NULL, -- Нельзя удалить факультет, если на нем имеется хотя бы одна кафедра.
[Name] nvarchar(50) NOT NULL,
HeadFK int, -- Если удаляется преподаватель, являющийся зав. кафедрой, то ссылка на него устанавливается в NULL.
Building char(3),
Fund decimal(8, 2),
CONSTRAINT FK_FacFK
FOREIGN KEY(FacFK)
REFERENCES Faculty(FacPK),
CONSTRAINT CK_Department_Building_Number_Legit CHECK(Building IN ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10')),
CONSTRAINT CK_Fund_Within_Values CHECK(Fund >= 20000.00 AND Fund <= 100000.00),
CONSTRAINT UQ_FacFK_Name UNIQUE (FacFK, [Name])
)
CREATE TABLE Teacher (
TchPK int PRIMARY KEY,
DepFK int, --Если удаляется кафедра, то автоматически устанавливаются в NULL все ссылки на нее.
[Name] nvarchar(50) NOT NULL,
Post nvarchar(15),
Tel char(7),
Hiredate date NOT NULL,
Salary decimal(6, 2) NOT NULL,
Commision decimal(6, 2) DEFAULT(0),
ChiefFK int,
CONSTRAINT FK_DepFK_Teacher
FOREIGN KEY(DepFK)
REFERENCES Department(DepPK)
ON DELETE SET NULL,
CONSTRAINT CK_Predefined_Post
CHECK(Post IN('ассистент', 'преподаватель', 'доцент', 'профессор')),
CONSTRAINT CK_Hiredate_Legit CHECK(Hiredate > '19500101'),
CONSTRAINT CK_Salary_Suffisient CHECK(Salary > 1000),
CONSTRAINT CK_Commision_IsPositive CHECK(Commision >= 0),
--CONSTRAINT CK_Commision_Suffisient CHECK((Commision >= Salary * 0.5) AND (Commision + Salary >= 1000 AND Commision + Salary <= 3000)),
CONSTRAINT CK_ChiefFK_NotSelforinted CHECK(ChiefFK <> TchPK),
CONSTRAINT FK_ChiefFK FOREIGN KEY (ChiefFK)
REFERENCES Teacher(TchPK)
--ON DELETE SET NULL
)
ALTER TABLE Faculty
ADD FOREIGN KEY(DekanFK) REFERENCES Teacher(TchPK) ON DELETE SET NULL
ALTER TABLE Department
ADD FOREIGN KEY(HeadFK) REFERENCES Teacher(TchPK) ON DELETE SET NULL
CREATE TABLE Sgroup (
GrpPK int PRIMARY KEY,
DepFK int,
Course decimal(1),
Num decimal(3),
Quantity decimal(2),
Curator int,
Rating decimal(3),
CONSTRAINT FK_DepFK
FOREIGN KEY(DepFK)
REFERENCES Department(DepPK)
ON DELETE SET NULL,
CONSTRAINT CK_Course_Legit CHECK(Course BETWEEN 1 AND 6),
CONSTRAINT CK_Num_Legit CHECK(Num > 0 AND Num < 700),
CONSTRAINT CK_Quantity_Within CHECK(Quantity BETWEEN 1 AND 50),
CONSTRAINT FK_Curator
FOREIGN KEY(Curator)
REFERENCES Teacher(TchPK)
ON DELETE SET NULL,
CONSTRAINT CK_Rating_Within CHECK(Rating BETWEEN 1 AND 100),
CONSTRAINT UQ_DepFK_Num UNIQUE(DepFK, Num),
CONSTRAINT UQ_DepFK_Curator UNIQUE(DepFK, Curator)
)
CREATE TABLE [Subject] (
SbjPK int PRIMARY KEY,
[Name] varchar(50) UNIQUE NOT NULL
)
CREATE TABLE Room (
RoomPK int PRIMARY KEY,
Num int NOT NULL,
Seats decimal(3),
[Floor] decimal(2),
Building char(5),
CONSTRAINT CK_Seats_WIthin CHECK(Seats BETWEEN 1 AND 300),
CONSTRAINT CK_Floor_Within CHECK([Floor] BETWEEN 1 AND 16),
CONSTRAINT CK_Building_Legit CHECK(Building IN ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10')),
CONSTRAINT UQ_Num_Building UNIQUE(Num, Building)
)
CREATE TABLE Lecture (
TchFK int,
GrpFK int,
SbjFK int,
RoomFK int,
[Type] nvarchar(15) NOT NULL,
[Day] char(3) NOT NULL,
[Week] decimal(1) NOT NULL,
Lesson decimal(1) NOT NULL,
CONSTRAINT FK_TchFK
FOREIGN KEY(TchFK)
REFERENCES Teacher(TchPK)
ON DELETE SET NULL,
CONSTRAINT FK_GrpFK
FOREIGN KEY(GrpFK)
REFERENCES Sgroup(GrpPK)
ON DELETE CASCADE,
CONSTRAINT FK_SbjFK
FOREIGN KEY(SbjFK)
REFERENCES [Subject](SbjPK),
CONSTRAINT FK_RoomFK
FOREIGN KEY(RoomFK)
REFERENCES Room(RoomPK),
CONSTRAINT CK_Type_Correct
CHECK([Type] IN('лекция', 'лабораторная', 'семинар', 'пркатика')),
CONSTRAINT CK_Day_Correct
CHECK([Day] IN('пон', 'втр', 'срд', 'чет', 'пят' , 'суб', 'вск')),
CONSTRAINT CK_Week_Correct CHECK([Week] >= 1 AND [Week] <= 2),
CONSTRAINT CK_Lesson_Correct CHECK(Lesson BETWEEN 1 AND 8),
CONSTRAINT UQ_Lesson_For_Group UNIQUE(GrpFK, [Day], [Week], Lesson),
CONSTRAINT UQ_Lesson_For_Teacher UNIQUE(TchFK, [Day], [Week], Lesson)
)
INSERT INTO FACULTY VALUES
(1, 'Informatics', NULL, '1', 57398.00),
(2, 'Economy', NULL, '3' , 62681.00),
(3, 'Linguistics', NULL, '4', 49800.00),
(4, 'Computer Science', NULL, '2' , 81520.00),
(5, 'Chemistry', NULL, '5' , 31900.00)
INSERT INTO DEPARTMENT VALUES
(1,5,'SE',NULL,'5',20378.00),
(2,1,'CAD',NULL,'5',21000.00),
(3,3,'DBMS',NULL,'4',22000.00),
(4,2,'FTM',NULL,'3',20459.00),
(5,4,'TTH',NULL,'2',36900.00),
(6,1,'MMS',NULL,'4',29729.00),
(7,3,'LTL',NULL,'1', 24752.00),
(8,4,'PPOC',NULL,'1',21421.00),
(9,1,'NHTR',NULL,'4',25454.00),
(10,1,'MHGR',NULL,'2',28943.00),
(11,3,'MOOC',NULL,'4',27414.00),
(12,2,'RTF',NULL,'7',29248.00),
(13,5,'FAT',NULL,'7',29450.00),
(14,2,'NCCM',NULL,'7',29814.00)
INSERT INTO TEACHER VALUES
(1,1,'Andrew','ассистент','2281319','20010201',1250,80,24),-- Head
(2,1,'John','профессор','2281550 ','20010701',1400,150,4),
(3,2,'Bill','преподаватель','9012935','20021117',1240,80,1),
(4,2,'Albert','ассистент','4498081','20011111',1260,100,8),-- Head
(5,3,'Lamar','преподаватель','0965404','20021117',1000,280,1),
(6,4,'Kingston','преподаватель','6963002','20061117',1540,100,1),
(7,5,'Kristian','преподаватель','1777712','20021117',3240,350,4),
(8,2,'Ada','профессор','4075250','20021117',1940,130,NULL),-- DEKAN Fac1
(9,4,'Kayley','профессор','6864222','20041117',1240,180,NULL),-- DEKAN Fac2
(10,6,'Cloe','профессор','3311492','20021117',2200,200,4),
(11,3,'Kelly','преподаватель','7180545','20151117',1950,170,15),-- Head
(12,7,'Jamarcus','профессор','8755603','20041117',1740,350,4),
(13,4,'Amanda','преподаватель','0254198','20021117',1900,50,9),-- Head
(14,8,'Wilson','ассистент','4162636','20151117',1740,80,1),
(15,11,'Madilyn','профессор','2079257','20021117',1900,50, NULL),-- DEKAN Fac3
(16,5,'Nayeli','преподаватель','3559204','20061117',1900,50,19),-- Head
(17,9,'Carlie','преподаватель','6865611','20041117',1900,50,4),
(18,10,'Gabrielle','преподаватель','1983117','20021117',1900,50,4),
(19,5,'Titus','профессор','2108213','20021117',1900,50,NULL),-- DEKAN Fac4
(20,6,'Raiden','преподаватель','9215871','20021117',1900,50,8),-- Head
(21,11,'Brent','преподаватель','1193138','20021117',1900,50,4),
(22,12,'Malakai','преподаватель','0544036','20021117',1900,50,4),
(23,13,'Helena','преподаватель','4053542','20041117',1900,50,4),
(24,1,'Hannah','профессор','5368240','20021117',1900,50,NULL),-- DEKAN Fac5
(25,14,'Pierce','ассистент','7649789','20061117',1900,50,4),
(26,1,'Andy','преподаватель','7066178','20151117',1900,50,4),
(27,7,'Iyana','преподаватель','9867794','20041117',1900,50,15),-- Head
(28,2,'Arthur','преподаватель','7618538','20021117',1900,50,4),
(29,8,'Ernesto','преподаватель','9482840','20151117',1900,50,19),-- Head
(30,3,'Jade','ассистент','3330943','20021117',1900,50,4),
(31,9,'Monica','преподаватель','6851067','20021117',1900,50,8),-- Head
(32,10,'Braxton','преподаватель','7814909','20081117',1900,50,8),-- Head
(33,11,'Bradyn','преподаватель','3694426','20021117',1900,50,15),-- Head
(34,4,'Colton','преподаватель','6217583','20081117',1900,50,4),
(35,12,'Giancarlo','преподаватель','6957418','20081117',1900,50,9),-- Head
(36,5,'Camryn','преподаватель','6740639','20021117',1900,50,4),
(37,6,'Camila','ассистент','4423168','20021117',1900,50,4),
(38,13,'Morgan','преподаватель','2784966','20061117',1900,50,24),-- Head
(39,7,'Cassandra','ассистент','8316731','20161117',1900,50,4),
(40,14,'Trevon','преподаватель','3611268','20081117',1900,50,9)-- Head
INSERT INTO SGROUP VALUES
(1,1,1,101,33,2,20),
(2,5,1,102,35,3,22),
(3,3,2,205,20,6,15),
(4,4,4,405,25,9,37),
(5,6,4,406,18,14,15),
(6,1,2,206,21,11,15),
(7,3,4,407,32,5,15),
(8,5,3,306,30,8,15),
(9,2,1,205,36,12,15),
(10,4,3,307,17,10,15),
(11,2,1,205,15,13,15),
(12,6,4,408,34,7,15)
INSERT INTO [SUBJECT] VALUES
(1,'Pascal'),
(2,'C'),
(3,'OS'),
(4,'Inernet'),
(5,'Chemistry'),
(6,'TEU'),
(7,'Databases'),
(8,'Architecture'),
(9,'Kidle'),
(10,'Unification'),
(11,'Saturation'),
(12,'Sociology'),
(13,'English'),
(14,'Archeology'),
(15,'German')
INSERT INTO ROOM VALUES
(1,101,20,1,'5'),
(2,316,150,3,'4'),
(3,201,150,2,'8'),
(4,202,30,6,'9'),
(5,501,100,5,'6'),
(6,224,85,2,'3'),
(7,211,150,12,'2'),
(8,271,100,15,'2'),
(9,255,60,8,'6'),
(10,106,100,6,'1'),
(11,407,60,7,'1'),
(12,402,100,6,'2'),
(13,321,85,7,'4'),
(14,123,85,9,'7')
INSERT INTO LECTURE VALUES
(1,1,1,1,'лекция','пон',1,1),
(1,2,2,2,'лабораторная','пон',1,2),
(2,3,3,3,'лекция','втр',1,3),
(3,4,4,4,'практика','срд',1,4),
(4,4,5,5,'практика','чет',2,5),
(5,5,6,6,'семинар','пят',2,6),
(6,6,7,7,'семинар','срд',1,7),
(7,7,8,8,'практика','пят',2,8),
(8,8,9,9,'лабораторная','срд',1,1),
(9,9,10,10,'лекция','пят',2,2),
(10,10,11,11,'семинар','чет',1,3),
(11,11,12,12,'практика','срд',2,4),
(12,12,13,13,'лабораторная','пят',2,5),
(13,1,14,14,'практика','втр',2,6),
(14,2,15,1,'лекция','пят',2,7),
(15,3,16,2,'семинар','втр',1,8)
-- Update Faculty
UPDATE Faculty
SET DekanFK = 8
WHERE FacPK = 1
UPDATE Faculty
SET DekanFK = 9
WHERE FacPK = 2
UPDATE Faculty
SET DekanFK = 15
WHERE FacPK = 3
UPDATE Faculty
SET DekanFK = 19
WHERE FacPK = 4
UPDATE Faculty
SET DekanFK = 24
WHERE FacPK = 5
-- Update Deartment
UPDATE Department
SET HeadFK = 1
WHERE DepPK = 1
UPDATE Department
SET HeadFK = 4
WHERE DepPK = 2
UPDATE Department
SET HeadFK = 11
WHERE DepPK = 3
UPDATE Department
SET HeadFK = 13
WHERE DepPK = 4
UPDATE Department
SET HeadFK = 16
WHERE DepPK = 5
UPDATE Department
SET HeadFK = 20
WHERE DepPK = 6
UPDATE Department
SET HeadFK = 27
WHERE DepPK = 7
UPDATE Department
SET HeadFK = 29
WHERE DepPK = 8
UPDATE Department
SET HeadFK = 31
WHERE DepPK = 9
UPDATE Department
SET HeadFK = 32
WHERE DepPK = 10
UPDATE Department
SET HeadFK = 33
WHERE DepPK = 11
UPDATE Department
SET HeadFK = 35
WHERE DepPK = 12
UPDATE Department
SET HeadFK = 38
WHERE DepPK = 13
UPDATE Department
SET HeadFK = 40
WHERE DepPK = 14