-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathBank-Database-Design.sql
554 lines (459 loc) · 18.1 KB
/
Bank-Database-Design.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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/* ## MS-SQL PROJECT on Banking Transactions by Gyan Kumar GM ! */
-- PHASE I of project begins
--Q1. Create a database for a banking application called 'Bank'.
create database dbBankGM;
GO
/* NOTE: Unlike asked in question database name has been used as 'dbBankGM' instead of 'Bank' in order to make it unique
from other students. Following code is used to select 'dbBankGM' as current database */
use dbBankGM;
GO
--Q2. Create all the tables mentioned in the database diagram.
--Q3. Create all the constraints based on the database diagram.
--NOTE: Solution to questions 2 and 3 are provided at once as follows. This was confirmed with teacher!
/* CREATE TABLE & ADD CONSTRAINTS Section */
-- Creating tables only with primary keys first
--Creating table named UserLogins
CREATE TABLE UserLogins
(
UserLoginID SMALLINT NOT NULL IDENTITY(1,1),
UserLogin VARCHAR(50) NOT NULL,
UserPassword VARCHAR(20) NOT NULL,
CONSTRAINT pk_UL_UserLoginID PRIMARY KEY(UserLoginID)
);
GO
--Creating table named UserSecurityQuestions
CREATE TABLE UserSecurityQuestions
(
UserSecurityQuestionID TINYINT NOT NULL IDENTITY(1,1),
UserSecurityQuestion VARCHAR(50) NOT NULL,
CONSTRAINT pk_USQ_UserSecurityQuestionID PRIMARY KEY(UserSecurityQuestionID)
);
GO
--Creating table named AccountType
CREATE TABLE AccountType
(
AccountTypeID TINYINT NOT NULL IDENTITY(1,1),
AccountTypeDescription VARCHAR(30) NOT NULL,
CONSTRAINT pk_AT_AccountTypeID PRIMARY KEY(AccountTypeID)
);
GO
--Creating table named SavingsInterestRates
/* NOTE: Altered the table to accept datatype as NUMERIC(9,2) in order to avoid Arithmetic Conversion error using
code "ALTER TABLE SavingsInterestRates ALTER COLUMN IntetestRatesValue NUMERIC(9,2);" */
CREATE TABLE SavingsInterestRates
(
InterestSavingRatesID TINYINT NOT NULL IDENTITY(1,1),
InterestRatesValue NUMERIC(9,9) NOT NULL,
InterestRatesDescription VARCHAR(20) NOT NULL,
CONSTRAINT pk_SIR_InterestSavingRatesID PRIMARY KEY(InterestSavingRatesID)
);
GO
--Creating table named AccountStatusType
CREATE TABLE AccountStatusType
(
AccountStatusTypeID TINYINT NOT NULL IDENTITY(1,1),
AccountStatusTypeDescription VARCHAR(30) NOT NULL,
CONSTRAINT pk_AST_AccountStatusTypeID PRIMARY KEY(AccountStatusTypeID)
);
GO
--Creating table named FailedTransactionErrorType
CREATE TABLE FailedTransactionErrorType
(
FailedTransactionErrorTypeID TINYINT NOT NULL IDENTITY(1,1),
FailedTransactionErrorTypeDescription VARCHAR(50) NOT NULL,
CONSTRAINT pk_FTET_FailedTransactionErrorTypeID PRIMARY KEY(FailedTransactionErrorTypeID)
);
GO
--Creating table named LoginErrorLog
CREATE TABLE LoginErrorLog
(
ErrorLogID INT NOT NULL IDENTITY(1,1),
ErrorTime DATETIME NOT NULL,
FailedTransactionXML XML,
CONSTRAINT pk_LEL_ErrorLogID PRIMARY KEY(ErrorLogID)
);
GO
--Creating table named Employee
CREATE TABLE Employee
(
EmployeeID INT NOT NULL IDENTITY(1,1),
EmployeeFirstName VARCHAR(25) NOT NULL,
EmployeeMiddleInitial CHAR(1),
EmployeeLastName VARCHAR(25),
EmployeeisManager BIT,
CONSTRAINT pk_E_EmployeeID PRIMARY KEY(EmployeeID)
);
GO
--Creating table named TransactionType
CREATE TABLE TransactionType
(
TransactionTypeID TINYINT NOT NULL IDENTITY(1,1),
TransactionTypeName CHAR(10) NOT NULL,
TransactionTypeDescription VARCHAR(50),
TransactionFeeAmount SMALLMONEY,
CONSTRAINT pk_TT_TransactionTypeID PRIMARY KEY(TransactionTypeID)
);
GO
-- Creating tables with foreign key and combination of both primary and foreign keys
--Creating table named FailedTransactionLog
CREATE TABLE FailedTransactionLog
(
FailedTransactionID INT NOT NULL IDENTITY(1,1),
FailedTransactionErrorTypeID TINYINT NOT NULL,
FailedTransactionErrorTime DATETIME,
FailedTransactionErrorXML XML,
CONSTRAINT pk_FTL_FailedTransactionID PRIMARY KEY(FailedTransactionID),
CONSTRAINT fk_FTET_FailedTransactionErrorTypeID FOREIGN KEY(FailedTransactionErrorTypeID) REFERENCES FailedTransactionErrorType(FailedTransactionErrorTypeID)
);
GO
--Creating table named UserSecurityAnswers
CREATE TABLE UserSecurityAnswers
(
UserLoginID SMALLINT NOT NULL IDENTITY(1,1),
UserSecurityAnswers VARCHAR(25) NOT NULL,
UserSecurityQuestionID TINYINT NOT NULL,
CONSTRAINT pk_USA_UserLoginID PRIMARY KEY(UserLoginID),
CONSTRAINT fk_UL_UserLoginID FOREIGN KEY(UserLoginID) REFERENCES UserLogins(UserLoginID),
CONSTRAINT fk_USQ_UserSecurityQuestionID FOREIGN KEY(UserSecurityQuestionID) REFERENCES UserSecurityQuestions(UserSecurityQuestionID)
);
GO
--Creating table named Account
CREATE TABLE Account
(
AccountID INT NOT NULL IDENTITY(1,1),
CurrentBalance INT NOT NULL,
AccountTypeID TINYINT NOT NULL REFERENCES AccountType (AccountTypeID),
AccountStatusTypeID TINYINT NOT NULL,
InterestSavingRatesID TINYINT NOT NULL,
CONSTRAINT pk_A_AccounID PRIMARY KEY(AccountID),
CONSTRAINT fk_AST_AccountStatusTypeID FOREIGN KEY(AccountStatusTypeID) REFERENCES AccountStatusType(AccountStatusTypeID),
CONSTRAINT fk_SIR_InterestSavingRatesID FOREIGN KEY(InterestSavingRatesID) REFERENCES SavingsInterestRates(InterestSavingRatesID)
);
GO
--Creating table named LoginAccount
--NOTE: Unlike ER diagram table name has been used as LoginAccounts instead of Login-Account
CREATE TABLE LoginAccount
(
UserLoginID SMALLINT NOT NULL,
AccountID INT NOT NULL,
CONSTRAINT fk_UL_UserLogins FOREIGN KEY(UserLoginID) REFERENCES UserLogins(UserLoginID),
CONSTRAINT fk_A_Account FOREIGN KEY(AccountID) REFERENCES Account(AccountID)
);
GO
--Creating table named Customer
CREATE TABLE Customer
(
CustomerID INT NOT NULL IDENTITY(1,1),
AccountID INT NOT NULL,
CustomerAddress1 VARCHAR(30) NOT NULL,
CustomerAddress2 VARCHAR(30),
CustomerFirstName VARCHAR(30) NOT NULL,
CustomerMiddleInitial CHAR(1),
CustomerLastName VARCHAR(30) NOT NULL,
City VARCHAR(20) NOT NULL,
State CHAR(2) NOT NULL,
ZipCode CHAR(10) NOT NULL,
EmailAddress CHAR(40) NOT NULL,
HomePhone VARCHAR(10) NOT NULL,
CellPhone VARCHAR(10) NOT NULL,
WorkPhone VARCHAR(10) NOT NULL,
SSN VARCHAR(9),
UserLoginID SMALLINT NOT NULL,
CONSTRAINT pk_C_CustomerID PRIMARY KEY(CustomerID),
CONSTRAINT fk_A_AccountID FOREIGN KEY(AccountID) REFERENCES Account(AccountID),
CONSTRAINT fk_UL_C_UserLoginID FOREIGN KEY(UserLoginID) REFERENCES UserLogins(UserLoginID)
);
GO
--Creating table named CustomerAccount
--NOTE: Unlike ER diagram table name has been used as CustomerAccounts instead of Customer-Account
CREATE TABLE CustomerAccount
(
AccountID INT NOT NULL ,
CustomerID INT NOT NULL,
CONSTRAINT fk_A_CA_AccountID FOREIGN KEY(AccountID) REFERENCES Account(AccountID),
CONSTRAINT fk_C_CA_CustomerID FOREIGN KEY(CustomerID) REFERENCES Customer(CustomerID)
);
GO
--Creating table named TransactionLog
CREATE TABLE TransactionLog
(
TransactionID INT NOT NULL IDENTITY(1,1),
TransactionDate DATETIME NOT NULL,
TransactionTypeID TINYINT NOT NULL,
TransactionAmount Money NOT NULL,
NewBalance Money NOT NULL,
AccountID INT NOT NULL,
CustomerID INT NOT NULL,
EmployeeID INT NOT NULL,
UserLoginID SMALLINT NOT NULL,
CONSTRAINT pk_TL_TransactionID PRIMARY KEY(TransactionID),
CONSTRAINT fk_TT_TL_TransactionTypeID FOREIGN KEY(TransactionTypeID) REFERENCES TransactionType(TransactionTypeID),
CONSTRAINT fk_A_TL_AccountID FOREIGN KEY(AccountID) REFERENCES Account(AccountID),
CONSTRAINT fk_C_TL_CustomerID FOREIGN KEY(CustomerID) REFERENCES Customer(CustomerID),
CONSTRAINT fk_E_TL_EmployeeID FOREIGN KEY(EmployeeID) REFERENCES Employee(EmployeeID),
CONSTRAINT fk_UL_TL_UserLoginID FOREIGN KEY(UserLoginID) REFERENCES UserLogins(UserLoginID)
);
GO
--Creating table named OverDraftLog
CREATE TABLE OverDraftLog
(
AccountID INT NOT NULL IDENTITY(1,1),
OverDraftDate DATETIME NOT NULL,
OverDraftAmount Money NOT NULL,
OverDraftTransactionXML XML NOT NULL,
CONSTRAINT Pk_ODL_AccountID PRIMARY KEY(AccountID),
CONSTRAINT fk_A_ODL_AccountID FOREIGN KEY(AccountID) REFERENCES Account(AccountID)
);
GO
--Q4. Insert at least 5 rows in each table.
/** INSERT rows section **/
insert into UserLogins values('User1', 'Pass1');
insert into UserLogins values('User2', 'Pass2');
insert into UserLogins values('User3', 'Pass3');
insert into UserLogins values('User4', 'Pass4');
insert into UserLogins values('User5', 'Pass5');
GO
insert into UserSecurityQuestions values('What is your favourite food?');
insert into UserSecurityQuestions values('What is your favourite food?');
insert into UserSecurityQuestions values('What is your favourite food?');
insert into UserSecurityQuestions values('What is your favourite food?');
insert into UserSecurityQuestions values('What is your favourite food?');
GO
Insert into AccountType values('Savings');
Insert into AccountType values('Checking');
GO
--Inserting 5 records into table SavingsInterestRates
insert into SavingsInterestRates values(0.5, 'Low');
insert into SavingsInterestRates values(2, 'Medium');
insert into SavingsInterestRates values(3, 'High');
insert into SavingsInterestRates values(4, 'Very high');
insert into SavingsInterestRates values(5, 'Super high');
GO
select * from AccountStatusType;
insert into AccountStatusType values('Closed');
insert into AccountStatusType values('Active');
insert into AccountStatusType values('Dormant');
insert into AccountStatusType values('Passive');
insert into AccountStatusType values('Active');
GO
insert into FailedTransactionErrorType values('Withdraw limit reached');
insert into FailedTransactionErrorType values('Daily limit reached');
insert into FailedTransactionErrorType values('No tenough balance');
insert into FailedTransactionErrorType values('Invalid denomination');
insert into FailedTransactionErrorType values('ATM machine down');
GO
insert into LoginErrorLog values(TRY_CAST('2015/6/4 07:30:56' AS DATETIME), 'Bad Connection');
insert into LoginErrorLog values(TRY_CAST('2018/6/9 12:34:57' AS DATETIME), 'Invalid User');
insert into LoginErrorLog values(TRY_CAST('2016/4/5 02:14:00' AS DATETIME), 'Wrong Password');
insert into LoginErrorLog values(TRY_CAST('2014/7/5 05:56:59' AS DATETIME), 'Server issue');
insert into LoginErrorLog values(TRY_CAST('2009/10/12 08:34:15' AS DATETIME), 'Datacenter down');
GO
insert into Employee values('E3', 'K', 'E3', '0');
insert into Employee values('E5', 'B', 'E5', '1');
insert into Employee values('E7', 'P', 'E7', '0');
insert into Employee values('E9', 'R', 'E9', '1');
insert into Employee values('E11', 'K', 'E11', '1');
GO
insert into TransactionType values('Balance', 'See money', '0');
insert into TransactionType values('Transfer', 'Send money', '450');
insert into TransactionType values('Receive', 'Get money', '300');
insert into TransactionType values('Paid', 'Paid to John', '45000');
insert into TransactionType values('Statement', 'Checked monthly transaction', '0');
GO
insert into FailedTransactionLog values(1, TRY_CAST('2015/6/4 07:30:56' AS DATETIME), 'First');
insert into FailedTransactionLog values(2, TRY_CAST('2018/6/9 12:34:57' AS DATETIME), 'Second');
insert into FailedTransactionLog values(3, TRY_CAST('2016/4/5 02:14:00' AS DATETIME), 'Third');
insert into FailedTransactionLog values(4, TRY_CAST('2014/7/5 05:56:59' AS DATETIME), 'Fourth');
insert into FailedTransactionLog values(5, TRY_CAST('2009/10/12 08:34:15' AS DATETIME), 'Fifth');
GO
insert into UserSecurityAnswers values('Apples', 1);
insert into UserSecurityAnswers values('Spiderman', 2);
insert into UserSecurityAnswers values('School1', 3);
insert into UserSecurityAnswers values('Ram', 4);
insert into UserSecurityAnswers values('Toyota', 5);
GO
insert into Account values(15000.7, 1, 1, 1);
insert into Account values(25000.5, 2, 2, 2);
insert into Account values(17000.2, 1, 1, 1);
insert into Account values(45000, 2, 2, 2);
insert into Account values(2320, 2, 2, 2);
GO
insert into LoginAccount values(1, 1);
insert into LoginAccount values(2, 2);
insert into LoginAccount values(3, 3);
insert into LoginAccount values(4, 4);
insert into LoginAccount values(5, 5);
GO
insert into Customer values(1, 'Address1', 'Address2', 'Customer1', 'U', 'CLastname1', 'Ottawa', 'ON', '3A5z9z', '[email protected]', '141655555', '453554464', '3462325', 'A12345', 1);
insert into Customer values(2, 'Address1', 'Address2', 'Customer2', 'K', 'CLastname2', 'Hamilton', 'ON', 'fe3453', '[email protected]', '141655555', '567435345', '6332423', 'D34353', 2);
insert into Customer values(3, 'Address1', 'Address2', 'Customer3', 'P', 'CLastname3', 'Vacouver', 'BC', 'fdf45', '[email protected]', '141655555', '681316226', '9202521', 'J56361', 3);
insert into Customer values(4, 'Address1', 'Address2', 'Customer4', 'B', 'CLastname4', 'London', 'ON', '23ffbfs', '[email protected]', '141655555', '795197107', '8674252', 'I78369', 4);
insert into Customer values(5, 'Address1', 'Address2', 'Customer5', 'K', 'CLastname5', 'Calgary', 'AB', 'hg4536', '[email protected]', '141655555', '909077988', '9209371', 'K10377', 5);
GO
insert into CustomerAccount values(1, 1);
insert into CustomerAccount values(2, 2);
insert into CustomerAccount values(3, 3);
insert into CustomerAccount values(4, 4);
insert into CustomerAccount values(5, 5);
GO
insert into TransactionLog values('2015/6/4 07:30:56', 1,15000.7, 7869878, 1, 1, 1, 1);
insert into TransactionLog values('2018/6/9 12:34:57', 2,435435, 675687, 2, 2, 2, 2);
insert into TransactionLog values('2016/4/5 02:14:00', 3,855869.3, 34512356, 3, 3, 3, 3);
insert into TransactionLog values('2014/7/5 05:56:59', 4,1276303.6, 4643234, 4, 4, 4, 4);
insert into TransactionLog values('2009/10/12 08:34:15', 5,1696737.9, 325344, 5, 5, 5, 5);
GO
insert into OverDraftLog values('2015/6/4 07:30:56', 0, 'Clear');
insert into OverDraftLog values('2018/6/9 12:34:57', 5, 'Pending');
insert into OverDraftLog values('2016/4/5 02:14:00', 10, 'Clear');
insert into OverDraftLog values('2014/7/5 05:56:59', 15, 'Pending');
insert into OverDraftLog values('2009/10/12 08:34:15', 20, 'Clear');
-- PHASE II of project begins
--Q1. Create a view to get all customers with checking account from ON province.
DROP VIEW VW_Customer_ON;
GO
CREATE VIEW VW_Customer_ON AS
SELECT DISTINCT c.* FROM Customer c
JOIN Account a
ON c.AccountID = a.AccountId
JOIN AccountType at
ON a.AccountTypeID = at.AccountTypeID
WHERE at.AccountTypeDescription = 'Checking' and c.State = 'ON';
GO
--Q2. Create a view to get all customers with total account balance (including interest rate) greater than 5000.
DROP VIEW VW_Customer_AMT;
GO
CREATE VIEW VW_Customer_ON AS
SELECT c.CustomerFirstName, SUM(a.CurrentBalance) AS Ac_Balance, SUM(a.CurrentBalance + (a.CurrentBalance * s.InterestSavingRatesID)/100) AS Total_Ac_Balance
FROM Customer c
JOIN Account a
ON c.AccountID = a.AccountId
JOIN SavingsInterestRates s
ON a.InterestSavingRatesID = s.InterestSavingRatesID
GROUP BY c.CustomerFirstName
HAVING SUM(a.CurrentBalance + (a.CurrentBalance * s.InterestRatesValue)/100) > 5000;
GO
--Q3. Create a view to get counts of checking and savings accounts by customer.
DROP VIEW VW_Customer_ACC;
GO
CREATE VIEW VW_Customer_ACC
AS
SELECT c.CustomerFirstName, at.AccountTypeDescription, COUNT(*) AS Total_AC_Types FROM Customer c
JOIN Account a
ON c.AccountID = a.AccountId
JOIN AccountType at
ON a.AccountTypeID = at.AccountTypeID
GROUP BY c.CustomerFirstName, at.AccountTypeDescription;
GO
--Q4. Create a view to get any particular user�s login and password using AccountId.
DROP VIEW VW_Account_UL;
GO
CREATE VIEW VW_Account_UL
AS
SELECT DISTINCT ul.UserLogin, ul.UserPassword
FROM UserLogins ul
JOIN LoginAccount la
ON ul.UserLoginID = la.UserLoginID
--JOIN Account a
--ON la.AccountID = a.AccountID;
WHERE la.AccountID = '1'
GO;
--Q5. Create a view to get all customers� overdraft amount.
DROP VIEW VW_Customer_OD;
GO
CREATE VIEW VW_Customer_OD
AS
SELECT DISTINCT c.CustomerFirstName, o.OverDraftAmount
FROM OverDraftLog o
JOIN Customer c
ON o.AccountID = c.AccountID;
GO
--Q6. Create a stored procedure to add "User_" as a prefix to everyone's login (username).
DROP PROCEDURE sp_Update_Login;
GO
CREATE PROCEDURE sp_Update_Login
AS
UPDATE UserLogins
SET UserLogin = Concat('User_', UserLogin);
GO
EXEC sp_Update_Login;
GO
--Q7. Create a stored procedure that accepts AccountId as a parameter and returns customer's full name.
DROP PROCEDURE sp_Customer_Details;
GO
CREATE PROCEDURE sp_Customer_Details @AccountID INT
AS
SELECT c.CustomerFirstName + ' ' + c.CustomerMIddleInitial + ' ' + c.CustomerLastName AS Customer_Full_Name
FROM Customer c
JOIN Account a
ON c.AccountID = a.AccountId
WHERE a.AccountID = @AccountID;
GO
EXEC sp_Costumer_Details 2;
GO
--Q8. Create a stored procedure that returns error logs inserted in the last 24 hours.
DROP PROCEDURE sp_Errors_24;
GO
CREATE PROCEDURE sp_Errors_24
AS
SELECT * FROM LoginErrorLog
WHERE ErrorTime BETWEEN DATEADD(hh, -24, GETDATE()) AND GETDATE();
GO
EXEC sp_Errors_24;
GO
--Q9. Create a stored procedure that takes a deposit as a parameter and updates CurrentBalance value for that particular account.
DROP PROCEDURE sp_Update_cBalance_After_Deposit;
GO
CREATE PROCEDURE sp_Update_cBalance_After_Deposit @AccountID INT, @Deposit INT
AS
UPDATE Account
SET CurrentBalance = CurrentBalance + @Deposit
where AccountID = @AccountID;
GO
EXEC sp_Update_cBalance_After_Deposit 3, 300;
GO
--Q10. Create a stored procedure that takes a withdrawal amount as a parameter and updates CurrentBalance value for that particular account.
DROP PROCEDURE sp_Update_cBalance_After_Withdraw;
GO
CREATE PROCEDURE sp_Update_cBalance_After_Withdraw @AccountID INT, @Withdraw INT
AS
UPDATE Account
SET CurrentBalance = CurrentBalance - @Withdraw
WHERE AccountID = @AccountID;
GO
EXEC sp_Update_cBalance_After_Withdraw 3, 300;
GO
--Q11. Create a stored procedure to remove all security questions for a particular login.
DROP PROCEDURE sp_Delete_Question;
GO
CREATE PROCEDURE sp_Delete_Question @UserLoginID SMALLINT
AS
DELETE UserSecurityQuestions
FROM UserSecurityQuestions uq
JOIN UserSecurityAnswers ua
ON ua.UserSecurityQuestionID = uq.UserSecurityQuestionID
JOIN UserLogins ul
ON ua.UserLoginID = ul.UserLoginID
WHERE ul.UserLoginID = @UserLoginID;
GO
EXEC sp_Delete_Question 5;
GO
--Q12. Delete all error logs created in the last hour.
DROP PROCEDURE sp_Delete_Errors;
GO
CREATE PROCEDURE sp_Delete_Errors
AS
delete FROM LoginErrorLog
WHERE ErrorTime BETWEEN DATEADD(hh, -1, GETDATE()) AND GETDATE();
GO
EXEC sp_Delete_Errors;
GO
--Q13. Write a query to remove SSN column from Customer table.
DROP PROCEDURE sp_Remove_Column;
GO
CREATE PROCEDURE sp_Remove_Column
AS
ALTER TABLE CUSTOMER
DROP COLUMN CustomerAddress1;
GO
EXEC sp_Remove_Column;
GO