1. SQL Portfolio Marcus Matthews marcusmatthews@setfocus.com http://www.linkedin.com/in/marcuswmatthews
2. Table of Contents Introduction 2 Jungle Books 3 Library 7 Piggy Bank 11 Block Flix 19
3. Introduction This portfolio contains examples of my development skills in MS SQL Server. It is a result of my work during an 8-week hands-on experience with The SetFocus Master's Program. SetFocus utilizes Microsoft Official Curriculum in conjunction with its own materials to produce some of the following coursework: RDBMS and XML Querying using Transact SQL Implementing and maintaining a MS SQL Server 2008 Database Designing an MS SQL Server 2008 Infrastructure Designing security for MS SQL Server 2008 Designing High Availability Database Solutions using MS SQL Server 2008 Troubleshooting and Optimizing Database Solutions using MS SQL Server 2008 SQL Server Integration Services SQL Server reporting Services In addition to the coursework and programming labs, the program included challenging real-world projects where I have applied the skills experienced in class.
4. Jungle Books Jungle Books is company which uses a database to store data for books, authors, customers that purchase these items and order records. This database diagram was used to create the database and show the relationship between entities within it. The application developer that we are working with fictitiously will use this design for a client application using .NET.
5. Jungle Books SELECT [o].[OrderID] AS 'Order ID' ,[o].[CustomerID] AS 'Cust ID' ,c.Name ,(SELECT SUM([oi].[QuantityOrdered])) AS '# of items' FROM [Orders] o JOIN [OrderItems] oi ON oi.OrderID = o.OrderID JOIN Customers AS c ON c.CustomerID = o.CustomerID GROUP BY o.OrderID, o.CustomerID, c.Name Order BY (SELECT SUM([oi].[QuantityOrdered])) desc; The select statement for the Large Orders report is used to return orders from the Jungle Books' database that contain the highest number of items sold per order, OrderID, CustomerID, Customer Name and the items within their order. The result set is displayed in descending order listing the order with the largest quantity first.
6. Jungle Books The report named Expired Cards is a select statement used to generate a list of customers whose credit cards have expired and those that will expire within 30 days. SELECT CustomerID AS 'ID', [Name], expiryDate AS 'Expires' FROM customers WHERE expirydate < (getDate() +30)
7. Library The Library Database is designed to maintain records of the day-to-day lending operations such: items available or on loan, overdue books, fees, member information, and reservations. This database has been used for number of projects: Create queries against the Library Database to return results using UNION and several join types. Search for book titles, by name, ISBN, and author. Create new member accounts for juveniles and adults. Set up a reservation for an item that is currently unavailable to loan once it has been returned.
9. Library SELECT lh.member_no, m.lastname, m.firstname, SUM(lh.fine_paid) AS "Fines Paid" from loanhistlh INNER JOIN member m ON lh.member_no = m.member_no GROUP BY lh.member_no, m.lastname, m.firstname HAVING SUM(lh.fine_paid) IS NOT NULL ORDER BY SUM(lh.fine_paid) DESC, lh.member_no
10. Library SELECT r.isbn, t.title, m.member_no, m.lastname, 'adult' AS [member_type] FROM member m INNER JOIN reservation r ON r.member_no = m.member_no INNER JOIN item i ON i.isbn = r.isbn INNER JOIN title t ON t.title_no = i.title_no INNER JOIN adult a ON a.member_no = m.member_no WHERE r.isbn = 288 UNION SELECT r.isbn, t.title, m.member_no, m.lastname, 'juvenile' AS [member_type] FROM member m INNER JOIN reservation r ON r.member_no = m.member_no INNER JOIN item i ON i.isbn = r.isbn INNER JOIN title t ON t.title_no = i.title_no INNER JOIN juvenile j ON j.member_no = m.member_no WHERE r.isbn = 288 GROUP BY r.isbn, t.title, m.member_no, m.lastname ORDER BY [member_type]
11. Piggy Bank The Piggy Bank Database is used to simulate the daily operations of a financial institution, providing its user access to their personal information and manage accounts. This database has been used for a number of projects: Using stored procedures this database is capable of accounting for typical banking transactions, such as deposits, funds withdrawal, account transfers or if you are a new customer, create a new account. Update account information capabilities to change an address, add another account or member to an existing account transaction history.
13. Piggy BankCreate new customer Stored procedure SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO /* written by: Marcus Matthews Date: 05/10/2010 Errors 'Last name cannot be null', 11, 1 'First name cannot be null', 11, 2 'Street cannot be null', 11, 3 'City cannot be null', 11, 4 'State cannot be null', 11, 5 'Zip code cannot be null', 11, 6 'Home Phone number cannot be null', 11, 7 */ CREATE PROCEDURE [dbo].[usp_CreateNewCustomer] @CustomerLastName nvarchar(20)=null, @CustomerFirstName nvarchar(20)=null, @CustomerMiddleName nvarchar(20)=null, @Street nvarchar(50) =null, @City nvarchar(20) =null, @State nchar(2) =null, @ZipCode nvarchar(10) =null, @Email nvarchar(50) =null, @HomePhone nvarchar(15) =null, @WorkPhone nvarchar(15) =null, @CellPhone nvarchar(15) =null AS BEGIN TRY
14. Piggy BankCreate new customer Stored procedure (cont.) -- Test parameters. All required fields must have a value, only test those that do not allow nulls If @CustomerLastName is null BEGIN RAISERROR ('Last name cannot be null', 11, 1) END If @CustomerFirstName is null BEGIN RAISERROR ('First name cannot be null', 11, 2) END If @Street is null BEGIN RAISERROR ('Street cannot be null', 11, 3) END If @City is null BEGIN RAISERROR ('City cannot be null', 11, 4) END If @State is null BEGIN RAISERROR ('State cannot be null', 11, 5) END If @ZipCode is null BEGIN RAISERROR ('Zip code cannot be null', 11, 6) END
15. Piggy Bank Create new customer Stored procedure (cont.) If @HomePhone is null BEGIN RAISERROR ('Home Phone number cannot be null', 11, 7) END --Create customer and insert data BEGIN INSERT INTO dbo.Customer ( CustomerFirstName, CustomerLastName, CustomerMiddleName, Street, City, [State], ZipCode, Email, HomePhone, WorkPhone, CellPhone ) VALUES ( @CustomerFirstName, @CustomerLastName, @CustomerMiddleName, @Street, @City, @State, @ZipCode, @Email, @HomePhone, @WorkPhone, @CellPhone )
16. Piggy Bank Create new customer Stored procedure (cont.) END RETURN END TRY BEGIN CATCH -- IF @@TRANCOUNT > 0 -- ROLLBACK TRAN DECLARE @ErrorMessage NVARCHAR(4000); DECLARE @ErrorSeverity INT; DECLARE @ErrorState INT; SELECT @ErrorMessage = ERROR_MESSAGE(), @ErrorSeverity = ERROR_SEVERITY(), @ErrorState = ERROR_STATE(); RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState); END CATCH GO
17. Piggy BankAccount Balance Stored procedure GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO /*This stored procedure is used to return the current account balance and last 5 transactions. Error messages for omitted data in required fields. written by: Marcus Matthews Date: 05/15/2010 Errors 'Customer ID cannot be null', 11, 1 'Account ID cannot be null', 11, 2 'Account numbers provided do not match', 11, 3 */ CREATE PROCEDURE [dbo].[GetBalance] @CustomerID int =null, @AccountIDint =null AS BEGIN TRY If @CustomerID is null BEGIN RAISERROR ('Customer ID cannot be null', 11, 1) END If @AccountID is null BEGIN RAISERROR ('Account ID cannot be null', 11, 2) END
18. Piggy BankAccount Balance Stored procedure (cont.) -- Make sure accounts match IF EXISTS(SELECT a.AccountID FROM Account a JOIN CustomerAccount ca ON ca.AccountID = a.AccountID JOIN Customer c ON c.CustomerID = ca.CustomerID WHERE a.AccountID = @AccountID ) -- Make sure accounts match IF EXISTS(SELECT a.AccountID FROM Account a JOIN CustomerAccount ca ON ca.AccountID = a.AccountID JOIN Customer c ON c.CustomerID = ca.CustomerID WHERE a.AccountID = @AccountID ) -- shows current balance with last 5 transactions. BEGIN SELECT DISTINCT TOP(5) c.CustomerID, a.CurrentBalance, t.transactionID, t.transactionDate, t.TransactionAmount FROM Account a JOIN [Transaction] t ON t.AccountID = a.AccountID JOIN Customer c ON c.customerID= t.CustomerID WHERE a.accountID = @AccountID AND c.CustomerID = @CustomerID END
19. Piggy BankAccount Balance Stored procedure (cont.) ELSE BEGIN RAISERROR ('Account numbers provided do not match', 11, 3) END RETURN END TRY BEGIN CATCH DECLARE @ErrorMessage NVARCHAR(4000); DECLARE @ErrorSeverity INT; DECLARE @ErrorState INT; SELECT @ErrorMessage = ERROR_MESSAGE(), @ErrorSeverity = ERROR_SEVERITY(), @ErrorState = ERROR_STATE(); RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState); END CATCH GO
22. XML Data Original XML Modified XML There were a couple issues we had to overcome to process the XML. One of the challenges of processing the XML was formatting it properly and creating a schema. Another problem we had to overcome was entering the quantity of the movie into our Inventory Table. This was done by using a OLE DB Command Transformation and calling a proc (LoadMovieInventory) to insert records in a While Loop until it looped the appropriate amount of times.
25. Checking Out an Online Movie Member David Smith checks out movie Dark Knight (MemberID 2, MovieID 5, InventoryID 50) Rental Transaction Tables:
26. Checking Out a Movie (Cont.) Current Member Queue (GetMemberQueView) When Member returns Dark Night they will receive Saving Private Ryan.
27. Returning a Movie David Smith (2) returns (2) the movie Dark Knight (50) Rental Transactions Showing return of Dark Knight (50) and Shipment (1) of Saving Private Ryan (20)
28. Returning a Movie (Cont.) Member Transaction Showing return of Dark Knight and Shipment of Saving Private Ryan
29. Returning a Movie (Cont.) Movie Inventory for Dark Knight Showing Available (1)
30. Returning a Movie (Cont.) Member Queue Remains the same… when Saving Private Ryan is returned Gold Finger will be shipped.
31. Returning a Movie Continued When Saving Private Ryan was returned, it was deleted from the queue.
35. Update Movie Queue uspUpdateMemberQue Pre-Update Judith Hayward Only has Goldfinger Post-Update Judith Hayward added FrankensteinWill not allow movie to be in the queue more than once
36. Monthly Member Debit/Credit uspMonthlyMemberDebit uspMonthlyMemberCredit uspGetMemberTransactions (Lookup) TransTypeID 4 is Debit TransTypeID 3 is Credit