+ All Categories
Home > Documents > Microsoft Transact-SQL : the definitive guideMicrosoft Transact-SQL : the definitive guide Subject...

Microsoft Transact-SQL : the definitive guideMicrosoft Transact-SQL : the definitive guide Subject...

Date post: 13-Aug-2020
Category:
Upload: others
View: 13 times
Download: 0 times
Share this document with a friend
13
fir y ti rvrrTUTTnrr i t i r FHE DEFINITIVE GUIDE : ',! .4 '. ^pHIHri^^lV ;\}'\^X$:^u^'! :: : ^ JEFFREY GARBUS PLAMEN ratchev Alvin Chang Joe Celko g JONES & BARTLETT LEARN IN G
Transcript
Page 1: Microsoft Transact-SQL : the definitive guideMicrosoft Transact-SQL : the definitive guide Subject Sudbury, Mass. [u.a.], Jones and Bartlett Learning, 2011 Keywords Signatur des Originals

fir y ti rvrrTUTTnrr i t i rFHE DEFINITIVE GUIDE: ',! .4

1

'..^pHIHri^^lV

;\}'\^X$:^u^'! ::: ^

JEFFREY GARBUS

PLAMEN ratchev

Alvin Chang

Joe Celko

g

JONES & BARTLETT

LEARN IN G

Page 2: Microsoft Transact-SQL : the definitive guideMicrosoft Transact-SQL : the definitive guide Subject Sudbury, Mass. [u.a.], Jones and Bartlett Learning, 2011 Keywords Signatur des Originals

Contents

About the Authors xvii

Foreword xix

Part 1: All About Programming 1

Chapter 1 Introduction 3

SQL Variations 3

lof5 3

2 of 5 4

3 of 5 4

4 of 5 4

5 of 5 5

Where We Go from Here 6

Chapter 2 SQL Basics 9

SELECT Statement and Logical Query ProcessingJoins 13

Cross Join 13

Inner Join 15

Outer Join 17

Subqueries 20

Table Expressions 23

UNION Operator 25

UPDATE Statement 27

TOP Option 31

CASE Expression 34

Page 3: Microsoft Transact-SQL : the definitive guideMicrosoft Transact-SQL : the definitive guide Subject Sudbury, Mass. [u.a.], Jones and Bartlett Learning, 2011 Keywords Signatur des Originals

vi Contents

CUBE, ROLLUP, and GROUPING SETS OptionsSELECT INTO Statement 41

Dynamic SQL 42

Summary 45

Additional Reading 45

Exercise 46

Exercise Solution 46

Chapter 3 Tables 49

Table Creation 49

Real and Implied Limitations 51

Data Types 52

Exact Numerics 52

Approximate Numerics 53

Date and Time 54

Character/Unicode Strings 55

Other Data Types 56

Working with CLR User-Defined Types 56

Data Type Selection 57

Identity Columns 58

ROWGUIDCOL 60

Computed Columns 60

Some General Tips for Creating Tables 61

Temporary Tables 62

Creating Temporary Tables 62

"Permanent" Temporary Tables 63

Table Variables 63

Summary 63

Exercise 64

Exercise Solution 64

Chapter 4 Views 65

Why Use a View? 65

The View in General 66

WITH CHECK OPTION 67

WITH ENCRYPTION 68

Page 4: Microsoft Transact-SQL : the definitive guideMicrosoft Transact-SQL : the definitive guide Subject Sudbury, Mass. [u.a.], Jones and Bartlett Learning, 2011 Keywords Signatur des Originals

WITH SCHEMABINDING 68

Limitations and Data Modification 68

Indexed Views 70

Requirements for an Indexed View 70

Partitioned Views and Horizontal PartitioningModifying Views 74

View Info 74

Summary 75

Exercise 75

Exercise Solution 75

Chapter 5 Built-in Functions and SET Options 77

Transact-SQL 77

Scalar Functions 77

Mathematical Functions 78

String Functions 79

