SQL Server. 2012 for developers. Bryan Syverson. Joel Murach. Mike Murach &
Associates, Inc. 4340 N. Knoll Ave. •. Fresno, CA 93722 www.murach.com •.
TRAINING & REFERENCE
murach's
SQL Server 2012
for developers Bryan Syverson Joel Murach
Mike Murach & Associates, 4340 N. Knoll Ave.
www.murach.com
•
•
Fresno, CA 93722
[email protected]
Inc.
Expanded
Expanded Section 1 Chapter
1
An
contents
introduction to SQL
An introduction to relational databases and SQL An introduction to client/server The hardware components of The software components of
a
systems
client/server system
a
6 8
introduction to the relational database model
An How
a
database table is organized
How the tables in
a
a
table
are
are
related
12
defined
14
How relational databases compare to other data models
An introduction to SQL and SQL-based
history of SQL comparison of Oracle, DB2, MySQL,
systems
A brief A
Typical
statements for
SQL
How to query a single table How to join data from two or How to add,
update,
22
with database
objects
and delete data in
a
28
table
30 32
How to work with other database
objects
How to work with views How to work with stored
How to
an
and user-defined functions
application program
models
36
38 38
How to use ADO.NET from a .NET
40
Visual Basic
42
C# code that
2
access
34 34
procedures, triggers,
SQL from
use
24 26
tables
more
SQL coding guidelines
Common data
20
22
statements
working
16
18 18
and SQL Server
The Transact-SQL statements An introduction to the
10 10
relational database
How the columns in
4 4
client/server system
Other client/server system architectures
Chapter
contents
How to
application code that retrieves data from a SQL Server database retrieves data from a SQL Server database
use
the
Management Studio
An introduction to SQL Server 2012 A summary of the How to start and
How
to
SQL
stop
Server 2012 tools
the database
engine
enable remote connections
An introduction to the How to connect to
a
Management Studio
database
database
a
How to back up
How to restore How to set the
a
a
54
56
58 58
database
60
database
compatibility
52
58
database
a
50
54
server
How to manage the database files How to attach
50
52
How to navigate through the database objects
How to detach
44
60
level for
a
database
62
V
vi
Expanded
contents
How to view and
64
modify the database
How to view the database
64
diagrams
How to view the column definitions of
a
66
table
66
How to modify the column definitions How to view the data of How
to
a
How to work with
68
table table
68
queries
70
the data of
modify
How to enter and execute
a
a
70
query
How to handle syntax errors
72
How to open and save queries An introduction to the Query Designer
74
76
How to use Books Online How to
78
Books Online
display
78
How to look up information
78
Section 2 The essential SQL skills Chapter
3
How to retrieve data from a
single table 86
An introduction to the SELECT statement The basic syntax of the SELECT statement
86
SELECT statement
88
examples
How to code the SELECT clause How
to
code column
How
to name
90
specifications
the columns in
How to code
90
a
result set
92 94
string expressions
How to code arithmetic
96
expressions
How to
use
functions
How to
use
the DISTINCT keyword to eliminate duplicate
98
How to
use
the TOP clause
to return a
subset of selected
rows
102
rows
How to code the WHERE clause How to
use
How to
use
104 104
comparison operators the AND, OR, and NOT logical operators
106
How to use the IN operator
108
How to
the BETWEEN operator the LIKE operator
110
the IS NULL clause
114
How
use
to use
How to
use
112
How to code the ORDER BY clause How to How
sort a
to sort a
result set by
a
result
an
How to retrieve
Chapter
4
100
a
set
by
column
alias,
range of selected
an
expression,
How to code
an
When and how
inner
to use
joins
column number
118 120
or more
tables 126 126
join correlation
116 or a
rows
How to retrieve data from two How to work with inner
116
name
names
128
How to work with tables from different databases
130
How to
132
compound join conditions How to use a self-join Inner joins that join more than two tables How to use the implicit inner join syntax use
134 136 138
Expanded
How to work with outer How to code Outer
join examples
Outer
joins
that
join
Other skills for
140 142
more than two tables
144
with
working
How to combine inner and outer How to use cross
140
joins
join
outer
an
146
joins joins
146 148
joins
How to work with unions The syntax of
a
150
union
150
Unions that combine data from different tables Unions that combine data from the How to
Chapter 5
use
150
same table
152
the EXCEPT and INTERSECT operators
How to code summary How to work with that
use
160
aggregate functions
160
aggregate functions
162
How to group and summarize data
164
How to code the GROUP BY and HAVING clauses
Queries that
use
the GROUP BY and HAVING clauses
How the HAVING clause compares to the WHERE clause How to code complex search conditions
How to summarize data
168
170
172
the ROLLUP operator
172
How to
use
the CUBE operator
174
the GROUPING SETS operator the OVER clause
176
to use use
How to code
How to How
use
184
subqueries
184
subqueries
subqueries compare
How to code How to use
178
subqueries
An introduction to
subqueries
subqueries
186
to joins
in search conditions
with the IN operator subquery with
How to compare the result of a
188 188
an
expression
190 192
How to
use
the ALL
How to
use
the ANY and SOME
keyword
194
keywords
How to code correlated subqueries
196
How to
198
use
the EXISTS operator
Other ways to
use
200
subqueries
How to code
subqueries
in the FROM clause
How to code
subqueries
in the SELECT clause
Guidelines for A A
working
with complex
200 202
queries
query that
How to work with
uses
common table
How to code
How to
a
206
expressions
recursive CTE
How
use
208 208
210
insert, update, and delete data
How to create test tables How to
204 204
subqueries procedure for building complex queries complex
How to code a CTE
Chapter 7
166
use
How to
6
using SQL Server extensions
164
How to How
Chapter
154
queries
How to code aggregate functions
Queries
contents
216
the SELECT INTO statement
216
of the database
216
to use a
copy
vii
viii
Expanded contents
How to insert How
to insert
How to insert
a
single
multiple
218
rows
to
insert default values and null values
How
to
insert
How
to
How
to
How
to
modify existing
perform
222
224
rows
basic
224
update operation use subqueries in an update operation use joins in an update operation a
existing
How to perform
basic delete operation
use
a
subqueries
How to merge How to
220
selected from another table
rows
How to delete How to
perform a
How to code
8
218
row
How
How to
Chapter
218
new rows
more
and
226 228
230
rows
joins
in
a
delete
230 232
operation
234
rows basic merge
complex
234
operation
merge
234
operations
How to work with data
types
A review of the SQL data types
240
Data type overview
240
The numeric data types
242
The string data types
244
The date/time data types The large value data types
246
How to convert data
250
248
How data conversion works
Chapter 9
250
How to convert data using the CAST function
252
How to convert data
254
using
the CONVERT function
How to
use
the TRY_CONVERT function
256
How to
use
other data conversion functions
258
How to
use
functions
How to work with
string data
A summary of the string functions How to solve common problems that
262 262
occur with
string
data
How to work with numeric data A summary of the numeric functions How to solve common problems that
266 268 268
occur
with numeric data
How to work with date/time data
270
272
A summary of the date/time functions
272
How
276
to parse
How to How to How
to
dates and times
perform operations perform a date search perform a time search
on
dates and times
Other functions you should know about How to
use
the CASE function
278 280 282
284 284
How
to use the
IIF and CHOOSE functions
286
How
to use the
COALESCE and ISNULL functions
288
How to
use
the GROUPING function
290
How to
use
the
functions
292
How to
use
the analytic functions
296
ranking
Expanded
Section 3
Database design and
Chapter 10
How to How to
design
design
implementation
database
a data structure
The basic steps for How to
a
designing
a
304
data structure
304
the data elements
identify
306
How to subdivide the data elements
308
How to
310
How How
the tables and
assign columns to identify the primary and foreign keys to enforce the relationships between tables uWntify
312 314
How normalization works How to
identify
316
the columns to be indexed
How to normalize
a
318
data structure
320
The seven normal forms
320
How to
the first normal form
322
How to
apply apply
the second normal form
324
How to
apply
the third normal form
326
When and how to denormalize a data structure
Chapter 11
contents
How to create and maintain databases,
328
tables,
and sequences with SQL statements An introduction to DDL
334
The SQL statements for data definition
334
Rules for
336
coding object
names
How to create databases,
tables,
and indexes
a
database
338
How to create
a
table
340
How to create an index
How to
use
How to
snippets
342
to create
database
objects
344
constraints
346
An introduction to constraints
346
How to use check constraints
348
How to use
350
How to
use
foreign key
constraints
change databases and
How to delete
an
index, table,
or
tables
database
How to alter a table
How
to create a
How to
use a
How to delete
How to alter
The
358 358
used to create the AP database works
script
use
360 360
How the DDL statements work
How to
356 356
sequence
a sequence
script
How the
352
356
sequence
sequence a
352 354
How to work with sequences
Chapter 12
338
How to create
360
the
for database
Management Studio design
How to work with
a
database
368
How to create
a
database
368
How to delete
a
database
368
iX
X
Expanded contents 370
How to work with tables How to create, modify, How
work with
to
or
delete
370
table
a
372
foreign key relationships
How to work with indexes and keys
374
How
to
work with check constraints
376
How
to
examine table
378
How to generate
dependencies
380
scripts
380
How to generate scripts for databases and tables How to generate
a
change script when you modify
Section 4
Advanced SQL skills
Chapter 13
How to work with views
a
table
388
An introduction to views
388
How views work
390
Benefits of using views
392
How to create and manage views How to create a view Examples
392
394
that create views
How to create an
updatable
How to delete
modify
or
a
view
396 398
view
400
How to use views How
update rows through
to
How to insert rows How to delete rows How to
use
How to
the
400
view
through
402
through
a view
402
catalog
or
a
a view
use the
How to create
404
views
View
modify
a
406
Designer
406
view
How to delete a view
Chapter
14
382
406
How to code scripts 410
An introduction to scripts
410
How to work with
scripts
The
statements for
Transact-SQL
412
script processing
How to work with variables and
temporary tables
414
How to work with scalar variables
414
How
416
to work
with table variables
How to work with temporary tables A
comparison
of the five types of
418
Transact-SQL
How to control the execution of a
perform conditional
How to
How to
perform repetitive processing
How
handle
How to
use
objects
420
422 422
object
424 426 428
errors
surround-with
Advanced
script
processing
How to test for the existence of a database
to
table
430
snippets
432
scripting techniques
How to use the system functions
432
How to
434
How A
change
to use
script
How
the session
settings
436
dynamic SQL
that summarizes the structure of
to use
the SQLCMD
utility
a
database
438 442
Expanded
Chapter 15
How to code stored
and
procedures, functions,
triggers
Procedural
programming options in Transact-SQL
Stored
procedures,
user-defined functions, and
How to code stored
procedures
An introduction to stored
procedures
How to
ci
ate a stored
448
triggers
450 450 452
procedure
How to declare and work with parameters
454
How to call
with parameters How to work with return values
456
How
460
to
procedures
validate data and raise
A stored
procedure
How to pass
a
How to delete
table
that manages insert as a
change
or
458
errors
462
operations
parameter
468
stored
procedure
470
procedures
472
a
How to work with system stored
How to code user-defined functions
474
An introduction to user-defined functions
474
How to create
a
scalar-valued function
How to create
a
simple
How to create
a
multi-statement table-valued function
How to delete
or
476 478
table-valued function
change
How to code
480
function
a
482
484
triggers
How to create a
484
How to
486
trigger use AFTER triggers INSTEAD OF triggers
488
How to
use
How to
use
triggers
to enforce data
How to
use
triggers
to work with DDL statements
How to delete
change
or
490
consistency
492 494
trigger
a
How to work with How to use cursors in
cursors
SQL Server
500
An introduction to cursors The
seven
SQL
types of
SQL
How to use
cursors
How
a cursor
to
declare
How to
use
the
How to
use
the
a
row
use
the
How to
update
504
to retrieve data
506 506 508
a cursor
@@FETCH_STATUS system function @@CURSOR_ROWS system function
cursor or
through
concurrency
delete data
Additional cursor
514
a cursor
514
options
through
516
a cursor
cursors
for
518 518
use
by
an
application
How to manage transactions and How to work with transactions How transactions maintain data
SQL
512
dynamic SQL
Transact-SQL
statements for
510
processing techniques
How to use cursors with How to code
502
cursors
processing
using
How to modify data How to
500
Server
statements for cursor
How to retrieve
Chapter 17
448 448
Scripts
Chapter 16
contents
handling
integrity
transactions
program
520
locking 524 524 526
xi
xii
Expanded contents 528
How to work with nested transactions How to work with
530
points
save
An introduction to concurrency and locking How concurrency and The four concurrency
locking
532
are related
that locks
problems
prevent
can
Lockable
538
locking
538
and lock escalation
resources
Lock modes and lock
540
promotion
542
Lock mode compatibility
How to
544
prevent deadlocks
544
Two transactions that deadlock
Coding techniques
Chapter 18
How to manage database How to work with SQL Server
How How
change
to
delete
login IDs
550 552
Server
554
IDs IDs or
change login
or
How to work with database
passwords
How to work with
560
The SQL
562
permissions
Server
How to grant
or
revoke schema
How to
or
revoke database permissions
grant
562
object permissions object permissions
revoke
or
How to grant or revoke server
564 566
permissions
568 570
permissions
572
How to work with roles
572
How to work with the fixed server roles
574
How to work with user-defined server roles
How to display information about
server roles
and role members
576
How to work with the fixed database roles
578
How to work with user-defined database roles
580
How to How to How to
information about database roles and role members
display deny permissions granted by work with application roles
How to manage How to work with
role membership
How to
assign database
How to
assign
How to work with database
How
ID
login by login ID database objects
and roles
permissions
user
584
588 588
IDs
access
582 586
security using the Management Studio
login
How to work with the server roles for a
Chapter 19
556 558
users
How to work with schemas
How to grant
550
security
SQL
login
to create
security
the authentication mode
An introduction to How to
546
prevent deadlocks
that
534 536
How to set the transaction isolation level
How SQL Server manages
532
to
590 592 594
596
permissions
to work with XML 602
An introduction to XML An XML document
602
An XML schema
604
How to work with the xml data
type
606
How to store data in the xml data type
606
How to work with the XML Editor
608
Expanded
How
to use the methods of the xml data type An example that parses the xml data type Another example that parses the xml data type
610 614 616
How to work with XML schemas How to add How to
Chapter 20
an
use an
618
XML schema to
a
XML schema
validate the xml data type
to
database
618 620
How to view
an
XML schema
622
How to drop
an
XML schema
622
Two
skills for
more
working with XML
624
How to use the FOR XML clause of the SELECT statement
624
How to
628
use
the OPENXML statement
How to work with BLOBs An introduction to BLOBs Pros and
cons
Pros and
cons
When
to use
How to
of
storing of storing
634
BLOBs in files
634
BLOBs in
634
a
column
FILESTREAM storage for BLOBs
SQL to work with
use
How to insert,
update, and delete binary data
binary
varbinary(max)
a
634
varbinary(max) column
a
A .NET
table with
a
How to create
How to retrieve
column
636 636
that
application
uses a
varbinary(max)
column
640
A data access class that reads and writes use
FILESTREAM
638 638
application
The event handlers for the form
How to
636 636
data
The user interface for the
642
binary data
648
storage
How to enable FILESTREAM storage on the server
648
How to create
a
database with FILESTREAM storage
650
How to create
a
table with
652
FILESTREAM column
a
How to insert, update, and delete FILESTREAM data
652
How to retrieve FILESTREAM data
652
A data
654
class that
access
uses
FILESTREAM storage
Section 5
CLR integration
Chapter
An introduction to CLR
21
contents
An introduction to CLR How CLR
integration
The five types of CLR When to use CLR
use
How to start
a
664 666
objects
666 668
integration
Visual Studio to work with CLR SQL Server Database project
specify the type of CLR object How to enter and edit the code for CLR How to
The code for
a
How to create
CLR a
664
integration
works
objects
How to enable CLR
How to
integration
deploy
How to test and debug
a
670 670
672
objects
674 676
object
database for
How to compile and
objects
testing
CLR
CLR
objects
object
678 680 686
X\\l
xiv
Introduction
How to
Chapter 22
SQL to work with CLR
use
objects
688 688
How to
assembly deploy a CLR object
How
drop
688
How to
to
deploy
an
an
688
assembly
procedures, functions,
How to code CLR stored
and triggers How to work with CLR stored How to
use
the
SqlTypes
How to declare a stored
procedures
namespace to map data
types
How to
698
use
the
to return data
698
use
output parameters to return data
700
How to return How to raise A stored
A script
SqlPipe object
an
702
integer value
702
an error
procedure
that manages insert
that calls the stored
operations
710 710
How to declare a function How to work with the
704 708
procedure
How to work with CLR functions
SqlFunction
712
attribute
A scalar-valued function that returns an amount due
714
A table-valued function that returns a table with two columns
716
How to work with CLR
718
triggers
trigger SqlTrigger attribute..... A trigger that updates a column How to use the SqlTriggerContext object
718
A
726
How
to
declare
a
720
How to work with the
Chapter 23
694
696
procedure
How to create a connection How to
694
trigger
722 724
that works with DDL statements
How to code
aggregate functions
and user-defined types How to work with How to declare an
732
aggregate functions
732
aggregate
How to work with the
SqlUserDefinedAggregate
attribute
An aggregate that returns a comma-delimited
How to work with user-defined How to declare How
to
string
types
SqlUserDefinedType
type for
an
email address
that works with a user-defined
740
744 744
user-defined type
work with the
A user-defined
SQL
a
734 736
An aggregate that returns a trimmed average
type
attribute
746 748 754