USE lab9 GO -------------------- ---- -------------------- ---- ------

  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
USE lab9
GO
------------------------ 1 ------------------------
------------------- Table Init --------------------
IF OBJECT_ID('[dbo].[Authors]', 'U') IS NOT NULL
DROP TABLE [dbo].[Authors]
GO
CREATE TABLE [dbo].[Authors]
(
[Id] int NOT NULL IDENTITY(1, 1),
[FirstName] varchar(50) NOT NULL,
[LastName] varchar(50) NOT NULL,
CONSTRAINT PK_Authors PRIMARY KEY CLUSTERED ([Id]),
);
GO
-------------------- Triggers ---------------------
---------------------- Insert -----------------------
IF OBJECT_ID('[dbo].[AuthorsInsertTrig]', 'U') IS NOT NULL
DROP TRIGGER [dbo].[AuthorsInsertTrig]
GO
CREATE TRIGGER [dbo].[AuthorsInsertTrig]
ON [dbo].[Authors]
AFTER INSERT
AS
IF EXISTS(
SELECT * FROM inserted
WHERE [FirstName] = '' or [LastName] = ''
)
BEGIN
RAISERROR('Empty name is not allowed', 16, 1)
ROLLBACK TRANSACTION
RETURN
END
PRINT 'Generated keys:'
SELECT [Id] as [GeneratedId] from Inserted
GO
---------------------- Update -----------------------
IF OBJECT_ID('[dbo].[AuthorsUpdateTrig]', 'U') IS NOT NULL
DROP TRIGGER [dbo].[AuthorsUpdateTrig]
GO
CREATE TRIGGER [dbo].[AuthorsUpdateTrig]
ON [dbo].[Authors]
AFTER UPDATE
AS
IF UPDATE([FirstName]) OR UPDATE([LastName])
BEGIN
IF EXISTS(
SELECT * FROM inserted
WHERE [FirstName] = '' or [LastName] = ''
)
BEGIN
RAISERROR('Empty name is not allowed', 16, 1)
ROLLBACK TRANSACTION
RETURN
END
END
GO
---------------------- Delete -----------------------
IF OBJECT_ID('[dbo].[AuthorsDeleteTrig]', 'U') IS NOT NULL
DROP TRIGGER [dbo].[AuthorsDeleteTrig]
GO
CREATE TRIGGER [dbo].[AuthorsDeleteTrig]
ON [dbo].[Authors]
AFTER DELETE
AS
DECLARE @delCursor CURSOR
SET @delCursor = CURSOR
FORWARD_ONLY STATIC FOR
SELECT [Id] FROM Deleted
DECLARE @id int
OPEN @delCursor
FETCH NEXT FROM @delCursor INTO @id
WHILE (@@FETCH_STATUS = 0)
BEGIN
PRINT 'Deleted line id: ' + CAST(@id AS varchar(13))
FETCH NEXT FROM @delCursor INTO @id
END
GO
---------------------- Tests ----------------------
INSERT INTO [dbo].[Authors]
VALUES ('A', 'A'), ('A', 'B'), ('C', 'C')
GO
-- INSERT INTO [dbo].[Authors]
-- VALUES ('', 'D')
-- GO
-- UPDATE [dbo].[Authors]
-- SET [LastName] = ''
-- WHERE [Id] = 1
-- GO
-- DELETE FROM [dbo].[Authors]
-- WHERE [FirstName] = 'A'
-- GO
------------------------ 2 ------------------------
------------------- Tables Init -------------------
IF OBJECT_ID('[dbo].[Readers]', 'U') IS NOT NULL
BEGIN
IF OBJECT_ID('[dbo].[TakenBooks]', 'U') IS NOT NULL
DROP TABLE [dbo].[TakenBooks]
DROP TABLE [dbo].[Readers]
END
GO
CREATE TABLE [dbo].[Readers]
(
[Id] int NOT NULL IDENTITY(1, 1),
[FirstName] varchar(50) NOT NULL,
[LastName] varchar(50) NOT NULL,
CONSTRAINT PK_Readers PRIMARY KEY CLUSTERED ([Id]),
);
GO
IF OBJECT_ID('[dbo].[Books]', 'U') IS NOT NULL
DROP TABLE [dbo].[Books]
GO
CREATE TABLE [dbo].[Books]
(
[ISBN] varchar(20) NOT NULL,
[Name] varchar(100) NOT NULL,
CONSTRAINT PK_Books PRIMARY KEY CLUSTERED ([ISBN]),
);
GO
IF OBJECT_ID('[dbo].[TakenBooks]', 'U') IS NOT NULL
DROP TABLE [dbo].[TakenBooks]
GO
CREATE TABLE [dbo].[TakenBooks]
(
[Id] int NOT NULL IDENTITY(1, 1),
[ReaderId] int NOT NULL,
[ISBN] varchar(20) NOT NULL,
[TakingDate] date NOT NULL DEFAULT(getDate()),
[DueDate] date NOT NULL,
[Returned] int NOT NULL DEFAULT(0),
CONSTRAINT PK_TakenBooks PRIMARY KEY CLUSTERED ([Id]),
CONSTRAINT FK_TakenBooks_Readers FOREIGN KEY ([ReaderId]) REFERENCES [dbo].[Readers] ([Id]),
CONSTRAINT FK_TakenBooks_Books FOREIGN KEY ([ISBN]) REFERENCES [dbo].[Books] ([ISBN]),
CONSTRAINT CHK_Returned CHECK ([Returned] BETWEEN 0 AND 1)
);
GO
-------------------- View Init --------------------
IF OBJECT_ID('[dbo].[ReadingBooksView]') IS NOT NULL
DROP VIEW [dbo].[ReadingBooksView]
GO
CREATE VIEW [dbo].[ReadingBooksView]
AS
SELECT r.[Id] as [ReaderId], tb.[ISBN], tb.[TakingDate], tb.[DueDate]
FROM [dbo].[Readers] as r INNER JOIN [dbo].[TakenBooks] as tb
ON r.[Id] = tb.[ReaderId]
WHERE tb.[Returned] = 0
GO
-------------------- Triggers ---------------------
---------------------- Insert -----------------------
IF OBJECT_ID('[dbo].[RBViewInsertTrig]', 'U') IS NOT NULL
DROP TRIGGER [dbo].[RBViewInsertTrig]
GO
CREATE TRIGGER [dbo].[RBViewInsertTrig]
ON [dbo].[ReadingBooksView]
INSTEAD OF INSERT
AS
IF EXISTS (SELECT * FROM Inserted WHERE [ReaderId] NOT IN (SELECT [ReaderId] FROM [dbo].[Readers]))
BEGIN
RAISERROR('Nonexistent Reader(s)', 16, 1)
ROLLBACK TRANSACTION
RETURN
END
IF EXISTS (SELECT * FROM Inserted WHERE [ISBN] NOT IN (SELECT [ISBN] FROM [dbo].[Books]))
BEGIN
RAISERROR('Nonexistent Book(s)', 16, 1)
ROLLBACK TRANSACTION
RETURN
END
INSERT INTO [dbo].[TakenBooks]
(ReaderId, ISBN, TakingDate, DueDate)
SELECT [ReaderId], [ISBN], [TakingDate], [DueDate]
FROM Inserted
GO
---------------------- Update -----------------------
IF OBJECT_ID('[dbo].[RBViewUpdateTrig]', 'U') IS NOT NULL
DROP TRIGGER [dbo].[RBViewUpdateTrig]
GO
CREATE TRIGGER [dbo].[RBViewUpdateTrig]
ON [dbo].[ReadingBooksView]
INSTEAD OF UPDATE
AS
IF UPDATE([ReaderId])
BEGIN
RAISERROR('ReaderId cant be modified', 16, 1)
ROLLBACK TRANSACTION
RETURN
END
IF UPDATE([ISBN])
BEGIN
RAISERROR('ISBN cant be modified', 16, 1)
ROLLBACK TRANSACTION
RETURN
END
UPDATE [dbo].[TakenBooks]
SET [TakingDate] = ins.[TakingDate], [DueDate] = ins.[DueDate]
FROM
(SELECT *, row_number() over (order by [ReaderId]) as row_num from inserted) as ins
JOIN
(SELECT *, row_number() over (order by [ReaderId]) as row_num from deleted) as del
ON ins.row_num = del.row_num
WHERE [dbo].[TakenBooks].[ISBN] = del.[ISBN] AND [dbo].[TakenBooks].[TakingDate] = del.[TakingDate]
AND [dbo].[TakenBooks].[ReaderId] = del.[ReaderId]
GO
---------------------- Delete -----------------------
IF OBJECT_ID('[dbo].[RBViewDeleteTrig]', 'U') IS NOT NULL
DROP TRIGGER [dbo].[RBViewDeleteTrig]
GO
CREATE TRIGGER [dbo].[RBViewDeleteTrig]
ON [dbo].[ReadingBooksView]
INSTEAD OF DELETE
AS
UPDATE [dbo].[TakenBooks]
SET [Returned] = 1
FROM deleted as del
WHERE [dbo].[TakenBooks].[ISBN] = del.[ISBN] AND [dbo].[TakenBooks].[TakingDate] = del.[TakingDate]
GO
---------------------- Tests ----------------------
-- INSERT INTO [dbo].[Books]
-- VALUES
-- (1, 'War and Peace'),
-- (2, 'War of worlds')
-- INSERT INTO [dbo].[Readers]
-- VALUES
-- ('Robert', 'B'),
-- ('Victor', 'C')
-- INSERT INTO [dbo].[TakenBooks]
-- VALUES
-- (1, 1, CONVERT(datetime, '4.11.2018', 104), getDate(), 0),
-- (2, 1, CONVERT(datetime, '4.09.2019', 104), getDate(), 1),
-- (2, 1, CONVERT(datetime, '22.11.2018', 104), getDate(), 0),
-- (2, 2, CONVERT(datetime, '7.01.2018', 104), getDate(), 0)
-- SELECT * FROM [dbo].[Readers]
-- SELECT * FROM [dbo].[TakenBooks]
-- SELECT * FROM ReadingBooksView
-- GO
-- INSERT INTO ReadingBooksView
-- VALUES (1, 2, CONVERT(datetime, '4.11.2018', 104), getDate())
-- GO
-- UPDATE ReadingBooksView
-- SET [DueDate] = CONVERT(datetime, '4.11.2020', 104)
-- WHERE [ReaderId] = 1
-- GO
-- DELETE FROM ReadingBooksView
-- WHERE [ReaderId] = 2
-- GO
-- SELECT * FROM [dbo].[Readers]
-- SELECT * FROM [dbo].[TakenBooks]
-- SELECT * FROM ReadingBooksView
-- GO