Chris Mc Glothen Sql Portfolio

31
.SQL Portfolio Name: Chris McGlothen Email: Chris.McGlothen@setfoc us.com Phone: (702) 839- 9568

description

My portfolio showing examples of use, creation, and maintenance of SQL Server 2005 database objects.

Transcript of Chris Mc Glothen Sql Portfolio

Page 1: Chris Mc Glothen Sql Portfolio

.SQL Portfolio

Name: Chris McGlothenEmail: [email protected]

Phone: (702) 839-9568

Page 2: Chris Mc Glothen Sql Portfolio

Table of Contents

• Bank Project• ER Diagram• Stored Procedures• User Defined Functions• Triggers

• Library Project• ER Diagram• SSRS Reports

Page 3: Chris Mc Glothen Sql Portfolio

Table of Contents (cont.)

• Online Movie Project• ER Diagram• XML Schema Collection• Shred XML file • Backup and Recovery Plan• Security Considerations• Users/Roles• Application

Page 4: Chris Mc Glothen Sql Portfolio

Bank Project

• Introduction: • Develop custom stored procedures to

manage transactions for a client bank operation.• Shred initial data from XML file into

database

Page 5: Chris Mc Glothen Sql Portfolio

ER Diagram

Page 6: Chris Mc Glothen Sql Portfolio

TRY/CATCH BlocksBEGIN TRY

--Body of Stored Procedure, including validation and BEGIN TRANSACTION statementEND TRYBEGIN CATCH

--Rollback the transaction--Perform while loop to close all uncommitted transactionsDECLARE @tmpCount intSELECT @tmpCount = CONVERT(int, @@TRANCOUNT)IF @tmpCount >= 0

BEGINWHILE @tmpCount > 0

BEGINROLLBACK TRANSET @tmpCount = @tmpCount - 1

ENDEND

--Declare local variables to hold the error information passes back to the callerDECLARE @Message nvarchar(4000);DECLARE @Severity int;DECLARE @State int;--Set the variables to the error valuesSELECT

@Message = ERROR_MESSAGE(),@Severity = ERROR_SEVERITY(),@State = ERROR_STATE();

--Raise the error to the caller with the error variablesRAISERROR(@Message, @Severity, @State)RETURN -1

END CATCH

Page 7: Chris Mc Glothen Sql Portfolio

Debit Account Procedure

Page 8: Chris Mc Glothen Sql Portfolio

Transfer Funds Procedure

Page 9: Chris Mc Glothen Sql Portfolio

Interest with Avg. Daily Balance

Page 10: Chris Mc Glothen Sql Portfolio

Get Customer Data w/ Dynamic WHERE Clause

CREATE PROCEDURE uspGetCustomer@AccountIDint = NULL -- NULL, @CustomerID int = NULL -- NULL, @Name varchar(50) = NULL -- NULL, @Phone varchar(12) = NULL -- NULL

ASBEGIN TRY

--Use the ISNULL function to create a dynamic WHERE clauseSELECT *FROM vwCustomersWHERE AccountID = ISNULL(@AccountID, AccountID)

AND CustomerID = ISNULL(@CustomerID, CustomerID)AND [Name] LIKE ISNULL('%' + @Name + '%', '%')AND (HomePhone LIKE ISNULL('%' + @Phone + '%', '%')

OR WorkPhone LIKE ISNULL('%' + @Phone + '%', '%')OR CellPhone LIKE ISNULL('%' + @Phone + '%', '%'))

ORDER BY TransactionDate DESCEND TRY

Page 11: Chris Mc Glothen Sql Portfolio

Calculate Monthly Interest FunctionCREATE FUNCTION [dbo].[ufnCalcMonthlyInterest] ( @Balance money, @ID int, @AcctType

tinyint )RETURNS moneyASBEGIN

DECLARE @InterestToBeAdded moneyDECLARE @MonthlyRate decimal(6,6)

