sql server specialist certificate programstevestedman.com/wp-content/uploads/2011/11/dba... ·...

31
SQL Server Specialist Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions Performance Tuning Steve Stedman - Instructor

Upload: others

Post on 07-Oct-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SQL Server Specialist Certificate Programstevestedman.com/wp-content/uploads/2011/11/DBA... · Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions

SQL Server Specialist Certificate Program

Maintaining SQL Server 2005

Week 5 – Working with TSQL and Transactions

Performance Tuning

Steve Stedman - Instructor

Page 2: SQL Server Specialist Certificate Programstevestedman.com/wp-content/uploads/2011/11/DBA... · Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions

This Weeks Overview

● Review from Last Week + Homework● TSQL – Transact SQL● Transactions● Performance Tuning● Class Project● Review and Homework

Page 3: SQL Server Specialist Certificate Programstevestedman.com/wp-content/uploads/2011/11/DBA... · Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions

Topics from last week

● Stored Procedures● Functions● Triggers

Page 4: SQL Server Specialist Certificate Programstevestedman.com/wp-content/uploads/2011/11/DBA... · Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions

Homework

● Practice● Assignment● Reading

Page 5: SQL Server Specialist Certificate Programstevestedman.com/wp-content/uploads/2011/11/DBA... · Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions

Preparation

We will be using the AdventureWorks database.

Page 6: SQL Server Specialist Certificate Programstevestedman.com/wp-content/uploads/2011/11/DBA... · Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions

1.TSQL

● Overview● SELECT statements● JOINS (INNER AND OUTER)● Flow Control● Complex Criteria

○ Aggregate Functions, PIVOT / UNPIVOT, Full Text Search

Page 7: SQL Server Specialist Certificate Programstevestedman.com/wp-content/uploads/2011/11/DBA... · Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions

Overview

● What is TSQL○ Transact-SQL○ Microsoft’s Proprietary extension to SQL

● Why use TSQL

Page 8: SQL Server Specialist Certificate Programstevestedman.com/wp-content/uploads/2011/11/DBA... · Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions

SELECT statements

● Used to retrieve data from the database.

SELECT *FROM HumanResources.Employee E

Page 9: SQL Server Specialist Certificate Programstevestedman.com/wp-content/uploads/2011/11/DBA... · Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions

INNER JOINS

● Return only rows that match from both tables

SELECT *FROM HumanResources.Employee EINNER JOIN HumanResources.EmployeeAddress as EA ON E.EmployeeId = EA.EmployeeId

Page 10: SQL Server Specialist Certificate Programstevestedman.com/wp-content/uploads/2011/11/DBA... · Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions

OUTER JOINS

● Returns rows with matching data, as well as rows with nonmatching data

● LEFT OUTER JOIN○ All the rows from the LEFT table

● RIGHT OUTER JOIN○ All the rows from the RIGHT table

● FULL OUTER JOIN○ All the rows from both the LEFT and

RIGHT

Page 11: SQL Server Specialist Certificate Programstevestedman.com/wp-content/uploads/2011/11/DBA... · Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions

Example: LEFT OUTER JOIN

SELECT E.EmployeeID, E.LoginID, EA.* FROM HumanResources.Employee ELEFT OUTER JOIN HumanResources.EmployeeAddress as EA ON E.EmployeeId = EA.EmployeeId

Page 12: SQL Server Specialist Certificate Programstevestedman.com/wp-content/uploads/2011/11/DBA... · Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions

Example: RIGHT OUTER JOIN

SELECT E.EmployeeID, E.LoginID, EA.* FROM HumanResources.Employee ERIGHT OUTER JOIN HumanResources.EmployeeAddress as EA ON E.EmployeeId = EA.EmployeeId

Page 13: SQL Server Specialist Certificate Programstevestedman.com/wp-content/uploads/2011/11/DBA... · Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions

Flow Control

● BEGIN and END● WHILE● BREAK and CONTINUE● GOTO● IF and ELSE● RETURN● WAITFOR

Page 14: SQL Server Specialist Certificate Programstevestedman.com/wp-content/uploads/2011/11/DBA... · Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions

Example: IF and ELSE

IF DATEPART(dw, GETDATE()) = 7 OR DATEPART(dw, GETDATE()) = 1PRINT 'It is the weekend.'ELSEPRINT 'It is a weekday.'

Page 15: SQL Server Specialist Certificate Programstevestedman.com/wp-content/uploads/2011/11/DBA... · Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions

Example: BEGIN and END

IF DATEPART(dw, GETDATE()) = 7 OR DATEPART(dw, GETDATE()) = 1BEGINPRINT 'It is the weekend.'PRINT 'Get some rest!'ENDELSEBEGINPRINT 'It is a weekday.'PRINT 'Get to work!'END