Date Functions 81

System Functions 83

Function Examples 86

SET Options 90

SET Examples 94

More about SET Options 95

Summary 97

Exercise 97

Exercise Solution 97

Chapter 6 Programming Basics 99

Batches 99

Comments 100

Local Variables 101

Notes on Local Variables 102

PRINT Statement 103

Notes on the PRINT Statement 103

RAISERROR Statement 104

RAISERROR Options 104

Standardizing Error Message Syntax 106

Page 5: Microsoft Transact-SQL : the definitive guideMicrosoft Transact-SQL : the definitive guide Subject Sudbury, Mass. [u.a.], Jones and Bartlett Learning, 2011 Keywords Signatur des Originals

vim Contents

Error Handling with TRY.. .CATCH 107

Notes on TRY.. .CATCH 108

Statement Blocks: BEGIN...END 109

Conditional Execution: IF...ELSE 110

IF EXISTS 111

Notes on IF...ELSE 112

Repeated Execution: WHILE 112

Control Transfer: GOTO 113

Event Handling: WAITFOR 113

Notes on WAITFOR 114

Exiting Batches with RETURN 115

Summary 115

Exercise 115

Exercise Solution 116

Chapter 7 Transactions 117

ACID Properties 118

Transactions and Batch Flow 122

Nested Transactions 122

Transactions and Stored Procedures 123

Locking 126

Granularity 127

Identifying Locks 128

Transaction Isolation Levels 129

Deadlocks 130

What Happens at Update Time 133

Summary 134

Chapter 8 Cursors 135

Cursors 136

Cursor Types 137

Cursor Steps 137

Transact-SQL Cursor Types 137

Dynamic Cursors 137

Static Cursors 138

Keyset Cursors 138

Page 6: Microsoft Transact-SQL : the definitive guideMicrosoft Transact-SQL : the definitive guide Subject Sudbury, Mass. [u.a.], Jones and Bartlett Learning, 2011 Keywords Signatur des Originals

Contents ix

T-SQL Cursor Usage 138

Declaring Cursors 139

ANSI-92 Syntax 139

T-SQL Syntax 139

Opening Cursors 141

Notes on Open Cursors 141

Fetching Rows 141

Navigation Options 142

Fetch Examples 142

Notes on Fetch 143

Simple Cursor Example 144

Modification with Cursors 144

Notes on Cursor Modification 145

Closing the Cursor 145

Deallocating the Cursor 146

Scope of Cursors and Name Conflicts 146

Cursor Variables 146

Cursor Options 147

Cursor Lock Granularity 147

Cursors and Locking 147

A Sample Cursor Program 148

Notes on Cursors 149

Cursor Pros and Cons 149

Cursor Concurrency 150

Acquiring Locks 150

Using Locks 151

Optimizing Cursors 151

Summary 152

Chapter 9 Stored Procedures 153

Creating Stored Procedures 154

User Interfaces 156

Table-Valued Parameters 159

Parameter Sniffing 161

Error Handling 162

Transactions in Stored Procedures 165

Page 7: Microsoft Transact-SQL : the definitive guideMicrosoft Transact-SQL : the definitive guide Subject Sudbury, Mass. [u.a.], Jones and Bartlett Learning, 2011 Keywords Signatur des Originals

x Contents

Summary 170

Additional Reading 170

Exercise 170

Exercise Solution 171

Chapter 10 User-Defined Functions 175

UDF Components 176

Types of Functions 177

Table-Valued Functions 178

Summary 180

Exercise 180

Exercise Solution 180

Chapter 11 Triggers 181

The Trigger Mechanism 182

Trigger Creation 183

Removing Triggers 184

Modifying Triggers 185

Deleted and Inserted Tables 185

What Happens on Delete 186

What Happens on Insert 188

Handling Multirow Inserts/Updates 189

If Update Test Syntax 190

Checking Columns for Modification 191

Decoding a Bitmask 192

Insert Trigger Example 193

Conditional Insert Trigger Example 194