SET @MonthlyRate = ((SELECT InterestRate FROM AccountType WHERE AccountTypeID = @AcctType) / 12)SET @InterestToBeAdded = (SELECT (@Balance * @MonthlyRate) FROM Account WHERE AccountID = @ID)

RETURN @InterestToBeAdded

END

Page 12: Chris Mc Glothen Sql Portfolio

DDL TriggerCREATE TRIGGER tgrSafetyON DATABASE FOR DROP_TABLE, ALTER_TABLE, DROP_VIEW, ALTER_VIEWASBEGIN

SET NOCOUNT ON;--Throw an errorRAISERROR ('You may not drop or alter objects from this database.', 11,1)

IF @@TRANCOUNT > 0BEGIN

--Rollback the transactionROLLBACK TRANSACTION

ENDEND

Page 13: Chris Mc Glothen Sql Portfolio

DML TriggerCREATE TRIGGER tgrGuardTransactionsON Transactions INSTEAD OF DELETE, UPDATEASBEGIN

--Throw an error RAISERROR ('Deletions and Updates not allowed from this table (source = instead of)', 11, 1)

IF @@TRANCOUNT > 0BEGIN

--Rollback the transactionROLLBACK TRANSACTION

ENDEND

Page 14: Chris Mc Glothen Sql Portfolio

Library Project

• Introduction: • Develop custom SSRS reports for an existing

database management system

Page 15: Chris Mc Glothen Sql Portfolio

ER Diagram

Page 16: Chris Mc Glothen Sql Portfolio

Most Active Members by Month and Year

Page 17: Chris Mc Glothen Sql Portfolio

Active Member Report ProcedureCREATE PROCEDURE [dbo].[uspGetActiveMembers]

@Month int, @Year int

ASDECLARE @LoanTable TABLE

(MemberNo int, FullName varchar(100), OutDate datetime, CurrentLoan bit

)INSERT INTO @LoanTable

SELECT m.Member_No, (m.FirstName + ' ' + ISNULL(m.MiddleInitial + ' ', '') + m.LastName), lh.Out_Date, 0

FROM Member mINNER JOIN LoanHist lh

ON m.Member_No = lh.Member_NoUNION

SELECT m.Member_No, (m.FirstName + ' ' + ISNULL(m.MiddleInitial + ' ', '') + m.LastName), l.Out_Date, 1

FROM Member mINNER JOIN Loan l

ON m.Member_No = l.Member_NoSELECT MemberNo, FullName, COUNT(*) AS TotalLoansFROM @LoanTable ltWHERE MONTH(OutDate) = @Month AND YEAR(OutDate) = @Year GROUP BY MemberNo, FullNameORDER BY TotalLoans DESC

Page 18: Chris Mc Glothen Sql Portfolio

Current Fines Report

Page 19: Chris Mc Glothen Sql Portfolio

Current Fine Report ProcedureCREATE PROCEDURE [dbo].[uspCurrentOverDue]

AS

SELECT CONVERT(varchar, l.Due_Date, 110) AS Due_Date, DATEDIFF(dd, l.Due_Date, GETDATE()) AS DaysOverDue, CAST((DATEDIFF(dd, l.Due_Date, GETDATE()) * .25) AS money) AS FineDue, l.ISBN, t.Title, (m.FirstName + ' ' + ISNULL(m.MiddleInitial + ' ', '') + m.LastName) AS FullName

FROM Loan lINNER JOIN Title t

ON l.Title_No = t.Title_NoINNER JOIN Member m

ON l.Member_No = m.Member_NoWHERE DATEDIFF(dd, l.Due_Date, GETDATE()) >0ORDER BY FullName

Page 20: Chris Mc Glothen Sql Portfolio

Kids Club Report

Page 21: Chris Mc Glothen Sql Portfolio