Page 16: SQL Server Specialist Certificate Programstevestedman.com/wp-content/uploads/2011/11/DBA... · Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions

Example: WHILE, BREAK, CONTINUEDECLARE @Counter INTSET @Counter = 10WHILE @Counter > 0BEGINPRINT 'The count is ' + CONVERT(VARCHAR(10), @Counter)SET @Counter = @Counter - 1END

Page 17: SQL Server Specialist Certificate Programstevestedman.com/wp-content/uploads/2011/11/DBA... · Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions

Grouping and Sorting

● ORDER BY - Example SELECT E.LoginID, E.Title, E.ManagerID FROM HumanResources.Employee E ORDER BY E.ManagerID

● GROUP BY - Example SELECT E.ManagerID FROM HumanResources.Employee E GROUP BY E.ManagerID

Page 18: SQL Server Specialist Certificate Programstevestedman.com/wp-content/uploads/2011/11/DBA... · Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions

Aggregate Functions

● AVG● COUNT● MAX● MIN● SUM● STDEV ● VAR

Page 19: SQL Server Specialist Certificate Programstevestedman.com/wp-content/uploads/2011/11/DBA... · Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions

PIVOT / UNPIVOT

● Cross-Tabulation SELECT [0], [1]FROM(SELECT E.SalariedFlag, E.VacationHoursFROM HumanResources.Employee E) AS HPIVOT(AVG(VacationHours)FOR SalariedFlag IN ([0], [1])) AS Pvt

Page 20: SQL Server Specialist Certificate Programstevestedman.com/wp-content/uploads/2011/11/DBA... · Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions

Lab Project – TSQL

● Using the AdventureWorks database, start with the following Query

SELECT * FROM HumanResources.Employee as E1. Modify the output to group by Title, and count the

number of people with each title2. Modify the query to group by the MaritalStatus and

Gender, and count the number of people in each group.

Page 21: SQL Server Specialist Certificate Programstevestedman.com/wp-content/uploads/2011/11/DBA... · Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions

● End of this section. Any Questions?

● 10 Minute Break

Page 22: SQL Server Specialist Certificate Programstevestedman.com/wp-content/uploads/2011/11/DBA... · Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions

1.Transactions

● BEGIN TRANSACTION● COMMIT TRANSACTION● ROLLBACK TRANSACTION● Transaction Sample

Page 23: SQL Server Specialist Certificate Programstevestedman.com/wp-content/uploads/2011/11/DBA... · Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions

BEGIN TRANSACTION

● Starts the transaction● Tracks data changes until COMMIT or

ROLLBACK● Ends only when you COMMIT or

ROLLBACK

Page 24: SQL Server Specialist Certificate Programstevestedman.com/wp-content/uploads/2011/11/DBA... · Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions

Ending a Transaction

● COMMIT TRANSACTION○ Saves Changes

● ROLLBACK TRANSACTION

○ Returns the data to the original state

Page 25: SQL Server Specialist Certificate Programstevestedman.com/wp-content/uploads/2011/11/DBA... · Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions

Transaction Sample

BEGIN TRANSACTION;UPDATE [HumanResources].[Employee] SET [Title] = @Title, [HireDate] = @HireDate WHERE [EmployeeID] = @EmployeeID;

INSERT INTO [HumanResources].[EmployeePayHistory] ([EmployeeID],[RateChangeDate],[Rate])VALUES (@EmployeeID, @RateChangeDate, @Rate);COMMIT TRANSACTION;

See: [HumanResources].[uspUpdateEmployeeHireInfo]

Page 26: SQL Server Specialist Certificate Programstevestedman.com/wp-content/uploads/2011/11/DBA... · Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions

● End of this section. Any Questions?

Page 27: SQL Server Specialist Certificate Programstevestedman.com/wp-content/uploads/2011/11/DBA... · Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions

1.Performance Tuning

● See Other Powerpoint presentation.

Page 28: SQL Server Specialist Certificate Programstevestedman.com/wp-content/uploads/2011/11/DBA... · Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions

● End of this section. Any Questions?

Page 29: SQL Server Specialist Certificate Programstevestedman.com/wp-content/uploads/2011/11/DBA... · Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions

Homework

● Homework for next week○ Work on your class projects

Page 30: SQL Server Specialist Certificate Programstevestedman.com/wp-content/uploads/2011/11/DBA... · Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions

Class Project

● Status Update● Review of research and

experimentation required for the group project.

● Presentations will be week 9

Page 31: SQL Server Specialist Certificate Programstevestedman.com/wp-content/uploads/2011/11/DBA... · Certificate Program Maintaining SQL Server 2005 Week 5 – Working with TSQL and Transactions

Questions?