Update Trigger Example 195

Trigger Limitations 196

Triggers during Transactions 197

Using Savepoints in Triggers 198

Nested Triggers 199

Additional Notes on Triggers 200

Summary 201

Exercise 202

Exercise Solution 202

Page 8: Microsoft Transact-SQL : the definitive guideMicrosoft Transact-SQL : the definitive guide Subject Sudbury, Mass. [u.a.], Jones and Bartlett Learning, 2011 Keywords Signatur des Originals

Contents xi

Chapter 12 Common Table Expressions 203

CTE Basics 203

CTE and Recursion 205

Multiple Anchor Members 206

Multiple Recursive Members 207

Recursion Limit 212

Uses for Common Table Expressions 213

Traversing a Hierarchy 214

Date Ranges 214

Parsing CSV Values 215

Beyond 32767 216

Summary 219

Part 2: Focus on Performance 221

Chapter 13 Understanding Graphical Query Plans 223

What Is an Execution Plan? 223

Retrieving Queiy Plans 224

Graphical Execution Plan Sections 226

Reading the Graphical Execution Plan 228

Analyzing Graphical Execution Plans 229

Common Operators in Graphical Plans 234

Clustered Index Scan/Nonclustered Index Scan/Table Scan 234Clustered Index Seek/Index Seek 235

(Bookmark) Key Lookup/RID Lookup 237

Nested Loop Join 240

Merge Join 242

Hash Match 244

Sort 248

Stream Aggregate 250

Compute Scalar 251

Summary 252

Additional Reading 252

Chapter 14 Indexes 253

How Does the Index Work? 254

Covered Queries 255

Page 9: Microsoft Transact-SQL : the definitive guideMicrosoft Transact-SQL : the definitive guide Subject Sudbury, Mass. [u.a.], Jones and Bartlett Learning, 2011 Keywords Signatur des Originals

XII Contents

Index Classification 256

Clustered Indexes 256

Nonclustered Indexes 257

Composite Index 259

Index Architecture 259

Index Structure and Access 260

Clustered versus Nonclustered Index Architecture 261

Clustered Index Architecture 262

Nonclustered Index Architecture 263

Data Storage and Data Access 264

Data Storage 264

Data Access 264

Indexing Strategy 265

When Not to Index 270

When to Use an Index 272

Index Creation and Maintenance 274

CREATE INDEXCommand 274

DROP INDEXCommand 275

FILLFACTOR Option 276

Database Reorganization 277

DBCC Command 278

Database Defragmentation 279

SQL Server Index Optimization 282

Auto Create Statistics 283

Index Selection 284

Column Selectivity and Joins 284

Operators and Clauses 285

INCLUDE Columns 287

Filtered Indexes 288

SYS.INDEX and SYS.SYSINDEXES System Tables 288

Constraints 292

Data Integrity 293

Domain Integrity 293

Entity Integrity 294

Referential Integrity 296

User-Defined Integrity 296

Page 10: Microsoft Transact-SQL : the definitive guideMicrosoft Transact-SQL : the definitive guide Subject Sudbury, Mass. [u.a.], Jones and Bartlett Learning, 2011 Keywords Signatur des Originals

Contents xiii

Constraint Types 297

PRIMARY KEY Constraints 297

UNIQUE Constraints 298

CHECK Constraints 299

FOREIGN KEY Constraints 300

NOT NULL Constraint 301

ER Diagrams with Key Constraints 302

Summary 303

Chapter 15 Join Optimization 303

Does Join Order Matter? 306

Nested Loops Join (aka Nested Iteration Join) 308

Merge Joins 310

Hash Joins 310

In-Memory Hash Join 311

Grace Hash Join 312

Recursive Hash Join 312

Forcing a Join Order 313

Influencing the Joins 314

Things to Note While Giving Hints 316

Self-Joins 317

Matching Data Self-Join 318

Parent-Child Self-Join 320

Outer Joins 323

Summary 324