Kids Club Report Procedure• CREATE PROCEDURE [dbo].[uspGetKids]• AS• DECLARE @KidsTable TABLE• (• MemberNo int• , FullName varchar(100)• , Age int• )• INSERT INTO @KidsTable• (• MemberNo• , FullName• , Age• )• SELECT j.Member_No, (m.FirstName + ' ' + ISNULL(m.MiddleInitial + ' ', '') + m.LastName) AS FullName, 7 AS Age• FROM Juvenile j• INNER JOIN Member m ON j.Member_No = m.Member_No• WHERE j.Birth_Date BETWEEN (CAST(MONTH(GETDATE()) AS varchar) + '/' + CAST(DAY(GETDATE() + 1)AS varchar) + '/' + CAST((YEAR(DATEADD(yy, -8, GETDATE()))) AS varchar)) • AND CAST(MONTH(GETDATE()) AS varchar) + '/' + CAST(DAY(GETDATE())AS varchar) + '/' + CAST((YEAR(DATEADD(yy, -7, GETDATE()))) AS varchar)• UNION• SELECT j.Member_No, (m.FirstName + ' ' + ISNULL(m.MiddleInitial + ' ', '') + m.LastName) AS FullName, 8 AS Age• FROM Juvenile j• INNER JOIN Member m ON j.Member_No = m.Member_No• WHERE j.Birth_Date BETWEEN (CAST(MONTH(GETDATE()) AS varchar) + '/' + CAST(DAY(GETDATE() + 1)AS varchar) + '/' + CAST((YEAR(DATEADD(yy, -9, GETDATE()))) AS varchar)) • AND CAST(MONTH(GETDATE()) AS varchar) + '/' + CAST(DAY(GETDATE())AS varchar) + '/' + CAST((YEAR(DATEADD(yy, -8, GETDATE()))) AS varchar)• UNION• SELECT j.Member_No, (m.FirstName + ' ' + ISNULL(m.MiddleInitial + ' ', '') + m.LastName) AS FullName, 9 AS Age• FROM Juvenile j• INNER JOIN Member m ON j.Member_No = m.Member_No• WHERE j.Birth_Date BETWEEN (CAST(MONTH(GETDATE()) AS varchar) + '/' + CAST(DAY(GETDATE() + 1)AS varchar) + '/' + CAST((YEAR(DATEADD(yy, -10, GETDATE()))) AS varchar)) • AND CAST(MONTH(GETDATE()) AS varchar) + '/' + CAST(DAY(GETDATE())AS varchar) + '/' + CAST((YEAR(DATEADD(yy, -9, GETDATE()))) AS varchar)• SELECT *• FROM @KidsTable• ORDER BY Age

Page 22: Chris Mc Glothen Sql Portfolio

Online Movie Project

• Introduction: • Design and implement DBMS for a movie

rental chain• Database Design and Implementation• Stored Procedures• SSRS Reports• Backup and Recovery Plan• Database Security Strategy

Page 23: Chris Mc Glothen Sql Portfolio

ER Diagram

Page 24: Chris Mc Glothen Sql Portfolio

ER Diagram (cont.)

Page 25: Chris Mc Glothen Sql Portfolio

ER Diagram (cont.)

Page 26: Chris Mc Glothen Sql Portfolio

XML Schema Collection

Page 27: Chris Mc Glothen Sql Portfolio

XML File

Page 28: Chris Mc Glothen Sql Portfolio

Shredding the data

Page 29: Chris Mc Glothen Sql Portfolio

Backup and Recovery

• Implementation of Log Shipping between headquarters server and secondary server located off site

• Off site location for archive server to hold legacy data and static information and also acts as the monitor for the Log Shipping solution

• Implementation of RAID 5 on production servers• High aggregation transfer rates• Backup strategies implemented both locally and at

corporate levels

Page 30: Chris Mc Glothen Sql Portfolio

Backup and Recovery (cont.)

Page 31: Chris Mc Glothen Sql Portfolio

Security Considerations

• Implement Principle of Least Privilege• Role based security• User management via groups and integrated

windows security• Stored Procedures for all interactions with data• Exclusion of all dynamic SQL statements• Use of Certificates and Symmetric Encryption• Additional security considerations will be included in

the front end applications that interface with the data