Your SlideShare is downloading. ×
0
Sql Portfolio(March 31)
Sql Portfolio(March 31)
Sql Portfolio(March 31)
Sql Portfolio(March 31)
Sql Portfolio(March 31)
Sql Portfolio(March 31)
Sql Portfolio(March 31)
Sql Portfolio(March 31)
Sql Portfolio(March 31)
Sql Portfolio(March 31)
Sql Portfolio(March 31)
Sql Portfolio(March 31)
Sql Portfolio(March 31)
Sql Portfolio(March 31)
Sql Portfolio(March 31)
Sql Portfolio(March 31)
Sql Portfolio(March 31)
Sql Portfolio(March 31)
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Sql Portfolio(March 31)

490

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
490
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. /2005 Eric Hall [email_address] (314) 623 - 8587 Portfolio
  • 2. Table of Contents What is SetFocus 3 RDBMS Concepts 4 Writing Queries using MS SQL Server 2008 T SQL 7 Implementing a Microsoft SQL Server Database 13 SSIS – SQL Server Integration Services
  • 3. What is SetFocus? <ul><li>The SetFocus SQL Master’s Program is an intensive, hands–on, project oriented program allowing knowledge and valuable experience putting the SQL skill set to use in a simulated work environment. </li></ul><ul><li>Over 300 hours of in-depth hands on experience focused on SQL. </li></ul><ul><li>SetFocus projects are real world projects that are distributed just as I would receive in a position. I received project specifications and was expected to identify best courses of action with deadlines set for completion. </li></ul>
  • 4. RDBMS Concepts
  • 5. Normalization - What and Why <ul><li>We normalize data into a computerized and logical model by converting the data from its real world or physical model. </li></ul><ul><li>Displaying data to a user in a familiar form alleviates confusion when describing how the system will perform </li></ul><ul><li>In order to get the best performance from a database data needs to be organized. This helps it to function at its optimal efficiency. </li></ul><ul><li>Keys/constraints are needed to keep the Data Integrity and assist in the separation of duties amongst database programmers and administrators. </li></ul>
  • 6. Database Objects - Why <ul><li>Data retrieval needs to be as efficient as possible. Databases needs more tools to meet demands placed on them. </li></ul><ul><li>Views – is a highly utilized tool in a SQL database and can combine data from two or more tables. It also can utilize tables, functions, and even other views to produce results. </li></ul><ul><li>Functions – performs specific tasks by taking one or more arguments and return a result as a SQL object. </li></ul><ul><li>Stored Procedures – are complete T SQL programs that may consist of SQL statements and functions. </li></ul><ul><li>Triggers – is a special form of a stored procedure and is automatically run when an event such as INSERT, UPDATE, DELETE occurs in a table. Similar to Schemabinding. </li></ul>
  • 7. Writing Queries using MS SQL Server 2008 T-SQL Single Table Queries, Views and Stored Procedures, Joins, Unions, and Advanced Queries
  • 8. Single Table Queries <ul><li>The Cheap Books form displays available books below a certain price. The user enters 15 in the txtUnitPrice form field. Return ISBN, title and publisher in order by title. </li></ul><ul><li>Use JungleBooks </li></ul><ul><li>DECLARE @txtUnitPrice int </li></ul><ul><li>SET @txtUnitPrice = 15 </li></ul><ul><li>SELECT B.ISBN as ISBN, B.Title as Title, B.Publisher as Publisher </li></ul><ul><li>FROM dbo . Books as B </li></ul><ul><li>WHERE B . UnitPrice < @txtUnitPrice </li></ul><ul><li>ORDER BY b . Title </li></ul><ul><li>The Range of Custo mers form allows the user to view orders placed within a given range of customer IDs. The user enters 6 in the txtIDStart form field and 15 in txtIDEnd form field. Display all columns of data in order by Customer ID. Alias the columns in the result set as: ‘Order #’, ‘Cust ID’, ‘Order Date’. </li></ul><ul><li>  </li></ul><ul><li>Use JungleBooks </li></ul><ul><li>Declare @txtlDStart as int , @txtlDEnd as int </li></ul><ul><li>Set @txtlDStart = 6 </li></ul><ul><li>Set @txtlDEnd = 15 </li></ul><ul><li>Select OrderDate as [Order Date], CustomerID as [Cust ID], OrderID as [Order #] </li></ul><ul><li>From dbo.Orders </li></ul><ul><li>Where OrderID BETWEEN @txtlDStart AND @txtlDEnd </li></ul><ul><li>Order by [Cust ID] </li></ul>
  • 9. Advanced Single Table Query with Results <ul><li>The Expired Cards page is a report on credit cards that have expired and credit cards that will expire soon. Output the customer ID, customer name and expiry date of the card for records with an expiry date prior to 30 days after the current date (today). Display the records by expiry date in descending order. Alias as: ‘ID’, ‘Name’, ‘Expires’. </li></ul>Use JungleBooks Declare @Expiration datetime Set @Expiration = DATEADD ( day , 30 , Getdate ()) Select CustomerID as [ID] , Name as [Name] , Day ( ExpiryDate ) as [Expires] From dbo . Customers Where Day ( ExpiryDate ) < @Expiration Order by Expires DESC
  • 10. Advanced Queries Using Joins <ul><li>Write and execute a query on the Title, Item and Copy tables that returns the ISBN, copy_no, on_loan, title, translation, cover for rows in the copy table with an ISBN of 500 or 1000. Only available books should be displayed and hardback copies should be listed first. </li></ul><ul><li>Use library </li></ul><ul><li>Declare @onloan char ( 10 ) = 'N' </li></ul><ul><li>Select Distinct I . isbn , C . copy_no , C . on_loan , T . title , I . translation , I . cover </li></ul><ul><li>From dbo . title as T </li></ul><ul><li>Join dbo . item as I </li></ul><ul><li>on T . title_no = I . title_no </li></ul><ul><li>Join dbo . copy as C </li></ul><ul><li>on C . title_no = I . title_no </li></ul><ul><li>Where I . isbn IN( 500 , 1000 ) and C . on_loan = @onloan </li></ul><ul><li>Order by I . cover , C . on_loan </li></ul><ul><li>Retrieve the member’s full name and member_no from the Member table and the ISBN and log_date values fromthe Reservation table for member numbers 250, 341, and 1675. Order the results by member_no and log_date. You should show information for these members, even if they have no books on reserve. </li></ul><ul><li>Use library </li></ul><ul><li>Select M . firstname + N' ' + COALESCE ( M . middleinitial , + N'' ) + N' ' + M . lastname as [Full Name] , M . member_no , R . isbn , R . log_date </li></ul><ul><li>From dbo . reservation as R </li></ul><ul><li>Right Join dbo . member as M </li></ul><ul><li>on R . member_no = M . member_no </li></ul><ul><li>Where M . member_no IN( 250 , 341 , 1675 ) </li></ul><ul><li>Order by M . member_no , R . log_date </li></ul>
  • 11. Join/Union Query with Results Use library Declare @reserved# int = 288 , @txtAdult As Char ( 10 ) = 'Adult‘ , @txtJuvenile As Char ( 10 ) = 'Juvenile' Select R . isbn as ISBN , T . title as Title , R . member_no as [Member No.] , @txtAdult as 'Member Type' , M . lastname + N', ' + ( M . middleinitial , + N'' ) + N' ' + M . firstname as FullName From dbo . item as I Join dbo . reservation as R On I . isbn = R . isbn Join dbo . member as M On R . member_no = M . member_no Join dbo . adult as A On M . member_no = A . member_no Join dbo . title as T On I . title_no = T . title_no Where R . isbn = @reserved# Union All Select R . isbn , T . title , M . member_no , @txtJuvenile , M . lastname + N', ' + M . middleinitial , + N'' ) + N' ' + M . firstname as FullName From dbo . item as I Join dbo . reservation as R On I . isbn = R . isbn Join dbo . member as M On R . member_no = M . member_no Join juvenile as J On M . member_no = J . member_no Join dbo . title as T On I . title_no = T . title_no Where R . isbn = @reserved# Order by FullName Using joins and a UNION clause, write a query to retrieve a single list of members both adult and juvenile, who have reserved ISBN number 288. The list must include the ISBN, title, member_no and name (i.e.: Smith, John) of each member who has the reservation. Additionally, the list should indicate whether the member is an adult or a juvenile. Output the records by name.
  • 12. View and Stored Procedure Create a view in the TSQLFundamentals2008 database that returns the orderid, day of the week (Spelled Out), the name of the month (spelled Out), the day of the month, and the year based on the order date in the sales.orders table USE TSQLFundamentals2008 Create VIEW [dbo] . [MyDateView] AS SELECT  orderid , DATENAME ( dw , orderdate ) AS [Day Name] , DATENAME ( MM , orderdate ) AS [Month Name] , DAY ( orderdate ) AS [Day Of Month], YEAR ( orderdate ) AS [Order Year] FROM Sales . Orders; Create a stored procedure in the TSQLFundamentals2008 database that returns the order ID, the order date, the ship country. The employee full name, and the company name.  The ship country should be a parameter and the result set should be sorted by order date from most recent to oldest. USE [TSQLFundamentals2008] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE [dbo] . [usp_OrdersByCountry]       @ShipCountry as VarChar ( 25 )= 'USA' AS BEGIN SET NOCOUNT ON ; Select O . orderid, O . orderdate, O . shipcountry, E . firstname + N' ' + E . lastname As Employee , C . companyname From Sales . Orders As O Inner Join Sales . Customers As C       On C . custid = O . custid Inner Join HR . Employees As E       On E . empid = O . empid Where O . shipcountry = @ShipCountry Order By O . orderdate Desc END Edited to fit format of page
  • 13. Implementing a Microsoft SQL Server Database Piggy Bank Piggy Bank is a database produced by SetFocus students ranging from the CREATE DATABASE script to all stored procedures, Functions, Views, and Triggers required so that banking transactions may be done in a clean efficient manner. Deposits, withdrawals, bank statements, etc. are just a few of the requirements considered in this project.
  • 14. Add Customer Stored Procedure <ul><li>CREATE proc [dbo].[sp_addnewcustomer] </li></ul><ul><li>@CustomerFirstName nvarchar(20) = null </li></ul><ul><li>, @CustomerLastName nvarchar(30) = null </li></ul><ul><li>, @CustomerMiddleInitial nvarchar(1) = null </li></ul><ul><li>, @Street nvarchar(50) = null </li></ul><ul><li>, @City nvarchar(20) = null </li></ul><ul><li>, @State char(2) = null </li></ul><ul><li>, @ZipCode char(10) = null </li></ul><ul><li>, @Email nvarchar(30) = null </li></ul><ul><li>, @HomePhone char(12) = null </li></ul><ul><li>, @WorkPhone char(12) = null </li></ul><ul><li>, @CellPhone char(12) = null </li></ul><ul><li>, @CustomerID int OUTPUT </li></ul><ul><li>as </li></ul><ul><li>BEGIN TRY </li></ul><ul><li>If @CustomerLastName is null </li></ul><ul><li>Begin </li></ul><ul><li>RAISERROR('first name cant be null',11,1) </li></ul><ul><li>End </li></ul><ul><li>BEGIN TRAN </li></ul><ul><li>INSERT INTO [PiggyBank].[dbo].[Customer] </li></ul><ul><li>([CustomerFirstName] ,[CustomerLastName] ,[CustomerMiddleInitial] ,[Street] ,[City],[State] ,[ZipCode] ,[Email] ,[HomePhone]) </li></ul><ul><li>VALUES </li></ul><ul><li>( @CustomerFirstName, @CustomerLastName, @CustomerMiddleInitial, @Street, @City, @State, @ZipCode, @Email, @HomePhone) </li></ul><ul><li>SET @CustomerID = SCOPE_IDENTITY() </li></ul><ul><li>Return @CustomerID </li></ul>NOTE: Missing Code (Rollback/Catch Transaction) - Edited to fit format of page
  • 15. Deposit into Bank Account <ul><li>CREATE PROCEDURE [dbo].[sp_Deposit] </li></ul><ul><li> @AccountID int = null </li></ul><ul><li>,@CustomerID int = null </li></ul><ul><li>,@TransactionAmount money = null </li></ul><ul><li>,@TransactionID int OUTPUT </li></ul><ul><li>AS </li></ul><ul><li>BEGIN TRY </li></ul><ul><li>DECLARE @NewBalance money, @CurrentBalance money </li></ul><ul><li>SET @NewBalance = (SELECT CurrentBalance </li></ul><ul><li>FROM dbo.Account </li></ul><ul><li>WHERE AccountID = @AccountID) + @TransactionAmount </li></ul><ul><li>BEGIN TRAN </li></ul><ul><li>IF NOT EXISTS (SELECT AccountID </li></ul><ul><li> FROM dbo.Account </li></ul><ul><li> WHERE AccountID = @AccountID) </li></ul><ul><li>BEGIN </li></ul><ul><li>RAISERROR ('Account does not exist',3,1) </li></ul><ul><li>END </li></ul><ul><li>IF NOT EXISTS (SELECT CustomerID </li></ul><ul><li> FROM dbo.Customer </li></ul><ul><li> WHERE CustomerID = @CustomerID) </li></ul><ul><li>BEGIN </li></ul><ul><li>RAISERROR('Customer does not exist',3,1) </li></ul><ul><li>END </li></ul><ul><li>BEGIN </li></ul><ul><li>INSERT INTO [PiggyBank].[dbo].[Transactions] </li></ul><ul><li>([AccountID],[TransactionTypeID],[CustomerID],[TransactionDate],[TransactionAmount],[NewBalance]) </li></ul><ul><li>VALUES </li></ul><ul><li>(@AccountID,2,@CustomerID,GETDATE(),@TransactionAmount,@NewBalance) </li></ul><ul><li>UPDATE dbo.Account </li></ul><ul><li>SET CurrentBalance = @NewBalance </li></ul><ul><li>WHERE AccountID = @AccountID </li></ul><ul><li>SELECT @TransactionID = SCOPE_IDENTITY() </li></ul>Deposit of $2000 to AccountID 100001 (row number 2)
  • 16. Triggers in the Database <ul><li>CREATE TRIGGER trgDropTables </li></ul><ul><li>ON DATABASE FOR DROP_TABLE </li></ul><ul><li>AS </li></ul><ul><li>BEGIN TRAN </li></ul><ul><li>RAISERROR('TABLES ARE SET SO THAT THEY MAY NOT BE DROPPED FROM SYSTEM', 5,1) </li></ul><ul><li>ROLLBACK </li></ul><ul><li>GO </li></ul><ul><li>CREATE TRIGGER trgAlterTables </li></ul><ul><li>ON DATABASE FOR ALTER_TABLE </li></ul><ul><li>AS </li></ul><ul><li>BEGIN TRAN </li></ul><ul><li>RAISERROR('TABLES ARE SET SO THAT THEY MAY NOT BE ALTERED', 5,1) </li></ul><ul><li>ROLLBACK </li></ul><ul><li>GO </li></ul><ul><li>CREATE TRIGGER trgDropViews </li></ul><ul><li>ON DATABASE FOR DROP_VIEW </li></ul><ul><li>AS </li></ul><ul><li>BEGIN TRAN </li></ul><ul><li>RAISERROR('VIEWS ARE SET SO THAT THEY MAY NOT BE DROPPED FROM SYSTEM', 5,1) </li></ul><ul><li>ROLLBACK </li></ul><ul><li>GO </li></ul><ul><li>CREATE TRIGGER dbo.trgNODELETTRANSACTION </li></ul><ul><li>ON dbo.Transactions </li></ul><ul><li>AFTER DELETE </li></ul><ul><li>AS </li></ul><ul><li>BEGIN TRAN </li></ul><ul><li>RAISERROR('TRANSACTIONS CANNOT BE DELETED', 5,1) </li></ul><ul><li>ROLLBACK </li></ul><ul><li>GO </li></ul>
  • 17. SSIS – SQL Server Integration Services
  • 18. SSRS – SQL Server Reporting Services

×