SQL Management Studio SQL Configuration Manager Consoles SQLCMD PowerShell SQL Profiler SQL Database Tuning Advisor Last Week
How to setup maintenance plans over night Database encryption Data Source code (Stored procs) Best practices Typical maintenance plans Policies Last Week - Additional
Modify maintenance plan 2nd page in wizard (new plan)
Encrypting data - Transparent Data Encryption (TDE) http://msdn.microsoft.com/en-us/library/bb934049.aspx http://www.acorns.com.au/blog/?p=147 Encrypting Connections to SQL Server http://msdn.microsoft.com/en-us/library/ms189067.aspx Encrypting source code http://www.codeproject.com/KB/database/ProtectSQLCodeObject.aspx Database encryption
http://ola.hallengren.com/ Steps Backup Integrity check Index optimization Solution used in mission-critical environments in many organizations. Best practices - Maintenance
Security Best Practices http://download.microsoft.com/download/8/5/e/85eea4fa-b3bb-4426-97d0-7f7151b2011c/SQL2005SecBestPract.doc Security Best Practices Checklist http://technet.microsoft.com/en-us/library/cc966456.aspx
Best practices - Security
Create a schema called Salary Create a table called Employees in Schema Create a user called Manager Give only manager permission to update/insert/delete in schema Create a user called Peter Give Peter only read to schema (=salary) Create a user Alice Deny everything for Alice in Salary Homework?
Agenda New Data Types Inline variable assignment Table Value Parameters DDL Triggers CTE (Common Table Expressions) TOP %, XML Queries PIVOT/UNPIVOT ADO.NET
Datatypes - Exact Numerics bigint, int, smallint, tinyint -2^63 ... 2^63-1 0..255 Bit (0 or 1) decimal = numeric Exact type Numbers -10^38 +1 ... 10^38 – Money, smallmoney accuracy to a ten-thousandth money unit Smallmoney = smaller money
Datatypes – Approximate Numerics Floating point numeric data float real
Datatypes - text char Fixed length varchar Variable length
Datatypes - text char, varchar, text Ascii - 1 byte nchar, nvarchar, ntext Unicode - 2 bytes binary, varbinary, image
Date and Time SQL 2008 now has the following data types to represent time: DateTime SmallDateTime Date Time DateTime2 – really a BigDateTime Min Date is 1st Jan 0000 Max date 31st Dec 9999 – Y10K BUG!! DateTimeOffset
Date Time details
Datatypes - Other cursor table Timestamp = rowversion binary number Reflects data modifications uniqueidentifier = Guid Format: 04c4ce04-16c1-406f-a895-5dd321db7f0b
New Data Types Filestream Sparse Columns Filtered Indexes Spatial Data HierarchyID DATE and TIME data types
Example: Storing images I am designing an Employee table that needs to cater for employee photographs. What should I do?
blob vs. file system I am designing an Employee table that needs to cater for employee photographs. What should I do? You can store the image in the database (blob) You can store a URL to the image in the database (Recommended for SQL2005) http://www.ssw.com.au/ssw/standards/Rules/RulestoBetterSQLServerdatabases.aspx#ImageReplaceWithURL
You can store the image in the database (blob)
Database grows really big Backups take longer Your code needs to convert the bytes back into an image Your images are in sync with your data
You can store a URL to the image in the database (Recommended for SQL2005)
Database is smaller Easily validate or change the image (you can look at it on the file system) Data could become out of sync with the file system Need to backup the database and the file system
Filestream Filestream to the rescue Implemented as a special varbinary(max) where data is stored as a blob on the file system Allows you to have transactionally consistent Integrated backup and restore of your binary images Size limitation is the size of your hard drive’s free space
Problem Q: I’ve got an Contacts table with 200,000 rows. To support the latest Web 2.0 trends we want to also record the contact’s blog address. What should I do?
Solution? A: Just add a new BlogUrl column in
Solution? A: Just add a new BlogUrl column in Q: What’s the problem with that? A: Most of the entries in your table will be null, it wastes a lot of database space
Solution Use a sparse column These columns are new to SQL 2008 They are optimized for storing NULL values
Filtered Indexes Allows you to add an index to a column with a where clause Useful for indexing columns with null values in them
Spatial Data Types Geometry Geography Virtual Earth Integration Planar vs Geodetic Algorithms Separate install for spatial assemblies http://www.conceptdevelopment.net/Database/Geoquery/
Spatial Datatypes Hierarchy
Spatial Data Types Geometry allows you to represent and process polygons
How do I represent an Org Chart? Employee with a ManagerID column (self join) New HierarchyID data type Can be indexed using: Depth First Breadth First
Depth First Search A depth-first index, rows in a subtree are stored near each other. For example, all employees that report through a manager are stored near their managers' record.
Breadth First Search A breadth-first stores the rows each level of the hierarchy together. For example, the records of employees who directly report to the same manager are stored near each other.
Inline Variable Assignment Instead of: DECLARE @myVar intSET @myVar = 5 You can: DECLARE @myVar int = 5
Table Value Parameters Pass in a table as an argument to a SPROC Instead of: exec sp_MySproc 'murphy,35;galen,31;samuels,27;colton,42‘ SPROC needs to then parse that string
Table Value Parameters You can do this instead CREATE TYPE PeepsType AS TABLE (Name varchar(20), Age int) DECLARE @myPeeps PeepsType INSERT @myPeeps SELECT 'murphy', 35 INSERT @myPeeps SELECT 'galen', 31 INSERT @myPeeps SELECT 'samuels', 27 INSERT @myPeeps SELECT 'colton', 42exec sp_MySproc2 @myPeeps
Table Value Parameters The SPROC would look like this: CREATE PROCEDURE sp_MySproc2(@myPeeps PeepsType READONLY)
More features ROW NUMBER – see example TRY/Catch in queries – see example Top % WITH TIES select top with tie feature( if top 10 and there are 15 that match number 10 will bring back all 15)
Working with XML - RAW SELECT TOP 3 Person.FirstName, Person.LastName, PersonPhone.PhoneNumber FROM AdventureWorks.Person.Person INNER JOIN AdventureWorks.Person.PersonPhone ON PersonPhone.BusinessEntityID = Person.BusinessEntityID FOR XML RAW
Working with XML - RAW <row FirstName="Ken" LastName="Sánchez" PhoneNumber="697-555-0142"/> <row FirstName="Terri" LastName="Duffy" PhoneNumber="819-555-0175"/> <row FirstName="Roberto" LastName="Tamburello" PhoneNumber="212-555-0187"/>
Working with XML - RAW What happened to our relationships? RAW doesn’t show our table relationships but gives us a flat XML hierarchy
Working with XML - Auto SELECT TOP 3 Person.FirstName, Person.LastName, PersonPhone.PhoneNumber FROM AdventureWorks.Person.Person INNER JOIN AdventureWorks.Person.PersonPhone ON PersonPhone.BusinessEntityID = Person.BusinessEntityID FOR XML AUTO
Working with XML - Auto <AdventureWorks.Person.PersonFirstName="Ken" LastName="Sánchez"> <AdventureWorks.Person.PersonPhonePhoneNumber="697-555-0142"/> </AdventureWorks.Person.Person>
Working with XML - Auto Great, but what if I needed to format the XML to output into a certain schema
Working with XML - Explicit SELECT TOP 3 1 AS TAG, NULL AS PARENT, BusinessEntityID AS [Person!1!BusinessEntityID], FirstName AS [Person!1!FirstName!ELEMENT] FROM AdventureWorks.Person.Person FOR XML EXPLICIT
Working with XML - Explicit <Person BusinessEntityID="285“> <FirstName>Syed</FirstName> </Person> <Person BusinessEntityID="293"> <FirstName>Catherine</FirstName> </Person> <Person BusinessEntityID="295"> <FirstName>Kim</FirstName> </Person>
Working with XML - Explicit Can control how the XML gets output Ugly query Is there a better way?
Working with XML - PATH SELECT TOP 3 BusinessEntityID "Person/@BusinessEntityID", FirstName "Person/FirstName" FROM AdventureWorks.Person.Person FOR XML PATH ('')
Working with XML - XQuery XQuery is a query language for XML Data
XQuery – Declaring our XML string DECLARE @x XML SET @x = '<christmaslist><person name = "betty" gift = "camera"/><person name = "zach" gift = "elmo doll"/><person name = "brad" gift = "socks"/></christmaslist>'
ADO.NET ADO.NET gives you full control over how you access and retrieve data from the data source Strongly typed data sets Work in disconnected mode
ADO.NET SQLConnection Manages the connection to the database SQLCommand Defines the data to be read, updated etc. SQLDataAdapter Runs the SQLCommand against the database DataSet A complete in-memory copy of the data (tables, relationships, data types…) Search, filter, navigate your data – without even being connected to the database!
TSQL Tricks Randomize Select output Repeat statements with GO x
Resources 1/2 Spatial Data playground http://www.conceptdevelopment.net/Database/Geoquery/ Hidden Features in SQL Server http://stackoverflow.com/questions/121243/hidden-features-of-sql-server Top 10 Hidden Gems in SQL Server http://technet.microsoft.com/en-au/library/cc917696.aspx
What to do when you take over a new SQL Server box? http://www.brentozar.com/sql/blitz-minute-sql-server-takeovers/ Resources 2/2
Session 2 Lab T-SQL Enhancements Download from Course Materials Site (to copy/paste scripts) or type manually http://sharepoint.ssw.com.au/training/UTSSQL/
Where Else Can I Get Help? Where else can I get help? Free chats and webcasts List of newsgroups Microsoft community sites Community events and columns www.microsoft.com/technet/community