Professional Documents
Culture Documents
Specializes in
Application architecture and design SQL Performance Tuning and Optimization Agile, Scrum Certified Scrum Trainer Technology aficionado
Silverlight ASP.NET Windows Forms
Course Website
http://sharepoint.ssw.com.au/Training/UTSSQL/Pages/
Last Week
SQLCMD PowerShell
SQL Profiler
Course Overview
Session Date 1 Time 18:00 - 21:00 Topic SQL Server 2008 Management Studio
Tuesday 06-03-2012
Tuesday 13-03-2012 Tuesday 20-03-2012 Tuesday 27-03-2012 Tuesday 03-04-2012
18:00 - 21:00
T-SQL Enhancements
18:00 - 21:00
High Availability
18:00 - 21:00
CLR Integration
18:00 - 21:00
Full-Text Search
3.
Best practices
1. 2.
1. 2.
Database encryption
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
http://ola.hallengren.com/
Steps
Backup Integrity check Index optimization
Solution used in
mission-critical environments in many organizations.
Homework?
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
Agenda
PIVOT/UNPIVOT
ADO.NET
Money, smallmoney
float real
Datatypes - text
char
Fixed length
varchar
Variable length
Datatypes - text
Ascii - 1 byte
Unicode - 2 bytes
SQL 2008 now has the following data types to represent time:
DateTimeOffset
hh:mm:ss[.nnnn 00:00:00.0000000 through nnn] 23:59:59.9999999 0001-01-01 through 9999-12date YYYY-MM-DD 31 YYYY-MM-DD 1900-01-01 through 2079-06smalldatetime hh:mm:ss 06 YYYY-MM-DD 1753-01-01 through 9999-12datetime hh:mm:ss[.nnn] 31
datetime2
YYYY-MM-DD 0001-01-01 00:00:00.0000000 hh:mm:ss[.nnnn through 9999-12-31 100 nanoseconds 6 to 8 nnn] 23:59:59.9999999
YYYY-MM-DD 0001-01-01 00:00:00.0000000 datetimeoffset hh:mm:ss[.nnnn through 9999-12-31 100 nanoseconds 8 to 10 nnn] [+|-]hh:mm 23:59:59.9999999 (in UTC)
Yes
Yes
Datatypes - Other
cursor table
Timestamp = rowversion
uniqueidentifier = Guid
Format: 04c4ce04-16c1-406f-a895-5dd321db7f0b
I am designing an Employee table that needs to cater for employee photographs. What should I do?
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/RulestoBetterSQ LServerdatabases.aspx#ImageReplaceWithURL
blob
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
File system
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 drives free space
Problem
Q:\ Ive got an Contacts table with 200,000 rows. To support the latest Web 2.0 trends we want to also record the contacts blog address. What should I do?
Solution?
Solution?
Solution
These columns are new to SQL 2008 They are optimized for storing NULL values
Sparse Columns
Data type bit tinyint smallint int bigint real float smallmoney Nonsparse bytes 0.125 1 2 4 8 4 8 4 Sparse bytes 4.125 5 6 8 12 8 12 8 NULL percentage 98% 86% 76% 64% 52% 64% 52% 64%
money
smalldatetime datetime uniqueidentifier date
8
4 8 16 3
12
8 12 20 7
52%
64% 52% 43% 69%
Sparse Columns
Data type datetime2(0) datetime2(7) time(0) time(7) datetimetoffset(0) datetimetoffset (7) decimal/numeric(1,s) decimal/numeric(38,s) Nonsparse bytes 6 8 3 5 8 10 5 17 Sparse bytes 10 12 7 9 12 14 9 21 NULL Percentage 57% 52% 69% 60% 52% 49% 60% 42%
vardecimal(p,s)
Sparse Columns
Data type sql_variant varchar or char nvarchar or nchar varbinary or binary xml Nonsparse bytes Varies with the underlying data type 4+avg. data 4+avg. data 4+avg. data 4+avg. data 2+avg. data 2+avg. data 2+avg. data 2+avg. data 60% 60% 60% 60% Sparse bytes NULL Percentage
hierarchyId
4+avg. data
2+avg. data
60%
Filtered Indexes
Allows you to add an index to a column with a where clause Useful for indexing columns with null values in them
Geometry Geography
http://www.conceptdevelopment.net/Database/Geoquery/
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.
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.
Instead of:
You can:
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', 42 exec sp_MySproc2 @myPeeps
DDL Triggers
Auditing, regulating schema changes, capture events on create_table, alter_procedure, drop_login etc
PIVOT
More features
select top with tie feature( if top 10 and there are 15 that match number 10 will bring back all 15)
What happened to our relationships? RAW doesnt show our table relationships but gives us a flat XML hierarchy
Great, but what if I needed to format the XML to output into a certain schema
NULL AS PARENT,
BusinessEntityID AS [Person!1!BusinessEntityID], FirstName AS [Person!1!FirstName!ELEMENT] FROM AdventureWorks.Person.Person FOR XML EXPLICIT
FirstName "Person/FirstName"
FROM AdventureWorks.Person.Person FOR XML PATH ('')
XQuery - Querying
SELECT @x.exist('/christmaslist/person[@gift="socks"]') SELECT @x.exist('/christmaslist/person[@gift="lump of coal"]')
SELECT @x.exist('/christmaslist/person[@gift="Socks"])
SELECT @x.value('/christmaslist[1]/person[1]/@name', 'VARCHAR(20)) SELECT @x.query('/christmaslist/person')
XQuery - Querying
query() value()
exist()
nodes() modify()
XQuery - Resources
http://msdn.microsoft.com/en-us/library/ms345117.aspx
ADO.NET
ADO.NET gives you full control over how you access and retrieve data from the data source
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
Resources 1/2
http://www.conceptdevelopment.net/Database/Geoquery/
http://stackoverflow.com/questions/121243/hiddenfeatures-of-sql-server
http://technet.microsoft.com/en-au/library/cc917696.aspx
Resources 2/2
http://www.brentozar.com/sql/blitz-minute-sql-server-takeovers/
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/
www.microsoft.com/technet/community
3 things
3 things
Thank You!
Gateway Court Suite 10 81 - 91 Military Road Neutral Bay, Sydney NSW 2089 AUSTRALIA ABN: 21 069 371 900