-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTable Creation SQL.sql
1376 lines (1370 loc) · 40.3 KB
/
Table Creation SQL.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
USE [master]
GO
/****** Object: Database [GamerBase-S1G8-DEMO] Script Date: 2/19/2021 1:12:54 AM ******/
CREATE DATABASE [GamerBase-S1G8-DEMO]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'GamerData', FILENAME = N'D:\Database\MSSQL15.MSSQLSERVER\MSSQL\DATA\GamerBaseDemo.mdf' , SIZE = 10240KB , MAXSIZE = 25600KB , FILEGROWTH = 10%)
LOG ON
( NAME = N'GamerLog', FILENAME = N'D:\Database\MSSQL15.MSSQLSERVER\MSSQL\DATA\GamerBaseDemoLog.ldf' , SIZE = 5120KB , MAXSIZE = 25600KB , FILEGROWTH = 10%)
WITH CATALOG_COLLATION = DATABASE_DEFAULT
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET COMPATIBILITY_LEVEL = 150
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [GamerBase-S1G8-DEMO].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET ANSI_NULL_DEFAULT OFF
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET ANSI_NULLS OFF
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET ANSI_PADDING OFF
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET ANSI_WARNINGS OFF
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET ARITHABORT OFF
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET AUTO_CLOSE OFF
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET AUTO_SHRINK OFF
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET AUTO_UPDATE_STATISTICS ON
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET CURSOR_CLOSE_ON_COMMIT OFF
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET CURSOR_DEFAULT GLOBAL
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET CONCAT_NULL_YIELDS_NULL OFF
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET NUMERIC_ROUNDABORT OFF
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET QUOTED_IDENTIFIER OFF
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET RECURSIVE_TRIGGERS OFF
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET ENABLE_BROKER
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET DATE_CORRELATION_OPTIMIZATION OFF
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET TRUSTWORTHY OFF
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET ALLOW_SNAPSHOT_ISOLATION OFF
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET PARAMETERIZATION SIMPLE
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET READ_COMMITTED_SNAPSHOT OFF
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET HONOR_BROKER_PRIORITY OFF
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET RECOVERY FULL
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET MULTI_USER
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET PAGE_VERIFY CHECKSUM
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET DB_CHAINING OFF
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET TARGET_RECOVERY_TIME = 60 SECONDS
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET DELAYED_DURABILITY = DISABLED
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET ACCELERATED_DATABASE_RECOVERY = OFF
GO
EXEC sys.sp_db_vardecimal_storage_format N'GamerBase-S1G8-DEMO', N'ON'
GO
ALTER DATABASE [GamerBase-S1G8-DEMO] SET QUERY_STORE = OFF
GO
USE [GamerBase-S1G8-DEMO]
GO
/****** Object: User [robinsr2] Script Date: 2/19/2021 1:12:55 AM ******/
CREATE USER [robinsr2] FOR LOGIN [robinsr2] WITH DEFAULT_SCHEMA=[dbo]
GO
/****** Object: User [fryaj1] Script Date: 2/19/2021 1:12:55 AM ******/
CREATE USER [fryaj1] FOR LOGIN [fryaj1] WITH DEFAULT_SCHEMA=[dbo]
GO
ALTER ROLE [db_owner] ADD MEMBER [robinsr2]
GO
ALTER ROLE [db_owner] ADD MEMBER [fryaj1]
GO
/****** Object: UserDefinedFunction [dbo].[fn_GetConsoleID] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create function [dbo].[fn_GetConsoleID](@consoleName varchar(100))
returns int
begin
return (select ID as ID
from Console
where Name = @consoleName)
end
GO
/****** Object: UserDefinedFunction [dbo].[fn_GetGameID] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create function [dbo].[fn_GetGameID](@gameName varchar(100))
returns int
begin
return (select ID as ID
from Game
where Name = @gameName)
end
GO
/****** Object: UserDefinedFunction [dbo].[fn_GetListID] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE function [dbo].[fn_GetListID](@listName varchar(100))
returns int
begin
return (select ID as ID
from list
where Name = @listname)
end
GO
/****** Object: Table [dbo].[list] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[list](
[ID] [int] IDENTITY(0,1) NOT NULL,
[Name] [varchar](300) NOT NULL,
[OwnerUsername] [varchar](15) NULL,
CONSTRAINT [PK__list__3214EC27BF769EB6] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[GameAppearsOn] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[GameAppearsOn](
[GameID] [int] NOT NULL,
[ListID] [int] NOT NULL,
PRIMARY KEY CLUSTERED
(
[GameID] ASC,
[ListID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: UserDefinedFunction [dbo].[fn_GetGameLists] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create function [dbo].[fn_GetGameLists]()
returns table
as
return (
select distinct l.ID as ListID, l.Name as ListName
from GameAppearsOn g join list l on g.ListID = l.ID
)
GO
/****** Object: Table [dbo].[ConsoleAppearsOn] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ConsoleAppearsOn](
[ConsoleID] [int] NOT NULL,
[ListID] [int] NOT NULL,
PRIMARY KEY CLUSTERED
(
[ListID] ASC,
[ConsoleID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: UserDefinedFunction [dbo].[fn_GetConsoleLists] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE function [dbo].[fn_GetConsoleLists]()
returns table
as
return (
select distinct l.ID as ListID, l.Name as ListName
from ConsoleAppearsOn c join list l on c.ListID = l.ID
)
GO
/****** Object: Table [dbo].[Console] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Console](
[ID] [int] IDENTITY(0,1) NOT NULL,
[Name] [varchar](20) NOT NULL,
PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: UserDefinedFunction [dbo].[fn_ViewConsoleLists] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE function [dbo].[fn_ViewConsoleLists](@listID int)
returns table
as
return (
select Con.Name as ConsoleName
from ConsoleAppearsOn c join Console con on c.ConsoleID = con.ID
where c.ListID = @listID
)
GO
/****** Object: Table [dbo].[Game] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Game](
[ID] [int] IDENTITY(0,1) NOT NULL,
[Name] [varchar](50) NOT NULL,
[Description] [varchar](500) NULL,
[Genre] [varchar](30) NULL,
[ReleaseDate] [date] NULL,
[PublisherID] [int] NULL,
CONSTRAINT [PK__Game__3214EC27F738B4D8] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: UserDefinedFunction [dbo].[fn_ViewGameLists] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE function [dbo].[fn_ViewGameLists](@listID int)
returns table
as
return (
select game.Name as GameName
from GameAppearsOn g join Game game on g.GameID = game.ID
where g.ListID = @listID
)
GO
/****** Object: Table [dbo].[User] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[User](
[Username] [varchar](15) NOT NULL,
[PasswordSalt] [varchar](50) NULL,
[PasswordHash] [varchar](50) NULL,
PRIMARY KEY CLUSTERED
(
[Username] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: UserDefinedFunction [dbo].[fn_UserGameLists] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE function [dbo].[fn_UserGameLists](@user varchar(15))
returns table
as
return (
select distinct l.ID as ListId, l.Name as ListName, l.OwnerUsername as Owner
from [User] u join list l on u.Username = l.OwnerUsername join GameAppearsOn g on g.ListID = l.ID
where u.Username = @user
)
GO
/****** Object: UserDefinedFunction [dbo].[fn_UserConsoleLists] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE function [dbo].[fn_UserConsoleLists](@user varchar(15))
returns table
as
return (
select distinct l.ID as ListId, l.Name as ListName, l.OwnerUsername as Owner
from [User] u join list l on u.Username = l.OwnerUsername join ConsoleAppearsOn c on c.ListID = l.ID
where u.Username = @user
)
GO
/****** Object: Table [dbo].[Profile] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Profile](
[ID] [int] IDENTITY(0,1) NOT NULL,
[ProfileType] [varchar](30) NULL,
[ProfileName] [varchar](30) NOT NULL,
[OwnerUsername] [varchar](15) NULL,
PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: UserDefinedFunction [dbo].[fn_FetchProfileID] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE function [dbo].[fn_FetchProfileID](@user varchar(15), @profileType varchar(30), @profileName varchar(30))
returns table
as
return (
select p.ID
from Profile p
WHERE p.ProfileName = @profileName and p.ProfileType = @profileType and @user = p.OwnerUsername
)
GO
/****** Object: Table [dbo].[Rates] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Rates](
[GameID] [int] NOT NULL,
[RatingUser] [varchar](15) NOT NULL,
[Review] [varchar](200) NULL,
[Rating] [float] NULL,
PRIMARY KEY CLUSTERED
(
[GameID] ASC,
[RatingUser] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: View [dbo].[ReadRatings] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE view [dbo].[ReadRatings]
as
select RatingUser, Name as GameName, Review, Rating
from Rates r join Game g on r.GameID = g.ID
GO
/****** Object: UserDefinedFunction [dbo].[fn_UserReviews] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create function [dbo].[fn_UserReviews](@user varchar(15))
returns table
as
return (
select Name as GameName, Review, Rating
from Rates r join [User] u on r.RatingUser = u.Username join Game g on r.GameID = g.ID
where u.Username = @user
)
GO
/****** Object: Table [dbo].[PlayedOn] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[PlayedOn](
[GameID] [int] NOT NULL,
[ConsoleID] [int] NOT NULL,
PRIMARY KEY CLUSTERED
(
[GameID] ASC,
[ConsoleID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: UserDefinedFunction [dbo].[fn_PlayableOnConsole] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create function [dbo].[fn_PlayableOnConsole](@gameID int)
returns table
as
return (
select c.Name as Console
from Game g join PlayedOn p on g.ID = p.GameID
join Console c on p.ConsoleID = c.ID
where g.ID = @gameID
)
GO
/****** Object: UserDefinedFunction [dbo].[fn_GamesWithRatingAbove] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE function [dbo].[fn_GamesWithRatingAbove](@rating float)
returns table
as
Return (
Select g.Name, g.Description, g.Genre, r.Rating
from Game g join Rates r on g.ID = r.GameID
where r.Rating >= @rating
)
GO
/****** Object: UserDefinedFunction [dbo].[fn_UserProfiles] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE function [dbo].[fn_UserProfiles](@user varchar(15))
returns table
as
return (
select p.OwnerUsername, p.ProfileType, p.ProfileName
from [User] u join Profile p on u.Username = p.OwnerUsername
where u.Username = @user
)
GO
/****** Object: UserDefinedFunction [dbo].[fn_GamesOnConsole] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create function [dbo].[fn_GamesOnConsole](@console int)
returns table
as
return (
select c.Name as Console, g.Name as Game, g.Genre
from Game g join PlayedOn p on g.ID = p.GameID
join Console c on p.ConsoleID = c.ID
where c.ID = @console
)
GO
/****** Object: UserDefinedFunction [dbo].[fn_GamesWithRatingBelow] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create function [dbo].[fn_GamesWithRatingBelow](@rating float)
returns table
as
Return (
Select g.Name, g.Description, g.Genre, r.Rating
from Game g join Rates r on g.ID = r.GameID
where r.Rating <= @rating
)
GO
/****** Object: Table [dbo].[FriendsWith] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[FriendsWith](
[Friendee] [varchar](15) NOT NULL,
[Friender] [varchar](15) NOT NULL,
PRIMARY KEY CLUSTERED
(
[Friendee] ASC,
[Friender] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: UserDefinedFunction [dbo].[fn_ViewFriendsList] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[fn_ViewFriendsList]
(@Username nvarchar(20) = NULL)
RETURNS table
AS
Return (SELECT Friendee
FROM FriendsWith
WHERE Friender = @Username)
GO
/****** Object: UserDefinedFunction [dbo].[fn_AvailableGamesOnConsole] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE Function [dbo].[fn_AvailableGamesOnConsole](@consoleID int)
Returns Table
As
Return (Select Game.ID, Game.Name
From Game Join PlayedOn on Game.ID = PlayedOn.GameID
Where PlayedOn.ConsoleID = @consoleID)
GO
/****** Object: Table [dbo].[Publisher] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Publisher](
[ID] [int] IDENTITY(0,1) NOT NULL,
[Name] [varchar](25) NOT NULL,
PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: UserDefinedFunction [dbo].[fn_SortGames] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[fn_SortGames]
(@GameName nvarchar(64) = null,
@Publisher nvarchar(64) = null,
@Genre nvarchar(32) = null,
@ReleaseDate Date = null)
RETURNS table
AS
Return (SELECT Game.Name as name, Description, Genre, Publisher.Name as Publisher,ReleaseDate
FROM Game join Publisher on Game.PublisherID = Publisher.ID
WHERE Game.Name = @GameName OR Genre = @Genre OR ReleaseDate = @ReleaseDate OR PublisherID = (SELECT ID FROM Publisher WHERE Name = @Publisher))
GO
/****** Object: Table [dbo].[OwnsGame] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[OwnsGame](
[GameID] [int] NOT NULL,
[OwningUser] [varchar](15) NOT NULL,
PRIMARY KEY CLUSTERED
(
[GameID] ASC,
[OwningUser] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: UserDefinedFunction [dbo].[fn_viewOwnedGames] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create FUNCTION [dbo].[fn_viewOwnedGames]
(@user nvarchar(64))
RETURNS table
AS
Return (Select *
from OwnsGame og join Game g on og.GameID = g.ID
where og.OwningUser = @user)
GO
SET ANSI_PADDING ON
GO
/****** Object: Index [ConsoleNameIndex] Script Date: 2/19/2021 1:12:55 AM ******/
CREATE NONCLUSTERED INDEX [ConsoleNameIndex] ON [dbo].[Console]
(
[Name] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/****** Object: Index [GameNameIndex] Script Date: 2/19/2021 1:12:55 AM ******/
CREATE NONCLUSTERED INDEX [GameNameIndex] ON [dbo].[Game]
(
[Name] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/****** Object: Index [ListNameIndex] Script Date: 2/19/2021 1:12:55 AM ******/
CREATE NONCLUSTERED INDEX [ListNameIndex] ON [dbo].[list]
(
[Name] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
GO
ALTER TABLE [dbo].[ConsoleAppearsOn] WITH CHECK ADD CONSTRAINT [FK__ConsoleAp__Conso__44FF419A] FOREIGN KEY([ConsoleID])
REFERENCES [dbo].[Console] ([ID])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[ConsoleAppearsOn] CHECK CONSTRAINT [FK__ConsoleAp__Conso__44FF419A]
GO
ALTER TABLE [dbo].[ConsoleAppearsOn] WITH CHECK ADD CONSTRAINT [FK__ConsoleAp__ListI__45F365D3] FOREIGN KEY([ListID])
REFERENCES [dbo].[list] ([ID])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[ConsoleAppearsOn] CHECK CONSTRAINT [FK__ConsoleAp__ListI__45F365D3]
GO
ALTER TABLE [dbo].[FriendsWith] WITH CHECK ADD FOREIGN KEY([Friendee])
REFERENCES [dbo].[User] ([Username])
GO
ALTER TABLE [dbo].[FriendsWith] WITH CHECK ADD FOREIGN KEY([Friender])
REFERENCES [dbo].[User] ([Username])
GO
ALTER TABLE [dbo].[Game] WITH CHECK ADD CONSTRAINT [FK__Game__PublisherI__286302EC] FOREIGN KEY([PublisherID])
REFERENCES [dbo].[Publisher] ([ID])
GO
ALTER TABLE [dbo].[Game] CHECK CONSTRAINT [FK__Game__PublisherI__286302EC]
GO
ALTER TABLE [dbo].[GameAppearsOn] WITH NOCHECK ADD CONSTRAINT [FK__GameAppea__GameI__412EB0B6] FOREIGN KEY([GameID])
REFERENCES [dbo].[Game] ([ID])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[GameAppearsOn] NOCHECK CONSTRAINT [FK__GameAppea__GameI__412EB0B6]
GO
ALTER TABLE [dbo].[GameAppearsOn] WITH CHECK ADD CONSTRAINT [FK__GameAppea__ListI__4222D4EF] FOREIGN KEY([ListID])
REFERENCES [dbo].[list] ([ID])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[GameAppearsOn] CHECK CONSTRAINT [FK__GameAppea__ListI__4222D4EF]
GO
ALTER TABLE [dbo].[list] WITH CHECK ADD CONSTRAINT [FK__list__OwnerUsern__2B3F6F97] FOREIGN KEY([OwnerUsername])
REFERENCES [dbo].[User] ([Username])
GO
ALTER TABLE [dbo].[list] CHECK CONSTRAINT [FK__list__OwnerUsern__2B3F6F97]
GO
ALTER TABLE [dbo].[OwnsGame] WITH CHECK ADD CONSTRAINT [FK__OwnsGame__GameID__398D8EEE] FOREIGN KEY([GameID])
REFERENCES [dbo].[Game] ([ID])
GO
ALTER TABLE [dbo].[OwnsGame] CHECK CONSTRAINT [FK__OwnsGame__GameID__398D8EEE]
GO
ALTER TABLE [dbo].[OwnsGame] WITH CHECK ADD FOREIGN KEY([OwningUser])
REFERENCES [dbo].[User] ([Username])
GO
ALTER TABLE [dbo].[PlayedOn] WITH CHECK ADD FOREIGN KEY([ConsoleID])
REFERENCES [dbo].[Console] ([ID])
GO
ALTER TABLE [dbo].[PlayedOn] WITH CHECK ADD CONSTRAINT [FK__PlayedOn__GameID__3D5E1FD2] FOREIGN KEY([GameID])
REFERENCES [dbo].[Game] ([ID])
GO
ALTER TABLE [dbo].[PlayedOn] CHECK CONSTRAINT [FK__PlayedOn__GameID__3D5E1FD2]
GO
ALTER TABLE [dbo].[Profile] WITH CHECK ADD FOREIGN KEY([OwnerUsername])
REFERENCES [dbo].[User] ([Username])
GO
ALTER TABLE [dbo].[Rates] WITH CHECK ADD CONSTRAINT [FK__Rates__GameID__35BCFE0A] FOREIGN KEY([GameID])
REFERENCES [dbo].[Game] ([ID])
GO
ALTER TABLE [dbo].[Rates] CHECK CONSTRAINT [FK__Rates__GameID__35BCFE0A]
GO
ALTER TABLE [dbo].[Rates] WITH CHECK ADD FOREIGN KEY([RatingUser])
REFERENCES [dbo].[User] ([Username])
GO
/****** Object: StoredProcedure [dbo].[AddConsoleToList] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[AddConsoleToList]
@consoleID int,
@listID int
as
if(not exists(select * from Console where ID = @consoleID))
begin
print('@consoleID must exist in the console table')
return 1
end
if(not exists(select * from list where ID = @listID))
begin
print('@list must be an existing table')
return 2
end
if(exists(select * from ConsoleAppearsOn where ListID = @listID and ConsoleID = @consoleID))
begin
print('That console is already on the list!')
return 3
end
insert into ConsoleAppearsOn(ConsoleID, ListID)
values(@consoleID,@listID)
return 0
GO
/****** Object: StoredProcedure [dbo].[AddGameToList] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[AddGameToList]
@listID int,@gameID int
as
if(not exists(select * from list where ID = @listID))
begin
print('@listID must be an existing list')
return 1
end
if(not exists(select * from Game where ID = @gameID))
begin
print('@gameId must exist in the Games Table')
return 2
end
if(exists(select * from GameAppearsOn where ListID = @listID and GameID = @gameID))
begin
print('That game is already on the list!')
return 3
end
insert into GameAppearsOn(ListID,GameID)
values(@ListID,@gameID)
return 0
GO
/****** Object: StoredProcedure [dbo].[AddOwnedGame] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[AddOwnedGame]
@gameID int, @user varchar(15)
as
if(not exists(select * from Game where ID = @gameID))
begin
print('@gameId must exist in the Games Table')
return 1
end
if(not exists(select * from [User] where Username = @user))
begin
print('@user must be an existing user')
return 2
end
if(exists(select * from OwnsGame where OwningUser = @user and @gameID = GameID))
begin
print('You already own that game!')
return 3
end
insert into OwnsGame(GameID,OwningUser)
values(@gameID,@user)
GO
/****** Object: StoredProcedure [dbo].[CreateConsole] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[CreateConsole]
@consoleName varchar(20)
as
if(@consoleName is null)
begin
print('@consoleName cannot be null')
return 1
end
if(exists (select * from Console where Name = @consoleName))
begin
print('That console already exists')
return 2
end
insert into Console(Name)
values(@consoleName)
GO
/****** Object: StoredProcedure [dbo].[CreateFriendship] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[CreateFriendship]
@frienderUsername varchar(15),
@friendeeUsername varchar(15)
as
if(@friendeeUsername is null)
begin
print('@frienderUsername cannot be null')
return 1
end
if(not exists(select * from [User] where Username = @friendeeUsername))
begin
print('@friendeeUsername must be an existing user')
return 2
end
if(exists (select * from FriendsWith where Friendee = @friendeeUsername and Friender = @frienderUsername))
begin
print('You are already friends with that user')
return 3
end
insert into FriendsWith(Friendee, Friender)
values (@friendeeUsername, @frienderUsername)
insert into FriendsWith(Friendee, Friender)
values (@frienderUsername,@friendeeUsername)
GO
/****** Object: StoredProcedure [dbo].[CreateGame] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[CreateGame]
@gameName varchar(50),
@gameDescription varchar(500),
@gameGenre varchar(30),
@gameReleaseDate date,
@gamePublisher int
as
if(@gameName = '')
begin
print('@gameName cannot be null or empty')
return 1
end
if(@gamePublisher is not null and
@gamePublisher not in (select ID from Publisher where ID = @gamePublisher))
begin
print('@gamePublisher has to be null or is an existing Publisher')
return 2
end
if(exists (select * from Game where Name = @gameName))
begin
print('A game with that name already exists')
return 3
end
insert into Game([Name], [Description], Genre, ReleaseDate, PublisherID)
Values(@gameName,@gameDescription,@gameGenre,@gameReleaseDate,@gamePublisher)
return 0
GO
/****** Object: StoredProcedure [dbo].[CreateList] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[CreateList]
@listName varchar(300), @user varchar(15)
as
if(not exists(select * from [User] where Username = @user))
begin
print('@user must be an existing user')
return 1
end
if(@listName is null)
begin
print('@listName cannot be null');
return 2
end
if(exists (select * from list where Name = @listName))
begin
print('A list with that name already exists')
return 3
end
insert into list([Name],OwnerUsername)
values(@listName,@user)
return 0
GO
/****** Object: StoredProcedure [dbo].[CreatePlayedOn] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[CreatePlayedOn]
@gameID varchar(30),
@consoleID varchar(30)
as
if(not exists(select * from Game where ID = @gameID))
begin
print('@gameID must exist in the Game table')
return 1
end
if(not exists(select * from Console where ID = @consoleID))
begin
print('@consoleID must exist in the Console table')
return 2
end
if(exists(select * from PlayedOn where GameID = @gameID and ConsoleID = @consoleID))
begin
print('PlayedOn connection already exists')
return 3
end
insert into PlayedOn(GameID,ConsoleID)
values(@gameID,@consoleID)
return 0
GO
/****** Object: StoredProcedure [dbo].[CreateProfile] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[CreateProfile]
@profileType varchar(30),
@profileName varchar(30),
@OwnerUsername varchar(15)
as
if(@profileName is null)
begin
print('@profileName cannot be null')
return 1
end
if(not exists(select * from [User] where Username = @OwnerUsername))
begin
print('@OwnerUsername must be an existing User')
return 2
end
if(exists(select * from Profile where Profile.ProfileName = @profileName))
begin
print('profile already exists')
return 3
end
insert into Profile(ProfileType, ProfileName, OwnerUsername)
values(@profileType, @profileName, @OwnerUsername)
return 0
GO
/****** Object: StoredProcedure [dbo].[CreatePublisher] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[CreatePublisher] @name varchar(25)
as
if(@name is null)
begin
print('@name cannot be null')
return 1
end
insert into Publisher
values(@name)
return 0
GO
/****** Object: StoredProcedure [dbo].[CreateRating] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[CreateRating]
@gameID int, @user varchar(15), @review varchar(200), @rating float
as
if(not exists(select * from Game where ID = @gameID))
begin
print('@gameId must exist in the Games Table')
return 1
end
if(not exists(select * from [User] where Username = @user))
begin
print('@user must be an existing user')
return 2
end
if @rating > 5 or @rating < 0
begin
print('@rating must be inbetween 0 and 5')
return 3
end
if(exists(select * from Rates where Rates.GameID = @gameID and Rates.RatingUser = @user))
begin
print('rating already exists')
return 4
end
insert into rates (GameID,RatingUser,Review,Rating)
values(@gameID,@user,@review,@rating)
GO
/****** Object: StoredProcedure [dbo].[DeleteList] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[DeleteList] @listID int
as
if(not exists(select * from list where ID = @listID))
begin
print('Cannot delete a list that doesn''t exist')
return 1
end
delete from list where ID = @listID
return 0
GO
/****** Object: StoredProcedure [dbo].[DeleteProfile] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE Procedure [dbo].[DeleteProfile]
@profileName nvarchar(30),
@profileType nvarchar(30)
as
if(not exists(select * from Profile where ProfileName = @profileName))
begin
print('Profile must exist in the profile table')
return 1
end
if(not exists(select * from Profile where ProfileType = @profileType))
begin
print('Profile must exist in the profile table')
return 2
end
delete from Profile where ProfileName = @profileName and ProfileType = @profileType
return 0
GO
/****** Object: StoredProcedure [dbo].[DeleteRating] Script Date: 2/19/2021 1:12:55 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[DeleteRating]
@gameID int, @user varchar(15)
as
if(not exists(select * from Rates where RatingUser = @user and GameID = @gameID))
begin
print('Must select a valid review')
return 1
end
delete from Rates where GameID = @gameID and RatingUser = @user