-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.sql
430 lines (386 loc) · 12.8 KB
/
script.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
IF OBJECT_ID(N'[__EFMigrationsHistory]') IS NULL
BEGIN
CREATE TABLE [__EFMigrationsHistory] (
[MigrationId] nvarchar(150) NOT NULL,
[ProductVersion] nvarchar(32) NOT NULL,
CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId])
);
END;
GO
BEGIN TRANSACTION;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231219164424_Initial'
)
BEGIN
CREATE TABLE [Conferences] (
[ConferenceID] int NOT NULL IDENTITY,
[ConferenceName] nvarchar(50) NOT NULL,
CONSTRAINT [PK_Conferences] PRIMARY KEY ([ConferenceID])
);
END;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231219164424_Initial'
)
BEGIN
CREATE TABLE [Teams] (
[TeamID] int NOT NULL IDENTITY,
[TeamName] nvarchar(100) NOT NULL,
[Coach] nvarchar(max) NULL,
[City] nvarchar(100) NOT NULL,
[ConferenceID] int NULL,
CONSTRAINT [PK_Teams] PRIMARY KEY ([TeamID]),
CONSTRAINT [FK_Teams_Conferences_ConferenceID] FOREIGN KEY ([ConferenceID]) REFERENCES [Conferences] ([ConferenceID])
);
END;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231219164424_Initial'
)
BEGIN
CREATE TABLE [Players] (
[Id] int NOT NULL IDENTITY,
[FirstName] nvarchar(50) NOT NULL,
[LastName] nvarchar(50) NOT NULL,
[Height] decimal(18,2) NOT NULL,
[Weight] decimal(18,2) NOT NULL,
[DateOfBirth] datetime2 NOT NULL,
[Position] nvarchar(max) NULL,
[PointsPerGame] decimal(18,2) NOT NULL,
[ReboundsPerGame] decimal(18,2) NOT NULL,
[AssistsPerGame] decimal(18,2) NOT NULL,
[TeamID] int NULL,
CONSTRAINT [PK_Players] PRIMARY KEY ([Id]),
CONSTRAINT [FK_Players_Teams_TeamID] FOREIGN KEY ([TeamID]) REFERENCES [Teams] ([TeamID])
);
END;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231219164424_Initial'
)
BEGIN
CREATE INDEX [IX_Players_TeamID] ON [Players] ([TeamID]);
END;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231219164424_Initial'
)
BEGIN
CREATE INDEX [IX_Teams_ConferenceID] ON [Teams] ([ConferenceID]);
END;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231219164424_Initial'
)
BEGIN
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20231219164424_Initial', N'8.0.0');
END;
GO
COMMIT;
GO
BEGIN TRANSACTION;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231220152012_FormatDateChange'
)
BEGIN
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20231220152012_FormatDateChange', N'8.0.0');
END;
GO
COMMIT;
GO
BEGIN TRANSACTION;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231220152622_PlayersDecimalsChange'
)
BEGIN
DECLARE @var0 sysname;
SELECT @var0 = [d].[name]
FROM [sys].[default_constraints] [d]
INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id]
WHERE ([d].[parent_object_id] = OBJECT_ID(N'[Players]') AND [c].[name] = N'Weight');
IF @var0 IS NOT NULL EXEC(N'ALTER TABLE [Players] DROP CONSTRAINT [' + @var0 + '];');
ALTER TABLE [Players] ALTER COLUMN [Weight] decimal(5,1) NOT NULL;
END;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231220152622_PlayersDecimalsChange'
)
BEGIN
DECLARE @var1 sysname;
SELECT @var1 = [d].[name]
FROM [sys].[default_constraints] [d]
INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id]
WHERE ([d].[parent_object_id] = OBJECT_ID(N'[Players]') AND [c].[name] = N'ReboundsPerGame');
IF @var1 IS NOT NULL EXEC(N'ALTER TABLE [Players] DROP CONSTRAINT [' + @var1 + '];');
ALTER TABLE [Players] ALTER COLUMN [ReboundsPerGame] decimal(3,1) NOT NULL;
END;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231220152622_PlayersDecimalsChange'
)
BEGIN
DECLARE @var2 sysname;
SELECT @var2 = [d].[name]
FROM [sys].[default_constraints] [d]
INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id]
WHERE ([d].[parent_object_id] = OBJECT_ID(N'[Players]') AND [c].[name] = N'PointsPerGame');
IF @var2 IS NOT NULL EXEC(N'ALTER TABLE [Players] DROP CONSTRAINT [' + @var2 + '];');
ALTER TABLE [Players] ALTER COLUMN [PointsPerGame] decimal(3,1) NOT NULL;
END;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231220152622_PlayersDecimalsChange'
)
BEGIN
DECLARE @var3 sysname;
SELECT @var3 = [d].[name]
FROM [sys].[default_constraints] [d]
INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id]
WHERE ([d].[parent_object_id] = OBJECT_ID(N'[Players]') AND [c].[name] = N'Height');
IF @var3 IS NOT NULL EXEC(N'ALTER TABLE [Players] DROP CONSTRAINT [' + @var3 + '];');
ALTER TABLE [Players] ALTER COLUMN [Height] decimal(3,2) NOT NULL;
END;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231220152622_PlayersDecimalsChange'
)
BEGIN
DECLARE @var4 sysname;
SELECT @var4 = [d].[name]
FROM [sys].[default_constraints] [d]
INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id]
WHERE ([d].[parent_object_id] = OBJECT_ID(N'[Players]') AND [c].[name] = N'AssistsPerGame');
IF @var4 IS NOT NULL EXEC(N'ALTER TABLE [Players] DROP CONSTRAINT [' + @var4 + '];');
ALTER TABLE [Players] ALTER COLUMN [AssistsPerGame] decimal(3,1) NOT NULL;
END;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231220152622_PlayersDecimalsChange'
)
BEGIN
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20231220152622_PlayersDecimalsChange', N'8.0.0');
END;
GO
COMMIT;
GO
BEGIN TRANSACTION;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231221150505_CorrectIdsNames'
)
BEGIN
ALTER TABLE [Players] DROP CONSTRAINT [FK_Players_Teams_TeamID];
END;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231221150505_CorrectIdsNames'
)
BEGIN
ALTER TABLE [Teams] DROP CONSTRAINT [FK_Teams_Conferences_ConferenceID];
END;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231221150505_CorrectIdsNames'
)
BEGIN
EXEC sp_rename N'[Teams].[ConferenceID]', N'ConferenceId', N'COLUMN';
END;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231221150505_CorrectIdsNames'
)
BEGIN
EXEC sp_rename N'[Teams].[TeamID]', N'Id', N'COLUMN';
END;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231221150505_CorrectIdsNames'
)
BEGIN
EXEC sp_rename N'[Teams].[IX_Teams_ConferenceID]', N'IX_Teams_ConferenceId', N'INDEX';
END;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231221150505_CorrectIdsNames'
)
BEGIN
EXEC sp_rename N'[Players].[TeamID]', N'TeamId', N'COLUMN';
END;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231221150505_CorrectIdsNames'
)
BEGIN
EXEC sp_rename N'[Players].[IX_Players_TeamID]', N'IX_Players_TeamId', N'INDEX';
END;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231221150505_CorrectIdsNames'
)
BEGIN
EXEC sp_rename N'[Conferences].[ConferenceID]', N'Id', N'COLUMN';
END;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231221150505_CorrectIdsNames'
)
BEGIN
ALTER TABLE [Players] ADD CONSTRAINT [FK_Players_Teams_TeamId] FOREIGN KEY ([TeamId]) REFERENCES [Teams] ([Id]);
END;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231221150505_CorrectIdsNames'
)
BEGIN
ALTER TABLE [Teams] ADD CONSTRAINT [FK_Teams_Conferences_ConferenceId] FOREIGN KEY ([ConferenceId]) REFERENCES [Conferences] ([Id]);
END;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231221150505_CorrectIdsNames'
)
BEGIN
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20231221150505_CorrectIdsNames', N'8.0.0');
END;
GO
COMMIT;
GO
BEGIN TRANSACTION;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231222150439_AddedForeignKeysAsProps'
)
BEGIN
ALTER TABLE [Players] DROP CONSTRAINT [FK_Players_Teams_TeamId];
END;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231222150439_AddedForeignKeysAsProps'
)
BEGIN
ALTER TABLE [Teams] DROP CONSTRAINT [FK_Teams_Conferences_ConferenceId];
END;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231222150439_AddedForeignKeysAsProps'
)
BEGIN
DROP INDEX [IX_Teams_ConferenceId] ON [Teams];
DECLARE @var5 sysname;
SELECT @var5 = [d].[name]
FROM [sys].[default_constraints] [d]
INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id]
WHERE ([d].[parent_object_id] = OBJECT_ID(N'[Teams]') AND [c].[name] = N'ConferenceId');
IF @var5 IS NOT NULL EXEC(N'ALTER TABLE [Teams] DROP CONSTRAINT [' + @var5 + '];');
EXEC(N'UPDATE [Teams] SET [ConferenceId] = 0 WHERE [ConferenceId] IS NULL');
ALTER TABLE [Teams] ALTER COLUMN [ConferenceId] int NOT NULL;
ALTER TABLE [Teams] ADD DEFAULT 0 FOR [ConferenceId];
CREATE INDEX [IX_Teams_ConferenceId] ON [Teams] ([ConferenceId]);
END;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231222150439_AddedForeignKeysAsProps'
)
BEGIN
DECLARE @var6 sysname;
SELECT @var6 = [d].[name]
FROM [sys].[default_constraints] [d]
INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id]
WHERE ([d].[parent_object_id] = OBJECT_ID(N'[Teams]') AND [c].[name] = N'Coach');
IF @var6 IS NOT NULL EXEC(N'ALTER TABLE [Teams] DROP CONSTRAINT [' + @var6 + '];');
ALTER TABLE [Teams] ALTER COLUMN [Coach] nvarchar(50) NULL;
END;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231222150439_AddedForeignKeysAsProps'
)
BEGIN
DROP INDEX [IX_Players_TeamId] ON [Players];
DECLARE @var7 sysname;
SELECT @var7 = [d].[name]
FROM [sys].[default_constraints] [d]
INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id]
WHERE ([d].[parent_object_id] = OBJECT_ID(N'[Players]') AND [c].[name] = N'TeamId');
IF @var7 IS NOT NULL EXEC(N'ALTER TABLE [Players] DROP CONSTRAINT [' + @var7 + '];');
EXEC(N'UPDATE [Players] SET [TeamId] = 0 WHERE [TeamId] IS NULL');
ALTER TABLE [Players] ALTER COLUMN [TeamId] int NOT NULL;
ALTER TABLE [Players] ADD DEFAULT 0 FOR [TeamId];
CREATE INDEX [IX_Players_TeamId] ON [Players] ([TeamId]);
END;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231222150439_AddedForeignKeysAsProps'
)
BEGIN
DECLARE @var8 sysname;
SELECT @var8 = [d].[name]
FROM [sys].[default_constraints] [d]
INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id]
WHERE ([d].[parent_object_id] = OBJECT_ID(N'[Players]') AND [c].[name] = N'Position');
IF @var8 IS NOT NULL EXEC(N'ALTER TABLE [Players] DROP CONSTRAINT [' + @var8 + '];');
ALTER TABLE [Players] ALTER COLUMN [Position] nvarchar(50) NULL;
END;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231222150439_AddedForeignKeysAsProps'
)
BEGIN
ALTER TABLE [Players] ADD CONSTRAINT [FK_Players_Teams_TeamId] FOREIGN KEY ([TeamId]) REFERENCES [Teams] ([Id]) ON DELETE CASCADE;
END;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231222150439_AddedForeignKeysAsProps'
)
BEGIN
ALTER TABLE [Teams] ADD CONSTRAINT [FK_Teams_Conferences_ConferenceId] FOREIGN KEY ([ConferenceId]) REFERENCES [Conferences] ([Id]) ON DELETE CASCADE;
END;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20231222150439_AddedForeignKeysAsProps'
)
BEGIN
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20231222150439_AddedForeignKeysAsProps', N'8.0.0');
END;
GO
COMMIT;
GO