├── .gitattributes
├── images
├── ER-Diagram.png
└── DesignedEntities.png
├── README.md
└── Bank-Database-Design.sql
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
--------------------------------------------------------------------------------
/images/ER-Diagram.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gmgyan/Bank-Database-Design_MS-SQL/HEAD/images/ER-Diagram.png
--------------------------------------------------------------------------------
/images/DesignedEntities.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gmgyan/Bank-Database-Design_MS-SQL/HEAD/images/DesignedEntities.png
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Bank-Database-Design
2 |
3 | ### ER Diagram:
4 |
5 |
6 | ### Design Entities:
7 |
8 |
9 | ### Introduction:
10 | There are only two types of accounts at this time: Checking and Savings accounts. The provided column list should be separated into appropriate entities (tables) with relationships between these entities defined. The most efficient choices as far as your primary key constraints and foreign key constraints, and picked the appropriate data types for each of the columns.
11 |
12 | ### Project Goals:
13 | The goal of the project is to understand database entities in more depth and have practical experience of working with different objects of SQL.
14 |
15 | ### Phase I:
16 | 1. Create a database for a banking application called “Bank”.
17 | 2. Create all the tables mentioned in the database diagram.
18 | 3. Create all the constraints based on the database diagram.
19 | 4. Insert at least 5 rows in each table.
20 |
21 | ### Phase II:
22 | 1. Create a view to get all customers with checking account from ON province.
23 | 2. Create a view to get all customers with total account balance (including interest rate) greater than 5000.
24 | 3. Create a view to get counts of checking and savings accounts by customer.
25 | 4. Create a view to get any particular user’s login and password using AccountId.
26 | 5. Create a view to get all customers’ overdraft amount.
27 | 6. Create a stored procedure to add “User_” as a prefix to everyone’s login (username).
28 | 7. Create a stored procedure that accepts AccountId as a parameter and returns customer’s full name.
29 | 8. Create a stored procedure that returns error logs inserted in the last 24 hours.
30 | 9. Create a stored procedure that takes a deposit as a parameter and updates CurrentBalance value for particular account.
31 | 10. Create a stored procedure that takes a withdrawal amount as a parameter and updates CurrentBalance value for that account.
32 | 11. Create a stored procedure to remove all security questions for a particular login.
33 | 12. Delete all error logs created in the last hour.
34 | 13. Write a query to remove SSN column from Customer table.
35 | 14. Prepare a report to describe the project.
36 | 15. Prepare a presentation for the project.
37 |
38 |
39 |
--------------------------------------------------------------------------------
/Bank-Database-Design.sql:
--------------------------------------------------------------------------------
1 |
2 | /* ## MS-SQL PROJECT on Banking Transactions by Gyan Kumar GM ! */
3 |
4 | -- PHASE I of project begins
5 |
6 | --Q1. Create a database for a banking application called 'Bank'.
7 |
8 | create database dbBankGM;
9 | GO
10 |
11 | /* NOTE: Unlike asked in question database name has been used as 'dbBankGM' instead of 'Bank' in order to make it unique
12 | from other students. Following code is used to select 'dbBankGM' as current database */
13 |
14 | use dbBankGM;
15 | GO
16 |
17 | --Q2. Create all the tables mentioned in the database diagram.
18 | --Q3. Create all the constraints based on the database diagram.
19 |
20 | --NOTE: Solution to questions 2 and 3 are provided at once as follows. This was confirmed with teacher!
21 |
22 | /* CREATE TABLE & ADD CONSTRAINTS Section */
23 | -- Creating tables only with primary keys first
24 |
25 | --Creating table named UserLogins
26 | CREATE TABLE UserLogins
27 | (
28 | UserLoginID SMALLINT NOT NULL IDENTITY(1,1),
29 | UserLogin VARCHAR(50) NOT NULL,
30 | UserPassword VARCHAR(20) NOT NULL,
31 | CONSTRAINT pk_UL_UserLoginID PRIMARY KEY(UserLoginID)
32 | );
33 | GO
34 |
35 | --Creating table named UserSecurityQuestions
36 | CREATE TABLE UserSecurityQuestions
37 | (
38 | UserSecurityQuestionID TINYINT NOT NULL IDENTITY(1,1),
39 | UserSecurityQuestion VARCHAR(50) NOT NULL,
40 | CONSTRAINT pk_USQ_UserSecurityQuestionID PRIMARY KEY(UserSecurityQuestionID)
41 | );
42 | GO
43 |
44 |
45 | --Creating table named AccountType
46 | CREATE TABLE AccountType
47 | (
48 | AccountTypeID TINYINT NOT NULL IDENTITY(1,1),
49 | AccountTypeDescription VARCHAR(30) NOT NULL,
50 | CONSTRAINT pk_AT_AccountTypeID PRIMARY KEY(AccountTypeID)
51 | );
52 | GO
53 |
54 | --Creating table named SavingsInterestRates
55 | /* NOTE: Altered the table to accept datatype as NUMERIC(9,2) in order to avoid Arithmetic Conversion error using
56 | code "ALTER TABLE SavingsInterestRates ALTER COLUMN IntetestRatesValue NUMERIC(9,2);" */
57 | CREATE TABLE SavingsInterestRates
58 | (
59 | InterestSavingRatesID TINYINT NOT NULL IDENTITY(1,1),
60 | InterestRatesValue NUMERIC(9,9) NOT NULL,
61 | InterestRatesDescription VARCHAR(20) NOT NULL,
62 | CONSTRAINT pk_SIR_InterestSavingRatesID PRIMARY KEY(InterestSavingRatesID)
63 | );
64 | GO
65 |
66 | --Creating table named AccountStatusType
67 | CREATE TABLE AccountStatusType
68 | (
69 | AccountStatusTypeID TINYINT NOT NULL IDENTITY(1,1),
70 | AccountStatusTypeDescription VARCHAR(30) NOT NULL,
71 | CONSTRAINT pk_AST_AccountStatusTypeID PRIMARY KEY(AccountStatusTypeID)
72 | );
73 | GO
74 |
75 | --Creating table named FailedTransactionErrorType
76 | CREATE TABLE FailedTransactionErrorType
77 | (
78 | FailedTransactionErrorTypeID TINYINT NOT NULL IDENTITY(1,1),
79 | FailedTransactionErrorTypeDescription VARCHAR(50) NOT NULL,
80 | CONSTRAINT pk_FTET_FailedTransactionErrorTypeID PRIMARY KEY(FailedTransactionErrorTypeID)
81 | );
82 | GO
83 |
84 | --Creating table named LoginErrorLog
85 | CREATE TABLE LoginErrorLog
86 | (
87 | ErrorLogID INT NOT NULL IDENTITY(1,1),
88 | ErrorTime DATETIME NOT NULL,
89 | FailedTransactionXML XML,
90 | CONSTRAINT pk_LEL_ErrorLogID PRIMARY KEY(ErrorLogID)
91 | );
92 | GO
93 |
94 | --Creating table named Employee
95 | CREATE TABLE Employee
96 | (
97 | EmployeeID INT NOT NULL IDENTITY(1,1),
98 | EmployeeFirstName VARCHAR(25) NOT NULL,
99 | EmployeeMiddleInitial CHAR(1),
100 | EmployeeLastName VARCHAR(25),
101 | EmployeeisManager BIT,
102 | CONSTRAINT pk_E_EmployeeID PRIMARY KEY(EmployeeID)
103 | );
104 | GO
105 |
106 | --Creating table named TransactionType
107 | CREATE TABLE TransactionType
108 | (
109 | TransactionTypeID TINYINT NOT NULL IDENTITY(1,1),
110 | TransactionTypeName CHAR(10) NOT NULL,
111 | TransactionTypeDescription VARCHAR(50),
112 | TransactionFeeAmount SMALLMONEY,
113 | CONSTRAINT pk_TT_TransactionTypeID PRIMARY KEY(TransactionTypeID)
114 | );
115 | GO
116 |
117 | -- Creating tables with foreign key and combination of both primary and foreign keys
118 | --Creating table named FailedTransactionLog
119 | CREATE TABLE FailedTransactionLog
120 | (
121 | FailedTransactionID INT NOT NULL IDENTITY(1,1),
122 | FailedTransactionErrorTypeID TINYINT NOT NULL,
123 | FailedTransactionErrorTime DATETIME,
124 | FailedTransactionErrorXML XML,
125 | CONSTRAINT pk_FTL_FailedTransactionID PRIMARY KEY(FailedTransactionID),
126 | CONSTRAINT fk_FTET_FailedTransactionErrorTypeID FOREIGN KEY(FailedTransactionErrorTypeID) REFERENCES FailedTransactionErrorType(FailedTransactionErrorTypeID)
127 | );
128 | GO
129 |
130 | --Creating table named UserSecurityAnswers
131 | CREATE TABLE UserSecurityAnswers
132 | (
133 | UserLoginID SMALLINT NOT NULL IDENTITY(1,1),
134 | UserSecurityAnswers VARCHAR(25) NOT NULL,
135 | UserSecurityQuestionID TINYINT NOT NULL,
136 | CONSTRAINT pk_USA_UserLoginID PRIMARY KEY(UserLoginID),
137 | CONSTRAINT fk_UL_UserLoginID FOREIGN KEY(UserLoginID) REFERENCES UserLogins(UserLoginID),
138 | CONSTRAINT fk_USQ_UserSecurityQuestionID FOREIGN KEY(UserSecurityQuestionID) REFERENCES UserSecurityQuestions(UserSecurityQuestionID)
139 | );
140 | GO
141 | --Creating table named Account
142 | CREATE TABLE Account
143 | (
144 | AccountID INT NOT NULL IDENTITY(1,1),
145 | CurrentBalance INT NOT NULL,
146 | AccountTypeID TINYINT NOT NULL REFERENCES AccountType (AccountTypeID),
147 | AccountStatusTypeID TINYINT NOT NULL,
148 | InterestSavingRatesID TINYINT NOT NULL,
149 | CONSTRAINT pk_A_AccounID PRIMARY KEY(AccountID),
150 | CONSTRAINT fk_AST_AccountStatusTypeID FOREIGN KEY(AccountStatusTypeID) REFERENCES AccountStatusType(AccountStatusTypeID),
151 | CONSTRAINT fk_SIR_InterestSavingRatesID FOREIGN KEY(InterestSavingRatesID) REFERENCES SavingsInterestRates(InterestSavingRatesID)
152 | );
153 | GO
154 |
155 | --Creating table named LoginAccount
156 | --NOTE: Unlike ER diagram table name has been used as LoginAccounts instead of Login-Account
157 | CREATE TABLE LoginAccount
158 | (
159 | UserLoginID SMALLINT NOT NULL,
160 | AccountID INT NOT NULL,
161 | CONSTRAINT fk_UL_UserLogins FOREIGN KEY(UserLoginID) REFERENCES UserLogins(UserLoginID),
162 | CONSTRAINT fk_A_Account FOREIGN KEY(AccountID) REFERENCES Account(AccountID)
163 | );
164 | GO
165 |
166 | --Creating table named Customer
167 | CREATE TABLE Customer
168 | (
169 | CustomerID INT NOT NULL IDENTITY(1,1),
170 | AccountID INT NOT NULL,
171 | CustomerAddress1 VARCHAR(30) NOT NULL,
172 | CustomerAddress2 VARCHAR(30),
173 | CustomerFirstName VARCHAR(30) NOT NULL,
174 | CustomerMiddleInitial CHAR(1),
175 | CustomerLastName VARCHAR(30) NOT NULL,
176 | City VARCHAR(20) NOT NULL,
177 | State CHAR(2) NOT NULL,
178 | ZipCode CHAR(10) NOT NULL,
179 | EmailAddress CHAR(40) NOT NULL,
180 | HomePhone VARCHAR(10) NOT NULL,
181 | CellPhone VARCHAR(10) NOT NULL,
182 | WorkPhone VARCHAR(10) NOT NULL,
183 | SSN VARCHAR(9),
184 | UserLoginID SMALLINT NOT NULL,
185 | CONSTRAINT pk_C_CustomerID PRIMARY KEY(CustomerID),
186 | CONSTRAINT fk_A_AccountID FOREIGN KEY(AccountID) REFERENCES Account(AccountID),
187 | CONSTRAINT fk_UL_C_UserLoginID FOREIGN KEY(UserLoginID) REFERENCES UserLogins(UserLoginID)
188 | );
189 | GO
190 |
191 | --Creating table named CustomerAccount
192 | --NOTE: Unlike ER diagram table name has been used as CustomerAccounts instead of Customer-Account
193 | CREATE TABLE CustomerAccount
194 | (
195 | AccountID INT NOT NULL ,
196 | CustomerID INT NOT NULL,
197 | CONSTRAINT fk_A_CA_AccountID FOREIGN KEY(AccountID) REFERENCES Account(AccountID),
198 | CONSTRAINT fk_C_CA_CustomerID FOREIGN KEY(CustomerID) REFERENCES Customer(CustomerID)
199 | );
200 | GO
201 |
202 | --Creating table named TransactionLog
203 | CREATE TABLE TransactionLog
204 | (
205 | TransactionID INT NOT NULL IDENTITY(1,1),
206 | TransactionDate DATETIME NOT NULL,
207 | TransactionTypeID TINYINT NOT NULL,
208 | TransactionAmount Money NOT NULL,
209 | NewBalance Money NOT NULL,
210 | AccountID INT NOT NULL,
211 | CustomerID INT NOT NULL,
212 | EmployeeID INT NOT NULL,
213 | UserLoginID SMALLINT NOT NULL,
214 | CONSTRAINT pk_TL_TransactionID PRIMARY KEY(TransactionID),
215 | CONSTRAINT fk_TT_TL_TransactionTypeID FOREIGN KEY(TransactionTypeID) REFERENCES TransactionType(TransactionTypeID),
216 | CONSTRAINT fk_A_TL_AccountID FOREIGN KEY(AccountID) REFERENCES Account(AccountID),
217 | CONSTRAINT fk_C_TL_CustomerID FOREIGN KEY(CustomerID) REFERENCES Customer(CustomerID),
218 | CONSTRAINT fk_E_TL_EmployeeID FOREIGN KEY(EmployeeID) REFERENCES Employee(EmployeeID),
219 | CONSTRAINT fk_UL_TL_UserLoginID FOREIGN KEY(UserLoginID) REFERENCES UserLogins(UserLoginID)
220 | );
221 | GO
222 |
223 | --Creating table named OverDraftLog
224 | CREATE TABLE OverDraftLog
225 | (
226 | AccountID INT NOT NULL IDENTITY(1,1),
227 | OverDraftDate DATETIME NOT NULL,
228 | OverDraftAmount Money NOT NULL,
229 | OverDraftTransactionXML XML NOT NULL,
230 | CONSTRAINT Pk_ODL_AccountID PRIMARY KEY(AccountID),
231 | CONSTRAINT fk_A_ODL_AccountID FOREIGN KEY(AccountID) REFERENCES Account(AccountID)
232 | );
233 | GO
234 |
235 | --Q4. Insert at least 5 rows in each table.
236 | /** INSERT rows section **/
237 |
238 | insert into UserLogins values('User1', 'Pass1');
239 | insert into UserLogins values('User2', 'Pass2');
240 | insert into UserLogins values('User3', 'Pass3');
241 | insert into UserLogins values('User4', 'Pass4');
242 | insert into UserLogins values('User5', 'Pass5');
243 | GO
244 |
245 | insert into UserSecurityQuestions values('What is your favourite food?');
246 | insert into UserSecurityQuestions values('What is your favourite food?');
247 | insert into UserSecurityQuestions values('What is your favourite food?');
248 | insert into UserSecurityQuestions values('What is your favourite food?');
249 | insert into UserSecurityQuestions values('What is your favourite food?');
250 | GO
251 |
252 | Insert into AccountType values('Savings');
253 | Insert into AccountType values('Checking');
254 | GO
255 |
256 | --Inserting 5 records into table SavingsInterestRates
257 | insert into SavingsInterestRates values(0.5, 'Low');
258 | insert into SavingsInterestRates values(2, 'Medium');
259 | insert into SavingsInterestRates values(3, 'High');
260 | insert into SavingsInterestRates values(4, 'Very high');
261 | insert into SavingsInterestRates values(5, 'Super high');
262 | GO
263 |
264 | select * from AccountStatusType;
265 | insert into AccountStatusType values('Closed');
266 | insert into AccountStatusType values('Active');
267 | insert into AccountStatusType values('Dormant');
268 | insert into AccountStatusType values('Passive');
269 | insert into AccountStatusType values('Active');
270 | GO
271 |
272 | insert into FailedTransactionErrorType values('Withdraw limit reached');
273 | insert into FailedTransactionErrorType values('Daily limit reached');
274 | insert into FailedTransactionErrorType values('No tenough balance');
275 | insert into FailedTransactionErrorType values('Invalid denomination');
276 | insert into FailedTransactionErrorType values('ATM machine down');
277 | GO
278 |
279 | insert into LoginErrorLog values(TRY_CAST('2015/6/4 07:30:56' AS DATETIME), 'Bad Connection');
280 | insert into LoginErrorLog values(TRY_CAST('2018/6/9 12:34:57' AS DATETIME), 'Invalid User');
281 | insert into LoginErrorLog values(TRY_CAST('2016/4/5 02:14:00' AS DATETIME), 'Wrong Password');
282 | insert into LoginErrorLog values(TRY_CAST('2014/7/5 05:56:59' AS DATETIME), 'Server issue');
283 | insert into LoginErrorLog values(TRY_CAST('2009/10/12 08:34:15' AS DATETIME), 'Datacenter down');
284 | GO
285 |
286 | insert into Employee values('E3', 'K', 'E3', '0');
287 | insert into Employee values('E5', 'B', 'E5', '1');
288 | insert into Employee values('E7', 'P', 'E7', '0');
289 | insert into Employee values('E9', 'R', 'E9', '1');
290 | insert into Employee values('E11', 'K', 'E11', '1');
291 | GO
292 |
293 | insert into TransactionType values('Balance', 'See money', '0');
294 | insert into TransactionType values('Transfer', 'Send money', '450');
295 | insert into TransactionType values('Receive', 'Get money', '300');
296 | insert into TransactionType values('Paid', 'Paid to John', '45000');
297 | insert into TransactionType values('Statement', 'Checked monthly transaction', '0');
298 | GO
299 |
300 | insert into FailedTransactionLog values(1, TRY_CAST('2015/6/4 07:30:56' AS DATETIME), 'First');
301 | insert into FailedTransactionLog values(2, TRY_CAST('2018/6/9 12:34:57' AS DATETIME), 'Second');
302 | insert into FailedTransactionLog values(3, TRY_CAST('2016/4/5 02:14:00' AS DATETIME), 'Third');
303 | insert into FailedTransactionLog values(4, TRY_CAST('2014/7/5 05:56:59' AS DATETIME), 'Fourth');
304 | insert into FailedTransactionLog values(5, TRY_CAST('2009/10/12 08:34:15' AS DATETIME), 'Fifth');
305 | GO
306 |
307 | insert into UserSecurityAnswers values('Apples', 1);
308 | insert into UserSecurityAnswers values('Spiderman', 2);
309 | insert into UserSecurityAnswers values('School1', 3);
310 | insert into UserSecurityAnswers values('Ram', 4);
311 | insert into UserSecurityAnswers values('Toyota', 5);
312 | GO
313 |
314 | insert into Account values(15000.7, 1, 1, 1);
315 | insert into Account values(25000.5, 2, 2, 2);
316 | insert into Account values(17000.2, 1, 1, 1);
317 | insert into Account values(45000, 2, 2, 2);
318 | insert into Account values(2320, 2, 2, 2);
319 | GO
320 |
321 | insert into LoginAccount values(1, 1);
322 | insert into LoginAccount values(2, 2);
323 | insert into LoginAccount values(3, 3);
324 | insert into LoginAccount values(4, 4);
325 | insert into LoginAccount values(5, 5);
326 | GO
327 |
328 | insert into Customer values(1, 'Address1', 'Address2', 'Customer1', 'U', 'CLastname1', 'Ottawa', 'ON', '3A5z9z', 'user5@user.com', '141655555', '453554464', '3462325', 'A12345', 1);
329 | insert into Customer values(2, 'Address1', 'Address2', 'Customer2', 'K', 'CLastname2', 'Hamilton', 'ON', 'fe3453', 'user6@user.com', '141655555', '567435345', '6332423', 'D34353', 2);
330 | insert into Customer values(3, 'Address1', 'Address2', 'Customer3', 'P', 'CLastname3', 'Vacouver', 'BC', 'fdf45', 'user7@user.com', '141655555', '681316226', '9202521', 'J56361', 3);
331 | insert into Customer values(4, 'Address1', 'Address2', 'Customer4', 'B', 'CLastname4', 'London', 'ON', '23ffbfs', 'user8@user.com', '141655555', '795197107', '8674252', 'I78369', 4);
332 | insert into Customer values(5, 'Address1', 'Address2', 'Customer5', 'K', 'CLastname5', 'Calgary', 'AB', 'hg4536', 'user9@user.com', '141655555', '909077988', '9209371', 'K10377', 5);
333 | GO
334 |
335 | insert into CustomerAccount values(1, 1);
336 | insert into CustomerAccount values(2, 2);
337 | insert into CustomerAccount values(3, 3);
338 | insert into CustomerAccount values(4, 4);
339 | insert into CustomerAccount values(5, 5);
340 | GO
341 |
342 | insert into TransactionLog values('2015/6/4 07:30:56', 1,15000.7, 7869878, 1, 1, 1, 1);
343 | insert into TransactionLog values('2018/6/9 12:34:57', 2,435435, 675687, 2, 2, 2, 2);
344 | insert into TransactionLog values('2016/4/5 02:14:00', 3,855869.3, 34512356, 3, 3, 3, 3);
345 | insert into TransactionLog values('2014/7/5 05:56:59', 4,1276303.6, 4643234, 4, 4, 4, 4);
346 | insert into TransactionLog values('2009/10/12 08:34:15', 5,1696737.9, 325344, 5, 5, 5, 5);
347 | GO
348 |
349 | insert into OverDraftLog values('2015/6/4 07:30:56', 0, 'Clear');
350 | insert into OverDraftLog values('2018/6/9 12:34:57', 5, 'Pending');
351 | insert into OverDraftLog values('2016/4/5 02:14:00', 10, 'Clear');
352 | insert into OverDraftLog values('2014/7/5 05:56:59', 15, 'Pending');
353 | insert into OverDraftLog values('2009/10/12 08:34:15', 20, 'Clear');
354 |
355 |
356 | -- PHASE II of project begins
357 |
358 | --Q1. Create a view to get all customers with checking account from ON province.
359 |
360 | DROP VIEW VW_Customer_ON;
361 | GO
362 |
363 | CREATE VIEW VW_Customer_ON AS
364 | SELECT DISTINCT c.* FROM Customer c
365 | JOIN Account a
366 | ON c.AccountID = a.AccountId
367 | JOIN AccountType at
368 | ON a.AccountTypeID = at.AccountTypeID
369 | WHERE at.AccountTypeDescription = 'Checking' and c.State = 'ON';
370 | GO
371 |
372 | --Q2. Create a view to get all customers with total account balance (including interest rate) greater than 5000.
373 |
374 | DROP VIEW VW_Customer_AMT;
375 | GO
376 |
377 | CREATE VIEW VW_Customer_ON AS
378 | SELECT c.CustomerFirstName, SUM(a.CurrentBalance) AS Ac_Balance, SUM(a.CurrentBalance + (a.CurrentBalance * s.InterestSavingRatesID)/100) AS Total_Ac_Balance
379 | FROM Customer c
380 | JOIN Account a
381 | ON c.AccountID = a.AccountId
382 | JOIN SavingsInterestRates s
383 | ON a.InterestSavingRatesID = s.InterestSavingRatesID
384 | GROUP BY c.CustomerFirstName
385 | HAVING SUM(a.CurrentBalance + (a.CurrentBalance * s.InterestRatesValue)/100) > 5000;
386 | GO
387 |
388 | --Q3. Create a view to get counts of checking and savings accounts by customer.
389 |
390 | DROP VIEW VW_Customer_ACC;
391 | GO
392 |
393 | CREATE VIEW VW_Customer_ACC
394 | AS
395 | SELECT c.CustomerFirstName, at.AccountTypeDescription, COUNT(*) AS Total_AC_Types FROM Customer c
396 | JOIN Account a
397 | ON c.AccountID = a.AccountId
398 | JOIN AccountType at
399 | ON a.AccountTypeID = at.AccountTypeID
400 | GROUP BY c.CustomerFirstName, at.AccountTypeDescription;
401 | GO
402 |
403 | --Q4. Create a view to get any particular user�s login and password using AccountId.
404 |
405 | DROP VIEW VW_Account_UL;
406 | GO
407 |
408 | CREATE VIEW VW_Account_UL
409 | AS
410 | SELECT DISTINCT ul.UserLogin, ul.UserPassword
411 | FROM UserLogins ul
412 | JOIN LoginAccount la
413 | ON ul.UserLoginID = la.UserLoginID
414 | --JOIN Account a
415 | --ON la.AccountID = a.AccountID;
416 | WHERE la.AccountID = '1'
417 | GO;
418 |
419 |
420 | --Q5. Create a view to get all customers� overdraft amount.
421 |
422 | DROP VIEW VW_Customer_OD;
423 | GO
424 |
425 | CREATE VIEW VW_Customer_OD
426 | AS
427 | SELECT DISTINCT c.CustomerFirstName, o.OverDraftAmount
428 | FROM OverDraftLog o
429 | JOIN Customer c
430 | ON o.AccountID = c.AccountID;
431 | GO
432 |
433 | --Q6. Create a stored procedure to add "User_" as a prefix to everyone's login (username).
434 |
435 | DROP PROCEDURE sp_Update_Login;
436 | GO
437 |
438 | CREATE PROCEDURE sp_Update_Login
439 | AS
440 | UPDATE UserLogins
441 | SET UserLogin = Concat('User_', UserLogin);
442 | GO
443 | EXEC sp_Update_Login;
444 | GO
445 |
446 | --Q7. Create a stored procedure that accepts AccountId as a parameter and returns customer's full name.
447 |
448 | DROP PROCEDURE sp_Customer_Details;
449 | GO
450 |
451 | CREATE PROCEDURE sp_Customer_Details @AccountID INT
452 | AS
453 | SELECT c.CustomerFirstName + ' ' + c.CustomerMIddleInitial + ' ' + c.CustomerLastName AS Customer_Full_Name
454 | FROM Customer c
455 | JOIN Account a
456 | ON c.AccountID = a.AccountId
457 | WHERE a.AccountID = @AccountID;
458 | GO
459 |
460 | EXEC sp_Costumer_Details 2;
461 | GO
462 |
463 |
464 | --Q8. Create a stored procedure that returns error logs inserted in the last 24 hours.
465 |
466 | DROP PROCEDURE sp_Errors_24;
467 | GO
468 |
469 | CREATE PROCEDURE sp_Errors_24
470 | AS
471 | SELECT * FROM LoginErrorLog
472 | WHERE ErrorTime BETWEEN DATEADD(hh, -24, GETDATE()) AND GETDATE();
473 | GO
474 |
475 | EXEC sp_Errors_24;
476 | GO
477 |
478 | --Q9. Create a stored procedure that takes a deposit as a parameter and updates CurrentBalance value for that particular account.
479 |
480 | DROP PROCEDURE sp_Update_cBalance_After_Deposit;
481 | GO
482 |
483 | CREATE PROCEDURE sp_Update_cBalance_After_Deposit @AccountID INT, @Deposit INT
484 | AS
485 | UPDATE Account
486 | SET CurrentBalance = CurrentBalance + @Deposit
487 | where AccountID = @AccountID;
488 | GO
489 |
490 | EXEC sp_Update_cBalance_After_Deposit 3, 300;
491 | GO
492 |
493 | --Q10. Create a stored procedure that takes a withdrawal amount as a parameter and updates CurrentBalance value for that particular account.
494 |
495 | DROP PROCEDURE sp_Update_cBalance_After_Withdraw;
496 | GO
497 |
498 | CREATE PROCEDURE sp_Update_cBalance_After_Withdraw @AccountID INT, @Withdraw INT
499 | AS
500 | UPDATE Account
501 | SET CurrentBalance = CurrentBalance - @Withdraw
502 | WHERE AccountID = @AccountID;
503 | GO
504 |
505 | EXEC sp_Update_cBalance_After_Withdraw 3, 300;
506 | GO
507 |
508 |
509 | --Q11. Create a stored procedure to remove all security questions for a particular login.
510 |
511 | DROP PROCEDURE sp_Delete_Question;
512 | GO
513 |
514 | CREATE PROCEDURE sp_Delete_Question @UserLoginID SMALLINT
515 | AS
516 | DELETE UserSecurityQuestions
517 | FROM UserSecurityQuestions uq
518 | JOIN UserSecurityAnswers ua
519 | ON ua.UserSecurityQuestionID = uq.UserSecurityQuestionID
520 | JOIN UserLogins ul
521 | ON ua.UserLoginID = ul.UserLoginID
522 | WHERE ul.UserLoginID = @UserLoginID;
523 | GO
524 |
525 | EXEC sp_Delete_Question 5;
526 | GO
527 |
528 | --Q12. Delete all error logs created in the last hour.
529 |
530 | DROP PROCEDURE sp_Delete_Errors;
531 | GO
532 |
533 | CREATE PROCEDURE sp_Delete_Errors
534 | AS
535 | delete FROM LoginErrorLog
536 | WHERE ErrorTime BETWEEN DATEADD(hh, -1, GETDATE()) AND GETDATE();
537 | GO
538 |
539 | EXEC sp_Delete_Errors;
540 | GO
541 |
542 | --Q13. Write a query to remove SSN column from Customer table.
543 |
544 | DROP PROCEDURE sp_Remove_Column;
545 | GO
546 |
547 | CREATE PROCEDURE sp_Remove_Column
548 | AS
549 | ALTER TABLE CUSTOMER
550 | DROP COLUMN CustomerAddress1;
551 | GO
552 |
553 | EXEC sp_Remove_Column;
554 | GO
555 |
--------------------------------------------------------------------------------