-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageBoardSetupScript.sql
More file actions
233 lines (193 loc) · 4.26 KB
/
MessageBoardSetupScript.sql
File metadata and controls
233 lines (193 loc) · 4.26 KB
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
Use Master
GO
Create Database MessageBoardDB
GO
Use MessageBoardDB
GO
-- Create Tables
Create Table [dbo].[Category](
[CategoryId] [Int] NOT NULL Primary Key Identity(1, 1),
[CategoryName] [NVarChar](100) NOT NULL,
)
Create Table [dbo].[Users](
[UserId] [Int] NOT NULL Primary Key Identity(1, 1),
[Username] [VarChar](30) NOT NULL,
[Password] [VarChar](60) NOT NULL
)
GO
Create Table [dbo].[Messages] (
[MessageId] [Int] NOT NULL Primary Key Identity(1, 1),
[Title] [NVarChar](30) NOT NULL,
[Content] [NVarChar](500) NOT NULL,
[Dato] Datetime Not NULL Default Getdate(),
[UserId] [Int] NOT NULL References Users(UserId),
[CategoryId] [Int] NOT NULL References Category(CategoryId),
)
GO
------------------------------------------------------------------------------------------------------------
-- Create Sample Data
Insert Into Category(CategoryName)
Values('meh'),
('yeet');
GO
Insert Into Users(Username, Password)
Values('DefultUser','meh'),
('Peter','meh'),
('Nicolai','meh'),
('Casper','meh');
GO
Insert Into [Messages](Title, Content, UserId, CategoryId)
Values('meh', 'meh', 2, 1),
('Hacking', 'Ha Yeah like im gonna show you!', 2, 2),
('Toxisety', 'F*ck off you little St*p*de ******', 4, 1),
('Yeet', 'yeet', 1, 2);
GO
------------------------------------------------------------------------------------------------------------
--Category CRUD
--Create
Create Procedure CreateCategory
@NewCategoryName VarChar(100)
AS
IF NOT EXISTS (SELECT CategoryName FROM Category WHERE CategoryName = @NewCategoryName)
BEGIN
INSERT INTO Category (CategoryName)
VALUES (@NewCategoryName);
END;
GO
--ReadAll
Create Procedure ReadCategorys
As
Select *
From Category
GO
--GetoneCategory
Create Procedure ReadOneCategory
@CategoryId int
As
Select *
From Category
Where CategoryId = @CategoryId
GO
--Update
Create Procedure UpdateCategorys
@CategoryRename VarChar(100),
@CategoryId Int
As
UPDATE Category
SET CategoryName = @CategoryRename
WHERE CategoryID = @CategoryId
GO
--Delete
Create Procedure DeleteCategorys
@CategoryId Int
As
Delete Category WHERE CategoryID = @CategoryId
Go
------------------------------------------------------------------------------------------------------------
-- User CRUD
--Create
Create Procedure CreateUser
@Username VarChar(30),
@Password VarChar(60)
AS
Insert Into Users (Username, Password)
Values(@Username, @Password);
GO
--ReadAll
Create Procedure ReadUsers
As
Select UserId, Username
From Users
GO
--ReadOneUser
Create Procedure ReadOneUser
@UserId int
As
Select *
From Users
Where UserId = @UserId
GO
--ReadOneUserWithPassword
Create Procedure ReadOneUser
@UserId int,
@Password VarChar(60)
As
Select *
From Users
Where UserId = @UserId And Password = @Password
GO
--Update
Create Procedure UpdateUsers
@UpdateUsername VarChar(30),
@UpdatePassword VarChar(60),
@UserId Int
As
UPDATE Users
SET Username = @UpdateUsername, Password = @UpdatePassword
WHERE UserId = @UserId
GO
--Delete
Create Procedure DeleteUser
@UserId Int
As
Delete Users WHERE UserId = @UserId
Go
------------------------------------------------------------------------------------------------------------
--Message CRUD
--Create
CREATE Procedure CreateMessage
@Title VarChar(30),
@Content VarChar(500),
@UserId Int,
@CategoryId Int,
@MessageId int out
AS
Insert Into [Messages] (Title, Content, UserId, CategoryId)
Values(@Title, @Content, @UserId, @CategoryId);
set @MessageId = SCOPE_IDENTITY()
GO
--ReadAll
Create Procedure ReadMessages
As
Select *
From [Messages]
GO
--Update
Create Procedure UpdateMessage
@Title VarChar(30),
@Content VarChar(500),
@MessageId Int,
@CategoryId int
As
UPDATE [Messages]
SET Title = @Title, Content = @Content, CategoryId = @CategoryId
WHERE MessageId = @MessageId
GO
--Delete
Create Procedure DeleteMessage
@MessageId Int
As
Delete [Messages] WHERE MessageId = @MessageId
Go
--Read Messages in category
Create Procedure GetCategoryMessage
@CategoryId Int
As
Select *
From [Messages]
Where CategoryId = @CategoryId
GO
--Read top 10
Create Procedure GetTopTenMessages
As
Select Top 10 *
From [Messages]
go
Create Procedure GetMessage
@GetMessageId int
As
Select *
From [Messages]
Where [MessageId] = @GetMessageId
GO
------------------------------------------------------------------------------------------------------------