From 9ba8a99e820a0b747bb7e6f70e7ed6e89a88d628 Mon Sep 17 00:00:00 2001 From: Gnarwhal Date: Thu, 4 Feb 2021 10:56:33 -0500 Subject: [PATCH] Updated foreign keys in tables. --- sql/CreateTables.sql | 79 ++++++++++++++++++++++++++++++++++++------- sql/CreateUserSP.sql | Bin 0 -> 804 bytes 2 files changed, 66 insertions(+), 13 deletions(-) create mode 100644 sql/CreateUserSP.sql diff --git a/sql/CreateTables.sql b/sql/CreateTables.sql index a705e57..beb45bb 100644 --- a/sql/CreateTables.sql +++ b/sql/CreateTables.sql @@ -1,3 +1,32 @@ +------ DROP ALL TABLES ------ +-- +-- Courtesy of: https://stackoverflow.com/questions/8439650/how-to-drop-all-tables-in-a-sql-server-database +-- +----------------------------- + +--DECLARE @Sql NVARCHAR(500) DECLARE @Cursor CURSOR + +--SET @Cursor = CURSOR FAST_FORWARD FOR +--SELECT DISTINCT sql = 'ALTER TABLE [' + tc2.TABLE_SCHEMA + '].[' + tc2.TABLE_NAME + '] DROP [' + rc1.CONSTRAINT_NAME + '];' +--FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc1 +--LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc2 ON tc2.CONSTRAINT_NAME =rc1.CONSTRAINT_NAME + +--OPEN @Cursor FETCH NEXT FROM @Cursor INTO @Sql + +--WHILE (@@FETCH_STATUS = 0) +--BEGIN +--Exec sp_executesql @Sql +--FETCH NEXT FROM @Cursor INTO @Sql +--END + +--CLOSE @Cursor DEALLOCATE @Cursor +--GO + +--EXEC sp_MSforeachtable 'DROP TABLE ?' +--GO + +----------------------------- + CREATE TABLE [User] ( ID INT IDENTITY(0, 1) NOT NULL, Email VARCHAR(254) NOT NULL, @@ -28,6 +57,8 @@ CREATE TABLE [Achievement] ( Thumbnail VARCHAR(256) NULL PRIMARY KEY(ID) FOREIGN KEY(GameID) REFERENCES [Game](ID) + ON UPDATE CASCADE + ON DELETE CASCADE ) CREATE TABLE [Owns] ( @@ -35,47 +66,69 @@ CREATE TABLE [Owns] ( GameID INT NOT NULL, PlatformID INT NOT NULL PRIMARY KEY(UserID, GameID, PlatformID) - FOREIGN KEY(UserID) REFERENCES [User](ID), - FOREIGN KEY(GameID) REFERENCES [Game](ID), + FOREIGN KEY(UserID) REFERENCES [User](ID) + ON UPDATE CASCADE + ON DELETE CASCADE, + FOREIGN KEY(GameID) REFERENCES [Game](ID) + ON UPDATE CASCADE + ON DELETE CASCADE, FOREIGN KEY(PlatformID) REFERENCES [Platform](ID) + ON UPDATE CASCADE + ON DELETE CASCADE ) CREATE TABLE [Progress] ( UserID INT NOT NULL, - GameID INT NOT NULL, + PlatformID INT NOT NULL, AchievementID INT NOT NULL, Progress INT NOT NULL - PRIMARY KEY(UserID, GameID, AchievementID) - FOREIGN KEY(UserID) REFERENCES [User](ID), - FOREIGN KEY(GameID) REFERENCES [Game](ID), + PRIMARY KEY(UserID, AchievementID) + FOREIGN KEY(UserID) REFERENCES [User](ID) + ON UPDATE CASCADE + ON DELETE CASCADE, + FOREIGN KEY(PlatformID) REFERENCES [Platform](ID) + ON UPDATE CASCADE + ON DELETE CASCADE, FOREIGN KEY(AchievementID) REFERENCES [Achievement](ID) + ON UPDATE CASCADE + ON DELETE CASCADE ) CREATE TABLE [IsOn] ( UserID INT NOT NULL, PlatformID INT NOT NULL PRIMARY KEY(UserID, PlatformID) - FOREIGN KEY(UserID) REFERENCES [User](ID), + FOREIGN KEY(UserID) REFERENCES [User](ID) + ON UPDATE CASCADE + ON DELETE CASCADE, FOREIGN KEY(PlatformID) REFERENCES [Platform](ID) + ON UPDATE CASCADE + ON DELETE CASCADE ) CREATE TABLE [ExistsOn] ( GameID INT NOT NULL, - PlatformID INT NOT NULL + PlatformID INT NOT NULL, + PlatformGameID INT NOT NULL PRIMARY KEY(GameID, PlatformID) - FOREIGN KEY(GameID) REFERENCES [Game](ID), + FOREIGN KEY(GameID) REFERENCES [Game](ID) + ON UPDATE CASCADE + ON DELETE CASCADE, FOREIGN KEY(PlatformID) REFERENCES [Platform](ID) + ON UPDATE CASCADE ) CREATE TABLE [Rating] ( UserID INT NOT NULL, - GameID INT NOT NULL, AchievementID INT NOT NULL, Quality FLOAT NULL, Difficulty FLOAT NULL, [Description] VARCHAR(1024) NULL - PRIMARY KEY(UserID, GameID, AchievementID) - FOREIGN KEY(UserID) REFERENCES [User](ID), - FOREIGN KEY(GameID) REFERENCES [Game](ID), + PRIMARY KEY(UserID, AchievementID) + FOREIGN KEY(UserID) REFERENCES [User](ID) + ON UPDATE CASCADE + ON DELETE CASCADE, FOREIGN KEY(AchievementID) REFERENCES [Achievement](ID) + ON UPDATE CASCADE + ON DELETE CASCADE ) diff --git a/sql/CreateUserSP.sql b/sql/CreateUserSP.sql new file mode 100644 index 0000000000000000000000000000000000000000..0af4edf0408c18036a6c85ed0c1dd8b769eec58b GIT binary patch literal 804 zcmZ{iTT22_6ouETLH}Xi93djRy#?wx){t5_T7mRX8YOrMFG+vC`t~`djuFG`vv22| zwbnl0Uy)jhRaP4t=$7b45q?!2R|(WrU(cH8QHS`Erh3tn=GcyA*eR7jq4u?>oJwRy z)CU@p{lJ^5r#H8TI@Xy^RnWn@d$BrWOvi}5fA^fQYkS8#oin~An5JM0cFsSi+prX< zl$AQa0;5YMYLaWitBjXoBccSxP1lvEMm^R8xs*yu6k=o5`K{Ng8+J9PR)cIeqs6_h z=rdij{TIaB`goihY+gY_XZPC)HjNg$Lgh0z58xl5!*Df?zCMXkSddB!w=NU6o z-?K6uEo#GVSGzlIt8-4gQh!@7!&d^ge|5hv#%Fh5g=Z0QLxV4Iv-=;v;ma~OTI2o! Df{$|r literal 0 HcmV?d00001