Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лаб 4 - Использование Триггеров.doc
Скачиваний:
4
Добавлен:
30.08.2019
Размер:
97.28 Кб
Скачать

Упражнение 5.

Учет требований, связанных с совместным использованием нескольких таблиц.

CREATE SCHEMA Entertainment

GO

CREATE TABLE Entertainment.GamePlatform

(

GamePlatformId INT CONSTRAINT PKGamePlatform PRIMARY KEY,

Name VARCHAR (20) CONSTRAINT AKGamePlatform_Name UNIQUE)

CREATE TABLE Entertainment.Game

(

GameId INT CONSTRAINT PKGame PRIMARY KEY,

Name VARCHAR (20) CONSTRAINT AKGame_Name UNIQUE

)

CREATE TABLE Entertainment.GameInstance

(

GamePlatformID INT,

GameId INT,

DatePurchased SMALLDATETIME,

CONSTRAINT PKGameInstance PRIMARY KEY (GamePlatformId,GameId), CONSTRAINT Game$is_owned_on_platform_by$GameInstance

FOREIGN KEY (GameId) REFERENCES Entertainment.Game (GameId)

ON DELETE CASCADE,

CONSTRAINT GamePlatfоrm$is_linked_to$GameInstance

FOREIGN KEY (GamePlatformId)

REFERENCES Entertainment.GamePlatform(GamePlatformId)

ON DELETE CASCADE

)

-------------------------------------------------------------------------------------

INSERT INTO Entertainment.Game (GameId, Name)

VALUES (1, 'Super Mario Bros')

INSERT INTO Entertainment.Game (GameId, Name)

VALUES (2, 'Legend Of Zelda')

INSERT INTO Entertainment.GamePlatform (GamePlatformId, Name)

VALUES (1, 'Nintendo 64')

INSERT INTO Entertainment.GamePlatform (GamePlatformId, Name)

VALUES (2, 'GameCube')

INSERT INTO Entertainment.GameInstance (GamePlatformId, GameId, DatePurchased)

VALUES (1, 1,'20000204')

INSERT INTO Entertainment.GameInstance (GamePlatformId, GameId, DatePurchased)

VALUES (1, 2, '20030510 ')

INSERT Entertainment.GameInstance (GamePlatformId, GameId, DatePurchased)

VALUES (2, 2, '20030404 ')

-------------------------------------------------------------------------------------

SELECT GamePlatform.Name AS Platform,

Game.Name AS Game, GameInstance.DatePurchased

FROM Entertainment.Game AS Game

FULL OUTER JOIN Entertainment.GameInstance AS GameInstance ON Game.GameId = GameInstance.GameId

FULL OUTER JOIN Entertainment.GamePlatform

ON GamePlatform.GamePlatformId = GameInstance.GamePlatformId

Результат:

Platform

Game

DatePurchased

Nintendo 64

Super Mario Bros

2000-02-04 00:00:00

Nintendo 64

Legend Of Zelda

2003-05-10 00:00:00

GameCube

Legend Of Zelda

2003-04-04 00:00:00

CREATE TRIGGER Entertainment.GameInstance$delete ON Entertainment.GameInstance

FOR DELETE AS

BEGIN

DECLARE @rowsAffected INT, --stores the number of rows affected

@msg varchar (2000) --used to hold the error message

SET @rowsAffected = @@rowcount

IF @rowsAffected = 0 return

SET NOCOUNT ON --to avoid the rowcount messages

SET ROWCOUNT 0 --in case the client has modified the rowcount

BEGIN TRY

DELETE Game --where the GameInstance was delete

WHERE GameId IN (SELECT deleted.GameId

FROM deleted --and there are no GameInstances left

WHERE NOT EXISTS (Select *

FROM GameInstance

WHERE GameInstance.GameId = deleted.GameId))

END TRY

BEGIN CATCH

IF @@trancount > 0

ROLLBACK TRANSACTION

--or this will not get rolled back

-- EXECUTE dbo.ErrorLog$insert

DECLARE @ERROR_MESSAGE varchar (4000)

SET @ERROR_MESSAGE = ERROR_MESSAGE()

RAISERROR (@ERROR_MESSAGE,16,1)

END CATCH

END

-------------------------------------------------------------------------------------

DELETE Entertainment.GamePlatform

WHERE GamePlatformId = 1

GO

SELECT GamePlatform.Name AS Platform,

Game.Name AS Game, GameInstance.DatePurchased

FROM Entertainment.Game AS Game

FULL OUTER JOIN Entertainment.GameInstance AS GameInstance ON Game.GameId = GameInstance.GameId

FULL OUTER JOIN Entertainment.GamePlatform

ON GamePlatform.GamePlatformId = GameInstance.GamePlatformId

Результат:

Platform

Game

DatePurchased

GameCube

Legend Of Zelda

2003-04-04 00:00:00