Exercise 324

Exercise Solution 325

Chapter 16 Subquery Optimization 327

Subquery Basics 330

Optimizer Rewrites 332

Optimizing Subqueries in the SELECT Clause 334

Optimizing Subqueries in the WHERE Clause 337

Scalar Subqueries 340

Summary 342

Exercise 342

Exercise Solution 343

Page 11: Microsoft Transact-SQL : the definitive guideMicrosoft Transact-SQL : the definitive guide Subject Sudbury, Mass. [u.a.], Jones and Bartlett Learning, 2011 Keywords Signatur des Originals

xiv Contents

Chapter 17 Hardware 345

CPU 348

CPU Precision 349

CPU Speed 350

CPU Cache 350

Operating System Effects on Processor Use 351

Before You Buy 352

Once the System Is in Place 352

Memory 357

Memory Size 357

Memory Speed 358

SQL Server's Memory Use 358

Before You Buy 361

Once the System Is in Place 361

Front-Side Bus 366

Before You Buy 367

Once the System Is in Place 367

Before You Buy 370

Once the System Is in Place 370

I/O 372

Solid State Drives (SSDs) and SQL Server 375

SANs, NAS, and Other Storage Types 375

How SQL Server Uses Files 376

Before You Buy 378

Once the System Is in Place 378

The Virtualization Question 384

Summary 385

Part 3: Advanced SQL Techniques 387

Chapter 18 Set-Oriented Programming versus Procedural Code 389

A Bit of History 389

Classic Structured Programming 390

BEGIN-END 390

IF-THEN-ELSE 391

WHILE-DO 393

MERGE Statement 396

Summary 398

Page 12: Microsoft Transact-SQL : the definitive guideMicrosoft Transact-SQL : the definitive guide Subject Sudbury, Mass. [u.a.], Jones and Bartlett Learning, 2011 Keywords Signatur des Originals

Contents xv

Chapter 19 Dynamic SQL 399

Types of Dynamic SQL 399

T-SQL Dynamic SQL 399

.NET Application Dynamic SQL 402

Security Concerns 403

Table-Level Access Requirement 403

Using Impersonation with EXECUTE AS 404

SQL Injection 405

Unchecked User Inputs 405

Preventing SQL Injection 406

Using Certificates and EXECUTE AS to Circumvent

SQL Injection 406

Putting Dynamic SQL to Work 408

Cache Bloat from Dynamic SQL 409

Using T-SQL Stored Procedures 410

Using .NET Application Dynamic SQL 416

Administration and Maintenance 418

Summary 419

Chapter 20 Grouping Data 421

CUBE Option 426

ROLLUP Option 426

GROUPING Function 428

GROUPING SETS 429

GROUPINGJD Function 432

Summary 433

Additional Reading 434

Exercise 434

Exercise Solution 435

Chapter 21 Refactoring 437

Design Refactoring 437

Entity-Attribute-Value Problem 438

T-SQL Code Refactoring 445

Ranking Solutions 445

Refactoring Data Merge 449

Page 13: Microsoft Transact-SQL : the definitive guideMicrosoft Transact-SQL : the definitive guide Subject Sudbury, Mass. [u.a.], Jones and Bartlett Learning, 2011 Keywords Signatur des Originals

xvi Contents

Summary 453

Additional Reading 453

Exercise 453

Exercise Solution 456

Chapter 22 Why Other People's Code Stinks 457

Why Is That There? 458

Coding Standards 460

SELECT * 460

Parsing 461

Overnormalization 464

SQL Injection 465

System Tables 465

Cursors 465

Multiple Passes Through Data 465

Working Too Hard 465

Summary 465

Chapter 23 Case Study #1 467

ProcessAuditTrail 468

General Setup Script (Read Before Executing)

Chapter 24 Case Study #2 479

New Code (Rewritten) 480

Old Code 483

Chapter 25 Case Study #3 491

Very Bad 492

Bad 494

Not So Bad 496

Index 497


Recommended