Skip to content

Commit 4eefc45

Browse files
implemented two views for meta information, added basic trigger code with cursor
1 parent c8c6bfd commit 4eefc45

File tree

3 files changed

+164
-0
lines changed

3 files changed

+164
-0
lines changed

CreateViews.sql

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
USE [Music];
2+
GO
3+
4+
5+
----------------------------------------------------------------------
6+
-- a view which stores the number of genres in a playlist as key --
7+
-- value pairs. --
8+
----------------------------------------------------------------------
9+
IF OBJECT_ID('VW_NumberOfGenresInPlaylist', 'V') IS NOT NULL
10+
DROP VIEW [VW_NumberOfGenresInPlaylist];
11+
GO
12+
13+
CREATE VIEW [VW_NumberOfGenresInPlaylist]
14+
AS
15+
SELECT pt.[fk_playlist_id] AS "playlist_id",
16+
Count(DISTINCT t.[fk_genre_id]) As "Number of Genres"
17+
FROM [playlist_title] AS pt
18+
INNER JOIN [title] AS t ON t.[title_id] = pt.[fk_title_id]
19+
GROUP BY pt.[fk_playlist_id];
20+
GO
21+
22+
23+
----------------------------------------------------------------------
24+
-- a view which stores the number of interprets per album as key --
25+
-- value pairs. --
26+
----------------------------------------------------------------------
27+
IF OBJECT_ID('VW_NumberOfInterpretsInAlbum', 'V') IS NOT NULL
28+
DROP VIEW [VW_NumberOfInterpretsInAlbum];
29+
GO
30+
31+
CREATE VIEW [VW_NumberOfInterpretsInAlbum]
32+
AS
33+
SELECT t.[fk_album_id] AS "album_id",
34+
Count(DISTINCT ti.[fk_interpret_id]) As "Number of Interprets"
35+
FROM [title] AS t
36+
INNER JOIN [title_interpret] AS ti ON ti.fk_title_id = t.title_id
37+
GROUP BY t.[fk_album_id];
38+
GO

Triggers/TR_Template.sql

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
USE [Music];
2+
GO
3+
4+
5+
------------------------------------------------------------------------------
6+
-- checks if the trigger already exists using the sys.objects and drops it. --
7+
------------------------------------------------------------------------------
8+
IF EXISTS (SELECT * FROM sys.objects WHERE [name] = N'TR_' AND [type] = 'TR')
9+
DROP TRIGGER [TR_]
10+
GO
11+
12+
13+
------------------------------------------------------------------------------
14+
--
15+
------------------------------------------------------------------------------
16+
CREATE TRIGGER [TR_] ON ___table___
17+
INSTEAD OF INSERT -- SQL Server does not support BEFORE triggers
18+
AS BEGIN
19+
-- trigger code goes here
20+
END
21+
GO
22+
23+
24+
------------------------------------------------------------------------------
25+
-- test code for checking trigger behavior. --
26+
------------------------------------------------------------------------------
27+
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
USE [Music];
2+
GO
3+
4+
5+
------------------------------------------------------------------------------
6+
-- checks if the trigger already exists using the sys.objects and drops it. --
7+
------------------------------------------------------------------------------
8+
IF EXISTS (SELECT * FROM sys.objects WHERE [name] = N'TR_UniqueTitleNumberInAlbum' AND [type] = 'TR')
9+
DROP TRIGGER [TR_UniqueTitleNumberInAlbum];
10+
GO
11+
12+
13+
------------------------------------------------------------------------------
14+
--
15+
------------------------------------------------------------------------------
16+
CREATE TRIGGER [TR_UniqueTitleNumberInAlbum] ON [title]
17+
INSTEAD OF INSERT -- SQL Server does not support BEFORE triggers
18+
AS BEGIN
19+
DECLARE @TitleNumber SMALLINT,
20+
@TitleAlbum INT;
21+
22+
DECLARE CUR_InsertedTitle CURSOR FORWARD_ONLY FOR
23+
SELECT ins.[titlenumber], ins.[fk_album_id]
24+
FROM [inserted] AS ins;
25+
26+
BEGIN TRANSACTION;
27+
OPEN CUR_InsertedTitle;
28+
FETCH NEXT FROM CUR_InsertedTitle INTO @TitleNumber, @TitleAlbum;
29+
30+
WHILE @@FETCH_STATUS = 0 BEGIN;
31+
IF ((SELECT Count(*) FROM [title] AS t
32+
WHERE t.[titlenumber] = @TitleNumber
33+
AND t.[fk_album_id] = @TitleAlbum) > 0)
34+
BEGIN;
35+
ROLLBACK;
36+
37+
-- close cursor and free allocated memory to prevent memory leaks
38+
CLOSE CUR_InsertedTitle;
39+
DEALLOCATE CUR_InsertedTitle;
40+
41+
THROW 47000, 'Titlenumber already exists in this album', 2;
42+
END;
43+
44+
BEGIN TRY;
45+
INSERT INTO
46+
[title] ([name], [titlenumber], [duration], [bitrate], [fk_genre_id], [fk_album_id])
47+
VALUES ();
48+
END TRY BEGIN CATCH;
49+
ROLLBACK;
50+
51+
-- close cursor and free allocated memory to prevent memory leaks
52+
CLOSE CUR_InsertedTitle;
53+
DEALLOCATE CUR_InsertedTitle;
54+
55+
RETURN 1;
56+
END CATCH;
57+
58+
FETCH NEXT FROM CUR_InsertedTitle INTO @TitleNumber, @TitleAlbum;
59+
END;
60+
ROLLBACK;
61+
62+
-- close cursor and free allocated memory to prevent memory leaks
63+
CLOSE CUR_InsertedTitle;
64+
DEALLOCATE CUR_InsertedTitle;
65+
66+
/*
67+
DECLARE CUR_AlbumTitle CURSOR FORWARD_ONLY FOR
68+
SELECT t.[titlenumber] FROM [title] AS t
69+
INNER JOIN [inserted] AS ins ON ins.fk_album_id = t.fk_album_id;
70+
71+
BEGIN TRANSACTION;
72+
OPEN CUR_AlbumTitle;
73+
FETCH NEXT FROM CUR_AlbumTitle INTO @TitleNumber;
74+
75+
WHILE @@FETCH_STATUS = 0 BEGIN;
76+
IF (@TitleNumber = ANY(SELECT [titlenumber] FROM [inserted])) BEGIN;
77+
ROLLBACK;
78+
THROW 47000, 'Titlenumber already exists in this album', 2;
79+
END;
80+
81+
FETCH NEXT FROM CUR_AlbumTitle INTO @TitleNumber;
82+
END;
83+
ROLLBACK;*/
84+
85+
-- close cursor and free allocated memory to prevent memory leaks
86+
CLOSE CUR_AlbumTitle;
87+
DEALLOCATE CUR_AlbumTitle;
88+
END
89+
GO
90+
91+
92+
------------------------------------------------------------------------------
93+
-- test code for checking trigger behavior. --
94+
------------------------------------------------------------------------------
95+
INSERT INTO [album] ([name], [releaseyear]) VALUES ('Album #1', 2017);
96+
INSERT INTO [title] ([name], [titlenumber], [duration], [bitrate], [fk_genre_id], [fk_album_id]) VALUES ('Title #1', 1, 300, 20000, 1, 1);
97+
INSERT INTO [title] ([name], [titlenumber], [duration], [bitrate], [fk_genre_id], [fk_album_id]) VALUES ('Title #2', 1, 400, 30000, 1, 1);
98+
99+
SELECT * FROM [title];

0 commit comments

Comments
 (0)