Installation MS SQL Server
Conditions 1. Every laboratory must be passed and student gets mark for every laboratory 2. Student must pass finall test, what gives opportunity to get original Microsoft diploma. 3. Final mark will be an average of marks got during laboratory and test calculated with the formula: Final mark = (lab_mark + 2*test_mark)/3
Plan of lecture 1. Installation and configuration of MS SQLServer. 2. Creation and configuration of databases. 3. Users managment. 4. Server administration. 5. Transact SQL. 6. Integration Services. 7. Tuning queries. 8. OLAP.
MS SQL Server MS SQL Server relational database with services enabling management of stored data. Data models: OLTP On-Line Transaction Processing, OLAP On-Line Analitycal Processing.
Overview SQL Server versions First usable: 6.5 First popular: 7.0 Other: 2000, 2005 Current: 2008
MS SQLServer s tasks MS SQLServer as database management system: stores data for transactional and analytical processing, stores and handles relationships among data, ensures correctness of data and controls relational rules, answers client applications requests, preserves and secures data, restores data from backups after hardware or software failures.
MS SQL Server Client SQL Server Results Query OLTP OLAP Client application Relational Database System
Client server architecure Client components: 1. Client Application 2. Database API OLE DB, ODBC, JDBC 3. Client Net Libraries Server components: 1. Server Net Libraries 2. Open Data Service 3. Relational Engine 4. Storage Engine
Communication between client and server 1 Client Application Query Result Set Database API (OLE DB, ODBC, DB-Library) 3 SQL Server Server Net-Libraries 5 Open Data Services Query Result Set Client 2 Client Net-Library Relational Engine 4 Server Storage Engine Processor Memory Local Database
MS SQL Server components 1. SQL Server Database Engine 2. SQL Server Agent 3. Analysis Services 4. Reporting Services 5. Notification Services 6. Integration Services
SQL Server versions 1. Standard Edition 2. Workgroup Edition 3. Enterprise Edition 4. Developer Edition 5. Microsoft Database Engine (MSDE) (2k) 6. Express Edition (2k5) 7. Mobile Edition
Requirements 2k5 (32-bit) Component Processor Minimal requirements Pentium III, 600 MHz, better 1 GHz Memory Minimum 512 MB better 1 GB Hard disk MB 356 MB 150 MB (database server) 80 MB (client tools) 75 MB (Analysis, Reporting Services) 80 MB (English Query)
Instalation options 2k5 1. x86 based operating systems 2. x64 based operating systems 3. Itanium based operating systems
Installation parameters Instance naming: Default name computer Windows name Named instance format: Multiple instances <computer name>\<instance name>.
Instalation parameters Naming instances - rules: Shorter than 16 characters case sensitive, Reserved names Default and MSSQLServer, Classic rules no spaces, First character must be a letter, &, _, or #.
Installation parameters Authetication modes: Windows Authentication Mode SQL Server uses Windows 2000 or NT accounts. Mixed Mode SQL Sever uses Windows 2000 or NT accounts (Windows Authentication) or SQL Server accounts (SQL Server Authentication) Other paramaters: Collation Settings
Server components SQL Server, SQL Server Agent, SQL Server Integration Service, SQL Server Browser SQL Server FullText Search SQL Server Reporting Services SQL Server Analysis Services SQL Server VSS Writer Distributed Transaction Coordinator
Other components Client Connectivity Books Online Business Intelligence Development Studio (2k5) Code Samples
Database service 1. MS SQL Server: database engine, executes Transact-SQL queries and manages all files containing database: a. handles resources for users, b. coordinates user s requests, c. ensures data consistency.
Some other services SQL Server Reporting Service SQL Server Analysis Service SQL Server Notification Service...
Verification services Running services: Control Panel/Administration Tools/Services SQL Service Manager SQL Server Management Studio
Databases System Databases master model tempdb msdb distribution Adventure Works User_1 User_2 User Databases
Creating database The simplest command: CREATE DATABASE baza Copy of model database is created C:\...\Data\baza.mdf C:\...\Data\baza.ldf
Data files CREATE DATABASE baza ON (NAME=nazwa, FILENAME= nazwa_pliku, SIZE=10MB, MAXSIZE = 20MB UNLIMITED FILEGROWTH = 20% 2MB),... LOG ON (NAME=nazwa, FILENAME= nazwa_pliku, SIZE=10MB, MAXSIZE = 20MB UNLIMITED FILEGROWTH = 20% 2MB),...
Why do we need log? 1 Data modification 3 Save to log Cache Disk 2 Read page from disk Disk 4 Save change to database
CHECKPOINT update tematy set nazwa= dysk where nr>10 insert into pracownicy values(19, Kowalski ) update tablica set pole1= xyz where pole2>10 update tematy set nazwa= dysk where nr>10 insert into pracownicy values(19, Kowalski ) update tablica set pole1= xyz where pole2>10 update tablica set pole1= xyz where pole2>10 update tematy set nazwa= dysk where nr>10 insert into pracownicy values(19, Kowalski ) update tablica set pole1= xyz where pole2>10 update tablica set pole1= xyz where pole2>10
Transaction log Every data change is stored in log file first Every operation has transaction defined Begins and commits are stored in log CHECKPOINT place to which data is stored on disk Every data write to disk changes CHECKPOINT position In case of failure rollback of not commited transaction and rollforward of commited
Format of datafiles Page 8kB Extent 8 consecutive pages Uniform extent pages from one object Mixed extent contains pages from different objects
Begining of the file Header PFS GAM SGAM Page0: File Header logical name, prameters (size chcnge params Page1: PFS (Page Free Space) 1 byte about every page (8000 first pages 64MB) Page2: GAM (Global Allocation Map) bit for every extend free or full (64k extents = 2GB) Page3: SGAM (Shared Global Allocation Map) bit for every extent are there free pages? (64k extents = 2GB)
Meaning of GAM & SGAM bits Content of extent Bit GAM Bit SGAM Free 1 0 Uniform or full 0 0 Mixed with free pages 0 1
Objects allocation IAM page (Index Allocation Map) Pointers to first 8 pages Bitmap of extents (for 512 000 pages)
Max values... Databases 32.767 Databse size 1.048.516 TB Files for database 32.767 File size 32 TB Objects in database (tables, views, stored procedures, extended stored procedures, triggers, rules, defaults, and constraints) Columns in table 1024 2.147.483.647
Structure of database Database Primary Filegroup Filegroup Log Log File.mdf File.ndf File.ndf File.ndf
Division to files CREATE DATABASE baza ON PRIMARY (NAME=nazwa,FILENAME= nazwa_pliku,size=10mb,maxsize=20mb FILEGROWTH = 20%), (NAME=nazwa,FILENAME= nazwa_pliku,size=10mb,maxsize=20mb FILEGROWTH = 20%), FILEGROUP fg1 ( (NAME=nazwa,FILENAME= nazwa_pliku,size=10mb,maxsize=20mb FILEGROWTH = 20%), (NAME=nazwa,FILENAME= nazwa_pliku,size=10mb,maxsize=20mb FILEGROWTH=20%))
Filegroups PRIMARY for system objects User-defined users objects explicitly placed here using ON filegroup CREATE TABLE tablica (int pole1,int pole2) ON fg1; DEFAULT default place for objects
Creating new filegroup ALTER DATABASE baza ADD FILEGROUP newfg GO ALTER DATABASE baza ADD FILE (NAME = mojnowyplik', FILENAME = 'c:\...\plik.ndf, SIZE = 5MB) TO FILEGROUP newfg GO
Why more than one file? Performance faster access when more hd's Easier to maintain small files easier to handle backup of important files only Protection of data read-only files Protection of system data separate file for system tables
Database parameters Auto options Cursor options Recovery options SQL options State options
Auto options AUTO_CLOSE AUTO_CREATE_STATISTICS AUTO_UPDATE_STATISTICS AUTO_SHRINK
Cursor options CURSOR_CLOSE_ON_COMMIT CURSOR_DEFAULT [LOCAL GLOBAL]
Recovery options RECOVERY FULL BULK_LOGGED SIMPLE PAGE_VERIFY CHECKSUM TORN_PAGE_DETECTION NONE
Snapshot isolation (2k5) ALLOW_SNAPSHOT_ISOLATION ON: SNAPSHOT isolation level allowed transaction sees data as it was when it started OFF: SNAPSHOT isolation level switched off READ_COMMITED_SNAPSHOT ON: transaction on READ COMMITTED isolation level sees last committed data no waits OFF: transaction on READ COMMITTED isolation level locks during dirty read (like in SQL2k)
SQL options ANSI_NULL_DEFAULT ANSI_NULLS ANSI_PADDING ANSI_WARNINGS ARITHABORT arithmetic abort NUMERIC_ROUNDABORT CONCAT_NULL_YIELDS_NULL
QUOTED_IDENTIFIER -- doesn t work! SET QUOTED_IDENTIFIER OFF CREATE TABLE "select" ("identity" int IDENTITY, "order" int) -- works fine! SET QUOTED_IDENTIFIER ON CREATE TABLE "select" ("identity" int IDENTITY, "order" int) SELECT "identity","order" FROM "select" ORDER BY "order" -- always works CREATE TABLE [select] ([identity] int IDENTITY, [order] int) SELECT [identity],[order] FROM [select] ORDER BY [order] -- but it s not a good idea!
State options ON_LINE OFF_LINE READ_ONLY READ_WRITE SINGLE_USER RESTRICTED_USER SINGLE_USER RESTRICTED_USER MULTI_USER
Execution condition WITH <termination> WAIT (default) ROLLBACK AFTER 10 [SECONDS] ROLLBACK IMMEDIATE NO_WAIT
Examples ALTER DATABASE baza SET QUOTED_IDENTIFIER ON, RECOVERY FULL, MULTI_USER GO ALTER DATABASE baza SET SINGLE_USER WITH ROLLBACK AFTER 10 GO
Transact SQL Stored procedures EXEC sp_helpdb Functions SELECT DATABASEPROPERTYEX( baza','isoncommitenabled') DBCC DBCC SQLPERF DBCC SHRINKFILE
Data types Numeric Integer Exact floating point Floating point Currency Date and time Text BLOB Special
Numeric types int 4 bigint 8 smallint 2 tinyint 1 decimal(p[,s]) 2-17 numeric (p[,s]) 2-17 float(n) n(1-24) 7 digits 4B n(25-53) 15 digits 8B real 4 money 8 smallmoney 4
Date and text types datetime 8 smalldatetime 4 char(n) 0-8000 varchar(n) 0-8000 nchar(n) 0-8000 (4000 chars!) nvarchar(n) 0-8000 (4000 chars!) binary 0-8000 varbinary 0-8000
Other types image text ntext uniqueidentifier 16 bit 1 timestamp 8 0-2 GB 0-2 GB 0-2 GB sql_variant up to 8000
User defined types Type creation EXEC sp_addtype nazwisko, 'nvarchar(25)', NULL Type deletion EXEC sp_droptype nazwisko
Data row organization Header Normal fields Null Block Variable Block Variable length fields Header 4 bytes Null block 2B for columns number, Null Bitmap (1 bit for column if its null) Variable block 2B for columns number, 2B for column with pointer to column position Max row length 8060 B
Storing BLOBs Special tree structures It can be defined how big value will be directly in row. EXEC sp_tableoption 'tablica', 'text in row', '1000' Max row size: 8060 B. Big BLOBs are stored as pointers (16B)
IDENTITY column One in table Definition: IDENTITY[(seed,incr)] CREATE TABLE pracownicy ( nr_prac int IDENTITY(100,1), nazwisko varchar(30))) insert into pracownicy values ( Kowalski ); insert into pracownicy values ( Nowak ); insert into pracownicy values ( Iksiński ); nr_prac nazwisko ----------- ---------- 100 Kowalski 101 Nowak 102 Iksiński
IDENTITY control select IDENT_CURRENT('table') globally highest value of the column select @@identity last value inserted in the session (NULL if nothing was inserted) select IDENT_SEED( table ) select IDENT_INCR( table ) select SCOPE_IDENTITY() last IDENTITY value inserted (to any table)
Disabling IDENTITY CREATE TABLE pracownicy ( nrp int IDENTITY(100,1), nazw varchar(30))) insert into pracownicy(nazw) values ( Kowalski ); SET IDENTITY_INSERT pracownicy ON insert into pracownicy(nrp,nazw) values (200, Nowak ); SET IDENTITY_INSERT pracownicy OFF insert into pracownicy(nazw) values ( Iksiński ); nr_prac nazwisko ----------- ---------- 100 Kowalski 200 Nowak 201 Iksiński
Not-unique IDENTITY CREATE TABLE pracownicy ( nrp int IDENTITY(100,1), nazw varchar(30))) insert into pracownicy(nazw) values ( Kowalski ); insert into pracownicy(nazw) values ( Iksiński ); SET IDENTITY_INSERT pracownicy ON insert into pracownicy(nrp,nazw) values (100, Nowak ); SET IDENTITY_INSERT pracownicy OFF nr_prac nazwisko ----------- ---------- 100 Kowalski 101 Iksiński 100 Nowak
Uniqueidentifier type 16B (128b) GLOBALLY unique (GUID) CREATE TABLE pracownicy ( nrp uniqueidentifier NOT NULL DEFAULT newid(), nazw varchar(30))) insert into pracownicy(nazw) values ( Kowalski ); insert into pracownicy(nazw) values ( Iksiński ); insert into pracownicy(nazw) values ( Nowak ); nr_prac nazwisko ------------------------------------ ---------- 075860D6-FBAC-466E-A0F5-5EAF0D69543B Kowalski A5B7C0BD-0F23-4F79-9758-AF35A6D20D3D Iksiński FF8BEBC6-21F8-46A1-8108-8507BA9509E0 Nowak
timestamp (rowversion) CREATE TABLE tt (a int, b timestamp) insert into tt(a) values (1); insert into tt(a,b) values(2,null) a b ----------- ------------------ 1 0x00000000000000CC 2 0x00000000000000CD update tt set a=3 where a=1 a b ----------- ------------------ 3 0x00000000000000CF 2 0x00000000000000CD
CONSTRAINTS Default Check Primary Key Unique Foreign Key CREATE DEFAULT
Default constraints Not for IDENTITY Works only for inserts We may use variables or system functions: newid(), user, getdate() CREATE TABLE tablica( tekst varchar(50), wpisal varchar(30) default user, data_wpisu datetime default getdate() ) insert into tablica(tekst) values( Nowy tekst ) tekst wpisal data_wpisu ------------ ---------------- ----------------------- Nowy tekst pawel 2004-10-10 20:52:02.513
Check constraint Works for every data change CREATE TABLE pracownicy( pensja int check (pensja>0), data_ur datetime, opis varchar(50), constraint data_mniejsza check(data_ur<getdate())) insert into pracownicy values(10, 2100, ) INSERT statement conflicted with COLUMN CHECK constraint 'data_mniejsza'. insert into pracownicy values(-10, 1980, ) INSERT statement conflicted with COLUMN CHECK constraint 'CK pracownic pensj 7E6CC920'.
Check constraint May be used for more than one column CREATE TABLE projekty ( start datetime, koniec datetime, constraint koniec_po_starcie check (start<koniec)) Logic values may be used ALTER TABLE tablica ADD CONSTRAINT dataur CHECK (dataur>'01-01-1900' AND dataur<getdate())
Check constraint Usage of patterns CREATE TABLE adresy ( miasto varchar(30), kod char(6) check(kod LIKE '[0-9][0-9]-[0-9][0-9][0-9]'), ulica varchar(40) check(ulica like '[a-z]%[0-9]') ) insert into adresy values( Gliwice','22-445', Ala 6') insert into adresy values( Gliwice','22-44', Ala 6') insert into adresy values( Gliwice','22-442', 5Ala 6') insert into adresy values( Gliwice','22-442', 5 Ewa')
NOCHECK Doesn t check constraints on already inserted columns CREATE TABLE tablica ( a INT); INSERT INTO tablica VALUES (-1); ALTER TABLE tablica ADD CONSTRAINT mycheck CHECK (a > 1); ALTER TABLE statement conflicted with COLUMN CHECK constraint 'mycheck'. ALTER TABLE tablica WITH NOCHECK ADD CONSTRAINT mycheck CHECK (a > 1); The command(s) completed successfully.
Primary key One in one table (may be more than one column) Automatic index creation CREATE TABLE tablica ( a INT [[NON]CLUSTERED] PRIMARY KEY, b varchar(20)); CREATE TABLE tablica ( a INT, b varchar(20), PRIMARY KEY (a)); ALTER TABLE tablica ADD CONSTRAINT primary_key PRIMARY KEY NONCLUSTERED (a);
Unique May have one NULL value May be more than one in table Cannot use it with PK columns Automatic index creation
Foreign key No SET NULL May be for more than one column WITH NOCHECK may be used CREATE TABLE tablica ( a INT, b varchar(20), PRIMARY KEY (a,b)); CREATE TABLE tab2( x int, y varchar(20), foreign key (x,y) references tablica(a,b) on update cascade);
Turning off constraints When big amount of data is loaded When we know they fulfill conditions When they are not but it is easier to correct it in the database ALTER TABLE tablica NOCHECK CONSTRAINT mój_warunek... Data loading... ALTER TABLE tablica CHECK CONSTRAINT mój_warunek
CREATE DEFAULT One definition used in different places (also in user types) CREATE DEFAULT mojkraj AS Polska sp_bindefault mojkraj, pracownicy.kraj' sp_bindefault mojkraj, studenci.kraj' sp_bindefault mojkraj, adresy.kraj' Turning off default sp_unbindefault mojkraj, adresy.kraj'
Computed columns create table prac(nrp int, adr_domowy varchar(30), adr_praca varchar(30), koresp int, adr_koresp as CASE koresp WHEN 0 THEN adr_domowy ELSE adr_praca END ) insert into prac values(1,'chorzów','gliwice',0) select * from prac nrp adr_domowy adr_praca koresp adr_koresp --- ------------ ----------- ------ ----------- 1 Chorzów Gliwice 0 Chorzów update prac set koresp=1 select * from prac nrp adr_domowy adr_praca koresp adr_koresp --- ------------ ----------- ------ ----------- 1 Chorzów Gliwice 1 Gliwice
SQL and indexes Index creation create index nazwa on tablica(kolumna,...) create unique index nazwa on tablica(kolumna,...) create clustered index nazwa on tablica(kolumna,...) create unique clustered index nazwa on tablica(kolumna,...) Index dropping drop index tablica.nazwa
How to use indexes PK and UNIQUE are better First clustered Do not duplicate indexes! Indexed fields should be short Use UNIQUE if it is possible SELECT id, COUNT(*) FROM tablica GROUP BY id HAVING COUNT(*)>1
Indexes pros and cons Pros: Faster data searching Control of data uniqueness Cons: Slower data insertion Slower updates of indexed columns Storage (depends on column size) Difficult database administration
Composite indexes Max 16 columns (900 bytes) Order is important! create index xyz on prac(nazw,imie) create index xyz on prac(imie,nazw) First the most unique column Index is used only when query has first column in WHERE column!
FILLFACTOR option States how to fill pages Default 100% Decreasing increases disk storage but simplifies insert and update Without PAD_INDEX used only for leaves create index xyz on prac(nazw,imie) with [PAD_INDEX,] FILLFACTOR=70
DBCC application DBCC Database Consistency Checker About 130 different operations DBCC checkdb DBCC cleantable DBCC perflog DBCC showcontig DBCCC indexdefrag
DBCC showcontig DBCC SHOWCONTIG scanning 'tablica' table... Table: 'tablica' (2073058421); index ID: 1, database ID: 6 TABLE level scan performed. Pages Scanned: 3 Extents Scanned: 2 Extent Switches: 1 Avg. Pages per Extent: 1.5 Scan Density [Best Count:Actual Count]: 50.00% [1:2] Logical Scan Fragmentation 0.00% Extent Scan Fragmentation: 50.00% Avg. Bytes Free per Page: 246.7 Avg. Page Density (full): 96.95% DBCC execution completed. If DBCC printed error messages, contact your system administrator.