This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lms-create-tables.sql
executable file
·529 lines (460 loc) · 14.6 KB
/
lms-create-tables.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
---------------------------------------------------------------------------
--1. Criacao das tabelas
---------------------------------------------------------------------------
-- Create LMS database
CREATE DATABASE LMS;
-- Use LMS database
USE LMS;
---------------------------------------------------------------------------
--2. Criacao das tabelas
---------------------------------------------------------------------------
-- Create User table
CREATE TABLE [dbo].[User]
(
[UserId] INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
[TypeId] INT NOT NULL,
[Name] NVARCHAR(100) NOT NULL,
[Email] NVARCHAR(100) NOT NULL,
[RegistrationDate] DATETIME NOT NULL,
[Password] NVARCHAR(255) NOT NULL
);
-- Create Author table
CREATE TABLE [dbo].[Author]
(
[AuthorId] INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
[Name] NVARCHAR(100) NOT NULL,
[Country] NVARCHAR(50) NOT NULL,
[Birthdate] DATETIME NOT NULL
);
-- Create Category table
CREATE TABLE [dbo].[Category]
(
[CategoryId] INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
[Description] NVARCHAR(100) NOT NULL
);
-- Create Book table
CREATE TABLE [dbo].[Book]
(
[BookId] INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
[AuthorId] INT NOT NULL,
[CategoryId] INT NOT NULL,
[Title] NVARCHAR(200) NOT NULL,
[ISBN] NVARCHAR(50) NOT NULL,
[PublishedYear] INT NOT NULL,
[Image] VARBINARY(MAX) NULL,
CONSTRAINT FK_Book_Author FOREIGN KEY ([AuthorId]) REFERENCES [dbo].[Author]([AuthorId]),
CONSTRAINT FK_Book_Category FOREIGN KEY ([CategoryId]) REFERENCES [dbo].[Category]([CategoryId])
);
-- Create Loan table
CREATE TABLE [dbo].[Loan]
(
[LoanId] INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
[UserId] INT NOT NULL,
[BookId] INT NOT NULL,
[LoanDate] DATETIME NOT NULL,
[DueDate] DATETIME NOT NULL,
[ReturnDate] DATETIME NULL,
CONSTRAINT FK_Loan_User FOREIGN KEY ([UserId]) REFERENCES [dbo].[User]([UserId]),
CONSTRAINT FK_Loan_Book FOREIGN KEY ([BookId]) REFERENCES [dbo].[Book]([BookId])
);
---------------------------------------------------------------------------
--3. Criacao das Stored Procedures de Listagem
---------------------------------------------------------------------------
-- Create spListagem_User
CREATE OR ALTER PROCEDURE [dbo].[spListagem_User](
@tabela varchar(max),
@ordem varchar(max)
)
AS
BEGIN
SELECT * FROM [User]
ORDER BY CASE WHEN @ordem IS NULL THEN UserId ELSE @ordem END;
END
-- Create spListagem_Author
CREATE OR ALTER PROCEDURE [dbo].[spListagem_Author](
@tabela varchar(max),
@ordem varchar(max)
)
AS
BEGIN
SELECT * FROM [Author]
ORDER BY CASE WHEN @ordem IS NULL THEN AuthorId ELSE @ordem END;
END
-- Create spListagem_Category
CREATE OR ALTER PROCEDURE [dbo].[spListagem_Category](
@tabela varchar(max),
@ordem varchar(max)
)
AS
BEGIN
SELECT * FROM [Category]
ORDER BY CASE WHEN @ordem IS NULL THEN CategoryId ELSE @ordem END;
END
-- Create spListagem_Book
CREATE OR ALTER PROCEDURE [dbo].[spListagem_Book](
@tabela varchar(max),
@ordem varchar(max)
)
AS
BEGIN
SELECT [Book].*, [Author].Name as 'AuthorName', [Category].Description as 'CategoryDescription'
FROM Book
INNER JOIN [Author] ON [Author].AuthorId = [Book].AuthorId
INNER JOIN [Category] ON [Category].CategoryId = [Book].CategoryId
ORDER BY CASE WHEN @ordem IS NULL THEN BookId ELSE @ordem END;
END
-- Create spListagem_Loan
CREATE OR ALTER PROCEDURE [dbo].[spListagem_Loan](
@tabela varchar(max),
@ordem varchar(max)
)
AS
BEGIN
SELECT [Loan].*, [User].Name as 'UserName', [Book].Title as 'BookTitle'
FROM [Loan]
INNER JOIN [User] ON [User].UserId = [Loan].UserId
INNER JOIN [Book] ON [Book].BookId = [Loan].BookId
ORDER BY CASE WHEN @ordem IS NULL THEN LoanId ELSE @ordem END;
END
---------------------------------------------------------------------------
--4. Criacao das Stored Procedures genericas
---------------------------------------------------------------------------
-- Create spDelete
CREATE OR ALTER PROCEDURE [dbo].[spDelete]
@id INT,
@tabela NVARCHAR(50)
AS
BEGIN
DECLARE @sql NVARCHAR(MAX);
DECLARE @primaryKey NVARCHAR(50);
-- Assume que a coluna de chave primaria segue o padrao <NomeDaTabela>Id
SET @primaryKey = @tabela + 'Id';
-- Constr�i a query dinamicamente
SET @sql = 'DELETE FROM [' + @tabela + '] WHERE ' + @primaryKey + ' = @id';
-- Executa a query dinamica com o parametro @id
EXEC sp_executesql @sql, N'@id INT', @id;
END
-- Create spConsulta
CREATE OR ALTER PROCEDURE [dbo].[spConsulta]
@id INT,
@tabela NVARCHAR(50)
AS
BEGIN
DECLARE @sql NVARCHAR(MAX);
DECLARE @primaryKey NVARCHAR(50);
-- Assume que a coluna de chave primaria segue o padrao <NomeDaTabela>Id
SET @primaryKey = @tabela + 'Id';
-- Constroi a query dinamicamente
SET @sql = 'SELECT * FROM [' + @tabela + '] WHERE ' + @primaryKey + ' = @id';
-- Executa a query dinamica com o parametro @id
EXEC sp_executesql @sql, N'@id INT', @id;
END
---------------------------------------------------------------------------
--5. Criacao das Stored Procedures de CRUD da tabela User
---------------------------------------------------------------------------
-- Create spInsert_User
CREATE OR ALTER PROCEDURE [dbo].[spInsert_User]
@TypeId INT,
@Name NVARCHAR(100),
@Email NVARCHAR(100),
@RegistrationDate DATETIME,
@Password NVARCHAR(255)
AS
BEGIN
INSERT INTO [dbo].[User] (TypeId, Name, Email, RegistrationDate, Password)
VALUES (@TypeId, @Name, @Email, @RegistrationDate, @Password);
END
-- Create spUpdate_User
CREATE OR ALTER PROCEDURE [dbo].[spUpdate_User]
@UserId INT,
@TypeId INT,
@Name NVARCHAR(100),
@Email NVARCHAR(100),
@RegistrationDate DATETIME,
@Password NVARCHAR(255)
AS
BEGIN
UPDATE [dbo].[User]
SET TypeId = @TypeId,
Name = @Name,
Email = @Email,
RegistrationDate = @RegistrationDate,
Password = @Password
WHERE UserId = @UserId;
END
-- Create spConsulta_User
CREATE OR ALTER PROCEDURE [dbo].[spConsulta_User]
@Email NVARCHAR(100)
AS
BEGIN
SELECT * FROM [User] WHERE Email = @Email
END
-- Create spDelete_User_And_Loans
CREATE OR ALTER PROCEDURE [dbo].[spDelete_User_And_Loans]
@id INT
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
BEGIN TRANSACTION;
-- Deletar empréstimos relacionados ao usuário
DELETE FROM Loan
WHERE UserId = @id;
-- Deletar o usuário
DELETE FROM [User]
WHERE UserId = @id;
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
THROW;
END CATCH
END;
---------------------------------------------------------------------------
--6. Criacao das Stored Procedures de CRUD da tabela Author
---------------------------------------------------------------------------
-- Create spInsert_Author
CREATE OR ALTER PROCEDURE [dbo].[spInsert_Author]
@Name NVARCHAR(100),
@Country NVARCHAR(50),
@Birthdate DATETIME
AS
BEGIN
INSERT INTO [dbo].[Author] (Name, Country, Birthdate)
VALUES (@Name, @Country, @Birthdate);
END
-- Create spUpdate_Author
CREATE OR ALTER PROCEDURE [dbo].[spUpdate_Author]
@AuthorId INT,
@Name NVARCHAR(100),
@Country NVARCHAR(50),
@Birthdate DATETIME
AS
BEGIN
UPDATE [dbo].[Author]
SET Name = @Name,
Country = @Country,
Birthdate = @Birthdate
WHERE AuthorId = @AuthorId;
END
-- Create spDelete_Author_And_Books
CREATE OR ALTER PROCEDURE [dbo].[spDelete_Author_And_Books]
@id INT
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
BEGIN TRANSACTION;
-- Deletar empréstimos relacionados aos livros do autor
DELETE FROM Loan
WHERE BookId IN (SELECT BookId FROM Book WHERE AuthorId = @id);
-- Deletar livros relacionados ao autor
DELETE FROM Book
WHERE AuthorId = @id;
-- Deletar o autor
DELETE FROM Author
WHERE AuthorId = @id;
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
THROW;
END CATCH
END;
---------------------------------------------------------------------------
--7. Criacao das Stored Procedures de CRUD da tabela Category
---------------------------------------------------------------------------
-- Create spInsert_Category
CREATE OR ALTER PROCEDURE [dbo].[spInsert_Category]
@Description NVARCHAR(100)
AS
BEGIN
INSERT INTO [dbo].[Category] (Description)
VALUES (@Description);
END
-- Create spUpdate_Category
CREATE OR ALTER PROCEDURE [dbo].[spUpdate_Category]
@CategoryId INT,
@Description NVARCHAR(100)
AS
BEGIN
UPDATE [dbo].[Category]
SET Description = @Description
WHERE CategoryId = @CategoryId;
END
-- Create spDelete_Category_And_Books
CREATE OR ALTER PROCEDURE [dbo].[spDelete_Category_And_Books]
@id INT
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
BEGIN TRANSACTION;
-- Deletar empréstimos relacionados aos livros da categoria
DELETE FROM Loan
WHERE BookId IN (SELECT BookId FROM Book WHERE CategoryId = @id);
-- Deletar livros relacionados à categoria
DELETE FROM Book
WHERE CategoryId = @id;
-- Deletar a categoria
DELETE FROM Category
WHERE CategoryId = @id;
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
THROW;
END CATCH
END;
---------------------------------------------------------------------------
--8. Criacao das Stored Procedures de CRUD da tabela Book
---------------------------------------------------------------------------
-- Create spInsert_Book
CREATE OR ALTER PROCEDURE [dbo].[spInsert_Book]
@AuthorId INT,
@CategoryId INT,
@Title NVARCHAR(200),
@ISBN NVARCHAR(50),
@PublishedYear INT,
@Image VARBINARY(MAX) = NULL
AS
BEGIN
INSERT INTO [dbo].[Book] (AuthorId, CategoryId, Title, ISBN, PublishedYear, Image)
VALUES (@AuthorId, @CategoryId, @Title, @ISBN, @PublishedYear, @Image);
END
-- Create spUpdate_Book
CREATE OR ALTER PROCEDURE [dbo].[spUpdate_Book]
@BookId INT,
@AuthorId INT,
@CategoryId INT,
@Title NVARCHAR(200),
@ISBN NVARCHAR(50),
@PublishedYear INT,
@Image VARBINARY(MAX) = NULL
AS
BEGIN
UPDATE [dbo].[Book]
SET AuthorId = @AuthorId,
CategoryId = @CategoryId,
Title = @Title,
ISBN = @ISBN,
PublishedYear = @PublishedYear,
Image = @Image
WHERE BookId = @BookId;
END
-- Create spConsultaAvancada_Book
CREATE OR ALTER PROCEDURE [dbo].[spConsultaAvancada_Book]
@descricao VARCHAR(MAX),
@autor INT,
@categoria INT,
@dataInicial INT,
@dataFinal INT
AS
BEGIN
DECLARE @categIni INT
DECLARE @categFim INT
DECLARE @autorIni INT
DECLARE @autorFim INT
SET @categIni = CASE @categoria WHEN 0 THEN 0 ELSE @categoria END
SET @categFim = CASE @categoria WHEN 0 THEN 999999 ELSE @categoria END
SET @autorIni = CASE @autor WHEN 0 THEN 0 ELSE @autor END
SET @autorFim = CASE @autor WHEN 0 THEN 999999 ELSE @autor END
SELECT
Book.*, Category.Description AS 'CategoryDescription', Author.Name AS 'AuthorName'
FROM Book
INNER JOIN Category ON Book.categoryId = Category.CategoryId
INNER JOIN Author ON Book.authorId = Author.AuthorId
WHERE
Book.title LIKE '%' + @descricao + '%' AND
Book.publishedYear BETWEEN @dataInicial AND @dataFinal AND
Book.CategoryId BETWEEN @categIni AND @categFim AND
Book.AuthorId BETWEEN @autorIni AND @autorFim;
END
-- Create spDelete_Book_And_Loans
CREATE OR ALTER PROCEDURE [dbo].[spDelete_Book_And_Loans]
@id INT
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
BEGIN TRANSACTION;
-- Deletar empréstimos relacionados ao livro
DELETE FROM Loan
WHERE BookId = @id;
-- Deletar o livro
DELETE FROM Book
WHERE BookId = @id;
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
THROW;
END CATCH
END;
---------------------------------------------------------------------------
--9. Criacao das Stored Procedures de CRUD da tabela Loan
---------------------------------------------------------------------------
-- Create spInsert_Loan
CREATE OR ALTER PROCEDURE [dbo].[spInsert_Loan]
@UserId INT,
@BookId INT,
@LoanDate DATETIME,
@DueDate DATETIME,
@ReturnDate DATETIME = NULL
AS
BEGIN
INSERT INTO [dbo].[Loan] (UserId, BookId, LoanDate, DueDate, ReturnDate)
VALUES (@UserId, @BookId, @LoanDate, @DueDate, @ReturnDate);
END
-- Create spUpdate_Loan
CREATE OR ALTER PROCEDURE [dbo].[spUpdate_Loan]
@LoanId INT,
@UserId INT,
@BookId INT,
@LoanDate DATETIME,
@DueDate DATETIME,
@ReturnDate DATETIME = NULL
AS
BEGIN
UPDATE [dbo].[Loan]
SET UserId = @UserId,
BookId = @BookId,
LoanDate = @LoanDate,
DueDate = @DueDate,
ReturnDate = @ReturnDate
WHERE LoanId = @LoanId;
END
-- Create spConsulta_Loan
CREATE OR ALTER PROCEDURE [dbo].[spConsulta_Loan]
@UserId INT
AS
BEGIN
SELECT [Loan].*, [User].Name as 'UserName', [Book].Title as 'BookTitle'
FROM [Loan]
INNER JOIN [User] ON [User].UserId = [Loan].UserId
INNER JOIN [Book] ON [Book].BookId = [Loan].BookId
WHERE [Loan].UserId = @UserId
END
-- Create spConsultaAvancada_Loan
CREATE OR ALTER PROCEDURE [dbo].[spConsultaAvancada_Loan]
@usuario VARCHAR(MAX),
@descricao VARCHAR(MAX),
@dataInicial DATETIME,
@dataFinal DATETIME
AS
BEGIN
SELECT
[Loan].*, [User].Name AS 'UserName', [Book].Title AS 'BookTitle'
FROM [Loan]
INNER JOIN [User] ON [Loan].UserId = [User].UserId
INNER JOIN [Book] ON [Loan].BookId = [Book].BookId
WHERE
[User].Name LIKE '%' + @usuario + '%' AND
[Book].Title LIKE '%' + @descricao + '%' AND
Book.publishedYear BETWEEN @dataInicial AND @dataFinal;
END
---------------------------------------------------------------------------
--10. Criacao do sa do sistema
---------------------------------------------------------------------------
INSERT INTO [dbo].[User] (TypeId, Name, Email, RegistrationDate, Password)
VALUES (1, 'System Admin', 'sa', GETDATE(), '1234');