Vous êtes sur la page 1sur 200

Microsoft

SL Server

2008
A IIGI^^II`S
G!III
About tbe Autbor
is a prolcssor in thc cpartmcnt ol Computcr Scicncc at thc
Polytcchnic in Roscnhcim, Gcrmany. Hc is thc author ol SQL Ser.er 7: Beginner`s
Guide, SQL Ser.er 2000: Beginner`s Guide, and Microsoft SQL Ser.er 2003:
Beginner`s Guide, and is a lrcqucnt contributor to SQL Ser.er Magazine.
About the 7echnicaI ditor
Todd Meister has bccn using Microsolt tcchnologics lor ovcr tcn ycars. Hc's bccn a
tcchnical cditor on ovcr 40 titlcs ranging lrom SQL Scrvcr to thc .NT Framcwork.
!n addition, hc is an assistant dircctor lor computing scrviccs at 8all Statc Univcrsity
in Muncic, !ndiana. Hc livcs with his wilc, Kimbcrly, and thcir lour childrcn in
ccntral !ndiana. Contact Todd at tmcistcrCsycamorcsolutions.com.
Copyright 2008 by The McGraw-Hill Companies. Click here for terms of use.
Microsoft

SL Server

2008
A IIGI^^II`S
G!III
Dusan Petkovi c
Ncw York Chicago San Francisco Lisbon
London Madrid Mcxico City Milan
Ncw clhi San Juan Scoul Singaporc
Sydncy Toronto
Copyright 2008 by The McGraw-Hill Companies. All rights reserved. ManuIactured in the United States oI America. Except as
permitted under the United States Copyright Act oI 1976, no part oI this publication may be reproduced or distributed in any Iorm or
by any means, or stored in a database or retrieval system, without the prior written permission oI the publisher.
0-07-154639-1
The material in this eBook also appears in the print version oI this title: 0-07-154638-3.
All trademarks are trademarks oI their respective owners. Rather than put a trademark symbol aIter every occurrence oI a trademarked
name, we use names in an editorial Iashion only, and to the beneIit oI the trademark owner, with no intention oI inIringement oI the
trademark. Where such designations appear in this book, they have been printed with initial caps.
McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or Ior use in corporate train-
ing programs. For more inIormation, please contact George Hoare, Special Sales, at georgehoaremcgraw-hill.com or (212) 904-
4069.
TERMS OF USE
This is a copyrighted work and The McGraw-Hill Companies, Inc. ('McGraw-Hill) and its licensors reserve all rights in and to the
work. Use oI this work is subject to these terms. Except as permitted under the Copyright Act oI 1976 and the right to store and retrieve
one copy oI the work, you may not decompile, disassemble, reverse engineer, reproduce, modiIy, create derivative works based upon,
transmit, distribute, disseminate, sell, publish or sublicense the work or any part oI it without McGraw-Hill`s prior consent. You may use
the work Ior your own noncommercial and personal use; any other use oI the work is strictly prohibited. Your right to use the work may
be terminated iI you Iail to comply with these terms.
THE WORK IS PROVIDED 'AS IS. McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS
TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK,
INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE,
AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED
WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not
warrant or guarantee that the Iunctions contained in the work will meet your requirements or that its operation will be uninterrupted or
error Iree. Neither McGraw-Hill nor its licensors shall be liable to you or anyone else Ior any inaccuracy, error or omission, regardless
oI cause, in the work or Ior any damages resulting thereIrom. McGraw-Hill has no responsibility Ior the content oI any inIormation
accessed through the work. Under no circumstances shall McGraw-Hill and/or its licensors be liable Ior any indirect, incidental,
special, punitive, consequential or similar damages that result Irom the use oI or inability to use the work, even iI any oI them has been
advised oI the possibility oI such damages. This limitation oI liability shall apply to any claim or cause whatsoever whether such claim
or cause arises in contract, tort or otherwise.
DOI: 10.1036/0071546383
We hope you enjoy this
McGraw-Hill eBook! If
you`d like more information about this book,
its author, or related books and websites,
please click here.
FrofessIoaaI
Want to learn more?
v
Contents at a CIance
|art | 8as|c Ceacepts aa6 |asta||at|ea
C|+jter ke|at|eaa| 0atahase !stems-ka |atre6cct|ea . . . . . . . . . . . . . . . . . 1
C|+jter z ||aaa|aq the |asta||at|ea aa6 |asta|||aq !0| !erer . . . . . . . . . . . . . . . . 11
C|+jter 1 !0| !erer Maaaqemeat !tc6|e . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
|art || Iraasact-!0| |aaqcaqe
C|+jter + !0| Cempeaeats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6I
C|+jter ! 0ata 0ef|a|t|ea |aaqcaqe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
C|+jter 0cer|es . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
C|+jter I Me6|f|cat|ea ef a Iah|e's Ceateats . . . . . . . . . . . . . . . . . . . . . . . . . . 199
C|+jter !tere6 |rece6cres aa6 0ser-0ef|ae6 |cact|eas . . . . . . . . . . . . . . . . . . 11
C|+jter !stem Cata|eq . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
C|+jter |a6|ces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1I
C|+jter V|ews . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1I9
C|+jter z !eccr|t !stem ef 0atahase laq|ae . . . . . . . . . . . . . . . . . . . . . . . . . 11
C|+jter 1 Ceaccrreac Ceatre| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
C|+jter + Ir|qqers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16I
|art ||| !0| !erer: !stem k6m|a|strat|ea
C|+jter ! !stem la|reameat ef 0atahase laq|ae . . . . . . . . . . . . . . . . . . . . . . 189
C|+jter Maaaq|aq |astaaces aa6 Ma|ata|a|aq 0atahases . . . . . . . . . . . . . . . . . 4
C|+jter I 8ackcp aa6 keceer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
C|+jter kctemat|aq !stem k6m|a|strat|ea Iasks . . . . . . . . . . . . . . . . . . . . . 44I
vi Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
C|+jter 0ata kep||cat|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
C|+jter z 0cer 0pt|m|ter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
C|+jter z |erfermaace Ica|aq . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1I
|art |V !0| !erer aa6 8cs|aess |ate|||qeace
C|+jter zz 8cs|aess |ate|||qeace-ka |atre6cct|ea . . . . . . . . . . . . . . . . . . . . . . 49
C|+jter z1 M|creseft kaa|s|s !er|ces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
C|+jter z+ 8cs|aess |ate|||qeace aa6 Iraasact-!0| . . . . . . . . . . . . . . . . . . . . . . . I9
C|+jter z! M|creseft kepert|aq !er|ces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
C|+jter z 0pt|m|t|aq Iecha|ces fer 8cs|aess |ate|||qeace . . . . . . . . . . . . . . . . . 611
|art V 8eea6 ke|at|eaa| 0ata
C|+jter zI 0er|ew ef KM| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
C|+jter z !0| !erer aa6 KM| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
C|+jter z |atre6cct|ea te !pat|a| 0ata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6I1
|a6ex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681
vii
Contents
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxl
|ntroductlon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxlll
|art | 8as|c Ceacepts aa6 |asta||at|ea
C|+jter ke|at|eaa| 0atahase !stems-ka |atre6cct|ea . . . . . . . . . . . . . . . . . . 1
|+t+|+.e :.tem.. |a 0.er.|ew . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +
\+r|et el J.er |aterl+ce. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !
||.|c+| |+t+ |aaejeaaeace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !
|e|c+| |+t+ |aaejeaaeace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !
9aer 0jt|m|t+t|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|+t+ |ater|t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Ceacarreac Ceatre| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|+c|aj +aa |ece.er . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I
|+t+|+.e :ecar|t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I
|e|+t|ea+| |+t+|+.e :.tem. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I
\er||a w|t| t|e |ee|'. :+mj|e |+t+|+.e . . . . . . . . . . . . . . . . . . . . . . . . . . .
:9|. | |e|+t|ea+| |+t+|+.e |+aa+e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|+t+|+.e |e.|a . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Nerm+| |erm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
|at|t-|e|+t|ea.||j Meae| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !
:at+\ Cea.eat|ea. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I
Ceac|a.|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|\erc|.e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
C|+jter z ||aaa|aq the |asta||at|ea aa6 |asta|||aq !0| !erer . . . . . . . . . . . . . . . . 11
||+aa|a t|e |a.t+||+t|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . zz
|arje.e el t|e :9| :er.er :.tem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . zz
|+raw+re +aa Netwer| |ea|remeat. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z1
:9| :er.er |a|t|ea. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z+
|a.t+||+t|ea |ecemmeaa+t|ea. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z!
For more information about this title, click here
vi i i Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
|a.t+|||a :9| :er.er . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . zI
|elere :t+rt|a t|e :etaj |rer+m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z
:t+rt|a t|e :etaj |rer+m +aa |a.t+|||a |rerea|.|te. . . . . . . . . . . . . . . . . . . . z
|a.t+|||a :9| :er.er Cemjeaeat. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z
:t+rt|a +aa :tejj|a +a |a.t+ace el |+t+|+.e |a|ae . . . . . . . . . . . . . . . . . . . . . . . . . 1I
|ea|c+tea Ceaaect|ea te +a |a.t+ace el |+t+|+.e |a|ae . . . . . . . . . . . . . . . . . . . . . . . . 1
Ceac|a.|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
C|+jter 1 !0| !erer Maaaqemeat !tc6|e . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
:9| :er.er |rer+m 0reaj +aa |ee|. 0a||ae . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +
|atreaact|ea te :9| :er.er M+a+emeat :taa|e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +
Ceaaect|a te + :er.er . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +z
|e|.terea :er.er. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ++
0|ject |\j|erer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ++
0r+a|t|a +aa N+.|+t|a M+a+emeat :taa|e'. |+ae. . . . . . . . . . . . . . . . . . . . +!
J.|a M+a+emeat :taa|e w|t| |+t+|+.e |a|ae . . . . . . . . . . . . . . . . . . . . . . . . . . . . +
|am|a|.ter|a |+t+|+.e :er.er. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +
M+a+|a |+t+|+.e. J.|a 0|ject |\j|erer . . . . . . . . . . . . . . . . . . . . . . . . . . . +
|at|er|a |ct|.|t|e. J.|a :9| :er.er M+a+emeat :taa|e . . . . . . . . . . . . . . . . . . !I
Ceac|a.|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
|\erc|.e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
|art || Iraasact-!0| |aaqcaqe
C|+jter + !0| Cempeaeats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6I
:9|'. |+.|c 0|ject. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
||ter+| \+|ae. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|e||m|ter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Cemmeat. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I
|aeat|l|er. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I
|e.er.ea |ewera. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I
|+t+ Ije. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I
Namer|c |+t+ Ije. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I
C|+r+cter |+t+ Ije. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Iz
Iemjer+| |+t+ Ije. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Iz
M|.ce||+aeea. |+t+ Ije. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I+
||C|M|| w|t| \||||C|M|| :ter+e |erm+t . . . . . . . . . . . . . . . . . . . . . . . . . . I
Cont ent s i x
Ir+a.+ct-:9| |aact|ea. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I
|re+te |aact|ea. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I
:c+|+r |aact|ea. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
:c+|+r 0jer+ter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I
0|e|+| \+r|+||e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I
NJ|| \+|ae. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Ceac|a.|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|\erc|.e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
C|+jter ! 0ata 0ef|a|t|ea |aaqcaqe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Cre+t|a |+t+|+.e 0|ject. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z
Cre+t|ea el + |+t+|+.e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z
C|||I| I||||. | |+.|c |erm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I
C|||I| I|||| +aa |ec|+r+t|.e |ater|t Cea.tr+|at. . . . . . . . . . . . . . . . . . . . . . .
|elereat|+| |ater|t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !
Cre+t|a 0t|er |+t+|+.e 0|ject. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|ater|t Cea.tr+|at. +aa |em+|a. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Mea|l|a |+t+|+.e 0|ject. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
||ter|a + |+t+|+.e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +
||ter|a + I+||e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|eme.|a |+t+|+.e 0|ject. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z
Ceac|a.|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z
|\erc|.e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . zz
C|+jter 0cer|es . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
:|||CI :t+temeat. | |+.|c |erm +aa t|e \|||| C|+a.e . . . . . . . . . . . . . . . . . . . . . . . . z
\|||| C|+a.e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . zI
:a|aer|e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +z
:a|aer|e. +aa Cemj+r|.ea 0jer+ter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +1
:a|aer|e. +aa t|e |N 0jer+ter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ++
:a|aer|e. +aa |N\ +aa ||| 0jer+ter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +!
:|||CI :t+temeat. 0t|er C|+a.e. +aa |aact|ea. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +I
0|0J| |\ C|+a.e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +I
|re+te |aact|ea. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +
||\|N0 C|+a.e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !!
0|||| |\ C|+a.e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !
:|||CI :t+temeat +aa |||NI|I\ |rejert . . . . . . . . . . . . . . . . . . . . . . . . . . . . !
:et 0jer+ter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
x Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
C|:| |\jre..|ea. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +
C0M|JI| C|+a.e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Iemjer+r I+||e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Ie|a 0jer+ter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Iwe :at+\ |erm. te |mj|emeat Ie|a. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I
N+tar+| Ie|a . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I
C+rte.|+a |reaact . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . II
0ater Ie|a . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . II
|art|er |erm. el Ie|a 0jer+t|ea. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Cerre|+tea :a|aer|e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
:a|aer|e. +aa t|e |/|:I: |aact|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +
:|ea|a \ea J.e Ie|a. er :a|aer|e.! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !
I+||e |\jre..|ea. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|er|.ea I+||e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I
Cemmea I+||e |\jre..|ea. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Ceac|a.|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !
|\erc|.e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !
C|+jter I Me6|f|cat|ea ef a Iah|e's Ceateats . . . . . . . . . . . . . . . . . . . . . . . . . . 199
|N:||I :t+temeat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z
|a.ert|a + :|a|e |ew . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z
|a.ert|a Ma|t|j|e |ew. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z1
I+||e \+|ae Cea.tracter. +aa |N:||I . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z!
J|||I| :t+temeat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z!
||||I| :t+temeat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z
I|JNC|I| I|||| :t+temeat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z
I|e 0JI|JI C|+a.e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z
M||0| :t+temeat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z
Ceac|a.|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z1
|\erc|.e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z1
C|+jter !tere6 |rece6cres aa6 0ser-0ef|ae6 |cact|eas . . . . . . . . . . . . . . . . . . . 11
|receaar+| |\tea.|ea. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z
||ec| el :t+temeat. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z
|| :t+temeat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . zI
\|||| :t+temeat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z
|ec+| \+r|+||e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z
M|.ce||+aeea. |receaar+| :t+temeat. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . zz
|+aa||a |.eat. w|t| I|\ +aa C|IC| :t+temeat. . . . . . . . . . . . . . . . . . . . . . . . zz
Cont ent s xi
:terea |receaare. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . zzz
Cre+t|ea +aa |\ecat|ea el :terea |receaare. . . . . . . . . . . . . . . . . . . . . . . . . . . zz1
:terea |receaare. +aa C|| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . zz
J.er-|el|aea |aact|ea. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z11
Cre+t|ea +aa |\ecat|ea el J.er-|el|aea |aact|ea. . . . . . . . . . . . . . . . . . . . . . . . z11
J.er-|el|aea |aact|ea. +aa C|| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z+
Ceac|a.|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z+z
|\erc|.e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z+z
C|+jter !stem Cata|eq . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
|atreaact|ea te t|e :.tem C+t+|e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z++
0eaer+| |aterl+ce. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z+
C+t+|e \|ew. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z+
|a+m|c M+a+emeat \|ew. +aa |aact|ea. . . . . . . . . . . . . . . . . . . . . . . . . . . z+
|alerm+t|ea :c|em+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z!
|rejr|et+r |aterl+ce. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z!z
:.tem |receaare. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z!z
:.tem |aact|ea. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z!1
|rejert |aact|ea. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z!+
Ceac|a.|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z!!
|\erc|.e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z!!
C|+jter |a6|ces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1I
|atreaact|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z!
C|a.terea |aa|ce. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z
Neac|a.terea |aa|ce. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z
Ir+a.+ct-:9| +aa |aa|ce. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . zz
Cre+t|a |aa|ce. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . zz
0|t+|a|a |aae\ |r+meat+t|ea |alerm+t|ea . . . . . . . . . . . . . . . . . . . . . . . . . . z
|a|t|a |aae\ |alerm+t|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . zI
||ter|a |aa|ce. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z
|eme.|a +aa |ea+m|a |aa|ce. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . zI
0a|ae||ae. ler Cre+t|a +aa J.|a |aa|ce. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . zI
|aa|ce. +aa Ceaa|t|ea. |a t|e \|||| C|+a.e . . . . . . . . . . . . . . . . . . . . . . . . . . zI
|aa|ce. +aa Ie|a 0jer+ter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . zIz
Ce.er|a |aae\ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . zIz
|aa|ce. ea Cemjatea Ce|ama. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . zI1
\|rta+| Cemjatea Ce|ama. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . zI+
|er.|.teat Cemjatea Ce|ama. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . zI+
xi i Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Ceac|a.|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . zI!
|\erc|.e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . zI
C|+jter V|ews . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1I9
||| :t+temeat. +aa \|ew. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z
Cre+t|a + \|ew . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z
||ter|a +aa |eme.|a \|ew. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z+
|a|t|a |alerm+t|ea Ceacera|a \|ew. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z!
|M| :t+temeat. +aa \|ew. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z
\|ew |etr|e.+| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z
|N:||I :t+temeat +aa + \|ew . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . zI
J|||I| :t+temeat +aa + \|ew . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z
||||I| :t+temeat +aa + \|ew . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . zz
|aae\ea \|ew. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z1
Cre+t|a +a |aae\ea \|ew . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z1
Mea|l|a t|e :tractare el +a |aae\ea \|ew . . . . . . . . . . . . . . . . . . . . . . . . . . z!
|a|t|a |alerm+t|ea Ceacera|a |aae\ea \|ew. . . . . . . . . . . . . . . . . . . . . . . . . . z
|eael|t. el |aae\ea \|ew. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . zI
Ceac|a.|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z
|\erc|.e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z
C|+jter z !eccr|t !stem ef 0atahase laq|ae . . . . . . . . . . . . . . . . . . . . . . . . . 11
|at|eat|c+t|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
|mj|emeat|a +a |at|eat|c+t|ea Meae . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1+
|acrjt|a |+t+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1+
:ett|a Jj :.tem :ecar|t J.|a ||| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
M+a+|a |e|a. J.|a M+a+emeat :taa|e . . . . . . . . . . . . . . . . . . . . . . . . . . 1
:c|em+. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1z
J.er-:c|em+ :ej+r+t|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1z
||| :c|em+-|e|+tea :t+temeat. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
|+t+|+.e :ecar|t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1!
:ett|a Jj |+t+|+.e J.er |cceaat. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
|el+a|t |+t+|+.e :c|em+. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
|e|e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
||\ea :er.er |e|e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
||\ea |+t+|+.e |e|e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1z
|jj||c+t|ea |e|e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1zz
J.er-|el|aea |+t+|+.e |e|e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1z+
Cont ent s xi i i
|at|er|t+t|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1z
0||NI :t+temeat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1zI
||N\ :t+temeat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
||\0|| :t+temeat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11z
M+a+|a |erm|..|ea. J.|a M+a+emeat :taa|e . . . . . . . . . . . . . . . . . . . . . . . 111
Ir+c||a C|+ae. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11+
|+t+ :ecar|t +aa \|ew. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Ceac|a.|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
|\erc|.e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1+
C|+jter 1 Ceaccrreac Ceatre| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Ceacarreac Meae|. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1++
Ir+a.+ct|ea. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1+!
|rejert|e. el Ir+a.+ct|ea. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1+
Ir+a.+ct-:9| :t+temeat. +aa Ir+a.+ct|ea. . . . . . . . . . . . . . . . . . . . . . . . . . . . 1+I
Ir+a.+ct|ea |e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1!
|ec||a . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1!
|ec| Meae. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1!z
|ec| 0r+aa|+r|t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1!+
|ec| |.c+|+t|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1!!
|llect|a |ec|. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1!
||.j|+|a |ec| |alerm+t|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1!I
|e+a|ec| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1!
|.e|+t|ea |e.e|. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1!
Ceacarreac |re||em. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1!
|+t+|+.e |a|ae +aa |.e|+t|ea |e.e|. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
|ew \er.|ea|a . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1z
|||| C0MM|II|| :N||:|0I |.e|+t|ea |e.e| . . . . . . . . . . . . . . . . . . . . . . . . . . 11
:N||:|0I |.e|+t|ea |e.e| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1+
Ceac|a.|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1+
|\erc|.e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1!
C|+jter + Ir|qqers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16I
|atreaact|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Cre+t|a + |M| Ir|er . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Mea|l|a + Ir|er'. :tractare . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
J.|a |e|etea +aa |a.ertea I+||e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1I
xi v Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
|jj||c+t|ea |re+. ler |M| Ir|er. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1I
||I|| Ir|er. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1I
|N:I||| 0| Ir|er. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1I!
||r.t +aa |+.t Ir|er. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1I
||| Ir|er. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1I
|+t+|+.e-|e.e| Ir|er. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1I
:er.er-|e.e| Ir|er. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1I
Ir|er. +aa C|| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Ceac|a.|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1!
|\erc|.e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1!
|art ||| !0| !erer: !stem k6m|a|strat|ea
C|+jter ! !stem la|reameat ef 0atahase laq|ae . . . . . . . . . . . . . . . . . . . . . . 189
:.tem |+t+|+.e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
m+.ter |+t+|+.e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
meae| |+t+|+.e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
temja| |+t+|+.e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
m.a| |+t+|+.e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1z
||.| :ter+e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1z
|rejert|e. el |+t+ |+e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Ije. el |+t+ |+e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
|+r+||e| |rece..|a el I+.|. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Jt|||t|e. +aa t|e ||CC Cemm+aa . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
|cj Jt|||t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
.|cma Jt|||t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +
||CC Cemm+aa . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +z
Ceac|a.|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ++
|\erc|.e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ++
C|+jter Maaaq|aq |astaaces aa6 Ma|ata|a|aq 0atahases . . . . . . . . . . . . . . . . . 4
|ec|+r+t|.e M+a+emeat |r+mewer| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +
|e Ierm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +
J.|a |M| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +
M+|atea+ace ||+a \|t+ra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +
Ceac|a.|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +
|\erc|.e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +
Cont ent s x v
C|+jter I 8ackcp aa6 keceer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
|+c|aj Met|ea. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +!
|a|| |+t+|+.e |+c|aj . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +!
||llereat|+| |+c|aj . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +!
Ir+a.+ct|ea |e |+c|aj . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +!
|||e er |||ereaj |+c|aj . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +I
|erlerm|a |+c|aj . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +I
|+c||a Jj J.|a Ir+a.+ct-:9| :t+temeat. . . . . . . . . . . . . . . . . . . . . . . . . . . . +
|+c||a Jj J.|a M+a+emeat :taa|e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +z
|eterm|a|a \||c| |+t+|+.e. te |+c| Jj . . . . . . . . . . . . . . . . . . . . . . . . . . . . +z+
|erlerm|a |+t+|+.e |ece.er . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +z
|atem+t|c |ece.er . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +z
M+aa+| |ece.er . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +z
|ece.er Meae|. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +1!
||| |.+||+||||t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +1
J.|a + :t+aa| :er.er . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +1
J.|a |||| Iec|ae|e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ++
|+||e.er C|a.ter|a . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ++
|+t+|+.e M|rrer|a . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ++z
|e :||jj|a . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ++1
Ceac|a.|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +++
|\erc|.e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ++!
C|+jter kctemat|aq !stem k6m|a|strat|ea Iasks . . . . . . . . . . . . . . . . . . . . . . 44I
|aaa|a +aa Ceal|ar|a :9| :er.er |eat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ++
Cre+t|a Ie|. +aa 0jer+ter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +!
Cre+t|a + Ie| +aa |t. :tej. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +!
Cre+t|a + Ie| :c|eaa|e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +!1
Net|l|a 0jer+ter. +|eat t|e Ie| :t+ta. . . . . . . . . . . . . . . . . . . . . . . . . . . . . +!+
\|ew|a t|e Ie| ||.ter |e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +!!
||ert. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +!I
|rrer Me..+e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +!I
:9| :er.er |eat |rrer |e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +!
\|aaew. |jj||c+t|ea |e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +!
|el|a|a ||ert. te |+aa|e |rrer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +!
Ceac|a.|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +1
|\erc|.e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ++
x vi Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
C|+jter 0ata kep||cat|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
||.tr||atea |+t+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +
Met|ea. ler ||.tr||at|a |+t+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +
C|ee.|a + |+t+ ||.tr||at|ea Met|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +I
|ej||c+t|ea|a 0.er.|ew . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +
|a|||.|er., ||.tr||ater., +aa :a|.cr||er. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +
|a|||c+t|ea. +aa |rt|c|e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +I
|ej||c+t|ea Ije. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +I
|ej||c+t|ea Meae|. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +I
M+a+|a |ej||c+t|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +I
Ceal|ar|a t|e ||.tr||at|ea +aa |a|||c+t|ea :er.er. . . . . . . . . . . . . . . . . . . . . . +I
:ett|a Jj |a|||c+t|ea. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +
Ceal|ar|a :a|.cr|jt|ea :er.er. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +
Ceac|a.|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +1
|\erc|.e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +1
C|+jter z 0cer 0pt|m|ter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
||+.e. el 9aer |rece..|a . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +
|ew 9aer 0jt|m|t+t|ea \er|. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +I
9aer |a+|.|. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +
|aae\ :e|ect|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +
Ie|a 0raer :e|ect|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +z
Ie|a |rece..|a Iec|a|ae. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +z
Iee|. ler |a|t|a t|e 0jt|m|ter :tr+te . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ++
:|I :t+temeat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +!
M+a+emeat :taa|e +aa 0r+j||c+| |\ecat|ea ||+a. . . . . . . . . . . . . . . . . . . . . . . +
|\+mj|e. el |\ecat|ea ||+a. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !
|a+m|c M+a+emeat \|ew. +aa 9aer 0jt|m|ter . . . . . . . . . . . . . . . . . . . . . . !+
0jt|m|t+t|ea ||at. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !I
\| te J.e 0jt|m|t+t|ea ||at. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !I
Ije. el 0jt|m|t+t|ea ||at. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !
Ceac|a.|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !!
C|+jter z |erfermaace Ica|aq . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1I
|+cter. t|+t |llect |erlerm+ace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !
|+t+|+.e |jj||c+t|ea. +aa |erlerm+ace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !
|+t+|+.e |a|ae +aa |erlerm+ace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !z
:.tem |e.earce. +aa |erlerm+ace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !zz
Cont ent s x vi i
Mea|ter|a |erlerm+ace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !z
|erlerm+ace Mea|ter. |a 0.er.|ew . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !z
Mea|ter|a t|e C|J . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !z
Mea|ter|a Memer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !z
Mea|ter|a t|e ||.| :.tem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !1
Mea|ter|a t|e Netwer| |aterl+ce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !1z
C|ee.|a t|e |||t Iee| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !11
:9| :er.er |rel||er . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !1+
|+t+|+.e |a|ae Iaa|a |a.|.er . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !1!
:9| :er.er z |erlerm+ace Iee|. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !+z
|erlerm+ace |+t+ Ce||ecter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !+z
|e.earce 0e.eraer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !+1
Ceac|a.|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !+!
|\erc|.e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !+!
|art |V !0| !erer aa6 8cs|aess |ate|||qeace
C|+jter zz 8cs|aess |ate|||qeace-ka |atre6cct|ea . . . . . . . . . . . . . . . . . . . . . . . 49
0a||ae Ir+a.+ct|ea |rece..|a ... |a.|ae.. |ate|||eace . . . . . . . . . . . . . . . . . . . . . . . . . !!
0a||ae Ir+a.+ct|ea |rece..|a . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !!
|a.|ae.. |ate|||eace :.tem. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !!
|+t+ \+re|ea.e. +aa |+t+ M+rt. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !!z
|+t+ \+re|ea.e |e.|a J.|a ||mea.|ea+| Meae| . . . . . . . . . . . . . . . . . . . . . . . . . . . !!+
Ca|e. +aa I|e|r |rc||tectare. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !!
|re+t|ea. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !!
|ew Mac| te |re+te! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !!
||.|c+| :ter+e el + Ca|e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !
|+t+ |cce.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !z
Ceac|a.|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !1
|\erc|.e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !1
C|+jter z1 M|creseft kaa|s|s !er|ces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
|| |e.e|ejmeat :taa|e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !
Cre+t|a + New |reject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !I
Cre+t|a + |+t+ :earce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !
Cre+t|a + |+t+ :earce \|ew . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !
x vi i i Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
|a+|.|. :er.|ce. +aa Ca|e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !I
Cre+t|a + Ca|e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !I1
|e.|a|a :ter+e |re+t|ea +aa |rece..|a t|e Ca|e . . . . . . . . . . . . . . . . . . . !I1
|rew.|a + Ca|e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !I
Ceac|a.|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !II
C|+jter z+ 8cs|aess |ate|||qeace aa6 Iraasact-!0| . . . . . . . . . . . . . . . . . . . . . . . I9
|atreaact|ea te :9|/0||| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !
\|aaew Cea.tract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !
|\tea.|ea. el 0|0J| |\ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !I
CJ|| 0jer+ter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !I
|0||J| 0jer+ter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !
0|0J||N0 |aact|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !
0reaj|a :|I: 0jer+ter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !
0||| 9aer |aact|ea. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !1
|+a||a |aact|ea. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !1
:t+t|.t|c+| |re+te |aact|ea. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !
Nea.t+aa+ra |a+|t|c |aact|ea. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !I
I0| C|+a.e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !I
NI||| |aact|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
||\0I +aa JN||\0I 0jer+ter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Ceac|a.|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
|\erc|.e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
C|+jter z! M|creseft kepert|aq !er|ces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
|atreaact|ea te M|cre.elt |ejert|a :er.|ce. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|ejert M+a+er . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|ejert :er.er . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I
|ejert C+t+|e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Cre+t|a |ejert. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Cre+t|a |ejert. w|t| t|e |ejert :er.er |reject \|t+ra . . . . . . . . . . . . . . . . . . . .
Cre+t|a |+r+meter|tea |ejert. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I
|rece..|a +aa M+a+|a |ejert. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|cce..|a +aa |e||.er|a |ejert. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
0a-|em+aa |ejert. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|ejert :a|.cr|jt|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z
Ceac|a.|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z
|\erc|.e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . zz
Cont ent s xi x
C|+jter z 0pt|m|t|aq Iecha|ces fer 8cs|aess |ate|||qeace . . . . . . . . . . . . . . . . . . 611
|+t+ |+rt|t|ea|a . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z+
\+. te |+rt|t|ea \ear |+t+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z+
:tej. ler Cre+t|a |+rt|t|eaea I+||e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z!
Ce||ec+t|a I+||e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
:9| :er.er z +aa |+t+ |+rt|t|ea|a . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1z
0a|ae||ae. ler |+rt|t|ea|a I+||e. +aa |aa|ce. . . . . . . . . . . . . . . . . . . . . . . . . . . 1+
:t+r :c|em+ 9aer 0jt|m|t+t|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1+
Ceac|a.|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
|art V 8eea6 ke|at|eaa| 0ata
C|+jter zI 0er|ew ef KM| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
\er|a \|ae \e| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +z
/M|-|e|+tea |+aa+e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +1
/M||+.|c Ceacejt. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +1
/M| |ttr||ate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +
/M| N+me.j+ce. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +I
|ecameat Ije |el|a|t|ea. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +
/M| :c|em+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !
Ceac|a.|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !
C|+jter z !0| !erer aa6 KM| . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Met|ea. el :ter|a /M| |ecameat. |a |e|+t|ea+| |+t+|+.e. . . . . . . . . . . . . . . . . . . . . . !z
:ter|a /M| |ecameat. |a :9| :er.er . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !1
:ter|a /M| |ecameat. J.|a t|e /M| |+t+ Ije . . . . . . . . . . . . . . . . . . . . . . . !+
:ter|a /M| |ecameat. J.|a |ecemje.|t|ea . . . . . . . . . . . . . . . . . . . . . . . . .
|etr|e.|a :terea /M| |ecameat. +aa |r+meat. . . . . . . . . . . . . . . . . . . . . . . . . . . .
|re.eat|a |e|+t|ea+| |+t+ +. /M| |ecameat. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . z
||\ Meae . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
|JI0 Meae . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . +
|/|||C|I Meae . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . !
||I| Meae . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
||rect|.e. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I
:9| :er.er /9aer Met|ea. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
:9| :er.er z +aa /M| |a|+acemeat. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I
Ceac|a.|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I
xx Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
C|+jter z |atre6cct|ea te !pat|a| 0ata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6I1
|re.eat|a :j+t|+| |+t+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I+
0eeaet|c :j+t|+| Meae|. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I+
||+t :j+t|+| Meae|. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I!
:j+t|+| |+t+ Ije. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I!
0|0M|I|\ |+t+ Ije . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . I!
||llereace. |etweea t|e 0|0M|I|\ +aa 0|00||||\ |+t+ Ije. . . . . . . . . . . . . . .
Ceac|a.|ea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|a6ex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681
xxi
AcknowIedgments
I
irst, ! would likc to thank my sponsoring cditor, Vcndy Rinaldi. Sincc
1998, Vcndy has bccn in chargc ol all lour books that ! havc publishcd with
McGrawHill. ! apprcciatc vcry much hcr cxtraordinary support ovcr all thcsc
ycars. Also, ! would likc to acknowlcdgc thc important contributions ol Mandy Canalcs
and Todd Mcistcr.
Copyright 2008 by The McGraw-Hill Companies. Click here for terms of use.
This page intentionally left blank
xxiii
Introduction
T
hcrc arc a couplc ol rcasons why SQL Scrvcr, thc systcm that compriscs
atabasc nginc, Analysis Scrviccs, Rcporting Scrviccs and !ntcgration
Scrviccs is thc bcst choicc lor a broad spcctrum ol cnd uscrs and databasc
programmcrs building busincss applications:
SQL Scrvcr is ccrtainly thc bcst systcm lor Vindows opcrating systcms, bccausc
ol its tight intcgration (and low pricing). 8ccausc thc numbcr ol installcd
Vindows systcms is cnormous and still incrcasing rapidly, SQL Scrvcr is a widcly
uscd systcm.
atabasc nginc, as thc rclational databasc systcm componcnt, is thc casicst
databasc systcm to usc. !n addition to thc wcllknown uscr intcrlacc, Microsolt
applications, and managc systcm administration tasks.
Gcncrally, SQL Scrvcr isn't only a databasc systcm. !t is a platlorm that not
only managcs structurcd, scmistructurcd, and unstructurcd data but also ollcrs
comprchcnsivc, intcgratcd opcrational and analysis soltwarc that cnablcs organizations
to rcliably managc missioncritical inlormation.
CoaIs of tbe ook
Microsoft SQL Ser.er 2008: Beginner`s Guide lollows thrcc prcvious cditions that covcrcd
SQL Scrvcr 7, 2000, and 2005.
Gcncrally, all SQL Scrvcr uscrs who want to gct a good undcrstanding ol this
databasc systcm and to work succcsslully with it will lind this book vcry hclplul. (!l you
arc a ncw SQL Scrvcr uscr but undcrstand SQL, rcad thc scction "illcrcnccs 8ctwccn
SQL and TransactSQL Syntax" latcr in this introduction.)
This book addrcsscs uscrs ol all componcnts ol thc SQL Scrvcr systcm. For this
rcason, it is dividcd into scvcral parts: uscrs who want to lcarn morc about thc rclational
databasc componcnt callcd atabasc nginc will lind thc lirst thrcc parts ol thc book
thc most usclul. Thc lourth part ol thc book is dcdicatcd to busincss intclligcncc uscrs
who usc cithcr Analysis Scrviccs or rclational cxtcnsions conccrning 8!. Thc last part ol
thc book providcs insight lor uscrs who want to usc XML data and/or spatial data.
Copyright 2008 by The McGraw-Hill Companies. Click here for terms of use.
xxi v Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
SL Server 2008 New Features Described in tbe ook
SQL Scrvcr 2008 has a lot ol ncw lcaturcs, and almost all ol thcm arc discusscd in
this book. For cach lcaturc, at lcast onc running cxamplc is providcd to cnablc you to
undcrstand that lcaturc bcttcr. Thc lollowing tablc lists thc chaptcrs that dcscribc ncw
lcaturcs and providcs a bricl summary ol thc ncw lcaturcs introduccd in cach chaptcr.
C|+jter 1 I|e |ate|||:ea.e laact|ea+||t el :9| :er.er M+a+emeat :taa|e'. 9aer |a|ter |. ae.cr||ea |a t||. c|+jter.
|ate|||:ea.e jre.|ae. + ||.t el ejt|ea. t|+t m+|e |+aa+e relereace. e+.|| +cce..|||e. |a :9| :er.er z,
lreaeat| a.ea Ir+a.+ct-:9| e|emeat. +re .ajjertea.
C|+jter + I||. c|+jter ae.cr||e. .e.er+| aew le+tare., t|e me.t |mjert+at el w||c| |. t|e .ajjert ler t|e aew
temjer+| a+t+ tje. ||I|, I|M|, ||I|I|M|z, +aa ||I|I|M|0||:|I. Iwe et|er aew a+t+ tje., ||||:I|||M
+aa ||||||C|\||, +re +|.e e\j|+|aea. I|e \||||C|M|| .ter+e lerm+t |. a|.ca..ea |a aet+||, tee. (I||.
.ter+e lerm+t w+. |atreaacea w|t| :er.|ce |+c| z el :9| :er.er z!.}
C|+jter ! I|e cre+t|ea el a.er-ael|aea t+||e tje. |. .|ewa |a t||. c|+jter. I|e|r jr+ct|c+| a.e |a re|+t|ea te t+||e-
.+|aea j+r+meter. c+a |e leaaa |a C|+jter .
C|+jter |a t||. c|+jter ea c+a l|aa t|e .at+\ +aa e\+mj|e. ler t|e ea|+acea lerm el t|e 0|0J| |\ c|+a.e.
0|0J| |\(}.
C|+jter I I||. c|+jter ae.cr||e. t|e M||0| .t+temeat +. we|| +. t|e rew-.+|ae cea.tracter el t|e |N:||I .t+temeat.
I|e M||0| .t+temeat cem||ae. t|e .eaeace el ceaa|t|ea+| |N:||I +aa J|||I| cemm+aa. |a + .|a|e
+tem|c .t+temeat, aejeaa|a ea t|e e\|.teace el + recera. I|e rew-.+|ae cea.tracter +||ew. ea te .|mj||l
t|e w+ ea wr|te |N:||I .t+temeat..
C|+jter I+||e-.+|aea j+r+meter., w||c| +||ew ea te .|mj||l t|e t+.| el .eaa|a m+a j+r+meter. te + reat|ae, +re
ae.cr||ea |a t||. c|+jter.
C|+jter z I||. c|+jter ae.cr||e. |\tea.|||e |e M+a+emeat +aa Ir+a.j+reat |+t+ |acrjt|ea, w||c| ea|+ace t|e
a+t+ eacrjt|ea c+j+||||t|e. el t|e jre.|ea. .er.|ea. ||.e, t|e aew tr+c||a mec|+a|.m c+||ea C|C (c|+ae
a+t+ c+jtare} |. |atreaacea +aa |t. jr+ct|c+| a.e |. .|ewa.
C|+jter 1 |a|+acemeat. |a re|+t|ea te |ec| e.c+|+t|ea +re ae.cr||ea |a t||. c|+jter.
C|+jter I||. c|+jter ae.cr||e. twe tee|., el w||c| |ec|+r+t|.e M+a+emeat |r+mewer| (|M|} |. aew. |M| |. +
lr+mewer| ler m+a+|a |a.t+ace. el |+t+|+.e |a|ae. |t +||ew. ea te cre+te je||c|e., w||c| +re t|ea .+||a
ler t|e w|e|e |a.t+ace er j+rt el |t.
C|+jter I I||. c|+jter ae.cr||e. |ew ea c+a cemjre.. t|e ceateat el + |+c|aj, +aa |atreaace. .e.er+| ea|+acemeat.
re|+tea te a+t+|+.e m|rrer|a.
C|+jter I|e jeer-te-jeer tr+a.+ct|ea+| rej||c+t|ea .ajjert. t|e aew lerm el ceal||ct aetect|ea, w||c| |. e\j|+|aea
|a t||. c|+jter.
C|+jter z I|e aew ||at c+||ea |0|C|:||| +aa t|e aew aa+m|c m+a+emeat .|ew (...am_e\ec_jreceaare_.t+t.}
+re ae.cr||ea |ere. ||.e, t|e aew lerm el a.er-lr|eaa| a|.j|+ ler e\ecat|ea j|+a. |. .|ewa.
C|+jter z I||. c|+jter a|.ca..e., +mea et|er t||a., twe aew +aa .er |mjert+at jerlerm+ace tee|., |erlerm+ace
|+t+ Ce||ect|ea +aa |e.earce 0e.eraer.
| nt r oduc t i on xxv
C|+jter z+ :9| :er.er z |mj|emeat. t|e |0||J| +aa CJ|| laact|ea. w|t| .t+aa+ra|tea .at+\, w||c| +re ae.cr||ea
|ere. (|et| t|e laact|ea. e\|.tea |a t|e jre.|ea. .er.|ea., |at |+.e .ajjertea ea| t|e jrejr|et+r .at+\.}
I|e c|+jter +|.e a|.ca..e. + aew laact|ea c+||ea 0|0J||N0 :|I:.
C|+jter z I|e l|r.t j+rt el t||. c|+jter ae.cr||e. j+r+||e||.m ler a+t+ j+rt|t|ea|a. I|e .eceaa j+rt el t||. c|+jter |.
eat|re| aew +aa ae.cr||e. .t+r .c|em+ aer ejt|m|t+t|ea.
C|+jter z |t t|e eaa el t||. c|+jter, t|e m|aer ea|+acemeat. |a re|+t|ea te /M| .c|em+ +aa t|e /M| a+t+ tje
+re ae.cr||ea.
C|+jter z I||. |. +a eat|re| aew c|+jter ae.cr|||a .j+t|+| a+t+ +aa twe aew a+t+ tje., 0|0M|I|\ +aa 0|00||||\.
Drganization of tbe ook
Thc book has 29 chaptcrs and is dividcd into livc parts.
Part !, "8asic Conccpts and !nstallation," dcscribcs thc notion ol databasc systcms in
gcncral and atabasc nginc (SQL Scrvcr's rclational databasc systcm) in particular, as
wcll as thc installation proccss. !t includcs thc lollowing chaptcrs:
Chaptcr 1, "Rclational atabasc SystcmsAn !ntroduction," discusscs databascs
thc sample
convcntions that arc uscd in thc rcst ol thc book.
Chaptcr 2, "Planning thc !nstallation and !nstalling SQL Scrvcr," dcscribcs thc
thc installation ol SQL Scrvcr is a straightlorward task, thcrc arc ccrtain stcps
that warrant cxplanation.
Chaptcr 3, "SQL Scrvcr Managcmcnt Studio," dcscribcs thc componcnt callcd
in casc you want to crcatc databasc objccts and qucry data without knowlcdgc ol
SQL.
Part !!, "TransactSQL Languagc," is intcndcd lor cnd uscrs and application
programmcrs ol atabasc nginc. !t compriscs thc lollowing chaptcrs:
Chaptcr 4, "SQL Componcnts," dcscribcs thc lundamcntals ol thc most important
part ol a rclational databasc systcm: a databasc languagc. For all such systcms,
thcrc is only onc languagc that counts: SQL. !n this chaptcr, all componcnts ol
SQL Scrvcr's own databasc languagc, callcd TransactSQL, arc dcscribcd. You can
lunctions and opcrators ol TransactSQL arc dcscribcd.
xxv i Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de

contains all lorms ol thc ALTR statcmcnt, which is uscd to modily thc structurc
Chaptcr 6, "Qucrics," discusscs thc most important TransactSQL statcmcnt:
and cxplaincd with rclcrcncc to thc sample databasc.

SQL statcmcnts uscd lor updating data: !NSRT, UPAT, LT, and
MRG. ach ol thcsc statcmcnts is cxplaincd through numcrous cxamplcs.

cxtcnsions, which can bc uscd to crcatc powcrlul programs callcd storcd proccdurcs
can bc rcuscd. 8ccausc TransactSQL is a complctc computational languagc, all
proccdural cxtcnsions arc inscparablc parts ol thc languagc. Somc storcd proccdurcs
arc writtcn by uscrs, othcrs arc providcd by Microsolt and arc rclcrrcd to as systcm
Common Languagc Runtimc (CLR) is also discusscd in this chaptcr.
Chaptcr 9, "Systcm Catalog," dcscribcs onc ol thc most important parts ol a
charactcristic ol systcm tablcs ol atabasc nginc is that thcy cannot bc acccsscd
dircctly. atabasc nginc supports scvcral intcrlaccs that you can usc to qucry thc
systcm catalog.

(cspccially databasc application programmcrs) can usc to tunc thcir applications
dcscribcs thc rolc ol indiccs and givcs you guidclincs lor how to crcatc and usc
atabasc nginc.
Chaptcr 11, "\icws," cxplains how you crcatc vicws, discusscs thc practical usc
ol vicws (using numcrous cxamplcs), and cxplains a spccial lorm ol vicws callcd
indcxcd vicws.
| nt r oduc t i on xxv i i
Chaptcr 12, "Sccurity Systcm ol atabasc nginc," providcs answcrs to all your
qucstions conccrning sccurity ol data in thc databasc. !t addrcsscs qucstions about
authorization (which uscr has bccn grantcd lcgitimatc acccss to thc databasc
systcm) and authcntication (which acccss privilcgcs arc valid lor a particular uscr).
and R\K, which providc thc acccss privilcgcs ol databasc objccts against
2008, tracking data changcs.
Chaptcr 13, "Concurrcncy Control," dcscribcs concurrcncy control in dcpth.
supportcd by atabasc nginc. All TransactSQL statcmcnts rclatcd to
transactions arc also cxplaincd. Locking as a mcthod to solvc concurrcncy control
problcms is discusscd lurthcr. At thc cnd ol thc chaptcr, you will lcarn what
isolation lcvcls and dcadlocks arc.
Chaptcr 14, "Triggcrs," dcscribcs thc implcmcntation ol proccdural intcgrity
constraints using triggcrs. ach cxamplc in this chaptcr conccrns an intcgrity
problcm that you may lacc in your cvcryday lilc as a databasc application
shown in thc chaptcr.
Part !!!, "SQL Scrvcr: Systcm Administration," dcscribcs all objcctivcs ol atabasc
nginc systcm administration. !t compriscs thc lollowing chaptcrs:
Chaptcr 15, "Systcm nvironmcnt ol atabasc nginc," discusscs somc intcrnal
issucs conccrning atabasc nginc. !t providcs a dctailcd dcscription ol thc
atabasc nginc disk storagc clcmcnts, systcm databascs, and utilitics.
Chaptcr 16, "Managing !nstanccs and Maintaining atabascs," dcscribcs two
important componcnts ol atabasc nginc, cclarativc Managcmcnt Framcwork
and Maintcnancc Plan Vizard, which arc uscd to managc scrvcr instanccs and
databascs, rcspcctivcly.
Chaptcr 17, "8ackup and Rccovcry," providcs an ovcrvicw ol thc laulttolcrancc
mcthods uscd to implcmcnt a backup stratcgy using cithcr SQL Scrvcr
also dcscribcs how to cnsurc thc availability ol databasc systcms and databascs.
xxv i i i Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Chaptcr 18, "Automating Systcm Administration Tasks," dcscribcs thc atabasc
nginc componcnt, SQL Scrvcr Agcnt, that cnablcs you to automatc ccrtain
systcm administration jobs, such as backing up data and using thc schcduling and
opcrators, and alcrts.
Chaptcr 19, "ata Rcplication," providcs an introduction to data rcplication,
and subscriptions using thc cxisting wizards.
Chaptcr 20, "Qucry ptimizcr," dcscribcs thc rolc and thc work ol thc qucry
optimizcr. !t cxplains in dctail all thc atabasc nginc tools (thc ST statcmcnt,
SQL Scrvcr Managcmcnt Studio, and various dynamic managcmcnt vicws)
providcs optimization hints.
Chaptcr 21, "Pcrlormancc Tuning," discusscs pcrlormancc issucs and thc tools lor
tuning atabasc nginc that arc rclcvant to daily administration ol thc systcm.
Altcr introductory notcs conccrning thc mcasurcmcnts ol pcrlormancc, this
monitoring SQL Scrvcr.
Part !\, "SQL Scrvcr and 8usincss !ntclligcncc," discusscs busincss intclligcncc (8!)
and all rclatcd topics. Thcsc chaptcrs introducc Microsolt Analysis Scrviccs and
Microsolt Rcporting Scrviccs. SQL/LAP and cxisting optimization tcchniqucs in
rclation to 8! arc dcscribcd in dctail too. This part includcs thc lollowing chaptcrs:
Chaptcr 22, "8usincss !ntclligcnccAn !ntroduction," introduccs thc notion ol
warchousing proccss can bc cithcr a data warchousc or a data mart. 8oth typcs ol
Chaptcr 23, "Microsolt Analysis Scrviccs," discusscs thc architccturc ol Analysis
Scrviccs and thc main componcnt ol Analysis Scrviccs, 8usincss !ntclligcncc
cvclopmcnt Studio.
Chaptcr 24, "8usincss !ntclligcncc and TransactSQL," cxplains how you can
all SQL cxtcnsions, such as CU8 and RLLUP opcrators, rank lunctions, thc
TP n clausc, and thc P!\T rclational opcrator.
| nt r oduc t i on xxi x
Chaptcr 25, "Microsolt Rcporting Scrviccs," dcscribcs thc Microsolt cntcrprisc
chaptcr discusscs thc dcvclopmcnt cnvironmcnt that you usc to dcsign and crcatc
Chaptcr 26, "ptimizing Tcchniqucs lor 8usincss !ntclligcncc," dcscribcs two
ol busincss intclligcncc: data partitioning and star schcma join optimization.
partitioning typc is cntircly transparcnt to thc application.) !n rclation to star
joins is cxplaincd.
Part \, "8cyond Rclational ata," is dcdicatcd to two "nonrclational" topics, XML
and spatial data, bccausc SQL Scrvcr, as a data platlorm, docsn't havc to handlc only
rclational data. Thc lollowing chaptcrs arc includcd in this part:
Chaptcr 27, "vcrvicw ol XML," givcs you an ovcrvicw ol XML documcnts,
Ts, and XML Schcma. A running cxamplc is uscd to dcmonstratc how XML
documcnts can bc validatcd using cithcr a T or XML Schcma.
Chaptcr 28, "SQL Scrvcr and XML," discusscs SQLXML, Microsolt's sct ol
data typcs and lunctions that supports XML in SQL Scrvcr 2008, bridging thc
thc standardizcd data typc XML and cxplains how storcd XML documcnts can
bc rctricvcd. Altcr that, thc prcscntation ol rclational data as XML documcnts is
discusscd in dctail.
Chaptcr 29, "!ntroduction to Spatial ata," discusscs spatial data and two
arc also shown.
Almost all chaptcrs includc at thcir cnd numcrous cxcrciscs that you can usc
to improvc your knowlcdgc conccrning thc chaptcr's contcnt. All solutions to thc
givcn cxcrciscs can bc lound cithcr at McGrawHill Prolcssional's wcb sitc (www
.mhprolcssional.com) or my own homc pagc (www.lhroscnhcim.dc/~pctkovic).
xxx Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Cbanges from tbe Previous dition
!l you arc lamiliar with thc prcvious cdition ol this book, Microsoft SQL Ser.er 2003:
Beginner`s Guide, you should bc awarc that ! havc madc signilicant changcs in this
cdition. To makc thc book casicr to usc, ! scparatcd somc topics and dcscribcd thcm in
totally ncw chaptcrs. (For instancc, Chaptcr 20 is an cntircly ncw chaptcr and dcscribcs
thc qucry optimizcr in dcpth. !n thc prcvious cdition, this topic was dcscribcd lightly,
togcthcr with indiccs, in Chaptcr 9.) Thc lollowing tablc givcs you an outlinc ol
signif icant structural changcs in thc book (minor changcs arcn't listcd).
C|+jter z I|e eat|re c|+jter ceacera|a +|| l+cet. el :9| :er.er |a.t+||+t|ea (jre.|ea.| C|+jter I} |+. |eea
me.ea te t|e |e|aa|a el t|e |ee|.
C|+jter I|e ae.cr|jt|ea el t|e :|||CI .t+temeat |. aew .tre+m||aea +aa ae.cr||ea |a eae c|+jter.
(I|e aam|er el e\+mj|e. |. reaacea, |at t|ere +re .t||| II e\+mj|e. +aa 1 e\erc|.e..}
C|+jter I|e ..tem c+t+|e |. ae.cr||ea e+r||er |a t||. ea|t|ea t|+a |a t|e jr|er ea|t|ea (jre.|ea.| C|+jter }.
I|e re+.ea |. t|+t t|e eaer+| a|.ca..|ea el aa+m|c m+a+emeat .|ew. (|M\.} |. |.ea |a C|+jter ,
w|||e t|e ae.cr|jt|ea el .jec|l|c |M\. .t+rt. |a C|+jter +aa ceat|aae. t|rea|eat t|e re.t el
t|e |ee|.
C|+jter I||. c|+jter |. aew e\c|a.|.e| aea|c+tea te |aa|ce..
C|+jter 1 C|+jter 1 aee.a't ae.cr||e tr+a.+ct|ea. ea|, +. |a t|e jr|er ea|t|ea, |at |+aa|e. t|e eat|re tej|c el
ceacarreac ceatre|. (|er e\+mj|e, t|e .ect|ea 'Ceacarreac Meae|.' e\j|+|a. |a aet+|| |et| meae|.
.ajjertea | |+t+|+.e |a|ae.}
C|+jter + Ir|er. +re aew ae.cr||ea +lter ceacarreac ceatre| |ec+a.e t|e a.e + mec|+a|.m, rew .er.|ea|a,
t|+t |. re|+tea te t|e |+tter tej|c +aa t|a. ae.cr||ea |a C|+jter 1.
C|+jter I||. c|+jter |. eat|re| aew +aa ae.cr||e. twe cemjeaeat. re|+tea te m+a+emeat el .er.er
|a.t+ace. +aa m+|atea+ace el a+t+|+.e..
C|+jter z I||. c|+jter |. eat|re| aew +aa ae.cr||e. |a aet+|| t|e aer ejt|m|ter el |+t+|+.e |a|ae. (I||.
cemjeaeat w+. ae.cr||ea |a t|e jre.|ea. |ee| |a C|+jter teet|er w|t| |aa|ce..}
C|+jter z I||. c|+jter |. j+rt| aew, +aa ae.cr||e. a+t+ j+rt|t|ea|a +aa .t+r .c|em+ je|a ejt|m|t+t|ea.
C|+jter z I||. |. +a eat|re| aew c|+jter, w|t| .j+t|+| a+t+ +. t|e tej|c.
Differences etween SL and 7ransact-SL Syntax
TransactSQL, SQL Scrvcr's rclational databasc languagc, has scvcral nonstandardizcd
propcrtics that gcncrally arc not known to pcoplc who arc lamiliar with SQL only:
Vhcrcas thc scmicolon (,) is uscd in SQL to scparatc two SQL statcmcnts in a
statcmcnt group (and you will gcncrally gct an crror mcssagc il you do not includc
thc scmicolon), in TransactSQL, usc ol scmicolons is optional.
| nt r oduc t i on xxxi

gcncrally uscd to scparatc statcmcnt groups lrom cach othcr, whcrcas somc
TransactSQL statcmcnts (such as CRAT TA8L, CRAT !NX,
statcmcnt, G n (whcrc n - 1, 2, 3,.), mcans that thc corrcsponding statcmcnt
group will bc cxccutcd n timcs.

sample mcans
that thc statcmcnts that lollow arc rclatcd to thc sample databasc.
Working witb tbe SampIe Databases
!n contrast to its prcdcccssor, this cdition uscs scvcral samplc databascs:
sample databasc
Microsolt's AdventureWorks databasc
Microsolt's AdventureWorksDW databasc
An introductory book likc this rcquircs a samplc databasc that can bc casily
undcrstood by cach rcadcr. For this rcason, ! uscd a vcry simplc conccpt lor my own
sample databasc: it has only lour tablcs with scvcral rows cach. n thc othcr hand, its
logic is complcx cnough to dcmonstratc thc hundrcds ol cxamplcs includcd in thc tcxt
ol thc book. Thc sample databasc that you will usc in this book rcprcscnts a company
with dcpartmcnts and cmployccs. ach cmploycc bclongs to cxactly onc dcpartmcnt,
which itscll has onc or morc cmployccs. Jobs ol cmployccs ccntcr around projccts: cach
cmploycc works at thc samc timc lor onc or morc projccts, and cach projcct cngagcs
onc or morc cmployccs.
Thc tablcs ol thc sample databasc arc shown hcrc.
Thc department tablc:
6ept_ae 6ept_aame |ecat|ea
a |e.e+rc| |+||+.
az |cceaat|a :e+tt|e
a1 M+r|et|a |+||+.
xxxi i Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Thc employee tablc:
emp_ae emp_faame emp_|aame 6ept_ae
z!1+ M+tt|ew :m|t| a1
z |aa Ieae. a1
1 Ie|a |+rr|mere a
z1+ I+me. I+me. az
1 ||.+ |ertea| az
z! |||e |+a.e| az
z!! :|||| Me.er a
Thc project tablc:
pre[ect_ae |re[ect_aame hc6qet
j |je||e z
jz 0em|a| !
j1 Mercar !
Thc works_on tablc:
emp_ae pre[ect_ae [eh eater_6ate
z j |a+|.t z..
z j1 M+a+er z..
z!1+ jz C|er| zI.z.!
1 jz NJ|| zI..
z1+ jz NJ|| z.z.!
z! j1 |a+|.t zI..!
1 j M+a+er zI.+.!
z!! j NJ|| zI..
z!! jz C|er| z.z.
1 j1 C|er| z..!
z1+ j C|er| zI..+
| nt r oduc t i on xxxi i i
You can download thc sample databasc lrom McGrawHill Prolcssional's wcb sitc
(www.mhprolcssional.com) or my own homc pagc (www.lhroscnhcim.dc/~pctkovic).
Also, you can download all thc cxamplcs in thc book lrom my homc pagc.
Although thc samplc databasc can bc uscd lor many ol thc cxamplcs in this book,
lor somc cxamplcs, tablcs with lot ol rows arc ncccssary (to show optimization lcaturcs,
lor instancc). For this rcason, two Microsolt samplc databascsAdvcnturcVorks and
AdvcnturcVorksVarc also uscd. 8oth ol thcm can bc lound at thc Microsolt
CodcPlcx wcb sitc www.codcplcx.com/MSFT8ProdSamplcs.
This page intentionally left blank
Part #
Iaslc Conccpts
and Installatlon
Part I
Copyright 2008 by The McGraw-Hill Companies. Click here for terms of use.
This page intentionally left blank
In 7bis Cbapter
Database Systems:
An Dverview
ReIationaI Database
Systems
Database Design
Syntax Conventions
Iclatlonal Iatabasc
SystcmsAn Introductlon
Cbapter 1
Copyright 2008 by The McGraw-Hill Companies. Click here for terms of use.
4 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
T
his chaptcr dcscribcs databasc systcms in gcncral. First, it discusscs what a
databasc systcm is, and which componcnts it contains. ach componcnt is dc
scribcd briclly, with a rclcrcncc to thc chaptcr in which it is dcscribcd in dctail.
Thc sccond major scction ol thc chaptcr is dcdicatcd to rclational databasc systcms. !t
discusscs thc propcrtics ol rclational databasc systcms and thc corrcsponding languagc
uscd in such systcmsStructurcd Qucry Languagc (SQL).
Gcncrally, bclorc you implcmcnt a databasc, you havc to dcsign it, with all its objccts.
Thc third major scction ol thc chaptcr cxplains how you can usc normal lorms to
cnhancc thc dcsign ol your databasc, and also introduccs thc cntityrclationship modcl,
which you can usc to conccptualizc all cntitics and thcir rclationships. Thc linal scction
prcscnts thc syntax convcntions uscd throughout thc book.
Database Systems: An Dverview
A databasc systcm is an ovcrall collcction ol dillcrcnt databasc soltwarc componcnts
and databascs containing thc lollowing parts:
atabasc application programs
Clicnt componcnts
atabasc scrvcr(s)
atabascs
A databasc application program is spccialpurposc soltwarc that is dcsigncd and
implcmcntcd by uscrs or implcmcntcd by thirdparty soltwarc companics. !n contrast,
clicnt componcnts arc gcncralpurposc databasc soltwarc dcsigncd and implcmcntcd
by a databasc company. 8y using clicnt componcnts, uscrs can acccss data storcd on thc
samc or a rcmotc computcr.
Thc task ol a databasc scrvcr is to managc data storcd in a databasc. ach clicnt
communicatcs with a databasc scrvcr by scnding uscr qucrics to it. Thc scrvcr proccsscs
cach qucry and scnds thc rcsult back to thc clicnt.
!n gcncral, a databasc can bc vicwcd lrom two pcrspcctivcs, thc uscrs' and thc databasc
systcm's. Uscrs vicw a databasc as a collcction ol data that logically bclong togcthcr. For a
databasc systcm, a databasc is simply a scrics ol bytcs, usually storcd on a disk. Although
thcsc two vicws ol a databasc arc totally dillcrcnt, thcy do havc somcthing in common: thc
databasc systcm nccds to providc not only intcrlaccs that cnablc uscrs to crcatc databascs
Cha pt er l : Pel a t i ona l Da t a ba s e Sy s t ems An | nt r oduc t i on 5
and rctricvc or modily data, but also systcm componcnts to managc thc storcd data. Hcncc,
a databasc systcm must providc thc lollowing lcaturcs:
\aricty ol uscr intcrlaccs
Physical data indcpcndcncc
Logical data indcpcndcncc
Qucry optimization
ata intcgrity
Concurrcncy control
8ackup and rccovcry
atabasc sccurity
Thc lollowing scctions briclly dcscribc thcsc lcaturcs.
Variety of User Interfaces
Most databascs arc dcsigncd and implcmcntcd lor usc by many dillcrcnt typcs ol uscrs
with varicd lcvcls ol knowlcdgc. For this rcason, a databasc systcm should ollcr many
distinct uscr intcrlaccs. Thcsc intcrlaccs includc, among othcrs, mcnus and lorms lor
cnd uscrs, and an intcractivc qucry languagc lor cxpcricnccd uscrs.
PbysicaI Data Independence
Physical data indcpcndcncc mcans that thc databasc application programs do not
dcpcnd on thc physical structurc ol thc storcd data in a databasc. This important lcaturc
cnablcs you to makc changcs to thc storcd data without having to makc any changcs
to databasc application programs. For cxamplc, il thc storcd data is prcviously ordcrcd
using onc critcrion, and this ordcr is changcd using anothcr critcrion, thc modilication
ol thc physical data should not allcct thc cxisting databasc applications or thc cxisting
databasc schema (a dcscription ol a databasc gcncratcd by thc data dclinition languagc
ol thc databasc systcm).
LogicaI Data Independence
!n lilc proccssing (using traditional programming languagcs), thc dcclaration ol a lilc
is donc in application programs, so any changc to thc structurc ol that lilc usually
rcquircs thc modilication ol all programs using it. atabasc systcms providc logical data
indcpcndcnccin othcr words, it is possiblc to makc changcs to thc logical structurc ol
6 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
thc databasc without having to makc any changcs to thc databasc application programs.
For cxamplc, il thc structurc ol an objcct namcd PRSN cxists in thc databasc systcm
and you want to add an attributc to PRSN (say thc addrcss), you havc to modily
only thc logical structurc ol thc databasc, and not thc cxisting application programs.
uery Dptimization
vcry databasc systcm contains a subcomponcnt callcd an optimizer that considcrs
a varicty ol possiblc cxccution stratcgics lor qucrying thc data and thcn sclccts thc
most cllicicnt onc. Thc sclcctcd stratcgy is callcd thc execution plan ol thc qucry. Thc
optimizcr makcs its dccisions using considcrations such as how big thc tablcs arc that
arc involvcd in thc qucry, what indiccs cxist, and what 8oolcan opcrator (AN, R, or
NT) is uscd in thc VHR clausc. (This topic is discusscd in dctail in Chaptcr 20.)
Data Integrity
nc ol thc tasks ol a databasc systcm is to idcntily logically inconsistcnt data and rcjcct
its storagc in a databasc. (Thc datc Fcbruary 30 and thc timc 5:77:00 P.M. arc two
cxamplcs ol such data.) Additionally, most rcallilc problcms that arc implcmcntcd
using databasc systcms havc integrity constraints that must hold truc lor thc data. (nc
cxamplc ol an intcgrity constraint might bc thc company's cmploycc numbcr, which
must bc a livcdigit intcgcr.) Thc task ol maintaining intcgrity can bc handlcd by thc
uscr in application programs or by thc 8MS. As much as possiblc, this task should
bc handlcd by thc 8MS. (ata intcgrity is discusscd in two chaptcrs ol this book:
dcclarativc intcgrity in Chaptcr 5 and proccdural intcgrity in Chaptcr 14.)
Concurrency ControI
A databasc systcm is a multiuscr soltwarc systcm, mcaning that many uscr applications
acccss a databasc at thc samc timc. Thcrclorc, cach databasc systcm must havc somc
kind ol control mcchanism to cnsurc that scvcral applications that arc trying to updatc
thc samc data do so in somc controllcd way. Thc lollowing is an cxamplc ol a problcm
that can arisc il a databasc systcm docs not contain such control mcchanisms:
1. Thc owncrs ol bank account 4711 at bank X havc an account balancc ol 82,000.
2. Thc two joint owncrs ol this bank account, Mrs. A and Mr. 8, go to two dillcrcnt
bank tcllcrs, and cach withdraws 81,000 at the same time.
3. Altcr thcsc transactions, thc amount ol moncy in bank account 4711 should bc 80
and not 81,000.
All databasc systcms havc thc ncccssary mcchanisms to handlc cascs likc this
cxamplc. Concurrcncy control is discusscd in dctail in Chaptcr 13.
Cha pt er l : Pel a t i ona l Da t a ba s e Sy s t ems An | nt r oduc t i on 7
ackup and Recovery
A databasc systcm must havc a subsystcm that is rcsponsiblc lor rccovcry lrom hardwarc
or soltwarc crrors. For cxamplc, il a lailurc occurs whilc a databasc application updatcs
100 rows ol a tablc, thc rccovcry subsystcm must roll back all prcviously cxccutcd updatcs
to cnsurc that thc corrcsponding data is consistcnt altcr thc crror occurs. (Scc Chaptcr 17
lor lurthcr discussion on backup and rccovcry.)
Database Security
Thc most important databasc sccurity conccpts arc authcntication and authorization.
uthentication is thc proccss ol validating uscr crcdcntials to prcvcnt unauthorizcd uscrs
lrom using a systcm. Authcntication is most commonly cnlorccd by rcquiring thc uscr
to cntcr a (uscr) namc and a password. This inlormation is cvaluatcd by thc systcm
to dctcrminc whcthcr thc uscr is allowcd to acccss thc systcm. This proccss can bc
strcngthcncd by using cncryption.
uthorization is thc proccss that is applicd altcr thc idcntity ol a uscr is authcnticatcd.
uring this proccss, thc systcm dctcrmincs what rcsourccs thc particular uscr can usc.
!n othcr words, structural and systcm catalog inlormation about a particular cntity is
now availablc only to principals that havc pcrmission to acccss that cntity. (Chaptcr 12
discusscs thcsc conccpts in dctail.)
ReIationaI Database Systems
Thc componcnt ol Microsolt SQL Scrvcr callcd atabasc nginc is a rclational
databasc systcm. Thc notion ol rclational databasc systcms was lirst introduccd by
. F. Codd in his articlc "A Rclational Modcl ol ata lor Largc Sharcd ata 8anks"
in 1970. !n contrast to carlicr databasc systcms (nctwork and hicrarchical), relational
database systems arc bascd upon thc rclational data modcl, which has a strong
mathcmatical background.
/ 1c|c mc1e| |s c :c||e:||cs c/ :cs:e(|s, ||e|/ /e|c||css||(s, cs1 ||e|/ :css|/c|s|s ||c| c/e ase1 |c /e(/eses| 1c|c
c/ c /ec|-ac/|1 (/c||em.
Thc ccntral conccpt ol thc rclational data modcl is a rclationthat is, a tablc. Thcrclorc,
lrom thc uscr's point ol vicw, a rclational databasc contains tablcs and nothing but tablcs.
!n a tablc, thcrc arc onc or morc columns and zcro or morc rows. At cvcry row and column
position in a tablc thcrc is always cxactly onc data valuc.
8 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Working witb tbe ook's SampIe Database
Thc samplc databasc uscd in this book rcprcscnts a company with dcpartmcnts and
cmployccs. ach cmploycc in thc cxamplc bclongs to cxactly onc dcpartmcnt, which
itscll has onc or morc cmployccs. Jobs ol cmployccs ccntcr on projccts: cach cmploycc
works at thc samc timc on onc or morc projccts, and cach projcct cngagcs onc or morc
cmployccs.
Thc data ol thc sample databasc can bc rcprcscntcd using lour tablcs:
dcpartmcnt
cmploycc
projcct
works_on
Tablcs 11 through 14 show all thc tablcs ol thc sample databasc.
Thc department tablc rcprcscnts all dcpartmcnts ol thc company. ach dcpartmcnt
has thc lollowing attributcs:
dcpartmcnt (dcpt_no, dcpt_namc, location)
dept_no rcprcscnts thc uniquc numbcr ol cach dcpartmcnt. dept_name is its namc, and
location is thc location ol thc corrcsponding dcpartmcnt.
Thc employee tablc rcprcscnts all cmployccs working lor a company. ach cmploycc
has thc lollowing attributcs:
cmploycc (cmp_no, cmp_lnamc, cmp_lnamc, dcpt_no)
emp_no rcprcscnts thc uniquc numbcr ol cach cmploycc. emp_fname and emp_lname
arc thc lirst and last namc ol cach cmploycc, rcspcctivcly. Finally, dept_no is thc
numbcr ol thc dcpartmcnt to which thc cmploycc bclongs.
6ept_ae 6ept_aame |ecat|ea
a |e.e+rc| |+||+.
az |cceaat|a :e+tt|e
a1 M+r|et|a |+||+.
Iah|e 1-1 |e |e(c/|mes| c||e
Cha pt er l : Pel a t i ona l Da t a ba s e Sy s t ems An | nt r oduc t i on 9
emp_ae pre[ect_ae [eh eater_6ate
z j |a+|.t z..
z j1 M+a+er z..
z!1+ jz C|er| zI.z.!
1 jz NJ|| zI..
z1+ jz NJ|| z.z.!
z! j1 |a+|.t zI..!
1 j M+a+er zI.+.!
z!! j NJ|| zI..
z!! jz C|er| z.z.
1 j1 C|er| z..!
z1+ j C|er| zI..+
Iah|e 1-4 |e ac/|s_cs c||e
Iah|e 1-1 |e |m(|c,ee c||e
emp_ae emp_faame emp_|aame 6ept_ae
z!1+ M+tt|ew :m|t| a1
z |aa Ieae. a1
1 Ie|a |+rr|mere a
z1+ I+me. I+me. az
1 |||e |+a.e| az
z! ||.+ |ertea| az
z!! :|||| Me.er a
Iah|e 1-1 |e |/c(e:| c||e
pre[ect_ae pre[ect_aame hc6qet
j |je||e z
jz 0em|a| !
j1 Mercar !
10 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
ach projcct ol a company is rcprcscntcd in thc project tablc. This tablc has thc
lollowing columns:
projcct (projcct_no, projcct_namc, budgct)
project_no rcprcscnts thc uniquc numbcr ol cach projcct. project_name and budget
spccily thc namc and thc budgct ol cach projcct, rcspcctivcly.
Thc works_on tablc spccilics thc rclationship bctwccn cmployccs and projccts. !t has
thc lollowing columns:
works_on (cmp_no, projcct_no, job, cntcr_datc)
emp_no spccilics thc cmploycc numbcr and project_no spccilics thc numbcr ol thc
projcct on which thc cmploycc works. Thc combination ol data valucs bclonging
to thcsc two columns is always uniquc. job and enter_date spccily thc task and thc
starting datc ol an cmploycc in thc corrcsponding projcct, rcspcctivcly.
Using thc sample databasc, it is possiblc to dcscribc somc gcncral propcrtics ol
rclational databasc systcms:
Rows in a tablc do not havc any particular ordcr.
Columns in a tablc do not havc any particular ordcr.
vcry column must havc a uniquc namc within a tablc. n thc othcr hand, columns
sample databasc
has a dept_no column in thc department tablc and a column with thc samc namc
in thc employee tablc.)

row and column position ol a tablc thcrc is ncvcr a sct ol multiplc data valucs.
For cvcry tablc, thcrc is at lcast onc column (i.c., a combination ol columns) with
thc propcrty that no two rows havc thc samc combination ol data valucs lor thcsc
candidate
key. !l thcrc is morc than onc candidatc kcy within a tablc, thc databasc dcsigncr
dcsignatcs onc ol thcm as thc primary key ol thc tablc. For cxamplc, thc column
dept_no is thc primary kcy ol thc department tablc, thc columns emp_no and
project_no arc thc primary kcys ol thc tablcs employee and project, rcspcctivcly.
Finally, thc primary kcy lor thc works_on tablc is thc combination ol thc columns
emp_no, project_no.

atabasc nginc and all othcr rclational databasc systcms gcncrally allow thc
cxistcncc ol idcntical rows within a tablc.)
Cha pt er l : Pel a t i ona l Da t a ba s e Sy s t ems An | nt r oduc t i on 11
SL: A ReIationaI Database Language
Thc SQL Scrvcr rclational databasc languagc is callcd TransactSQL. !t is a dialcct
ol thc most important databasc languagc today: Structurcd Qucry Languagc (SQL).
Thc origin ol SQL is closcly conncctcd with thc projcct callcd Systcm R, which was
dcsigncd and implcmcntcd by !8M in thc carly 1980s. This projcct showcd that it
is possiblc, using thc thcorctical loundations ol thc work ol . F. Codd, to build a
rclational databasc systcm.
!n contrast to traditional languagcs likc C, C, and Java, SQL is a sctoricntcd
languagc. (Thc lormcr arc also callcd rccordoricntcd languagcs.) This mcans that SQL
can qucry many rows lrom onc or morc tablcs using just onc statcmcnt. This lcaturc is
onc ol thc most important advantagcs ol SQL, allowing thc usc ol this languagc at a
logically highcr lcvcl than thc lcvcl at which traditional languagcs can bc uscd.
Anothcr important propcrty ol SQL is its nonproccdurality. vcry program writtcn
in a proccdural languagc (C, C, Java) dcscribcs ho. a task is accomplishcd, stcp by
stcp. !n contrast to this, SQL, as any othcr nonproccdural languagc, dcscribcs .hat it is
that thc uscr wants. Thus, thc systcm is rcsponsiblc lor linding thc appropriatc way to
solvc uscrs' rcqucsts.
SQL contains two sublanguagcs: a data dclinition languagc (L) and a data
manipulation languagc (ML). L statcmcnts arc uscd to dcscribc thc schcma ol
databasc tablcs. Thc L contains thrcc gcncric SQL statcmcnts: CRAT objcct,
ALTR objcct, and RP objcct. Thcsc statcmcnts crcatc, altcr, and rcmovc databasc
objccts, such as databascs, tablcs, columns, and indcxcs. (Thcsc statcmcnts arc discusscd
in dctail in Chaptcr 5.)
!n contrast to thc L, thc ML cncompasscs all opcrations that manipulatc thc
data. Thcrc arc always lour gcncric opcrations lor manipulating thc databasc: rctricval,
inscrtion, dclction, and modilication. Thc rctricval statcmcnt SLCT is dcscribcd in
Chaptcrs 6, whilc thc !NSRT, LT, and UPAT statcmcnts arc discusscd in
dctail in Chaptcr 7.
Database Design
csigning a databasc is a vcry important phasc in thc databasc lilc cyclc, which
prcccdcs all othcr phascs cxccpt thc rcquircmcnts collcction and thc analysis. !l thc
databasc dcsign is crcatcd mcrcly intuitivcly and without any plan, thc rcsulting
databasc will most likcly not mcct thc uscr rcquircmcnts conccrning pcrlormancc.
Anothcr conscqucncc ol a bad databasc dcsign is supcrlluous data rcdundancy, which
in itscll has two disadvantagcs: thc cxistcncc ol data anomalics and thc usc ol an
unncccssary amount ol disk spacc.
12 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Normalization ol data is a proccss during which thc cxisting tablcs ol a databasc
arc tcstcd to lind ccrtain dcpcndcncics bctwccn thc columns ol a tablc. !l such
dcpcndcncics cxist, thc tablc is rcstructurcd into multiplc (usually two) tablcs, which
climinatcs any column dcpcndcncics. !l onc ol thcsc gcncratcd tablcs still contains data
dcpcndcncics, thc proccss ol normalization must bc rcpcatcd until all dcpcndcncics arc
rcsolvcd.
Thc proccss ol climinating data rcdundancy in a tablc is bascd upon thc thcory ol
lunctional dcpcndcncics. A functional dependency mcans that by using thc known valuc
ol onc column, thc corrcsponding valuc ol anothcr column can always bc uniqucly
dctcrmincd. (Thc samc is truc lor column groups.) Thc lunctional dcpcndcncics
bctwccn columns A and 8 is dcnotcd by A 8, spccilying that a valuc ol column
A can always bc uscd to dctcrminc thc corrcsponding valuc ol column 8. ("8 is
lunctionally dcpcndcnt on A.")
Thc lollowing cxamplc shows thc lunctional dcpcndcncy bctwccn two attributcs ol
thc tablc employee in thc samplc databasc.
XAMPL 1.1
cmp_no cmp_lnamc
8y having a uniquc valuc lor thc cmploycc numbcr, thc corrcsponding last namc ol
thc cmploycc (and all othcr corrcsponding attributcs) can bc dctcrmincd. (This kind ol
lunctional dcpcndcncy, whcrc a column is dcpcndcnt upon thc primary kcy ol a tablc, is
callcd tri.ial lunctional dcpcndcncy.)
Anothcr kind ol lunctional dcpcndcncy is callcd multi.alued dependency. !n contrast
to thc lunctional dcpcndcncy just dcscribcd, thc multivalucd dcpcndcncy is spccilicd
lor multivalucd attributcs. This mcans that by using thc known valuc ol onc attributc
(column), thc corrcsponding set of .alues ol anothcr multivalucd attributc can bc
uniqucly dctcrmincd. Thc multivalucd dcpcndcncy is dcnotcd by .
Thc ncxt cxamplc shows thc multivalucd dcpcndcncy that holds lor two attributcs ol
thc objcct 8K.
XAMPL 1.2
!S8N Authors
Thc !S8N ol a book always dctcrmincs all ol its authors. Thcrclorc, thc Authors
attributc is multivalucd dcpcndcnt on thc SBN attributc.
Cha pt er l : Pel a t i ona l Da t a ba s e Sy s t ems An | nt r oduc t i on 13
NormaI Forms
Normal lorms arc uscd lor thc proccss ol normalization ol data and thcrclorc lor thc
databasc dcsign. !n thcory, thcrc arc at lcast livc dillcrcnt normal lorms, ol which thc
lirst thrcc arc thc most important lor practical usc. Thc third normal lorm lor a tablc
can bc achicvcd by tcsting thc lirst and sccond normal lorms at thc intcrmcdiatc statcs,
and as such, thc goal ol good databasc dcsign can usually bc lullillcd il all tablcs ol a
databasc arc in thc third normal lorm.
|e ma||||c|ae1 1e(es1es:, |s ase1 |c |es| ||e /ca/|| sc/mc| /c/m c/ c |c||e. |e/e/c/e, |||s ||s1 c/ 1e(es1es:,
a||| sc| |e ase1 /a/||e/ |s |||s |cc|.
Iirst NormaI Iorm
First normal lorm (1NF) mcans that a tablc has no multivalucd attributcs or compositc
attributcs. (A compositc attributc contains othcr attributcs and can thcrclorc bc dividcd
into smallcr parts.) All rclational tablcs arc by dclinition in 1NF, bccausc thc valuc ol
any column in a row must bc atomicthat is, singlc valucd.
Tablc 15 dcmonstratcs 1NF using part ol thc works_on tablc lrom thc samplc
databasc. Thc rows ol thc works_on tablc could bc groupcd togcthcr, using thc cmploycc
numbcr. Thc rcsulting Tablc 16 is not in 1NF bccausc thc column project_no contains
a sct ol valucs (p1, p3).
Second NormaI Iorm
A tablc is in sccond normal lorm (2NF) il it is in 1NF and thcrc is no nonkcy column
dcpcndcnt on a partial primary kcy ol that tablc. This mcans il (A,8) is a combination
ol two tablc columns building thc kcy, thcn thcrc is no column ol thc tablc dcpcnding
cithcr on only A or only 8.
emp_ae pre[ect_ae .................
z j .................
z j1 .................
................ ................ ...............
Iah|e 1- |c/| c/ ||e ac/|s_cs c||e
14 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
For cxamplc, Tablc 17 shows thc works_on1 tablc, which is idcntical to thc works_on
tablc cxccpt lor thc additional column, dept_no. Thc primary kcy ol this tablc is thc
combination ol columns emp_no and project_no. Thc column dept_no is dcpcndcnt on
thc partial kcy emp_no (and is indcpcndcnt ol project_no), so this tablc is not in 2NF.
(Thc original tablc, works_on, is in 2NF.)
||e/, |c||e a||| c cse-:c|ams (/|mc/, |e, |s c|ac,s |s 1||.

7hird NormaI Iorm
A tablc is in third normal lorm (3NF) il it is in 2NF and thcrc arc no lunctional
dcpcndcncics bctwccn nonkcy columns. For cxamplc, thc employee1 tablc (scc Tablc 18),
which is idcntical to thc employee tablc cxccpt lor thc additional column, dept_name, is
not in 3NF, bccausc lor cvcry known valuc ol thc column dept_no thc corrcsponding valuc
ol thc column dept_name can bc uniqucly dctcrmincd. (Thc original tablc, employee, as
wcll as all othcr tablcs ol thc sample databasc arc in 3NF.)
emp_ae pre[ect_ae .................
z (j, j1} .................
................ ................ .................
Iah|e 1-6 ||s c||e |s |c| |s !||
Iah|e 1-I |e ac/|s_cs! c||e
emp_ae pre[ect_ae [eh eater_6ate 6ept_ae
z j |a+|.t z.. a1
z j1 M+a+er z.. a1
z!1+ jz C|er| zI.z.! a1
1 jz NJ|| zI.. a
............... ................ .................. ......................
Cha pt er l : Pel a t i ona l Da t a ba s e Sy s t ems An | nt r oduc t i on 15
ntity-ReIationsbip ModeI
Thc data in a databasc could casily bc dcsigncd using only onc tablc that contains all
data. Thc main disadvantagc ol such a databasc dcsign is its high rcdundancy ol data.
For cxamplc, il your databasc contains data conccrning cmployccs and thcir projccts
(assuming cach cmploycc works at thc samc timc on onc or morc projccts, and cach
projcct cngagcs onc or morc cmployccs), thc data storcd in a singlc tablc contains many
columns and rows. Thc main disadvantagc ol such a tablc is that data is dillicult to kccp
consistcnt bccausc ol its rcdundancy.
Thc entity-relationship (EF) model is uscd to dcsign rclational databascs by rcmoving
all cxisting rcdundancy in thc data. Thc basic objcct ol thc R modcl is an entitythat
is, a rcalworld objcct. ach cntity has scvcral attributes, which arc propcrtics ol thc
cntity and thcrclorc dcscribc it. 8ascd on its typc, an attributc can bc
Atomic (or single valued) An atomic attributc is always rcprcscntcd by a singlc
valuc lor a particular cntity. For cxamplc, a pcrson's marital status is always an
atomic attributc. Most attributcs arc atomic attributcs.
Multivalued A multivalucd attributc may havc onc or morc valucs lor a
particular cntity. For cxamplc, Location as thc attributc ol an cntity callcd
NTRPR!S is multivalucd, bccausc cach cntcrprisc can havc onc or morc
locations.
Composite Compositc attributcs arc not atomic bccausc thcy arc asscmblcd using
somc othcr atomic attributcs. A typical cxamplc ol a compositc attributc is a pcrson's
addrcss, which is composcd ol atomic attributcs, such as City, Zip, and Street.
Thc cntity PRSN in xamplc 1.3 has scvcral atomic attributcs, onc compositc
attributc, Address, and a multivalucd attributc, College_degree.
emp_ae emp_faame emp_|aame 6ept_ae 6ept_aame
z!1+ M+tt|ew :m|t| a1 M+r|et|a
z |aa Ieae. a1 M+r|et|a
1 Ie|a |+rr|mere a |e.e+rc|
z1+ I+me. I+me. az |cceaat|a
................. ................ ..................... .................. ...................
Iah|e 1-8 |e em(|c,ee! c||e
16 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
XAMPL 1.3
PRSN (Pcrsonal_no, F_namc, L_namc, Addrcss(City,Zip,Strcct),[Collcgc_dcgrcc])
ach cntity has onc or morc kcy attributcs that arc attributcs (or a combination ol two
or morc attributcs) whosc valucs arc uniquc lor cach particular cntity. !n xamplc 1.3,
thc attributc Personal_no is thc kcy attributc ol thc cntity PRSN.
8csidcs cntity and attributc, relationship is anothcr basic conccpt ol thc R modcl. A
rclationship cxists whcn an cntity rclcrs to onc (or morc) othcr cntitics. Thc numbcr ol
participating cntitics dclincs thc dcgrcc ol a rclationship. For cxamplc, thc rclationship
works_on bctwccn cntitics MPLY and PRJCT has dcgrcc two.
vcry cxisting rclationship bctwccn two cntitics must bc onc ol thc lollowing thrcc
typcs: 1:1, 1:N, or M:N. (This propcrty ol a rclationship is also callcd cardinality ratio.)
For cxamplc, thc rclationship bctwccn thc cntitics PARTMNT and MPLY
is 1:N, bccausc cach cmploycc bclongs to cxactly onc dcpartmcnt, which itscll has
onc or morc cmployccs. Also, thc rclationship bctwccn thc cntitics PRJCT and
MPLY is M:N, bccausc cach projcct cngagcs onc or morc cmployccs and cach
cmploycc works at thc samc timc on onc or morc projccts.
A rclationship can also havc its own attributcs. Figurc 11 shows an cxamplc ol an R
diagram. (Thc R diagram is thc graphical notation uscd to dcscribc thc R modcl.)
||qcre 1-1 |scm(|e c/ cs || ||c/cm
Cha pt er l : Pel a t i ona l Da t a ba s e Sy s t ems An | nt r oduc t i on 17
Using this notation, cntitics arc modclcd using rcctangular boxcs, with thc cntity namc
writtcn insidc thc box. Attributcs arc shown in ovals, and cach attributc is attachcd to a
particular cntity (or rclationship) using a straight linc. Finally, rclationships arc modclcd
using diamonds, and cntitics participating in thc rclationship arc attachcd to it using
straight lincs. Thc cardinality ratio ol cach cntity is writtcn on thc corrcsponding linc.
Syntax Conventions
This book uscs thc convcntions shown in Tablc 19 lor thc syntax ol thc TransactSQL
statcmcnts and lor thc indication ol thc tcxt.
|s :cs|/cs| |c |/c:|e|s cs1 |/c:es, a||:| |e|cs |c s,s|cs :cs|es||css, (c/es||eses, , |e|cs |c ||e s,s|cs c/
c s|c|emes| cs1 mas| c|ac,s |e |,(e1|
Ceaeat|ea |a6|cat|ea
||c||:s New term. er |tem. el emj|+.|..
J||||C|:| Ir+a.+ct-:9| |ewera.ler e\+mj|e, C|||I| I||||. |aa|t|ea+| |alerm+t|ea +|eat t|e |ewera. el
t|e Ir+a.+ct-:9| |+aa+e c+a |e leaaa |a C|+jter !.
|ewerc+.e \+r|+||e. |a Ir+a.+ct-:9| .t+temeat.ler e\+mj|e, C|||I| I|||| t+||ea+me. (I|e a.er ma.t
rej|+ce 't+||ea+me' w|t| t|e +cta+| a+me el t|e t+||e.}
.+r | .+rz ||tera+t|.e a.e el t|e |tem. .+r +aa .+rz. (\ea m+ c|ee.e ea| eae el t|e |tem. .ej+r+tea | t|e
.ert|c+| |+r.}
} ||tera+t|.e a.e el mere |tem..
|\+mj|e. e\jre..|ea | J:|| | NJ|| }
[ 0jt|ea+| |tem(.}.
|\+mj|e. |0| |0||[
} ... |tem(.} t|+t c+a |e reje+tea +a aam|er el t|me..
|\+mj|e. , yj+r+m tj} .
N+me el a+t+|+.e e|ject (a+t+|+.e |t.e|l, t+||e., ce|ama.} |a t|e te\t.
|el+a|t I|e ael+a|t .+|ae |. +|w+. aaaer||aea.
|\+mj|e. ||| | ||:I|NCI
Iah|e 1-9 :,s|cs tcs|es||css
18 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
ConcIusion
All databasc systcms providc thc lollowing lcaturcs:
\aricty ol uscr intcrlaccs
Physical data indcpcndcncc
Logical data indcpcndcncc
Qucry optimization
ata intcgrity
Concurrcncy control
8ackup and rccovcry
atabasc sccurity
Thc ncxt chaptcr shows you how to install SQL Scrvcr 2008.
xercises
.1.1
Vhat docs "data indcpcndcncc" mcan and which two lorms ol data indcpcndcncc cxist:
.1.2
Vhich is thc main conccpt ol thc rclational modcl:
.1.3
Vhat docs thc employee tablc rcprcscnt in thc rcal world: And what docs thc row in
this tablc with thc data lor Ann Joncs rcprcscnt:
.1.4
Vhat docs thc works_on tablc rcprcscnt in thc rcal world (and in rclation to thc othcr
tablcs ol thc sample databasc):
Cha pt er l : Pel a t i ona l Da t a ba s e Sy s t ems An | nt r oduc t i on 19
.1.5
Lct book bc a tablc with two columns: isbn and title. Assuming that isbn is uniquc
and thcrc arc no idcntical titlcs, answcr thc lollowing qucstions:
a. !s title a kcy ol thc tablc:
b. ocs isbn lunctionally dcpcnd on title:
c. !s thc book tablc in 3NF:
.1.6
Lct order bc a tablc with thc lollowing columns: order_no, customer_no, discount.
!l thc column customer_no is lunctionally dcpcndcnt on order_no and thc column
discount is lunctionally dcpcndcnt on customer_no, answcr thc lollowing qucstions
and cxplain in dctail your answcrs:
a. !s order_no a kcy ol thc tablc:
b. !s customer_no a kcy ol thc tablc:
.1.7
Lct company bc a tablc with thc lollowing columns: company_no, location. ach
company has onc or morc locations. !n which normal lorm is thc company tablc:
.1.8
Lct supplier bc a tablc with thc lollowing columns: supplier_no, article, city. Thc kcy
ol thc tablc is thc combination ol thc lirst two columns. ach supplicr dclivcrs scvcral
articlcs, and cach articlc is dclivcrcd by scvcral supplicrs. Thcrc is only onc supplicr in
cach city. Answcr thc lollowing qucstions:
a. !n which normal lorm is thc supplier tablc:
b. How can you rcsolvc thc cxisting lunctional dcpcndcncics:
.1.9
Lct R(A, 8, C) bc a rclation with thc lunctional dcpcndcncy: 8 C. (Thc attributcs A
and 8 build thc compositc kcy, and thc attributc C is lunctionally dcpcndcnt on 8.) !n
which normal lorm is thc rclation R:
.1.10
Lct R(A, 8, C) bc a rclation with thc lunctional dcpcndcncy: C 8. (Thc attributcs A
and 8 build thc compositc kcy, and thc attributc 8 is lunctionally dcpcndcnt on C.) !n
which normal lorm is thc rclation R:
This page intentionally left blank
In 7bis Cbapter
PIanning tbe InstaIIation
InstaIIing SL Server
Starting and Stopping an
Instance of Database ngine
Dedicated Connection
to an Instance of
Database ngine
Ilannlng thc Installatlon
and Installlng SQI Scrvcr
Cbapter 2
Copyright 2008 by The McGraw-Hill Companies. Click here for terms of use.
22 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
T
his chaptcr dcscribcs all tasks involvcd in thc installation ol thc SQL Scrvcr
systcm. First, thc ncccssary stcps in planning thc installation arc covcrcd. This is
lollowcd by discussion ol thc actual installation ol thc databasc scrvcr. Toward thc
cnd ol thc chaptcr, you will lcarn how to install, start, usc, and stop instanccs ol atabasc
nginc. You will also bc introduccd to thc dcdicatcd administration conncction, which can
bc uscd to conncct to an instancc ol atabasc nginc.
PIanning tbe InstaIIation
Thc spccilication ol an installation plan should always prcccdc thc actual installation
ol thc SQL Scrvcr systcm. Carclul planning is absolutcly ncccssary bccausc scvcral
dccisions havc to bc madc bclorc thc installation ol thc systcm is startcd. Thc systcm
administrator should havc clcar answcrs to thc lollowing qucstions bclorc bcginning thc
installation proccss:
Vhat is thc purposc ol thc SQL Scrvcr systcm:
Vhat arc thc hardwarc and nctwork rcquircmcnts:
How many uscrs will bc activc at thc samc timc:
/s ,ca c|/ec1, |sca, :9| :e/|e/ |s cs am|/e||c scme /c/ se|e/c| 1|//e/es| :cm(cses|s, sa:| cs |c|c|cse
|s|se, /sc|,s|s :e/||:es, |e(c/||s :e/||:es, cs1 |s|e/c||cs :e/||:es. |a/|s ||e |ss|c||c||cs (/c:ess, ,ca :cs
1e:|1e a||:| c/ ||ese :cm(cses|s ,ca acs| |c |ss|c|| cs ,ca/ :cm(a|e/. |s |||s :|c(|e/, :9| :e/|e/ /e/e/s |c c||
:cm(cses|s c/ ||c| s,s|em ||c| c/e |ss|c||e1 cs ,ca/ :cm(a|e/.
Purpose of tbe SL Server System
Thc purposc ol thc SQL Scrvcr systcm can bc manilold. For cxamplc, your systcm
may bc uscd cxclusivcly lor cducation, or it may bc a production systcm. !n thc casc ol
production systcms, you nccd to makc dccisions conccrning thc numbcr ol uscrs and thc
amount ol storcd data, bccausc thcsc systcms dillcr widcly. Anothcr dccision conccrning
modcrn databasc systcms is whcthcr thc systcm is uscd lor opcrational or analytical tasks.
!l yours is a largc databasc with a lcw hundrcd uscrs, or il your systcm carrics a hcavy
transaction load, pcrlormancc ol databasc opcrations will bc an issuc. !n both cascs, thc
usc ol multiproccssor computcrs will bc a gcncral rcquircmcnt to guarantcc scalability
and good rcsponsc timcs ol thc systcm. !l you havc a hugc databasc, insullicicnt disk
storagc could bc an issuc. !n this casc, thc systcm will usually pcrlorm bcttcr il you usc
scvcral smallcr disks instcad ol onc or two largc disks.
Cha pt er 2 : Pl a nni ng t he | ns t a l l a t i on a nd | ns t a l l i ng S QL S er v er 23
You must dillcrcntiatc bctwccn systcms uscd lor opcrational tasks (that is, systcms that
rcquirc last acccss and short transactions) and systcms uscd lor analytical tasks (systcms
that usc complcx rctricval opcrations on hugc databascs), bccausc both tasks cannot bc
optimally achicvcd using onc databasc scrvcr. For this rcason, atabasc nginc is uscd lor
opcrational tasks and Analysis Scrviccs is uscd lor analytical tasks. (For morc inlormation
on Analysis Scrviccs, scc Part !\ ol thc book.)
Hardware and Network Requirements
Thc lact that thc SQL Scrvcr systcm runs only on Microsolt opcrating systcms simplilics
dccisions conccrning hardwarc and nctwork rcquircmcnts. Thc systcm administrator has
to bc conccrncd only about thc hardwarc and nctwork rcquircmcnts.
Hardware Requirements
Vindows opcrating systcms arc supportcd on thc !ntcl and compatiblc systcm hardwarc
platlorms. Proccssor spccd should bc at a minimum 1 GHz.
ese/c||,, |ac :9| :e/|e/ e1|||cs /ca(s es|s|. ?1-||| cs1 +-|||. |e /ea|/emes|s /c/ ||ese |ac /ca(s 1|//e/.
|e/e/c/e, ||e |c|aes ||s|e1 |s |||s se:||cs c/e ese/c| |c|aes. |c/ esc:| /ea|/emes|s /c/ ec:| e1| ||cs, /e/e/ |c
||e :9| :e/|e/ 1^^? 3cc|s 0s||se c/||:|e |c/1ac/e cs1 :c/|ac/e |ea|/emes|s /c/ |ss|c|||s :9| :e/|e/ 1^^?,
a||:| ,ca :cs /|s1 c| |||(.//ms1s1.m|:/csc/|.:cm/es-as/|||/c/,/ms!+?:^(:9|.!^^.cs(s.
llicially, thc minimum rcquircmcnt lor main mcmory is 512M8. Howcvcr, almost
cvcrybody rccognizcs that such a minimal conliguration will not pcrlorm vcry wcll, and
as a gcncral guidclinc, main mcmory ol your computcr should bc at lcast 1G8 or morc.
Hard disk spacc rcquircmcnts dcpcnd on your systcm conliguration and thc applications
you choosc to install.
Network Requirements
To conncct to any SQL Scrvcr componcnts, you must havc a nctwork protocol cnablcd.
Thc SQL Scrvcr systcm can scrvc rcqucsts on scvcral protocols at oncc. Clicnts conncct
to thc systcm using a singlc protocol. !l thc clicnt program docs not know which protocol
thc systcm is listcning on, conligurc thc clicnt to scqucntially attcmpt multiplc protocols.
As a clicnt/scrvcr systcm, SQL Scrvcr allows clicnts to usc dillcrcnt nctwork protocols
to communicatc with thc scrvcr, and vicc vcrsa. uring conncctivity installation, thc systcm
administrator must dccidc which nctwork protocols (as librarics) should bc availablc to
24 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
givc clicnts acccss to thc systcm. Thc lollowing nctwork protocols can bc sclcctcd on thc
scrvcr sidc:
Sharcd mcmory
Transmission Control Protocol/!ntcrnct Protocol (TCP/!P)
Namcd Pipcs
\irtual !ntcrlacc Adaptcr (\!A) protocol
Conncctions to thc systcm lrom a clicnt running on thc samc computcr usc thc sharcd
mcmory protocol. Sharcd mcmory has no conligurablc propcrtics, and this protocol is
always tricd lirst.
Namcd Pipcs is an altcrnativc nctwork protocol on thc Vindows platlorms. Altcr thc
installation proccss, you can drop thc support lor Namcd Pipcs and usc anothcr nctwork
protocol lor communication bctwccn thc scrvcr and clicnts.
Thc TCP/!P nctwork protocol allows thc systcm to communicatc using standard
Vindows Sockcts as thc !ntcrnct protocol communication (!PC) mcthod across thc
TCP/!P protocol.
\irtual !ntcrlacc Adaptcr (\!A) protocol works with \!A hardwarc. For inlormation
about how to usc \!A, contact your hardwarc vcndor.
:|c/e1 memc/, |s sc| sa((c/|e1 cs /c||c|e/ :|as|e/s.

SL Server ditions
As you plan your installation, you nccd to know which kind ol SQL Scrvcr cditions
cxist. 8csidcs thc 32bit and 64bit cdition groups, Microsolt supports thc lollowing
cditions ol SQL Scrvcr 2008:
Express Edition
should bc uscd by application dcvclopcrs. For this rcason, thc product includcs thc
basic xprcss Managcr (XM) program and supports CLR intcgration and nativc
XML. Also, you can download SQL Scrvcr Managcmcnt xprcss lor SQL Scrvcr
xprcss, to casily managc a databasc. SQL Scrvcr xprcss is availablc as a lrcc
download at http://msdn.microsolt.com/cxprcss.
Cha pt er 2 : Pl a nni ng t he | ns t a l l a t i on a nd | ns t a l l i ng S QL S er v er 25
Workgroup Edition csigncd lor small busincsscs and should also bc uscd at
up to two proccssors and a maximum ol 2G8 ol RAM.
Standard Edition Providcs a 32bit vcrsion and 64bit support lor both x64 and
!taniumbascd systcms. !t also supports up to lour proccssors and includcs thc lull
rangc ol 8! lunctionality.
Enterprise Edition
lor timccritical applications with a hugc numbcr ol uscrs. !n contrast to Standard
dition, this cdition contains additional lcaturcs that can bc usclul lor vcry high
additional lcaturcs ol ntcrprisc dition arc data partitioning, databasc snapshots,
and onlinc databasc maintcnancc.
Developer Edition Allows dcvclopcrs to build and tcst any typc ol application
with SQL Scrvcr on 32 and 64bit platlorms. !t includcs all thc lunctionality
ol ntcrprisc dition, but is liccnscd only lor usc in dcvclopmcnt, tcsting, and
dcmonstration. ach liccnsc ol cvclopcr dition cntitlcs onc dcvclopcr to usc
thc soltwarc on as many systcms as ncccssary, additional dcvclopcrs can usc thc
soltwarc by purchasing additional liccnscs. For rapid dcploymcnt into production,
thc databasc systcm ol cvclopcr dition can casily bc upgradcd to ntcrprisc
dition.
Compact Edition A slim vcrsion ol SQL Scrvcr that you can usc on Pockct
PCs, smart phoncs, tablct PCs, and dcsktop PCs. Compact dition databascs arc
gcncrally uscd as cmbcddcd databascs lor dcsktopbascd applications.
InstaIIation Recommendations
uring thc installation proccss, you havc to makc many choiccs. As a gcncral guidclinc,
it is bcttcr to lamiliarizc yourscll with thcir cllccts bclorc running thc Sctup program.
You should answcr thc lollowing qucstions bclorc you start thc installation proccss:
Vhcrc will thc root dircctory bc storcd:
Should multiplc instanccs ol atabasc nginc bc uscd:
Vhich authcntication modc lor atabasc nginc should bc uscd:
26 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
3e/c/e ,ca s|c/| ||e |ss|c||c||cs (/c:ess, ,ca s|ca|1 esc:||, |sca a||:| :9| :e/|e/ 1^^? :cm(cses|s ,ca acs|
|c |ss|c|| (see c|sc ||a/e 1-+. |/ sc|, (|ecse |ec/s mc/e c|ca| ||e es|s||s :cm(cses|s cs1 mc|e ,ca/ 1e:|s|cs
sca. (/cs| c/ ||ese :cm(cses|s c/e 1es:/||e1 |s 1e|c|| |s |||s |cc|. |e 1es:/|(||cs c/ c||e/ :cm(cses|s :cs |e
/cas1 |s 3cc|s 0s||se.
Where to Store the Root Directory
Thc root dircctory is whcrc thc Sctup program storcs all program lilcs and thosc lilcs
that do not changc as you usc thc SQL Scrvcr systcm. 8y dclault, thc installation
proccss storcs all program lilcs in thc subdircctory Microsoft SQL Server, although
you can changc this sctting during thc installation proccss. Using thc dclault namc is
rccommcndcd bccausc it uniqucly dctcrmincs thc vcrsion ol thc systcm.
How Many Instances of Database ngine to Run
Vith atabasc nginc, you can install and usc scvcral dillcrcnt instanccs. An instance is
a databasc scrvcr that docs not sharc its systcm and uscr databascs with othcr instanccs
(scrvcrs) running on thc samc computcr.
Thcrc arc two instancc typcs:
clault
Namcd
Thc default instance ol thc databasc scrvcr opcratcs thc samc way as thc databasc
scrvcrs in carlicr vcrsions ol SQL Scrvcr, whcrc only onc databasc scrvcr without
instancc support cxistcd. Thc computcr namc on which thc instancc is running spccilics
solcly thc namc ol thc dclault instancc. Any instancc ol thc databasc scrvcr othcr than
thc dclault instancc is callcd a named instance. To idcntily a namcd instancc, you havc to
spccily its namc as wcll as thc namc ol thc computcr on which thc instancc is running:
lor cxamplc, NT811901\!NSTANC1. n onc computcr, thcrc can bc any numbcr ol
namcd instanccs (in addition to thc dclault instancc). Additionally, you can conligurc
namcd instanccs on a computcr that docs not havc thc dclault instancc.
Although all instanccs running on a computcr do not sharc most systcm rcsourccs
(SQL Scrvcr and SQL Scrvcr Agcnt scrviccs, systcm and uscr databascs, as wcll as
rcgistry kcys), thcrc arc somc componcnts that arc sharcd among thcm:
SQL Scrvcr program group
Analysis Scrviccs scrvcr
cvclopmcnt librarics
Cha pt er 2 : Pl a nni ng t he | ns t a l l a t i on a nd | ns t a l l i ng S QL S er v er 27
Thc cxistcncc ol only onc SQL Scrvcr program group on a computcr also mcans
that only onc copy ol cach utility cxists, which is rcprcscntcd by an icon in thc program
group. (This includcs SQL Scrvcr 8ooks nlinc, too.) Thcrclorc, cach utility works
with all instanccs conligurcd on a computcr.
You should considcr using multiplc instanccs il:

Your computcr is powcrlul cnough to managc multiplc instanccs
Thc main purposc ol multiplc instanccs is to dividc databascs that cxist in your
organization into dillcrcnt groups. For instancc, il thc systcm managcs databascs that
arc uscd by dillcrcnt uscrs (production databascs, tcst databascs, and samplc databascs),
you should dividc thcm to run undcr dillcrcnt instanccs. That way you can cncapsulatc
your production databascs lrom databascs that arc uscd by casual or incxpcricnccd uscrs.
A singlcproccssor machinc will not bc thc right hardwarc platlorm to run multiplc
instanccs ol atabasc nginc, bccausc ol limitcd rcsourccs. For this rcason, you should
considcr thc usc ol multiplc instanccs only with multiproccssor computcrs.
Which Authentication Mode to Choose
!n rclation to atabasc nginc, thcrc arc two dillcrcnt authcntication modcs:
Windows mode
systcm using thcir uscr accounts and group mcmbcrships.
Mixed mode Allows uscrs to conncct to atabasc nginc using Vindows
can bc sct up to usc Vindows sccurity subsystcm, whilc othcrs can usc thc SQL
Scrvcr sccurity subsystcm in addition to thc Vindows sccurity subsystcm.
Microsolt rccommcnds thc usc ol Vindows modc. (For dctails, scc Chaptcr 12.)
InstaIIing SL Server
!l you havc donc an installation ol a complcx soltwarc product bclorc, you probably
rccognizc that lccling ol unccrtainty that accompanics starting thc installation lor thc
lirst timc. This lccling comcs lrom thc complcxity ol thc product to bc installcd and thc
divcrsity ol qucstions to bc answcrcd during thc installation proccss. 8ccausc you may
not complctcly undcrstand thc product, you (or thc pcrson who installs thc soltwarc)
28 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
may bc lcss than conlidcnt that you can givc accuratc answcrs lor all thc qucstions
that thc Sctup program asks to complctc its tasks. This scction will hclp you to lind
your way through thc installation by giving you answcrs to most ol thc qucstions
bclorchand.
To bcgin thc installation, inscrt thc SQL Scrvcr 2008 \ into your \ drivc.
Thc nstall Shield wizard opcns and prompts you to spccily thc location in which to
savc thc cxtractcd lilcs. Vhcn you click Ncxt, thc nstall Shield wizard cxtracts all
ncccssary lilcs lrom thc \ and complctcs its task.
efore Starting tbe Setup Program
Thcrc arc scvcral tasks that you should complctc bclorc starting thc Sctup program.
Thc bcst placc to start is to rcad thc Rclcasc Notcs. Thc bcnclit ol Rclcasc Notcs is
that thcy contain thc ncwcst inlormation, which is not ncccssarily providcd in SQL
Scrvcr 8ooks nlinc (scc Chaptcr 3 lor morc dctails about 8ooks nlinc). Rcad this
inlormation carclully to gct a picturc ol lcaturcs that arc modilicd shortly bclorc thc
dclivcry ol thc linal rclcasc. (Rclcasc Notcs also contain thc list ol bugs, it might provc
usclul lor you to know about somc ol thcm.)
Microsolt's wcb sitc is an additional sourcc lor lurthcr inlormation. As with Rclcasc
Notcs, you can lind many intcrcsting documcnts on thc Microsolt wcb sitc. spccially
important arc whitc papcrs that Microsolt providcs during thc implcmcntation ol
cach ncw SQL Scrvcr vcrsion. n thc Microsolt sitc, you can scarch lor and download
documcnts that arc ol intcrcst to you.
Starting tbe Setup Program and InstaIIing Prerequisites
n thc nstallation Prerequisites pagc (scc Figurc 21), SQL Scrvcr 2008 Sctup idcntilics
which additional soltwarc componcnts arc ncccssary, bclorc you start thc installation ol
SQL Scrvcr 2008. To bcgin this proccss, click nstall.
InstaIIing SL Server Components
Altcr thc succcsslul installation ol prcrcquisitcs, thc installation proccss automatically
shows thc SQL Server nstallation Center (scc Figurc 22) with thc tasks that can bc
cxccutcd. To install SQL Scrvcr 2008, choosc New nstallation. This stcp launchcs a
wizard that guidcs you during thc installation proccss.
n thc lirst pagc ol thc wizard, thc System Configuration Check pagc (scc Figurc 23),
thc Sctup program scans your computcr lor conditions that may block thc installation
proccss. !l you click thc Show details button, you can scc all dctails conccrning thc
Cha pt er 2 : Pl a nni ng t he | ns t a l l a t i on a nd | ns t a l l i ng S QL S er v er 29
chcck proccss. To procccd with thc Sctup proccss, click Next. n thc Registration
nformation pagc, cntcr inlormation in thc corrcsponding tcxt boxcs and click Next.
n thc Ieature Selection pagc (scc Figurc 24), sclcct thc componcnts to install by
chccking thc corrcsponding chcck boxcs. Also, toward thc bottom ol thc pagc, you can
spccily thc dircctory in which to storc thc sharcd componcnts. Altcr that, click Next to
continuc.
||qcre 1-1 |ss|c||c||cs |/e/ea|s||es (ce
||qcre 1-1 :9| :e/|e/ |ss|c||c||cs tes|e/
30 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
||qcre 1-1 :,s|em tcs/|a/c||cs t|e:| (ce
||qcre 1-4 |ec|a/e :e|e:||cs (ce
Cha pt er 2 : Pl a nni ng t he | ns t a l l a t i on a nd | ns t a l l i ng S QL S er v er 31
tcm(cses|s c/ :9| :e/|e/ ||c| c/e se|e:|e1 a||| |e |ss|c||e1 cse c/|e/ ||e c||e/ |s ||e c/1e/ |s a||:| ||e, c/e
||s|e1 cs ||e |ec|a/e :e|e:||cs (ce. |e |ss|c||c||cs (/c:ess s|c/|s a||| ||e |ss|c||c||cs c/ |c|c|cse |s|se,
/c||cae1 |, ||e |ss|c||c||cs c/ /sc|,s|s :e/||:es, cs1 sc cs. 0s|, ||e se|e:|e1 :cm(cses|s a||| |e | ss|c||e1.
n thc nstance Configuration pagc (scc Figurc 25), you can choosc bctwccn thc
installation ol a dclault or namcd instancc. (A dctailcd discussion ol instanccs can bc
lound in thc scction "How Many !nstanccs ol atabasc nginc to Run" in this chaptcr.)
To install thc dclault instancc, click Default instance. !l a dclault instancc is alrcady
installcd and you sclcct Default instance, thc Sctup program upgradcs it and additionally
allows you to install additional componcnts. (Thcrclorc, you havc anothcr opportunity to
install componcnts that you skippcd in thc prcvious installation proccsscs.)
To install a ncw namcd instancc, click Named instance and thcn typc a ncw namc
in thc givcn tcxt box. !n thc lowcr part ol thc pagc, you can scc thc list ol instanccs
alrcady installcd on your systcm. As you can scc lrom Figurc 25, thc computcr on
which ! installcd thc instancc callcd !NSTANC1 alrcady contains thc dclault instancc.
(MSSQLSR\R is thc namc ol thc dclault instancc lor atabasc nginc.) Click Next
to continuc.
||qcre 1- |ss|cs:e tcs/|a/c||cs (ce
32 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Thc ncxt pagc, Server Configuration (scc Figurc 26), allows you to spccily uscrnamcs
and corrcsponding passwords lor scrviccs ol all componcnts that will bc installcd in thc
installation proccss. (You can apply onc account lor all scrviccs.)
To choosc thc collation ol your instancc, click thc Collation tab ol thc Server
Configuration pagc (scc Figurc 27). (Collation dclincs thc sorting bchavior lor your
instancc.) You can cithcr choosc thc dclault collations lor thc componcnts that will bc
installcd, or click Customize to sclcct somc othcr collations that arc supportcd by thc
systcm. Click Next to continuc.
n thc Database Engine Configuration pagc (scc Figurc 28), you choosc thc
authcntication modc lor your atabasc nginc systcm. As you alrcady know, atabasc
nginc supports Vindows authcntication modc and Mixcd modc. !l you sclcct thc
Windows authentication mode radio button, thc Sctup proccss crcatcs thc sa (systcm
administrator) login, which is disablcd by dclault. (For thc discussion ol accounts, scc
Chaptcr 12.) !l you choosc thc Mixed Mode radio button, you must cntcr and conlirm
thc systcm administrator login. Click Add Current User il you want to add onc or
morc uscrs that will havc unrcstrictcd acccss to thc instancc ol atabasc nginc.
||qcre 1-6 :e/|e/ tcs/|a/c||cs (:e/||:e /::cas|s |c|
Cha pt er 2 : Pl a nni ng t he | ns t a l l a t i on a nd | ns t a l l i ng S QL S er v er 33
||qcre 1-I :e/|e/ tcs/|a/c||cs (tc||c||cs |c|
||qcre 1-8 |c|c|cse |s|se tcs/|a/c||cs (/::cas| |/c||s|cs|s |c|
34 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
ca :cs :|cse ||e |s/c/mc||cs :cs:e/s|s c::cas| (/c||s|cs|s c/|e/ |ss|c||c||cs. |s ||c| :cse, ,ca |c|e |c
/es|c/| ||e |c|c|cse |s|se se/||:e :c||e1 /::9|:||l||.
Thc othcr tab ol thc Database Engine Configuration pagc, Data Directories (scc
Figurc 29), allows you to spccily thc locations lor all thc dircctorics in which atabasc
ngincrclatcd lilcs arc storcd. (Thc ncw lcaturc ol SQL Scrvcr 2008 is that thc storagc
location ol thc tempdb systcm databasc is spccilicd during thc installation proccss.) Click
Next to continuc.
Vhat appcars lor thc ncxt stcp dcpcnds on whcthcr or not you chosc to install
Analysis Scrviccs. (A Configuration pagc appcars lor cach SQL Scrvcr componcnt
that you chosc to install.) !l you did choosc to install it, Figurc 210 will appcar. Click
Next to continuc.
Similarly, what appcars lor thc ncxt stcp dcpcnds on whcthcr or not you dccidcd to
install Rcporting Scrviccs. !l you indicatcd that Rcporting Scrviccs should bc installcd,
thc Reporting Services Configuration pagc appcars. !n this stcp, you can dccidc just to
||qcre 1-9 |c|c|cse |s|se tcs/|a/c||cs (|c|c ||/e:|c/|es |c|
Cha pt er 2 : Pl a nni ng t he | ns t a l l a t i on a nd | ns t a l l i ng S QL S er v er 35
install thc rcport scrvcr (without its conliguration) or to install and conligurc it. Thc third
altcrnativc is to intcgratc thc rcport scrvcr with Microsolt llicc SharcPoint Scrvcr
(a scrvcr program that can bc uscd to lacilitatc collaboration, providc contcnt managcmcnt
lcaturcs, and implcmcnt busincss proccsscs). Altcr that, click Next to continuc.
n thc Error and Usage Reporting pagc (scc Figurc 211), spccily thc inlormation,
il any, that you would likc to scnd to Microsolt automatically. Clcar both chcck boxcs il
you do not want to takc part in this automatic rcporting. Click Next.
Thc last pagc, bclorc thc installation proccss actually starts, is thc Ready to nstall
pagc. This pagc allows you to rcvicw thc summary ol all SQL Scrvcr componcnts that
will bc installcd. To start thc installation proccss, click nstall. As you can scc lrom
Figurc 212, Sctup allows you to lollow thc progrcss ol your installation proccss. !l thc
installation proccss succccds, click Next.
Thc Complete pagc appcars, with thc location ol thc lilc in which thc summary log
is storcd. Click Close to complctc thc installation proccss. Altcr that you can usc all
componcnts, which you installcd during thc installation proccss.
Thc ncxt scction dcscribcs thc usc ol an instancc ol atabasc nginc.
||qcre 1-1 /sc|,s|s :e/||:es tcs/|a/c||cs (ce
36 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
||qcre 1-11 |//c/ cs1 |sce |e(c/||s (ce
||qcre 1-11 |ss|c||c||cs |/c/ess (ce
Cha pt er 2 : Pl a nni ng t he | ns t a l l a t i on a nd | ns t a l l i ng S QL S er v er 37
Starting and Stopping an
Instance of Database ngine
Thc most convcnicnt way to start an instancc ol atabasc nginc is automatically
with thc boot proccss ol thc computcr. Howcvcr, ccrtain circumstanccs might rcquirc
dillcrcnt handling ol thc systcm. Thcrclorc, atabasc nginc ollcrs scvcral options lor
starting an instancc:
SQL Scrvcr Managcmcnt Studio (scc Chaptcr 3)
sqlservr application
net command
Thc sqlservr application can bc uscd only to start an instancc ol atabasc nginc.
This application is invokcd using thc lollowing command:
sqlscrvr option_list
option_list contains all options that can bc invokcd using thc application. Tablc 21
dcscribcs thc most important options.
Thc net start, net stop, and net pause commands start, stop, and pausc an
instancc ol atabasc nginc, rcspcctivcly. Thc net command in xamplc 2.1 starts
thc MSSQLSR\R scrvicc.
XAMPL 2.1
nct start mssqlscrvcr
0pt|ea 0escr|pt|ea
-l |aa|c+te. t|+t t|e |a.t+ace |. .t+rtea w|t| t|e m|a|m+| ceal|ar+t|ea.
-m |aa|c+te. t|+t t|e |a.t+ace |. .t+rtea |a .|a|e-a.er meae. J.e t||. ejt|ea |l ea |+.e
jre||em. w|t| t|e ..tem +aa w+at te jerlerm m+|atea+ace ea |t (t||. ejt|ea ma.t |e a.ea
te re.tere t|e master a+t+|+.e}.
-. |ss|cs:e_scme :jec|l|e. t|e |a.t+ace el |+t+|+.e |a|ae. |l ae a+mea |a.t+ace |. .jec|l|ea, s|serr .t+rt.
t|e ael+a|t |a.t+ace el |+t+|+.e |a|ae.
Iah|e 1-1 /cs| |m(c/|cs| 0(||css c/ ||e s|se/|/ /((||:c||cs
38 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Dedicated Connection to an
Instance of Database ngine
Altcr thc succcsslul start ol an instancc ol atabasc nginc, thc ncxt stcp is to makc
a conncction to it. Gcncrally, you can makc thc conncction by using SQL Scrvcr
Managcmcnt Studio (scc Chaptcr 3). 8ut, thcrc arc ccrtain cxtraordinary situations
in which "normal" uscrs cannot conncct to thc instancc. !n that casc, you can usc thc
dcdicatcd administrator conncction (AC).
Thc AC is a spccial conncction that can bc uscd by 8As in casc ol cxtrcmc scrvcr
rcsourcc dcplction. vcn whcn thcrc arc not cnough rcsourccs lor othcr uscrs to conncct,
atabasc nginc will attcmpt to lrcc rcsourccs lor thc AC.
|/t |s c|c||c||e cs1 sa((c/|e1 ||/ca| ||e a|||||, as|s ||e c(||cs. (|c/ ||e 1es:/|(||cs c/ ||e
a|||||,, see t|c(|e/ !:.
ConcIusion
Thc installation ol SQL Scrvcr also rcquircs somc work in thc prcinstallation and
postinstallation phascs. !n thc prcinstallation phasc, thc systcm administrator dctcrmincs
soltwarc and hardwarc rcquircmcnts and prcparcs dillcrcnt spccilications lor thc
installation proccss.
Thc installation ol SQL Scrvcr 2008 is strcamlincd comparcd to thc prcvious vcrsion.
!nstallation is dividcd in two phascs. !n thc lirst phasc, Sctup gathcrs thc ncccssary
inlormation lrom you, including which componcnts you want to install, and altcr that
starts thc actual installation proccss.
Thc ncxt chaptcr dcscribcs thc most important componcnt ol SQL Scrvcr callcd SQL
Scrvcr Managcmcnt Studio.

In 7bis Cbapter
SL Server Program Croup
and ooks DnIine
Introduction to SL Server
Management Studio
SQI Scrvcr
Managcmcnt Studlo
Cbapter 3
Copyright 2008 by The McGraw-Hill Companies. Click here for terms of use.
40 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
T
his chaptcr lirst discusscs briclly thc SQL Scrvcr program group and SQL
Scrvcr 8ooks nlinc, thc onlinc rclcrcncc sourcc with all ncccssary inlormation
conccrning SQL Scrvcr. !t thcn introduccs SQL Scrvcr Managcmcnt Studio,
which is discusscd in dctail in rclation to atabasc nginc and activitics ol cnd uscrs.
You will also lcarn all thc lunctions ol Managcmcnt Studio, which arc ncccssary lor
crcating and cxccuting any TransactSQL statcmcnts.
SL Server Program Croup and ooks DnIine
To scc thc SQL Scrvcr program group, click Start, All Programs, and linally Microsoft
SQL Server 2008. Thc SQL Scrvcr program group compriscs all applications you will usc
during your work with this systcm. This chaptcr looks only at two particular applications,
SQL Scrvcr 8ooks nlinc (8L), which you can lind in thc Documentation and
Tutorials dircctory, and SQL Scrvcr Managcmcnt Studio, thc primary locus ol this
chaptcr. 8ooks nlinc is discusscd briclly hcrc.
8ooks nlinc is thc onlinc documcntation that is dclivcrcd and installcd with all
othcr SQL Scrvcr soltwarc componcnts. Thc toolbar ol thc Books Online window has,
among othcrs, buttons lor lour dillcrcnt lcaturcs:
Contcnts
!ndcx
Scarch
Hclp Favoritcs
!l you click thc Contents button, you scc thc contcnts ol thc wholc onlinc
documcntation dividcd into dillcrcnt books. ach spccilic topic is dividcd into subtopics,
which can bc opcncd by clicking thc corrcsponding plus () symbol ol thc trcc. You can
vicw thc contcnt ol cach subloldcr in thc samc waythat is, by clicking thc symbols ol
thc corrcsponding book and all supcrloldcrs ol that loldcr.
Thc ndex panc shows an alphabctically sortcd scqucncc ol cach kcyword that appcars
in thc onlinc documcntation. Thcrc arc two ways to sclcct onc ol thc kcywords: by
doublcclicking thc kcyword in thc list or by typing thc starting lcttcrs ol it. !n thc lattcr
casc, thc systcm sclccts (and highlights) thc alphabctically lirst kcyword in thc list that has
thc typcd lcttcr(s) at its bcginning.
Thc Search button is thc most uscd button. !t allows you to typc a phrasc (or a
singlc word) that is thcn uscd in thc scarch proccss to display all topics in which that
phrasc or word appcars. (SQL Scrvcr 2008 uscs its own Full Tcxt Scarch componcnt
to support this proccss.) 8csidcs thc local scarch in 8ooks nlinc, SQL Scrvcr 2008
scarchcs thc phrasc also in MSN nlinc and Codczonc Community. (Thc Codczonc
Cha pt er 3 : S QL S er ver Ma na gement St udi o 41
Community is a sitc lor thirdparty tips, codc samplcs, adviccs, and ncws lrom
indcpcndcnt cxpcrts on thc Microsolt .NT Framcwork and Microsolt \isual Studio.)
Thc rcsults ol thc scarch proccss arc displaycd in thc middlc ol thc dclault panc.
Altcr clicking thc Search button, thc corrcsponding rcsults appcar in thc panc. ach
rcsult includcs thc titlc ol thc documcnt and a short dcscription ol thc topic. Thc total
numbcr ol scarch rcsults appcars also in thc uppcrright corncr ol thc panc.
Thc Help Iavorites tab allows you to storc sclcctcd kcywords (and phrascs). Usc this
tab to display links to itcms, such as Hclp topics and wcb pagcs, that contain important
inlormation you might want to rcad again. This window also savcs scarch qucrics so
that you can savc complcx scarchcs lor luturc usc. To bookmark a topic or wcb sitc,
lirst opcn thc topic or wcb sitc that you want to add to your list ol lavoritcs. Thcn, on
thc Standard toolbar, click thc Add to Help Iavorites icon. To sclcct again onc ol thc
storcd topics, doublcclick thc cntry in thc list ol your lavoritcs.
Introduction to SL Server Management Studio
SQL Scrvcr 2008 providcs various tools that arc uscd lor dillcrcnt purposcs, such as
installation, conliguration, auditing, and pcrlormancc tuning. (All thcsc tools will bc
discusscd in dillcrcnt chaptcrs ol this book.)
||s :|c(|e/ |s 1e1|:c|e1 |c ||e c:||||||es c/ ||e es1 ase/. |e/e/c/e, cs|, ||e /as:||csc|||, c/ :9| :e/|e/
/cscemes| :|a1|c a||| /es(e:| |c ||e :/ec||cs c/ 1c|c|cse c|(e:|s |s 1es:/||e1 |s 1e|c||. /|| c1m|s|s|/c|||e
|cs|s cs1 c|| |cs|s /e|c|e1 |c /sc|,s|s :e/||:es cs1 c||e/ :cm(cses|s ||c| |||s |cc| sa((c/|s c/e 1| s:asse1
|e|ss|s |s |c/| |||.
Thc administrator's primary tool lor intcracting with thc systcm is SQL Scrvcr
Managcmcnt Studio. 8oth administrators and cnd uscrs can usc this tool to administcr
multiplc scrvcrs, dcvclop databascs, and rcplicatc data, among othcr things. To opcn this
tool, click thc Start mcnu, All Programs, Microsoft SQL Server 2008, and thcn SQL
Server Management Studio in thc SQL Scrvcr program group. vcry uscr with acccss
to thc particular databasc scrvcr can also usc SQL Scrvcr Managcmcnt Studio.
SQL Scrvcr Managcmcnt Studio compriscs scvcral dillcrcnt componcnts that arc
uscd lor thc authoring, administration, and managcmcnt ol thc ovcrall systcm. Thc
lollowing arc thc main componcnts uscd lor thcsc tasks:
Rcgistcrcd Scrvcrs
bjcct xplorcr
Qucry ditor
Solution xplorcr
42 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Thc lirst two componcnts arc discusscd in this introduction to SQL Scrvcr
Managcmcnt Studio. Qucry ditor and Solution xplorcr arc cxplaincd latcr in this
chaptcr, in thc scction "Authoring Activitics Using SQL Scrvcr Managcmcnt Studio."
To gct to thc main intcrlacc, you lirst must conncct to a scrvcr, as dcscribcd ncxt.
Connecting to a Server
Vhcn you opcn SQL Scrvcr Managcmcnt Studio, it displays thc Connect to Server
dialog box (scc Figurc 31), which allows you to spccily thc ncccssary paramctcrs to
conncct to a scrvcr:
Server Type For purposcs ol this chaptcr, choosc Database Engine.
||| :9| :e/|e/ /cscemes| :|a1|c, ,ca :cs mcsce c|(e:|s c/ |c|c|cse |s|se cs1 /sc|,s|s :e/|e/, cmcs
c||e/s. ||s :|c(|e/ 1emcss|/c|es ||e ase c/ /cscemes| :|a1|c cs|, a||| |c|c|cse |s|se.
Server Name Sclcct or typc thc namc ol thc scrvcr that you want to usc.
(Gcncrally, you can conncct SQL Scrvcr Managcmcnt Studio to any ol thc
installcd products on a particular scrvcr.)
||qcre 1-1 |e tcsse:| |c :e/|e/ 1|c|c |cs
Cha pt er 3 : S QL S er ver Ma na gement St udi o 43
Authentication Choosc bctwccn thc two authcntication typcs:
Windows Authentication Conncct to SQL Scrvcr using your Vindows
Microsolt.
SQL Server Authentication atabasc nginc uscs its own authcntication.
|c/ mc/e |s/c/mc||cs :cs:e/s|s :9| :e/|e/ /a||es||:c||cs, see t|c(|e/ !1.

Vhcn you click Connect, atabasc nginc connccts to thc spccilicd scrvcr. Altcr
connccting to thc databasc scrvcr, thc dclault SQL Scrvcr Managcmcnt Studio
window appcars (scc Figurc 32). Thc dclault appcarancc is similar to \isual Studio, so
uscrs can lcvcragc thcir cxpcricncc ol dcvcloping in \isual Studio to usc SQL Scrvcr
Managcmcnt Studio morc casily.
||qcre 1-1 :9| :e/|e/ /cscemes| :|a1|c. 1e/ca|| a|s1ca se|||ss
44 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
:9| :e/|e/ /cscemes| :|a1|c ||es ,ca c as|ae |s|e//c:e |c mcsce se/|e/s cs1 :/ec|e ae/|es c:/css c|| :9|
:e/|e/ :cm(cses|s. ||s mecss ||c| :9| :e/|e/ /cscemes| :|a1|c c//e/s cse |s|e//c:e /c/ |c|c|cse |s|se,
/sc|,s|s :e/||:es, |s|e/c||cs :e/||:es, cs1 |e(c/||s :e/||:es.
Registered Servers
Registered Servers is rcprcscntcd as a panc that allows you to maintain conncctions
to alrcady uscd scrvcrs. You can usc thcsc conncctions to chcck a scrvcr's status or
to managc its objccts. ach uscr has a scparatc list ol rcgistcrcd scrvcrs, which is
storcd locally. (!l thc Registered Servers panc isn't visiblc, sclcct its namc lrom thc
View mcnu.)
You can add ncw scrvcrs to thc list ol all scrvcrs, or rcmovc onc or morc cxisting
scrvcrs lrom thc list. You also can group cxisting scrvcrs into scrvcr groups. ach group
should contain thc scrvcrs that bclong togcthcr logically. You can also group scrvcrs
by scrvcr typc, such as atabasc nginc, Analysis Scrviccs, Rcporting Scrviccs, and
!ntcgration Scrviccs.
Dbject xpIorer
Thc Object Explorer panc contains a trcc vicw ol all thc databasc objccts in a scrvcr.
(!l thc Object Explorer panc isn't visiblc, sclcct Object Explorer lrom thc View mcnu.)
Thc trcc shows you a hicrarchy ol thc objccts on a scrvcr. Hcncc, il you cxpand a trcc,
thc logical structurc ol a corrcsponding scrvcr will bc shown.
To conncct Object Explorer to a scrvcr, rightclick thc scrvcr namc and choosc
Connect. To disconncct, click Disconnect in thc toolbar and sclcct thc Disconnect
lunction.
Object Explorer allows you to conncct to multiplc scrvcrs in thc samc panc. Thc scrvcr
can bc any ol thc cxisting scrvcrs lor atabasc nginc, Analysis Scrviccs, Rcporting
Scrviccs, or !ntcgration Scrviccs. This lcaturc is uscrlricndly, bccausc it allows you to
managc all scrvcrs ol thc samc or dillcrcnt typcs lrom onc placc.
0eject fs/etet |cs se|e/c| c||e/ /ec|a/es, es(|c|se1 |c|e/ |s |||s :|c(|e/.

Cha pt er 3 : S QL S er ver Ma na gement St udi o 45
Drganizing and Navigating Management Studio's Panes
You can dock or hidc cach ol thc pancs ol Managcmcnt Studio. 8y rightclicking thc
titlc bar at thc top ol thc corrcsponding panc, you can choosc bctwccn thc lollowing
prcscntation possibilitics:
Iloating
window on top ol thc rcst ol SQL Scrvcr Managcmcnt Studio windows. Such a
window can bc movcd anywhcrc around thc scrccn.
Dockable nablcs you to movc pancs ol SQL Scrvcr Managcmcnt Studio and
Tabbed Document You can crcatc a tabbcd grouping using thc csigncr window.
Vhcn this is donc, thc panc's statc changcs lrom dockablc to tabbcd documcnt.
Hide Closcs thc window. (Altcrnativcly, you can click thc in thc uppcrright
corncr ol thc window.) To display a closcd window, sclcct thc componcnt namc
lrom thc View mcnu.
Auto Hide Minimizcs thc panc and storcs it on thc lclt sidc ol thc scrccn. To
rcopcn (maximizc) such a panc, movc your mousc ovcr thc tabs on thc lclt sidc ol
thc scrccn and click thc push pin to pin thc panc in thc opcn position.
|e 1|//e/es:e |e|aees ||e h/4e cs1 4ate h/4e c(||css |s ||c| ||e /c/me/ c(||cs /emc|es ||e (cse //cm :9|
:e/|e/ /cscemes| :|a1|c, a|||e ||e |c||e/ :c||c(ses ||e (cse |c ||e s|1e (cse|.
To rcstorc thc dclault conliguration, click thc Window mcnu and sclcct Reset
Window Layout. Thc Object Explorer panc appcars on thc lclt, whilc thc Object
Explorer Details tab appcars on thc right sidc ol SQL Scrvcr Managcmcnt Studio.
(Thc Object Explorer Details tab displays inlormation about thc currcntly sclcctcd
nodc ol Object Explorer.)
ca a||| /|s1 ||c| c/|es ||e/e c/e se|e/c| ac,s c/ c::cm(||s||s ||e scme |cs| a||||s :9| :e/|e/ /cscemes| :|a1|c.
||s :|c(|e/ a||| |s1|:c|e mc/e ||cs cse ac, |c 1c |||ss, a|e/ecs cs|, c s|s|e me||c1 a||| |e ||es |s sa|seaes|
:|c(|e/s. ||//e/es| (ec(|e (/e/e/ 1|//e/es| me||c1s (scme |||e |c 1ca||e-:||:|, scme |||e |c :||:| ||e +/- s|ss, scme
|||e |c /|||-:||:|, c||e/s |||e |c ase ||e (a||-1cas mesas, cs1 c||e/s |||e |c ase ||e |e,|cc/1 s|c/|:a|s cs ma:| cs
(css|||e. |s(e/|mes| a||| ||e 1|//e/es| ac,s |c sc||c|e, cs1 ase ||e me||c1s ||c| /ee| mcs| sc|a/c| |c ,ca.
46 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Vithin thc Object Explorer and Registered Servers pancs, a subobjcct appcars
only il you click thc plus () sign ol its dircct prcdcccssor in thc trcc hicrarchy. To scc
thc propcrtics ol an objcct, rightclick thc objcct and choosc Properties. A minus ()
sign to thc lclt ol an objcct's namc indicatcs that thc objcct is currcntly cxpandcd. To
comprcss all subobjccts ol an objcct, click its minus sign. (Anothcr possibility would bc
Using Management Studio witb Database ngine
SQL Scrvcr Managcmcnt Studio has two main purposcs:
Administration ol thc databasc scrvcrs
Managcmcnt ol databasc objccts
Thc lollowing scctions dcscribc thcsc lcaturcs ol SQL Scrvcr Managcmcnt Studio.
Administering Database Servers
Thc administration tasks that you can pcrlorm by using SQL Scrvcr Managcmcnt
Studio arc, among othcrs, thc lollowing:
Rcgistcr scrvcrs
Connccting to a scrvcr
Crcatc ncw scrvcr groups
Start and stop SQL Scrvcr
Thc lollowing subscctions dcscribc thcsc administration tasks.
Registering Servers
SQL Scrvcr Managcmcnt Studio scparatcs thc activitics ol rcgistcring scrvcrs and
cxploring databascs and thcir objccts. (8oth ol thcsc activitics can bc donc using bjcct
xplorcr.) vcry scrvcr (local or rcmotc) must bc rcgistcrcd bclorc usc. A scrvcr can
bc rcgistcrcd during thc lirst cxccution ol SQL Scrvcr Managcmcnt Studio or latcr.
To rcgistcr a databasc scrvcr, rightclick thc loldcr ol your databasc scrvcr in Object
Explorer and choosc Register. (!l thc Object Explorer panc docsn't appcar on your
scrccn, sclcct View and click Object Explorer.) Thc New Server Registration dialog box
appcars, as shown in Figurc 33. Choosc thc namc ol thc scrvcr that you want to rcgistcr
and thc authcntication modc (Vindows Authcntication or SQL Scrvcr Authcntication).
Cha pt er 3 : S QL S er ver Ma na gement St udi o 47
Connecting to a Server
SQL Scrvcr Managcmcnt Studio also scparatcs thc tasks ol rcgistcring a scrvcr and
connccting to a scrvcr. This mcans that rcgistcring a scrvcr docs not automatically
conncct you to thc scrvcr. To conncct to a scrvcr lrom thc Object Explorer window,
rightclick thc scrvcr namc and choosc Connect.
Creating a New Server Croup
To crcatc a ncw scrvcr group in thc Registered Servers panc, rightclick Local Server
Groups and choosc New Server Group. !n thc New Server Group propcrtics dialog
box, cntcr a (uniquc) group namc and optionally dcscribc thc ncw group.
||qcre 1-1 |e |ea :e/|e/ |e|s|/c||cs 1|c|c |cs
48 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Managing MuItipIe Servers
SQL Scrvcr Managcmcnt Studio allows you to administcr multiplc databasc scrvcrs
(callcd instanccs) on onc computcr by using Object Explorer. ach instancc ol atabasc
nginc has its own sct ol databasc objccts (systcm and uscr databascs) that arc not sharcd
bctwccn dillcrcnt instanccs.
To managc a scrvcr and its conliguration, rightclick thc scrvcr namc in Object
Explorer and choosc Properties. Thc Server Properties dialog box (scc Figurc 34)
contains scvcral dillcrcnt pagcs, such as General, Security, and Permissions.
Thc General pagc shows gcncral propcrtics ol thc scrvcr. Thc Security pagc contains
thc inlormation conccrning thc authcntication modc ol thc scrvcr and thc login auditing
modc. Thc Permissions pagc shows all logins and rolcs that can acccss thc scrvcr. Thc
lowcr part ol thc pagc shows all pcrmissions that can bc grantcd to thc logins and rolcs.
||qcre 1-4 |e :e/|e/ |/c(e/||es 1|c|c |cs
Cha pt er 3 : S QL S er ver Ma na gement St udi o 49
You can rcplacc thc cxisting scrvcr namc with a ncw namc. Rightclick thc scrvcr in
thc Object Explorer window and choosc Register. Now you can rcnamc thc scrvcr and
modily thc cxisting scrvcr dcscription in thc Registered Server lramc.
|c sc| /escme se/|e/s, |e:case :|cs|s scmes :cs c//e:| c||e/ se/|e/s ||c| /e/e/es:e ||em.

Starting and Stopping Servers
A atabasc nginc scrvcr can bc startcd automatically cach timc thc Vindows opcrating
systcm starts or by using SQL Scrvcr Managcmcnt Studio. To start thc scrvcr using
Managcmcnt Studio, rightclick thc sclcctcd scrvcr in thc Object Explorer panc and
click Start in thc contcxt mcnu. Thc mcnu also contains Stop and Pause lunctions that
you can usc to stop or pausc thc activatcd scrvcr, rcspcctivcly.
Managing Databases Using Dbject xpIorer
Thc lollowing arc thc managcmcnt tasks that you can pcrlorm by using SQL Scrvcr
Managcmcnt Studio:
Crcatc databascs without using TransactSQL
Modily databascs without using TransactSQL
Managc databasc objccts and thcir usagc
Gcncratc and cxccutc SQL statcmcnts
Creating Databases Without Using 7ransact-SQL
You can crcatc a ncw databasc by using Object Explorer or thc TransactSQL languagc.
(atabasc crcation using TransactSQL is discusscd in Chaptcr 5.) As thc namc suggcsts,
you also usc Object Explorer to cxplorc thc objccts within a scrvcr. From thc Object
Explorer panc, you can inspcct all thc objccts within a scrvcr and managc your scrvcr and
databascs. Thc cxisting trcc contains, among othcr loldcrs, thc Databases loldcr. This
loldcr has scvcral subloldcrs, including onc lor thc systcm databascs and onc lor cach ncw
databasc that is crcatcd by a uscr. (Systcm and uscr databascs arc discusscd in dctail in
Chaptcr 15.)
To crcatc a databasc using Object Explorer, rightclick Databases and sclcct New
Database. !n thc New Database dialog box (scc Figurc 35), typc thc namc ol thc ncw
databasc in thc Database Name licld and thcn click OK. ach databasc has scvcral
50 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
dillcrcnt propcrtics, such as lilc typc, initial sizc, and so on. atabasc propcrtics can bc
sclcctcd lrom thc lclt panc ol thc New Database dialog box. Thcrc arc scvcral dillcrcnt
pagcs (propcrty groups):
Gcncral
Filcs (appcars only lor an cxisting databasc)
Filcgroups
ptions
Pcrmissions (appcars only lor an cxisting databasc)
||qcre 1- |e |ea |c|c|cse 1|c|c |cs
Cha pt er 3 : S QL S er ver Ma na gement St udi o 51
xtcndcd Propcrtics (appcars only lor an cxisting databasc)
Mirroring (appcars only lor an cxisting databasc)
Transaction Log Shipping (appcars only lor an cxisting databasc)
|e s,s|em 1|s(|c,s c|| (/c(e/|, /ca(s ||s|e1 /c/ cs es|s||s 1c|c|cse. |c/ c sea 1c|c|cse, cs s|cas |s
||a/e ?-:, ||e/e c/e cs|, ||/ee 1|//e/es| /ca(s. 6eaete/, 0t/eas, cs1 l//eqteas.
Thc General pagc ol thc Database Properties dialog box (scc Figurc 36) displays,
among othcr things, thc databasc namc, thc owncr ol thc databasc, its collation, and
rccovcry modcl. Thc propcrtics ol thc data lilcs that bclong to a particular databasc
||qcre 1-6 |c|c|cse |/c(e/||es 1|c|c |cs. ese/c| (ce
52 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
comprisc thc namc and initial sizc ol thc lilc, whcrc thc databasc will bc storcd, and thc
typc ol thc lilc (PR!MARY, lor instancc). A databasc can bc storcd in multiplc lilcs.
:9| :e/|e/ |cs 1,scm|: 1|s| s(c:e mcscemes|. ||s mecss ||c| 1c|c|cses :cs |e se| a( |c ca|cmc||:c||,
es(cs1 cs1 s|/|s| cs see1e1. |/ ,ca acs| |c :|cse ||e 4ateqtewtb (/c(e/|, c/ ||e l//es c(||cs, :||:| .
|s ||e 4ateqtewtb :c|ams cs1 mc|e ,ca/ :|cses |s ||e Cbeaqe 4ateqtewtb 1|c|c |cs. |e faee/e
4ateqtewtb :|e:| |cs s|ca|1 |e :|e:|e1 |c c||ca ||e 1c|c|cse |c ca|c/ca. |c:| ||me ||e/e |s |ssa//|:|es|
s(c:e a||||s ||e /||e a|es 1c|c |s c11e1 |c ||e 1c|c|cse, ||e se/|e/ a||| /eaes| ||e c11|||csc| s(c:e //cm ||e
c(e/c||s s,s|em. |e cmcas| (|s mec|,|es c/ ||e c11|||csc| s(c:e |s se| |, ||e sam|e/ |s ||e l//e 6tewtb
//cme c/ ||e scme 1|c|c |cs. ca :cs c|sc 1e:|1e a|e||e/ ||e /||e :cs /ca a|||ca| cs, /es|/|:||css (||e 1e/ca||
|c|ae c/ sc|. |/ ,ca /es|/|:| ||e /||e /ca||, ,ca |c|e |c s(e:|/, ||e mcs|mam /||e s|ce.
Thc Iilegroups pagc ol thc Database Properties dialog box displays thc namc(s) ol
thc lilcgroup(s) to which thc databasc lilc bclongs, thc art ol thc lilcgroup (dclault or
nondclault), and thc allowcd opcration on thc lilcgroup (rcad/writc or rcadonly).
Thc Options pagc ol thc Database Properties dialog box cnablcs you to display
and modily all databasclcvcl options. Thcrc arc scvcral groups ol options: Automatic,
Cursor, Miscellaneous, Recovery, and State. For instancc, thc lollowing lour options
cxist lor State:
Database Read-Only
Database State
NRMAL.)
Restrict Access
dclault valuc is MULT!_USR.)
Encryption Enabled
is Falsc.)
!l you choosc thc Permissions pagc, thc systcm opcns thc corrcsponding dialog
box and displays all uscrs and rolcs along with thcir pcrmissions. (For thc discussion ol
pcrmissions, scc Chaptcr 12.)
Modifying Databases Without Using 7ransact-SQL
Object Explorer can also bc uscd to modily an cxisting databasc. Using this componcnt,
you can modily lilcs and lilcgroups that bclong to thc databasc. To add ncw data lilcs,
rightclick thc databasc namc and choosc Properties. !n thc Database Properties
Cha pt er 3 : S QL S er ver Ma na gement St udi o 53
dialog box, sclcct Iiles, click Add, and typc thc namc ol thc ncw lilc. (!n thc Add Iile
dialog box, you can also changc thc autogrowth propcrtics and thc location ol cach
cxisting lilc.) You can also add a (sccondary) lilcgroup lor thc databasc by sclccting
Iilegroups and clicking Add.
0s|, ||e s,s|em c1m|s|s|/c|c/ c/ ||e 1c|c|cse case/ :cs mc1|/, ||e 1c|c|cse (/c(e/||es (as| mes||cse1.

To dclctc a databasc using Object Explorer, rightclick thc databasc namc and
choosc Delete.
Managing 7abIes Without Using 7ransact-SQL
Altcr you crcatc a databasc, your ncxt task is to crcatc all tablcs bclonging to it. Again,
you can crcatc tablcs by using cithcr Object Explorer or TransactSQL. Again, only
Object Explorer is discusscd hcrc.
To crcatc a tablc using Object Explorer, cxpand thc Databases loldcr, cxpand thc
databasc, rightclick thc Tables subloldcr, and thcn click New Table. Thc crcation ol a
tablc and all othcr databasc objccts using thc TransactSQL languagc will bc discusscd
in dctail in Chaptcr 5.
To dcmonstratc thc crcation ol a tablc using Object Explorer, thc department tablc
ol thc sample databasc will bc uscd as an cxamplc. ntcr thc namcs ol all columns with
thcir propcrtics. Column namcs, thcir data typcs, as wcll as thc NULL propcrty ol thc
column must bc cntcrcd in thc twodimcnsional matrix, as shown in thc topright panc
ol Figurc 37.
All data typcs supportcd by thc systcm can bc displaycd (and onc ol thcm sclcctcd)
by clicking thc arrow sign in thc Data Type column (thc arrow appcars altcr thc ccll
has bccn sclcctcd). Subscqucntly, you can typc cntrics in thc Length, Precision, and
Scale rows lor thc choscn data typc on thc Column Properties tab (scc thc bottom
right panc ol Figurc 37). Somc data typcs, such as CHAR, rcquirc a valuc lor thc
Length row, and somc, such as C!MAL, rcquirc a valuc in thc Precision and Scale
rows. n thc othcr hand, data typcs such as !NTGR do not nccd any ol thcsc
cntrics to bc spccilicd. (Thc valid cntrics lor a spccilicd data typc arc highlightcd in thc
list ol all possiblc column propcrtics.)
Thc chcck box in thc Allow Nulls column must bc chcckcd il you want a tablc
column to pcrmit NULL valucs to bc inscrtcd into that column. Similarly, il thcrc is a
dclault valuc, it should bc cntcrcd in thc Default Value or Binding row ol thc Column
Properties tab. (A dclault valuc is a valuc that will bc inscrtcd in a tablc column whcn
thcrc is no cxplicit valuc cntcrcd lor it.)
54 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Thc column dept_no is thc primary kcy ol thc department tablc. (For thc discussion
ol primary kcys ol thc sample databasc, scc Chaptcr 1.) To spccily a column as thc
primary kcy ol a tablc, you must rightclick thc column and choosc Set Primary Key.
Finally, closc thc right panc with thc inlormation conccrning thc ncw tablc. Altcr that,
thc systcm will display thc Choose Name dialog box, whcrc you can typc thc tablc
namc.
To vicw thc propcrtics ol an cxisting tablc, doublcclick thc loldcr ol thc databasc
to which thc tablc bclongs, doublcclick Tables, and thcn rightclick thc namc ol thc
tablc and choosc Properties. Figurc 38 shows thc Table Properties dialog box lor thc
employee tablc.
To rcnamc a tablc, rightclick thc namc ol thc tablc in thc Tables loldcr and choosc
Rename. To rcmovc a tablc, rightclick thc namc ol thc tablc in thc Tables loldcr in
thc databasc to which thc tablc bclongs and sclcct Delete.
||qcre 1-I t/ec||s ||e 1e(c/|mes| |c||e as|s :9| :e/|e/ /cscemes| :|a1|c
Cha pt er 3 : S QL S er ver Ma na gement St udi o 55
ca s|ca|1 sca :/ec|e ||e c||e/ ||/ee |c||es c/ ||e sem/e 1c|c|cse.

Altcr you havc crcatcd all lour tablcs ol thc sample databasc (employee, department,
project, and works_on), you can usc anothcr lcaturc ol SQL Scrvcr Managcmcnt Studio
to display thc corrcsponding cntityrclationship (R) diagram ol thc sample databasc.
(Thc proccss ol convcrting thc cxisting tablcs ol a databasc into thc corrcsponding R
diagram is callcd re.erse engineering.)
||qcre 1-8 c||e |/c(e/||es 1|c|c |cs /c/ ||e em(|c,ee |c||e
56 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
To scc thc R diagram ol thc sample databasc, rightclick thc Database Diagrams
subloldcr ol thc sample databasc and sclcct New Database Diagram.
|/ c 1|c|c |cs c(ess cs||s ,ca a|e||e/ ||e sa((c/| c|(e:|s s|ca|1 |e :/ec|e1, :||:| es.

Thc lirst (and only) stcp is to sclcct tablcs that will bc addcd to thc diagram. Altcr
adding all lour tablcs ol thc sample databasc, thc wizard complctcs thc work and crcatcs
thc diagram (scc Figurc 39).
Thc diagram in Figurc 39 is not thc linal diagram ol thc sample databasc, bccausc
although it shows all lour tablcs with thcir columns (and thc corrcsponding primary
kcys), it docs not show any rclationship bctwccn thc tablcs. A rclationship bctwccn
two tablcs is bascd on thc primary kcy ol onc tablc and thc (possiblc) corrcsponding
||qcre 1-9 ||/s| 1|c/cm c/ ||e scm(|e 1c|c|cse
Cha pt er 3 : S QL S er ver Ma na gement St udi o 57
column(s) ol thc othcr tablc. (For a dctailcd discussion ol thcsc rclationships and
rclcrcntial intcgrity, scc Chaptcr 5.)
Thcrc arc cxactly thrcc rclationships bctwccn thc cxisting tablcs ol thc sample
databasc: lirst, thc tablcs department and employee havc a 1:N rclationship, bccausc
lor cach valuc in thc primary kcy column ol thc department tablc (dept_no), thcrc
is onc or morc corrcsponding valucs in thc column dept_no ol thc employee tablc.
Analogously, thcrc is a rclationship bctwccn thc tablcs employee and works_on,
bccausc only thosc valucs that cxist in thc primary kcy ol thc employee tablc
(emp_no) appcar also in thc column emp_no ol thc works_on tablc. (Thc third
rclationship is bctwccn thc tablcs project and works_on, bccausc only valucs that
cxist in thc primary kcy ol thc project tablc (pr_no) appcar also in thc pr_no column
ol thc works_on tablc.
To crcatc cach ol thc thrcc rclationships dcscribcd, you havc to rcdcsign thc diagram
with thc column that corrcsponds to thc primary kcy column ol thc othcr tablc. (Such a
column is callcd a foreign key.) To scc how to do this, usc thc employee tablc and dclinc
its column dept_no as thc lorcign kcy ol thc department tablc:
1. Click thc crcatcd diagram, rightclick thc graphical lorm ol thc employee tablc
in thc dctail panc, and sclcct Relationships. !n thc Ioreign Key Relationships
dialog box, click Add.
2. !n thc dialog box, cxpand Tables and Columns Specification column and click
thc . button.
3. Sclcct thc tablc with thc corrcsponding primary kcy (thc department tablc).
4. Choosc thc dept_no column ol this tablc as thc primary kcy and thc column with
thc samc namc in thc employee tablc as thc lorcign kcy and click OK. Click Closc.
Figurc 310 shows thc modilicd diagram altcr all thrcc rclationships in thc sample
databasc havc bccn crcatcd.
Autboring Activities Using SL Server
Management Studio
SQL Scrvcr Managcmcnt Studio givcs you a complctc authoring cnvironmcnt lor all
typcs ol qucrics. You can crcatc, savc, load, and cdit qucrics. SQL Scrvcr Managcmcnt
Studio allows you to work on qucrics without bcing conncctcd to a particular scrvcr.
This tool also givcs you thc option ol dcvcloping your qucrics with dillcrcnt projccts.
Thc authoring capabilitics arc associatcd with Query Editor as wcll as Solution
xplorcr. Thc lormcr componcnt will bc discusscd ncxt.
58 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Query ditor
To launch thc Query Editor panc, click thc New Query button in thc toolbar ol SQL
Scrvcr Managcmcnt Studio. !l you cxpand it to show all thc possiblc qucrics, it shows
morc than just a atabasc nginc qucry. 8y dclault, you gct a ncw atabasc nginc
qucry, but othcr qucrics arc possiblc, such as MX qucrics, XMLA qucrics, and othcr
qucrics.
ncc you opcn Query Editor, thc status bar at thc bottom ol thc panc tclls you
whcthcr your qucry is in a conncctcd or disconncctcd statc. !l you arc not conncctcd
automatically to thc scrvcr, thc Connect to SQL Server dialog box appcars, whcrc you
can typc thc namc ol thc databasc scrvcr to which you want to conncct and sclcct thc
authcntication modc.
||qcre 1-1 |e /|sc| 1|c/cm c/ ||e scm(|e 1c|c|cse
Cha pt er 3 : S QL S er ver Ma na gement St udi o 59
||s:csse:|e1 e1|||s |cs mc/e /|es||||||, ||cs :csse:|e1 e1|||s. ca :cs e1|| ae/|es a|||ca| |c||s |c :|ccse
c se/|e/, cs1 ,ca :cs 1|s:csse:| c ||es 0aet, f4/tet a|s1ca //cm cse se/|e/ cs1 :csse:| || |c csc||e/
a|||ca| |c||s |c c(es csc||e/ a|s1ca. (ca :cs ase 1|s:csse:|e1 e1|||s |/ ,ca :||:| ||e Ceace/ |a||cs |s ||e
Ceaaect te J0l Jetret 1|c|c |cs.
Query Editor can bc uscd by cnd uscrs lor thc lollowing tasks:
Gcncrating and cxccuting TransactSQL statcmcnts

Gcncrating and analyzing cxccution plans lor gcncratcd qucrics
Graphically illustrating thc cxccution plan lor a sclcctcd qucry
Query Editor contains an intcrnal tcxt cditor and a sclcction ol buttons in its toolbar.
Thc main window is dividcd into a qucry panc (uppcr) and a rcsults panc (lowcr). Uscrs
cntcr thc TransactSQL statcmcnts (qucrics) that thcy want to cxccutc into thc qucry panc,
and altcr thc systcm has proccsscd thc qucrics, thc output is displaycd in thc rcsults panc.
Thc cxamplc shown in Figurc 311 dcmonstratcs a qucry cntcrcd into Qucry ditor
and thc output rcturncd. Thc lirst statcmcnt in thc qucry panc, US, spccilics thc
sample databasc as thc currcnt databasc. Thc sccond statcmcnt, SLCT, rctricvcs
all thc rows ol thc works_on tablc. Clicking thc Query button in thc Query Editor's
toolbar and thcn sclccting Execute
in thc rcsults panc ol Query Editor.
ca :cs c(es se|e/c| 1|//e/es| a|s1cas||c| |s, se|e/c| 1|//e/es| :csse:||css |c cse c/ mc/e |c|c|cse |s|se
|ss|cs:es. ca :/ec|e sea :csse:||css |, :||:||s ||e hew 0aet, |a||cs |s ||e |cc||c/.
Thc lollowing additional inlormation conccrning thc cxccution ol thc statcmcnt(s) is
displaycd in thc status bar at thc bottom ol thc Query Editor window:

atabasc scrvcr namc
Currcnt uscrnamc and scrvcr proccss !
60 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Currcnt databasc namc
lapscd timc lor thc cxccution ol thc last qucry

nc ol thc main lcaturcs ol SQL Scrvcr Managcmcnt Studio is that it's casy to usc,
and that also applics to thc Query Editor componcnt. Query Editor supports a lot ol
lcaturcs that makc coding ol TransactSQL statcmcnts casicr. First, Query Editor uscs
syntax highlighting to improvc thc rcadability ol TransactSQL statcmcnts. !t displays
all rcscrvcd words in bluc, all variablcs in black, strings in rcd, and commcnts in grccn.
(For a discussion ol rcscrvcd words, scc thc ncxt chaptcr.)
Thcrc is also thc contcxtscnsitivc hclp lunction callcd Dynamic Help that cnablcs
you to gct hclp on a particular statcmcnt. !l you do not know thc syntax ol a statcmcnt,
just highlight that statcmcnt in thc cditor and sclcct thc Dynamic Help option on thc
Help mcnu. You can also highlight options ol dillcrcnt TransactSQL statcmcnts to gct
thc corrcsponding tcxt lrom 8ooks nlinc.
||qcre 1-11 9ae/, |1||c/ a||| c ae/, cs1 ||s /esa||s
Cha pt er 3 : S QL S er ver Ma na gement St udi o 61
:9| :e/|e/ 1^^? sa((c/|s ||e :9| |s|e|||sesse |cc|. |s|e|||sesse |s c /c/m c/ ca|cmc|e1 ca|c:cm(|e|| cs. |s c||e/
ac/1s, |||s c11-|s c||cas ,ca |c c::ess 1es:/|(||css c/ //eaes||, ase1 e|emes|s c/ /cssc:|-:9| s|c|emes|s
a|||ca| as|s ||e |e,|cc/1. (|s /a|a/e |e/s|css c/ :9| :e/|e/, |||s /ec|a/e a||| |e es|es1e1 |c sa((c/| c11|||csc|
/cssc:|-:9| e|emes|s.
Object Explorer can also hclp you cdit qucrics. For instancc, il you want to scc
thc corrcsponding CRAT TA8L statcmcnt lor thc employee tablc, drill down
to this databasc objcct, rightclick thc tablc namc, sclcct Script Table as, and choosc
CREATE to New Query Editor Window. Figurc 312 shows thc Query Editor
window with thc CRAT TA8L statcmcnt. (This capability cxtcnds also to othcr
objccts, such as storcd proccdurcs and lunctions.)
Object Explorer is vcry usclul il you want to display thc graphical cxccution plan lor
a particular qucry. (Thc cxccution plan is thc plan sclcctcd by thc optimizcr to cxccutc
a givcn qucry. This topic is discusscd in dctail in Chaptcr 20.) !l you sclcct thc Query
||qcre 1-11 |e 9ae/, |1||c/ a|s1cas a||| ||e t||/| /3|| s|c|emes|
62 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
lunction and Display Estimated Execution Plan, thc systcm will display thc graphical
plan instcad ol thc rcsult sct lor thc givcn qucry (scc Figurc 313).
SoIution xpIorer
Qucry cditing in SQL Scrvcr Managcmcnt Studio is solutionbascd. !l you start a
blank qucry using thc New Query button, it will still bc bascd on a blank solution. You
can scc this by choosing thc componcnt callcd Solution Explorer lrom thc View mcnu,
right altcr opcning your blank qucry.
A solution can havc zcro, onc, or morc projccts associatcd with it. A blank solution
docs not contain any projcct. !l you want to associatc a projcct with thc solution, closc
your blank solution, Solution Explorer, and thc Query Editor window, and start a ncw
projcct by clicking thc Iile mcnu and sclccting New and thcn Project. !n thc New
Project window, choosc SQL Server Scripts. A projcct is a mcthod ol organizing lilcs
in a sclcctcd location. You can choosc a namc lor thc projcct and sclcct its location on
disk. Vhcn you crcatc a ncw projcct, by dclault you will start a ncw solution. You can
add a projcct to an cxisting solution using Solution Explorer.
||qcre 1-11 |e ese:a||cs (|cs /c/ ||e ae/, //cm ||a/e ?-!!
Cha pt er 3 : S QL S er ver Ma na gement St udi o 63
ncc thc ncw projcct and solution arc crcatcd, Solution Explorer shows nodcs in
cach projcct lor Connections, Queries, and Miscellaneous. To opcn a ncw Query
Editor window, rightclick thc Query nodc and choosc New Query.
SQL Scrvcr Managcmcnt Studio supports vcrsion control using thc intcgration
with Microsolt \isual Sourcc Salc. As alrcady statcd, Solution Explorer allows you to
organizc and storc rclatcd scripts as parts ol a projcct. Thcsc script lilcs can bc chcckcd
in and out ol thc vcrsion control systcm dircctly lrom Solution Explorer.
ConcIusion
This chaptcr covcrcd thc most important SQL Scrvcr tool: SQL Scrvcr Managcmcnt
Studio. SQL Scrvcr Managcmcnt Studio is vcry usclul lor cnd uscrs and administrators
alikc. First, it allows many administrativc lunctions to bc pcrlormcd. Thcsc arc touchcd
on hcrc but arc covcrcd in morc dctail latcr in thc book. This chaptcr discusscd most
important lunctions ol SQL Scrvcr Managcmcnt Studio conccrning cnd uscrs, such as
databasc and tablc crcation.
SQL Scrvcr Managcmcnt Studio contains scvcral componcnts:
Registered Servers Allows you to rcgistcr SQL Scrvcr instanccs and conncct
to thcm.
Object Explorer Contains a trcc vicw ol all thc databasc objccts in a scrvcr.
Query Editor Allows cnd uscrs to gcncratc, cxccutc, and storc TransactSQL
statcmcnts. Additionally, it providcs thc ability to analyzc qucrics by displaying
thc cxccution plan.
Solution Explorer Allows you to crcatc solutions. A solution can havc zcro or
morc projccts associatcd with it.
Thc ncxt chaptcr introduccs thc TransactSQL languagc and dcscribcs its main
componcnts. Altcr introducing thc basic conccpts and cxisting data typcs, thc chaptcr
also dcscribcs systcm lunctions that TransactSQL supports.
xercises
.3.1
Using SQL Scrvcr Managcmcnt Studio, crcatc a databasc callcd test. Storc thc databasc
in a lilc namcd testdate_a in thc dircctory C:\tmp and allocatc 10M8 ol spacc to it.
Conligurc thc lilc in which thc databasc is locatcd to grow in incrcmcnts ol 2M8, not to
cxcccd a total ol 20M8.
64 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
.3.2
Using SQL Scrvcr Managcmcnt Studio, changc thc transaction log lor thc test
databasc. Givc thc lilc an initial sizc ol 3M8, with growth ol 20 pcrccnt. Allow thc lilc
lor thc transaction log to autogrow.
.3.3
Using SQL Scrvcr Managcmcnt Studio, allow only thc databasc owncr and systcm
administrator to usc thc test databasc. !s it possiblc that both uscrs could usc thc
databasc at thc samc timc:
.3.4
Using SQL Scrvcr Managcmcnt Studio, crcatc all lour tablcs ol thc sample databasc
(scc Chaptcr 1) with all thcir columns.
.3.5
Using SQL Scrvcr Managcmcnt Studio, vicw which tablcs thc AdventureWorks
databasc contains. Altcr that, choosc thc person.address tablc and vicw its propcrtics.
.3.6
Using Qucry ditor, typc thc lollowing TransactSQL statcmcnt:
CREATE DATABASE test
xplain thc crror mcssagc shown in thc rcsult panc.
.3.7
Storc thc TransactSQL statcmcnt in .3.6 in thc lilc C:\tmp\crcatcdb.sql.
.3.8
Using Qucry ditor, how can you makc thc test databasc thc currcnt databasc:
.3.9
Using Qucry ditor, makc thc AdventureWorks databasc thc currcnt databasc and
cxccutc thc lollowing TransactSQL statcmcnt:
SELECT * IROM Sales.Customer
How can you stop thc cxccution ol thc statcmcnt:
.3.10
Using Qucry ditor, changc thc output ol thc SLCT statcmcnt (.3.9) so that thc
rcsults appcar as thc tcxt (and not as thc grid).
Part #
Jransact-SQI Ianguagc
Part II
Copyright 2008 by The McGraw-Hill Companies. Click here for terms of use.
This page intentionally left blank
In 7bis Cbapter
SL's asic Dbjects
Data 7ypes
7ransact-SL Functions
ScaIar Dperators
NULL VaIues
SQI Componcnts
Cbapter 4
Copyright 2008 by The McGraw-Hill Companies. Click here for terms of use.
68 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
T
his chaptcr introduccs thc clcmcntary objccts and basic opcrators supportcd
by thc TransactSQL languagc. First, thc basic languagc clcmcnts, including
constants, idcntilicrs, and dclimitcrs, arc dcscribcd. Thcn, bccausc cvcry
clcmcntary objcct has a corrcsponding data typc, data typcs arc discusscd in dctail.
Additionally, all cxisting opcrators and lunctions arc cxplaincd. At thc cnd ol thc
chaptcr, NULL valucs arc introduccd.
SL's asic Dbjects
Thc languagc ol atabasc nginc, TransactSQL, has thc samc basic lcaturcs as othcr
common programming languagcs:
Litcral valucs (also callcd constants)
climitcrs
Commcnts

Rcscrvcd kcywords
Thc lollowing scctions dcscribc thcsc lcaturcs.
LiteraI VaIues
A literal valuc is an alphanumcrical, hcxadccimal, or numcric constant. A string
constant contains onc or morc charactcrs ol thc charactcr sct cncloscd in two singlc
straight quotation marks (' ') or doublc straight quotation marks (" ") (singlc quotation
marks arc prclcrrcd duc to thc multiplc uscs ol doublc quotation marks, as discusscd
in a momcnt). !l you want to includc a singlc quotation mark within a string dclimitcd
by singlc quotation marks, usc two consccutivc singlc quotation marks within thc
string. Hcxadccimal constants arc uscd to rcprcscnt nonprintablc charactcrs and othcr
binary data. ach hcxadccimal constant bcgins with thc charactcrs '0x' lollowcd by an
cvcn numbcr ol charactcrs or numbcrs. xamplcs 4.1 and 4.2 illustratc somc valid and
invalid string constants and hcxadccimal constants.
XAMPL 4.1
Somc valid string constants and hcxadccimal constants lollow:
'Philadclphia'
"8crkclcy, CA 94710"
'9876'
'Apostrophc is displaycd likc this: can''t' (notc thc two consccutivc singlc quotation marks)
0x53514C0
Cha pt er 4 : S QL Component s 69
XAMPL 4.2
Thc lollowing arc not string constants:
'A8'C' (odd numbcr ol singlc quotation marks)
'Ncw York" (samc typc ol quotation marksinglc or doublcmust bc uscd at cach
cnd ol thc string)
Thc numcric constants includc all intcgcr, lixcdpoint, and lloatingpoint valucs with
and without signs (scc xamplc 4.3).
XAMPL 4.3
Thc lollowing arc numcric constants:
130
130.00
0.3575 (scicntilic notationnm mcans n multiplicd by 10
m
)
22.33
A constant always has a data typc and a lcngth, and both dcpcnd on thc lormat ol thc
constant. Additionally, cvcry numcric constant has a prccision and a scalc lactor. (Thc
data typcs ol thc dillcrcnt kinds ol litcral valucs arc cxplaincd latcr in this chaptcr.)
DeIimiters
!n TransactSQL, doublc quotation marks havc two mcanings. !n addition to cnclosing
strings, doublc quotation marks can also bc uscd as dclimitcrs lor socallcd delimited
identif iers. climitcd idcntilicrs arc a spccial kind ol idcntilicr usually uscd to allow thc
usc ol rcscrvcd kcywords as idcntilicrs and also to allow spaccs in thc namcs ol databasc
objccts.
||//e/es||c||cs |e|aees s|s|e cs1 1ca||e ac|c||cs mc/|s acs /|/s| |s|/c1a:e1 |s ||e :9|^1 s|cs1c/1. |s ||e
:cse c/ |1es||/|e/s, |||s s|cs1c/1 1|//e/es||c|es |e|aees /ea|c/ cs1 1e||m||e1 |1es||/|e/s. ac |e, 1|//e/es:es
c/e ||c| 1e||m||e1 |1es||/|e/s c/e es:|cse1 |s 1ca||e ac|c||cs mc/|s cs1 c/e :cse sess||||e. (/cssc:|-:9| c|sc
sa((c/|s ||e ase c/ sac/e |/c:|e|s |ss|ec1 c/ 1ca||e ac|c||cs mc/|s. |ca||e ac|c||cs mc/|s c/e cs|, ase1
/c/ 1e||m|||s s|/|ss. ese/c||,, 1e||m||e1 |1es||/|e/s ae/e |s|/c1a:e1 |c c||ca ||e s(e:|/|:c||cs c/ |1es||/|e/s,
a||:| c/e c||e/a|se |1es||:c| |c /ese/|e1 |e,ac/1s. :(e:|/|:c||,, 1e||m||e1 |1es||/|e/s (/c|e:| ,ca //cm as|s
scmes (|1es||/|e/s, |c/|c||e scmes ||c| :ca|1 |e |s|/c1a:e1 cs /ese/|e1 |e,ac/1s |s cse c/ ||e /a|a/e :9|
s|cs1c/1s. /|sc, 1e||m||e1 |1es||/|e/s mc, :cs|c|s :|c/c:|e/s ||c| c/e sc/mc||, |||ec| a||||s |1es||/|e/ scmes,
sa:| cs ||cs|s.
70 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
!n TransactSQL, thc usc ol doublc quotation marks is dclincd using thc QUT_
!NT!F!R option ol thc ST statcmcnt. !l this option is sct to N, which is thc
dclault valuc, an idcntilicr in doublc quotation marks will bc dclincd as a dclimitcd
idcntilicr. !n this casc, doublc quotation marks cannot bc uscd lor dclimiting strings.
Comments
Thcrc arc two dillcrcnt ways to spccily a commcnt in a TransactSQL statcmcnt. Using
thc pair ol charactcrs / and / marks thc cncloscd tcxt as a commcnt. !n this casc, thc
commcnt may cxtcnd ovcr scvcral lincs. Furthcrmorc, thc charactcrs (two hyphcns)
indicatc that thc rcmaindcr ol thc currcnt linc is a commcnt. (Thc two hyphcns comply
with thc ANS! SQL standard, whilc / and / arc thc cxtcnsions ol TransactSQL.)
!n TransactSQL, idcntilicrs arc uscd to idcntily databasc objccts such as databascs,
tablcs, and indiccs. Thcy arc rcprcscntcd by charactcr strings that may includc up to 128
charactcrs and can contain lcttcrs, numcrals, or thc lollowing charactcrs: _, , #, and 8.
ach namc must bcgin with a lcttcr or onc ol thc lollowing charactcrs: _, , or #. Thc
charactcr # at thc bcginning ol a tablc or storcd proccdurc namc dcnotcs a tcmporary
objcct, whilc at thc bcginning ol a namc dcnotcs a variablc. As indicatcd carlicr, thcsc
rulcs don't apply to dclimitcd idcntilicrs (also known as quotcd idcntilicrs), which can
contain, or bcgin with, any charactcr (othcr than thc dclimitcrs thcmsclvcs).
Reserved keywords
ach programming languagc has a sct ol namcs with rcscrvcd mcanings, which must bc
writtcn and uscd in thc dclincd lormat. Namcs ol this kind arc callcd reser.ed key.ords.
TransactSQL uscs a varicty ol such namcs, which, as in many othcr programming
languagcs, cannot bc uscd as objcct namcs, unlcss thc objccts arc spccilicd as dclimitcd
or quotcd idcntilicrs.
|s /cssc:|-:9|, ||e scmes c/ c|| 1c|c |,(es cs1 s,s|em /as:||css, sa:| cs t|/|/t|| cs1 |||||, c/e sc|
/ese/|e1 |e,ac/1s. |e, :cs ||e/e/c/e |e ase1 /c/ 1esc||s c|(e:|s. (|c sc| ase 1c|c |,(es cs1 s,s|em /as:||css
cs c|(e:| scmes| :a:| c ase mc|es /cssc:|-:9| s|c|emes|s 1|//|:a|| |c /ec1 cs1 as1e/s|cs1.
Cha pt er 4 : S QL Component s 71
Data 7ypes
All thc data valucs ol a column must bc ol thc samc data typc. (Thc only cxccption
spccilics thc valucs ol thc SQL_\AR!ANT data typc.) TransactSQL uscs dillcrcnt
data typcs, which can bc catcgorizcd as lollows:
Numcric data typcs
Charactcr data typcs
Tcmporal (datc and/or timc) data typcs
Misccllancous data typcs
C!MAL with \ARC!MAL storagc typc
Thc lollowing scctions dcscribc all thcsc catcgorics.
Numeric Data 7ypes
Numcric data typcs arc uscd to rcprcscnt numbcrs. Thc lollowing tablc shows thc list ol
all numcric data typcs:
0ata Ipe lxp|aaat|ea
|NI|0|| |ejre.eat. |ateer .+|ae. t|+t c+a |e .terea |a + |te.. I|e r+ae el .+|ae. |. -z,+I,+1,+ te
z,+I,+1,+I. |NI |. t|e .|ert lerm ler |NI|0||.
:M||||NI |ejre.eat. |ateer .+|ae. t|+t c+a |e .terea |a z |te.. I|e r+ae el .+|ae. |. -1zI te 1zII.
I|N\|NI |ejre.eat. aeaae+t|.e |ateer .+|ae. t|+t c+a |e .terea |a |te. I|e r+ae el .+|ae. |. te z!!.
||0|NI |ejre.eat. |ateer .+|ae. t|+t c+a |e .terea |a |te.. I|e r+ae el .+|ae. |. -z
1
te z
1
- .
||C|M||(j,.[} |e.cr||e. l|\ea-je|at .+|ae.. I|e +rameat p (jrec|.|ea} .jec|l|e. t|e tet+| aam|er el a||t. w|t|
+..amea aec|m+| je|at s (.c+|e} a||t. lrem t|e r||t. ||C|M|| .+|ae. +re .terea, aejeaa|a ea t|e
.+|ae el p, |a ! te I |te.. ||C |. t|e .|ert lerm ler ||C|M||.
NJM|||C(j,.[} :aeam ler ||C|M||.
|||| J.ea ler l|e+t|a-je|at .+|ae.. I|e r+ae el je.|t|.e .+|ae. |. +jjre\|m+te| z.z1| - 1 te .I| +
1, +aa t|e r+ae el ae+t|.e .+|ae. |. +jjre\|m+te| -.| - 1 te -.| + 1 (t|e .+|ae tere
c+a +|.e |e .terea}.
||0|I(j}[ |ejre.eat. l|e+t|a-je|at .+|ae., |||e ||||. p ael|ae. t|e jrec|.|ea w|t| p < z! +. .|a|e jrec|.|ea
(+ |te} +aa p > z! +. aea||e jrec|.|ea ( |te}.
M0N|\ J.ea ler rejre.eat|a meaet+r .+|ae.. M0N|\ .+|ae. cerre.jeaa te -|te ||C|M|| .+|ae. +aa
+re reaaaea te lear a||t. +lter t|e aec|m+| je|at.
:M|||M0N|\ Cerre.jeaa. te t|e a+t+ tje M0N|\ |at |. .terea |a + |te..
72 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Cbaracter Data 7ypes
Thcrc arc two gcncral lorms ol charactcr data typcs. Thcy can bc strings ol singlcbytc
charactcrs or strings ol Unicodc charactcrs. (Unicodc uscs scvcral bytcs to spccily onc
charactcr.) Furthcr, strings can havc lixcd or variablc lcngth. Thc lollowing charactcr
data typcs arc uscd:
0ata Ipe lxp|aaat|ea
C|||(a}[ |ejre.eat. + l|\ea-|eat| .tr|a el .|a|e-|te c|+r+cter., w|ere a |. t|e aam|er el c|+r+cter.
|a.|ae t|e .tr|a. I|e m+\|mam .+|ae el a |. . C||||CI||(a} |. +a +aa|t|ea+| ea|.+|eat lerm
ler C|||(a}. |l a |. em|ttea, t|e |eat| el t|e .tr|a |. +..amea te |e .
\||C|||(a}[ |e.cr||e. + .+r|+||e-|eat| .tr|a el .|a|e-|te c|+r+cter. ( < a
a+t+ tje, t|e .+|ae. ler t|e \||C||| a+t+ tje +re .terea |a t|e|r +cta+| |eat|. I||. a+t+ tje |+.
twe .aeam.. C||| \||\|N0 +aa C||||CI|| \||\|N0.
NC|||(a}[ :tere. l|\ea-|eat| .tr|a. el Ja|ceae c|+r+cter.. I|e m+|a a|llereace |etweea t|e C||| +aa NC|||
a+t+ tje. |. t|+t e+c| c|+r+cter el t|e NC||| a+t+ tje |. .terea |a z |te., w|||e e+c| c|+r+cter el
t|e C||| a+t+ tje a.e. |te el .ter+e .j+ce. I|erelere, t|e m+\|mam aam|er el c|+r+cter. |a +
ce|ama el t|e NC||| a+t+ tje |. +.
N\||C|||(a}[ :tere. .+r|+||e-|eat| .tr|a. el Ja|ceae c|+r+cter.. I|e m+|a a|llereace |etweea t|e \||C||| +aa
t|e N\||C||| a+t+ tje. |. t|+t e+c| N\||C||| c|+r+cter |. .terea |a z |te., w|||e e+c| \||C|||
c|+r+cter a.e. |te el .ter+e .j+ce. I|e m+\|mam aam|er el c|+r+cter. |a + ce|ama el t|e
N\||C||| a+t+ tje |. +.
|e l/|t|/| 1c|c |,(e |s |1es||:c| |c ||e t|/| 1c|c |,(e es:e(| /c/ cse 1|//e/es:e. |/ ||e :cs|es| c/ c t|/|(s
s|/|s |s s|c/|e/ ||cs :|c/c:|e/s, ||e /es| c/ ||e s|/|s |s (c11e1 a||| ||cs|s. (/ |c|ae c/ ||e l/|t|/| 1c|c
|,(e |s c|ac,s s|c/e1 |s ||s c:|ac| |es||.
7emporaI Data 7ypes
TransactSQL supports thc lollowing tcmporal data typcs:
ATT!M
SMALLATT!M
AT
T!M
ATT!M2
ATT!MFFST
Cha pt er 4 : S QL Component s 73
Thc ATT!M and SMALLATT!M data typcs spccily a datc and timc,
with cach valuc bcing storcd as an intcgcr valuc in 4 bytcs or 2 bytcs, rcspcctivcly.
\alucs ol ATT!M and SMALLATT!M arc storcd intcrnally as two scparatc
numcric valucs. Thc datc valuc ol ATT!M is storcd in thc rangc 01/01/1753
to 12/31/9999. Thc analog valuc ol SMALLATT!M is storcd in thc rangc
01/01/1900 to 06/06/2079. Thc timc componcnt is storcd in thc sccond 4bytc
(or 2bytc lor SMALLATT!M) licld as thc numbcr ol thrcchundrcdths ol a sccond
(ATT!M) or minutcs (SMALLATT!M) that havc passcd sincc midnight.
Thc usc ol ATT!M and SMALLATT!M is rathcr inconvcnicnt il
you want to storc only thc datc part or timc part. For this rcason, SQL Scrvcr 2008
introduccs thc ncw data typcs AT and T!M, which storc just thc AT or T!M
componcnt ol a ATT!M, rcspcctivcly. Thc AT data typc is storcd in 3 bytcs
and has thc rangc 01/01/0001 to 12/31/9999. Thc T!M data typc is storcd in 35
bytcs and has an accuracy ol 100 nanoscconds (ns).
Thc ATT!M2 data typc is also a ncw data typc that storcs highprccision datc
and timc data. Thc data typc can bc dclincd lor variablc lcngths dcpcnding on thc
rcquircmcnt. (Thc storagc sizc is 68 bytcs). Thc accuracy ol thc timc part is 100 ns.
This data typc isn't awarc ol aylight Saving Timc.
All thc tcmporal data typcs dcscribcd thus lar don't havc support lor thc timc zonc.
Thc ncw data typc callcd ATT!MFFST has thc timc zonc ollsct portion. For
this rcason, it is storcd in 68 bytcs. (All othcr propcrtics ol this data typc arc analogous
to thc corrcsponding propcrtics ol ATT!M2.)
Thc datc valuc in TransactSQL is by dclault spccilicd as a string in a lormat
likc 'mmm dd yyyy' (c.g., 'Jan 10 1993') insidc two singlc quotation marks or doublc
quotation marks. (Notc that thc rclativc ordcr ol month, day, and ycar can bc controllcd
by thc ST ATFRMAT statcmcnt. Additionally, thc systcm rccognizcs numcric
month valucs with dclimitcrs ol / or .) Similarly, thc timc valuc is spccilicd in thc
lormat 'hh:mm' and atabasc nginc uscs 24hour timc (23:24, lor instancc).
/cssc:|-:9| sa((c/|s c |c/|e|, c/ |s(a| /c/mc|s /c/ |/||/| |c|aes. /s ,ca c|/ec1, |sca, |c|| c|(e:|s c/e
|1es||/|e1 se(c/c|e|,, ||as, 1c|e cs1 ||me |c|aes :cs |e s(e:|/|e1 |s cs, c/1e/ c/ c|cse. |/ cse c/ ||e |c|aes |s
cm|||e1, ||e s,s|em ases ||e 1e/ca|| |c|aes. (|e 1e/ca|| |c|ae /c/ ||me |s !1.^^ //.
xamplcs 4.4 and 4.5 show thc dillcrcnt ways, how datc or timc valucs can bc
writtcn using thc dillcrcnt lormats.
XAMPL 4.4
Thc lollowing datc dcscriptions can bc uscd:
'28/5/1959' (with ST ATFRMAT dmy)
'May 28, 1959'
'1959 MAY 28'
74 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
XAMPL 4.5
Thc lollowing timc cxprcssions can bc uscd:
'8:45 AM'
'4 pm'
MisceIIaneous Data 7ypes
TransactSQL supports scvcral data typcs that do not bclong to any ol thc data typc
groups dcscribcd prcviously:
8inary data typcs
8!T
Largc objcct data typcs
CURSR (discusscd in Chaptcr 8)
UN!QU!NT!F!R
SQL_\AR!ANT
TA8L (discusscd in Chaptcr 8)
XML (discusscd in Chaptcr 28)
Spatial (c.g., GGRAPHY and GMTRY) data typcs (discusscd in
Chaptcr 29 )
H!RARCHY!
T!MSTAMP data typc

Thc lollowing scctions dcscribc cach ol thcsc data typcs (othcr than thosc dcsignatcd
as bcing discusscd in anothcr chaptcr).
8inary and 8I7 Data 7ypes
8!NARY and \AR8!NARY arc thc two binary data typcs. Thcy dcscribc data objccts
bcing rcprcscntcd in thc intcrnal lormat ol thc systcm. Thcy arc uscd to storc bit
strings. For this rcason, thc valucs arc cntcrcd using hcxadccimal numbcrs.
Thc valucs ol thc 8!T data typc arc storcd in a singlc bit. Thcrclorc, up to 8 bit
columns arc storcd in 1 bytc. Thc lollowing tablc summarizcs thc propcrtics ol thcsc
data typcs:
Cha pt er 4 : S QL Component s 75
0ata Ipe lxp|aaat|ea
||N||\(a}[ :jec|l|e. + ||t .tr|a el l|\ea |eat| w|t| e\+ct| a |te. ( < a
\||||N||\(a}[ :jec|l|e. + ||t .tr|a el .+r|+||e |eat| w|t| aj te a |te. ( < a
||I J.ea ler .jec|l|a t|e |ee|e+a a+t+ tje w|t| t|ree je..|||e .+|ae.. |||:|, I|J|, +aa NJ||.
Large Ob|ect Data 7ypes
Largc objccts (L8s) arc data objccts with thc maximum lcngth ol 2G8. Thcsc objccts
arc gcncrally uscd to storc largc tcxt data and to load modulcs and audio/vidco lilcs.
TransactSQL supports two dillcrcnt ways to spccily and acccss L8s:
Usc thc data typcs \ARCHAR(MAX), N\ARCHAR(MAX), and
\AR8!NARY(MAX)
Usc thc socallcd tcxt/imagc data typc
Thc lollowing subscctions dcscribc thc two lorms ol L8s.
7be MAX Specifier Starting with SQL Scrvcr 2005, you can usc thc samc programming
modcl to acccss valucs ol standard data typcs and L8s. !n othcr words, you can usc
convcnicnt systcm lunctions and string opcrators to work with L8s.
atabasc nginc uscs thc MAX spccilicr with thc data typcs \ARCHAR,
N\ARCHAR, and \AR8!NARY to dclinc variablclcngth columns. Vhcn you usc
MAX by dclault (instcad ol an cxplicit valuc), thc systcm analyzcs thc lcngth ol thc
particular string and dccidcs whcthcr to storc thc string as a convcnicnt valuc or as
a L8. Thc MAX spccilicr indicatcs that thc sizc ol column valucs can rcach thc
maximum L8 sizc ol thc currcnt systcm. (!n a luturc vcrsion ol SQL Scrvcr, it is
possiblc that MAX will havc a highcr maximum valuc lor strings.)
Although thc databasc systcm dccidcs how a L8 will bc storcd, you can ovcrridc
this dclault spccilication using thc sp_tableoption systcm proccdurc with thc LARG_
\ALU_TYPS_UT_F_RV option. !l thc option's valuc is sct to 1, thc data in
columns dcclarcd using thc MAX spccilicr will bc storcd scparatcly lrom all othcr data.
!l this option is sct to 0, atabasc nginc storcs all valucs lor thc row sizc 8060 bytcs
as rcgular row data.
!n SQL Scrvcr 2008, you can apply thc ncw F!LSTRAM attributc to a
\AR8!NARY(MAX) column to storc largc binary data dircctly in an NTFS lilc
systcm. Thc main advantagc ol this attributc is that thc sizc ol thc corrcsponding L8
is limitcd only by thc lilc systcm volumc sizc.
76 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
7X7, N7X7, and IMAC Data 7ypes Thc data typcs TXT, NTXT, and !MAG
constitutc thc socallcd tcxt/imagc data typcs. ata objccts ol thc typc !MAG can
contain any kind ol data (load modulcs, audio/vidco), whilc data objccts ol thc data
typcs TXT and NTXT can contain any tcxt data (that is, printablc data).
Thc tcxt/imagc data typcs arc storcd by dclault scparatcly lrom all othcr valucs ol a
databasc using a 8trcc structurc that points to thc lragmcnts ol that data. (A B-tree
structure is a trcclikc data structurc in which all ol thc bottom nodcs arc thc samc numbcr
ol lcvcls away lrom thc root ol thc trcc.) For columns ol a tcxt /imagc data typc, atabasc
nginc storcs a 16bytc pointcr in thc data row that spccilics whcrc thc data can bc lound.
!l thc amount ol tcxt/imagc data is lcss than 32K8, thc pointcr points to thc root
nodc ol thc 8trcc structurc, which is 84 bytcs long. Thc root nodc points to thc
physical blocks ol thc data. !l thc amount ol thc data is grcatcr than 32K8, atabasc
nginc builds intcrmcdiatc nodcs bctwccn thc data blocks and thc root nodc.
For cach tablc that contains morc than onc column with such data, all valucs ol thc
columns arc storcd togcthcr. Howcvcr, onc physical pagc can hold only tcxt/imagc data
lrom a singlc tablc.
Although tcxt/imagc data is storcd scparatcly lrom all othcr data, you can modily
this using thc sp_tableoption systcm proccdurc with thc TXT_!N_RV option.
Using this option, you can spccily thc maximum numbcr ol bytcs, which arc storcd
togcthcr with thc rcgular data.
Thc tcxt/imagc data typcs discusscd this lar havc scvcral limitations. You can't usc
thcm as local variablcs (in storcd proccdurcs or in groups ol TransactSQL statcmcnts).
Also, thcy can't bc a part ol an indcx or can't bc uscd in thc lollowing clauscs ol
thc SLCT statcmcnt: VHR, RR 8Y, and GRUP 8Y. Thc biggcst
problcm conccrning all tcxt/imagc data typcs is that you havc to usc spccial opcrators
(RATXT, VR!TTXT, and UPATTXT) to work with such data.
|e |es|/|mce 1c|c |,(es c/e mc/|e1 cs c 1e(/e:c|e1 /ec|a/e cs1 a||| |e /emc|e1 |s c /a|a/e |e/s|cs c/
|c|c|cse |s|se. |se l/|t|/|(///, |l/|t|/|(///, cs1 l/|3||/|(/// |ss|ec1.
UNIQUIDN7IIIR Data 7ype
As its namc implics, a valuc ol thc UN!QU!NT!F!R data typc is a uniquc
idcntilication numbcr storcd as a 16bytc binary string. This data typc is closcly rclatcd to
thc globally uniquc idcntilicr (GU!), which guarantccs uniqucncss worldwidc. Hcncc,
using this data typc, you can uniqucly idcntily data and objccts in distributcd systcms.
Thc initialization ol a column or a variablc ol thc UN!QU!NT!F!R typc can
bc providcd using thc lunctions NV! and NVSQUNT!AL!, as wcll as with
Cha pt er 4 : S QL Component s 77
a string constant writtcn in a spccial lorm using hcxadccimal digits and hyphcns. (Thc
lunctions NV! and NVSQUNT!AL! arc dcscribcd in thc scction "Systcm
Functions" latcr in this chaptcr.)
A column ol thc UN!QU!NT!F!R data typc can bc rclcrcnccd using thc
kcyword RVGU!CL in a qucry to spccily that thc column contains ! valucs.
(This kcyword docs not gcncratc any valucs.) A tablc can havc scvcral columns ol thc
UN!QU!NT!F!R typc, but only onc ol thcm can havc thc RVGU!CL
kcyword.
SQL_VARIAN7 Data 7ype
Thc SQL_\AR!ANT data typc can bc uscd to storc valucs ol various data typcs at thc
samc timc, such as numcric valucs, strings, and datc valucs. (Thc only typcs ol valucs
that cannot bc storcd arc T!MSTAMP valucs.) ach valuc ol an SQL_\AR!ANT
column has two parts: thc data valuc and thc inlormation that dcscribcs thc valuc. (This
inlormation contains all propcrtics ol thc actual data typc ol thc valuc, such as lcngth,
scalc, and prccision.)
TransactSQL supports thc SQL_\AR!ANT_PRPRTY lunction, which
displays thc attachcd inlormation lor cach valuc ol an SQL_\AR!ANT column. For
thc usc ol thc SQL_\AR!ANT data typc, scc xamplc 5.5 in Chaptcr 5.
|e:|c/e c :c|ams c/ c |c||e as|s ||e :9|_l/||/| 1c|c |,(e cs|, |/ || |s /ec||, se:essc/,. / :c|ams s|ca|1
|c|e |||s 1c|c |,(e |/ ||s |c|aes mc, |e c/ 1|//e/es| |,(es c/ |/ || |s sc| (css|||e |c 1e|e/m|se ||e |,(e c/ c :c|ams
1a/|s ||e 1c|c|cse 1es|s (/c:ess.
HIRARCHYID Data 7ype
Thc H!RARCHY! data typc is uscd to storc an cntirc hicrarchy. !t is implcmcntcd
as a Common Languagc Runtimc (CLR) uscrdclincd typc that compriscs scvcral
systcm lunctions lor crcating and opcrating on hicrarchy nodcs. Thc lollowing
lunctions, among othcrs, bclong to thc mcthods ol this data typc: GetAncestor(),
GetDescendant(), Read(), and Write(). (Thc dctailcd dcscription ol this data typc is
outsidc thc scopc ol this book.)
7IMS7AMP Data 7ype
Thc T!MSTAMP data typc spccilics a column bcing dclincd as \AR8!NARY(8) or
8!NARY(8), dcpcnding on nullability ol thc column. Thc systcm maintains a currcnt
valuc (not a datc or timc) lor cach databasc, which it incrcmcnts whcncvcr any row with
78 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
a T!MSTAMP column is inscrtcd or updatcd and scts thc T!MSTAMP column
to that valuc. Thus, T!MSTAMP columns can bc uscd to dctcrminc thc rclativc timc
whcn rows wcrc last changcd. (RV\RS!N is a synonym lor T!MSTAMP.)
|e |c|ae s|c/e1 |s c |/|://| :c|ams |ss| |m(c/|cs| |, ||se|/. ||s :c|ams |s asac||, ase1 |c 1e|e:| a|e||e/ c
s(e:|/|: /ca |cs |ees :|cse1 s|s:e ||e |cs| ||me || acs c::esse1.
DCIMAL witb VARDCIMAL Storage Format
Thc C!MAL data typc is typically storcd on thc disk as lixcdlcngth data. Sincc
SQL Scrvcr 2005 SP2, this data typc can bc storcd as a variablclcngth column by
using thc ncw storagc lormat callcd \ARC!MAL. Using \ARC!MAL, you can
signilicantly rcducc thc storagc spacc lor a C!MAL column in which valucs havc
signilicant dillcrcncc in thcir lcngths.
l/|||t|//| |s c s|c/ce /c/mc| cs1 sc| c 1c|c |,(e.

Thc \ARC!MAL storagc lormat is hclplul whcn you havc to spccily thc largcst
possiblc valuc lor a licld in which thc storcd valucs usually arc much smallcr. Tablc 41
shows this.
|e l/|||t|//| s|c/ce /c/mc| ac/|s ||e scme ac, /c/ ||e ||t|//| 1c|c |,(e cs ||e l/|t|/| 1c|c |,(e
ac/|s /c/ c|(|csame/|:c| 1c|c.
|rec|s|ea Ne. ef 8tes: Vkk0lC|Mk| Ne. ef 8tes: ||xe6 |eaqth
er NJ|| z !
+ !
z z 1
1 I
1 z I
Iah|e 4-1 |am|e/ c/ 3,|es /c/ :|c/|s l/|||t|//| cs1 ||se1 |es||
Cha pt er 4 : S QL Component s 79
To cnablc thc \ARC!MAL storagc lormat, you lirst havc to cnablc it lor thc
databasc and thcn cnablc it lor thc particular tablc ol that databasc. Thc sp_db_
vardecimal_storage_format systcm proccdurc is uscd lor thc lirst stcp, as xamplc 4.6
shows.
XAMPL 4.6
XC sp_db_vardccimal_storagc_lormat 'samplc', 'N',
Thc \ARC!MAL STRAG FRMAT option ol thc sp_table option
systcm proccdurc is uscd to turn on this storagc lor thc tablc. xamplc 4.7 turns on thc
\ARC!MAL storagc lormat lor thc project tablc.
XAMPL 4.7
XC sp_tablcoption 'projcct', 'vardccimal storagc lormat', 1
As you alrcady know, thc main rcason to usc thc \ARC!MAL storagc lormat is
to rcducc thc storagc sizc ol thc data. !l you want to tcst how much storagc spacc could
bc gaincd by using this storagc lormat, usc thc dynamic managcmcnt vicw callcd
sys.sp_estimated_rowsize_reduction_for_vardecimal. This dynamic managcmcnt
vicw givcs you a dctailcd cstimatc lor thc particular tablc.
7ransact-SL Functions
TransactSQL lunctions can bc cithcr aggrcgatc lunctions or scalar lunctions. Thc
lollowing scctions dcscribc thcsc lunction typcs.
Aggregate Functions
ggregate functions arc applicd to a group ol data valucs lrom a column. Aggrcgatc
lunctions always rcturn a singlc valuc. TransactSQL supports scvcral groups ol
aggrcgatc lunctions:
Convcnicnt aggrcgatc lunctions
Statistical aggrcgatc lunctions

Analytic aggrcgatc lunctions
80 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Statistical and analytic aggrcgatcs arc discusscd in Chaptcr 24. Uscrdclincd
aggrcgatcs arc bcyond thc scopc ol this book. That lcavcs thc convcnicnt aggrcgatc
lunctions, dcscribcd ncxt:
AVG Calculatcs thc arithmctic mcan (avcragc) ol thc data valucs containcd
MAX and MN Calculatc thc maximum and minimum data valuc ol thc
valucs.
SUM
contain numcric valucs.
COUNT
rcturns thc numbcr ol rows (whcthcr or not particular columns havc NULL
valucs).
COUNT_BG
that CUNT_8!G rcturns a valuc ol thc 8!G!NT data typc.
Thc usc ol convcnicnt aggrcgatc lunctions with thc SLCT statcmcnt arc
dcscribcd in dctail in Chaptcr 6.
ScaIar Functions
!n addition to aggrcgatc lunctions, TransactSQL providcs scvcral scalar lunctions that
arc uscd in thc construction ol scalar cxprcssions. (A scalar lunction opcratcs on a singlc
valuc or list ol valucs, as opposcd to aggrcgatc lunctions, which opcratc on thc data
lrom multiplc rows.) Scalar lunctions can bc catcgorizcd as lollows:
Numcric lunctions
atc lunctions
String lunctions
Systcm lunctions
Mctadata lunctions
Thc lollowing scctions dcscribc thcsc lunction typcs.
Cha pt er 4 : S QL Component s 81
Numeric Iunctions
Numcric lunctions within TransactSQL arc mathcmatical lunctions lor modilying
numcric valucs. Thc lollowing numcric lunctions arc availablc:
|cact|ea lxp|aaat|ea
||:(a} |etara. t|e +|.e|ate .+|ae (|.e., ae+t|.e .+|ae. +re retaraea +. je.|t|.e} el t|e aamer|c e\jre..|ea
a. |\+mj|e.
:|||CI ||:(-!.II} !.II, :|||CI ||:(.1+} .1+
|C0:(a} C+|ca|+te. +rc ce.|ae el a. a +aa t|e re.a|t|a .+|ae |e|ea te t|e ||0|I a+t+ tje.
|:|N(a} C+|ca|+te. t|e +rc .|ae el a. a +aa t|e re.a|t|a .+|ae |e|ea te t|e ||0|I a+t+ tje.
|I|N(a} C+|ca|+te. t|e +rc t+aeat el a. a +aa t|e re.a|t|a .+|ae |e|ea te t|e ||0|I a+t+ tje.
|INz(a,m} C+|ca|+te. t|e +rc t+aeat el a/m. a, m, +aa t|e re.a|t|a .+|ae |e|ea te t|e ||0|I a+t+ tje.
C||||N0(a} |etara. t|e .m+||e.t |ateer .+|ae re+ter er ea+| te t|e .jec|l|ea j+r+meter. |\+mj|e..
:|||CI C||||N0(+.} !
:|||CI C||||N0(-+.} -+
C0:(a} C+|ca|+te. t|e ce.|ae el a. a +aa t|e re.a|t|a .+|ae |e|ea te t|e ||0|I a+t+ tje.
C0I(a} C+|ca|+te. t|e cet+aeat el a. a +aa t|e re.a|t|a .+|ae |e|ea te t|e ||0|I a+t+ tje.
||0|||:(a} Cea.ert. r+a|+a. te aeree.. |\+mj|e..
:|||CI ||0|||:(||(}/z} .
:|||CI ||0|||:(.I!} +z.I
|/|(a} C+|ca|+te. t|e .+|ae e
a
. |\+mj|e. :|||CI |/|(} z.I1
||00|(a} C+|ca|+te. t|e |+re.t |ateer .+|ae |e.. t|+a er ea+| te t|e .jec|l|ea .+|ae a. |\+mj|e.
:|||CI ||00|(+.} +
|00(a} C+|ca|+te. t|e a+tar+| (|.e., |+.e e} |e+r|t|m el a. |\+mj|e..
:|||CI |00(+.I} .!+
:|||CI |00(.z} -z.z
|00(a} C+|ca|+te. t|e |e+r|t|m (|+.e } ler a. |\+mj|e..
:|||CI |00(+.I} .I
:|||CI |00(.z} -.z
||(} |etara. t|e .+|ae el t|e aam|er j| (1.+}.
|0\||(\,} C+|ca|+te. t|e .+|ae x

. |\+mj|e..
:|||CI |0\||(1.z,!} z!.!
:|||CI |0\||(,.!}
|||||N:(a} Cea.ert. aeree. te r+a|+a.. |\+mj|e..
:|||CI |||||N:(.} .!I
:|||CI |||||N:(+z.I} .I!
||N| |etara. + r+aaem aam|er |etweea +aa w|t| + ||0|I a+t+ tje.
82 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
|cact|ea lxp|aaat|ea
|0JN|(a, j,t[} |eaaa. t|e .+|ae el t|e aam|er a | a.|a t|e jrec|.|ea p. J.e je.|t|.e .+|ae. el p te reaaa ea
t|e r||t .|ae el t|e aec|m+| je|at +aa a.e ae+t|.e .+|ae. te reaaa ea t|e |elt .|ae. |a ejt|ea+|
j+r+meter t c+a.e. a te |e traac+tea. |\+mj|e..
:|||CI |0JN|(!.+!I,1} !.+!I
:|||CI |0JN|(1+!.+!I,-} 1!.
:|||CI |0JN|(1+!.+!I,-,} 1+.
|0\C0JNI_||0 |etara. t|e aam|er el rew. t|+t |+.e |eea +llectea | t|e |+.t Ir+a.+ct-:9| .t+temeat e\ecatea
| t|e ..tem. I|e retara .+|ae el t||. laact|ea |+. t|e ||0|NI a+t+ tje.
:|0N(a} |etara. t|e .|a el t|e .+|ae a +. + aam|er (+ ler je.|t|.e, - ler ae+t|.e, +aa ler tere}.
|\+mj|e.
:|||CI :|0N(.}
:|N(a} C+|ca|+te. t|e .|ae el a. a +aa t|e re.a|t|a .+|ae |e|ea te t|e ||0|I a+t+ tje.
:9|I(a} C+|ca|+te. t|e .a+re reet el a. |\+mj|e.
:|||CI :9|I(} 1
:9J|||(a} |etara. t|e .a+re el t|e |.ea e\jre..|ea. |\+mj|e.
:|||CI :9J|||(}
I|N(a} C+|ca|+te. t|e t+aeat el a. a +aa t|e re.a|t|a .+|ae |e|ea te t|e ||0|I a+t+ tje.
Date Iunctions
atc lunctions calculatc thc rcspcctivc datc or timc portion ol an cxprcssion or rcturn
thc valuc lrom a timc intcrval. TransactSQL supports thc lollowing datc lunctions:
|cact|ea lxp|aaat|ea
0|I||I|(} |etara. t|e carreat ..tem a+te +aa t|me. |\+mj|e.
:|||CI 0|I||I|(} z-- 1.1.1.1
||I||||I(|tem,a+te} |etara. t|e .jec|l|ea j+rt |tem el + a+te 6ate +. +a |ateer. |\+mj|e..
:|||CI ||I||||I(meat|, '..z!'} ( I+aa+r}
:|||CI ||I||||I(wee|a+, '..z!'} I (I :aaa+}
||I|N|M|(|tem,a+te} |etara. t|e .jec|l|ea j+rt |tem el t|e a+te 6ate +. + c|+r+cter .tr|a. |\+mj|e.
:|||CI ||I|N|M|(wee|a+, '..z!'} :+tara+
||I|||||(|tem,a+t,a+tz} C+|ca|+te. t|e a|llereace |etweea t|e twe a+te j+rt. 6at1 +aa 6at1 +aa retara. t|e re.a|t
+. +a |ateer |a aa|t. .jec|l|ea | t|e .+|ae |tem. |\+mj|e.
:|||CI ||I|||||(e+r, ||rt||+te, 0|I||I|(}} |: +e ||0M emj|eee, -> retara. t|e +e
el e+c| emj|eee.
||I||||(|,a,a} |aa. t|e aam|er a el aa|t. .jec|l|ea | t|e .+|ae | te t|e |.ea a+te 6. |\+mj|e.
:|||CI ||I||||(||\,1,||re|+te} |: +e ||0M emj|eee, -> +aa. t|ree a+. te t|e
.t+rt|a a+te el emj|emeat el e.er emj|eee (.ee t|e samp|e a+t+|+.e}.
Cha pt er 4 : S QL Component s 83
String Iunctions
String lunctions arc uscd to manipulatc data valucs in a column, usually ol a charactcr
data typc. TransactSQL supports thc lollowing string lunctions:
|cact|ea lxp|aaat|ea
|:C||(c|+r+cter} Cea.ert. t|e .jec|l|ea c|+r+cter te t|e ea|.+|eat |ateer (|:C||} ceae. |etara. +a |ateer.
|\+mj|e.
:|||CI |:C||('|'} !
C|||(|ateer} Cea.ert. t|e |:C|| ceae te t|e ea|.+|eat c|+r+cter. |\+mj|e.
:|||CI C|||(!} '|'.
C||||N||/(t,tz} |etara. t|e .t+rt|a je.|t|ea w|ere t|e j+rt|+| .tr|a t1 l|r.t eccar. |a t|e .tr|a t1. |etara.
|l t1 aee. aet eccar |a t1. |\+mj|e.
:|||CI C||||N||/('||', 't+||e'} 1.
|||||||NC|(t,tz} |etara. +a |ateer, t|rea| +, t|+t |. t|e a|llereace el :0JN||/ .+|ae. el twe .tr|a. t1 +aa
t1. (:0JN||/ retara. + aam|er t|+t .jec|l|e. t|e .eaaa el + .tr|a. \|t| t||. met|ea, .tr|a.
w|t| .|m||+r .eaaa. c+a |e aeterm|aea.} |\+mj|e.
:|||CI |||||||NC|('.je|||a', 'te|||a'} z (.eaaa. + ||tt|e ||t .|m||+r, aee.a't .eaaa
.|m||+r}
|||I(t, |eat|} |etara. t|e l|r.t |eaqth c|+r+cter. lrem t|e .tr|a t.
||N(t} |etara. t|e aam|er el c|+r+cter., |a.te+a el t|e aam|er el |te., el t|e .jec|l|ea .tr|a
e\jre..|ea, e\c|aa|a tr+|||a ||+a|..
|0\||(t} Cea.ert. +|| ajjerc+.e |etter. el t|e .tr|a t1 te |ewerc+.e |etter.. |ewerc+.e |etter. +aa
aam|er., +aa et|er c|+r+cter., ae aet c|+ae. |\+mj|e.
:|||CI |0\||('||0'} '||'
|I||M(t} |eme.e. |e+a|a ||+a|. |a t|e .tr|a t. |\+mj|e.
:|||CI |I||M(' :tr|a'} ':tr|a'
NC|||(|} |etara. t|e Ja|ceae c|+r+cter w|t| t|e .jec|l|ea |ateer ceae, +. ael|aea | t|e Ja|ceae
.t+aa+ra.
9J0I|N|M|(c|+r_.tr|a} |etara. + Ja|ceae .tr|a w|t| t|e ae||m|ter. +aaea te m+|e t|e |ajat .tr|a + .+||a ae||m|tea
|aeat|l|er.
||I|N||/(j,e\jr} |etara. t|e .t+rt|a je.|t|ea el t|e l|r.t eccarreace el + j+ttera p |a + .jec|l|ea e\jre..|ea
expr, er tere. |l t|e j+ttera |. aet leaaa. |\+mj|e..
} :|||CI ||I|N||/('.', '|ea.tr|a'} +,
z} :|||CI ||0|I(Ceat+ctN+me, ||N(Ceat+ctN+me}-||I|N||/(' ',Ceat+ctN+me}} |:
||r.t_a+me ||0M Ca.temer.,
(I|e .eceaa aer retara. +|| l|r.t a+me. lrem t|e ccstemers ce|ama.}
|||||C|(.tr,.trz,.tr1} |ej|+ce. +|| eccarreace. el t|e str1 |a t|e str1 w|t| t|e str1. |\+mj|e.
:|||CI |||||C|('.|+.e' , '.' , '|e'} |e|+.e
84 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
|cact|ea lxp|aaat|ea
|||||C|I|(t,|} |eje+t. .tr|a t | t|me.. |\+mj|e.
:|||CI |||||C|I|('+',} '++++++++++'
||\||:|(t} ||.j|+. t|e .tr|a t |a t|e re.er.e eraer. |\+mj|e.
:|||CI ||\||:|('c+|ca|+te'} 'et+|ac|+c'
||0|I(t,|eat|} |etara. t|e |+.t |eaqth c|+r+cter. lrem t|e .tr|a t. |\+mj|e.
:|||CI ||0|I('Nete|ee|',+} '|ee|'
|I||M(t} |eme.e. tr+|||a ||+a|. el t|e .tr|a t. |\+mj|e.
:|||CI |I||M('Nete|ee| '} 'Nete|ee|'
:0JN||/(+} |etara. + lear-c|+r+cter :0JN||/ ceae te aeterm|ae t|e .|m||+r|t |etweea twe .tr|a..
|\+mj|e.
:|||CI :0JN||/('.je|||a'} :+!
:||C|(|eat|} |etara. + .tr|a w|t| .j+ce. el |eat| .jec|l|ea | |eaqth. |\+mj|e.
:|||CI :||C| ' '
:I|(l,|ea ,a[[} Cea.ert. t|e .jec|l|ea l|e+t e\jre..|ea f |ate + .tr|a. |ea |. t|e |eat| el t|e .tr|a |ac|aa|a
aec|m+| je|at, .|a, a||t., +aa .j+ce. ( | ael+a|t}, +aa 6 |. t|e aam|er el a||t. te t|e
r||t el t|e aec|m+| je|at te |e retaraea. |\+mj|e.
:|||CI :I|(1.+!I,+,z} '1.+'
:IJ||(t,+,|eat|,tz} |ej|+ce. t|e j+rt|+| .tr|a t1 w|t| t|e j+rt|+| .tr|a t1 .t+rt|a +t je.|t|ea +, rej|+c|a
|eaqth c|+r+cter. el t1. |\+mj|e..
:|||CI :IJ||('Nete|ee|',!,, ' |a + '} 'Nete |a + |ee|'
:|||CI :IJ||('Nete|ee|',,+, '|+aa'} '|+aa|ee|'
:J|:I||N0(t,+,|eat|} Cre+te. + j+rt|+| .tr|a lrem .tr|a t .t+rt|a +t t|e je.|t|ea + w|t| + |eat| el |eaqth.
|\+mj|e.
:|||CI :J|:I||N0('w+rare|e',,+} 'w+ra'
JN|C0|| |etara. t|e |ateer .+|ae, +. ael|aea | t|e Ja|ceae .t+aa+ra, ler t|e l|r.t c|+r+cter el t|e
|ajat e\jre..|ea.
J||||(t} Cea.ert. +|| |ewerc+.e |etter. el .tr|a t te ajjerc+.e |etter.. Jjjerc+.e |etter. +aa aam|er.
ae aet c|+ae. |\+mj|e.
:|||CI J||||('|e\er'} '|0\||'
System Iunctions
Systcm lunctions ol TransactSQL providc cxtcnsivc inlormation about databasc
objccts. Most systcm lunctions usc an intcrnal numcric idcntilicr (!), which is
assigncd to cach databasc objcct by thc systcm at its crcation. Using this idcntilicr,
thc systcm can uniqucly idcntily cach databasc objcct. Systcm lunctions providc
inlormation about thc databasc systcm. Thc lollowing tablc dcscribcs scvcral systcm
lunctions. (For thc complctc list ol all systcm lunctions, plcasc scc 8ooks nlinc.)
Cha pt er 4 : S QL Component s 85
|cact|ea lxp|aaat|ea
C|:I(+ |: tje (|eat|}[ Cea.ert. +a e\jre..|ea a |ate t|e .jec|l|ea a+t+ tje tpe (|l je..|||e}. a cea|a |e +a
.+||a e\jre..|ea. |\+mj|e.
:|||CI C|:I(1 |: ||0|NI} 1
C0|||:C|(+

,+
z
,.} |etara. ler + |.ea ||.t el e\jre..|ea. a
1
, a
1
,... t|e .+|ae el t|e l|r.t e\jre..|ea t|+t |. aet
NJ||.
C0|_||N0I|(e|j,ce|} |etara. t|e |eat| el t|e ce|ama ce| |e|ea|a te t|e a+t+|+.e e|ject (t+||e er .|ew}
eh[. |\+mj|e.
:|||CI C0|_||N0I|('ca.temer.', 'ca.t_||'}
C0N\||I(tje(|eat|}[,+} |a|.+|eat te C|:I, |at t|e +rameat. +re .jec|l|ea a|llereat|. C0N\||I c+a |e a.ea
w|t| +a a+t+ tje.
CJ|||NI_I|M|:I|M| |etara. t|e carreat a+te +aa t|me. |\+mj|e.
:|||CI CJ|||NI_I|M|:I|M| 'z-- I.zz.!!.I'
CJ|||NI_J:|| |etara. t|e a+me el t|e carreat a.er.
||I|||N0I|(t} C+|ca|+te. t|e |eat| (|a |te.} el t|e re.a|t el t|e e\jre..|ea t. |\+mj|e.
:|||CI ||I|||N0I|(|reaactN+me} ||0M jreaact.. (I||. aer retara. t|e |eat| el
e+c| l|e|a.}
0|I|N:|NJ||('a|a+me'} |etara. |l t|e a.e el NJ|| .+|ae. |a t|e a+t+|+.e 6haame cemj||e. w|t| t|e |N:| :9|
.t+aa+ra. (:ee +|.e t|e e\j|+a+t|ea el NJ|| .+|ae. +t t|e eaa el t||. c|+jter.} |\+mj|e.
:|||CI 0|I|N:|NJ||('|a.eatare\er|.'}
|:NJ||(e\jr, .+|ae} |etara. t|e .+|ae el expr |l t|+t .+|ae |. aet aa||, et|erw|.e, |t retara. a|ce
(.ee |\+mj|e !.zz}.
|:NJM|||C(e\jre..|ea} |eterm|ae. w|et|er +a e\jre..|ea |. + .+||a aamer|c tje.
N|\||(} Cre+te. + aa|ae || aam|er t|+t cea.|.t. el + -|te ||a+r .tr|a |ateaaea te .tere
.+|ae. el t|e JN|9J||||NI||||| a+t+ tje.
N|\:|9J|NI|||||(} Cre+te. + 0J|| t|+t |. re+ter t|+a +a 0J|| jre.|ea.| eaer+tea | t||. laact|ea ea +
.jec|l|ea cemjater. (I||. laact|ea c+a ea| |e a.ea +. + ael+a|t .+|ae ler + ce|ama.}
NJ||||(e\jr

,e\jr
z
} |etara. t|e NJ|| .+|ae |l t|e e\jre..|ea. expr
1
+aa expr
1
+re ea+|. |\+mj|e.
:|||CI NJ||||(jreject_ae, 'j'} ||0M jreject.. (I|e aer retara. NJ|| ler t|e jreject
w|t| t|e jreject_ae 'j'}.
:||\||||0|||I\(jrejerta+me} |etara. t|e jrejert |alerm+t|ea +|eat t|e a+t+|+.e .er.er.
:\:I|M_J:|| |etara. t|e |e|a || el t|e carreat a.er. |\+mj|e.
:|||CI :\:I|M_J:|| |I|1+z\aa.+a
J:||_||(a.er_a+me[} |etara. t|e |aeat|l|er el t|e a.er cser_aame. |l ae a+me |. .jec|l|ea, t|e |aeat|l|er el
t|e carreat a.er |. retr|e.ea. |\+mj|e.
:|||CI J:||_||('ae.t'} z
J:||_N|M|(|a[} |etara. t|e a+me el t|e a.er w|t| t|e |aeat|l|er |6. |l ae a+me |. .jec|l|ea, t|e a+me el
t|e carreat a.er |. retr|e.ea. |\+mj|e.
:|||CI J:||_N|M| 'ae.t'
86 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
All string lunctions can bc ncstcd in any ordcr, lor cxamplc, R\RS
(CURRNT_USR).
Metadata Iunctions
Gcncrally, mctadata lunctions rcturn inlormation conccrning thc spccilicd databasc
and databasc objccts. Thc lollowing tablc dcscribcs scvcral mctadata lunctions. (For thc
complctc list ol all mctadata lunctions, plcasc scc 8ooks nlinc.)
|cact|ea lxp|aaat|ea
C0|_N|M|(t+|_|a, ce|_|a} |etara. t|e a+me el + ce|ama |e|ea|a te t|e t+||e w|t| t|e || tah_|6 +aa
ce|ama || ce|_|6. |\+mj|e.
:|||CI C0|_N|M|(0|I|CI_||('emj|eee'} , 1} 'emj_|a+me'
C0|JMN||0|||I\
(|a, ce|, jrejert}
|etara. t|e |alerm+t|ea +|eat t|e .jec|l|ea ce|ama. |\+mj|e.
:|||CI C0|JMN||0|||I\(e|ject_|a('jreject'}, 'jreject_ae', '|||C|:|0N'} +
||I|||:|||0|||I\(a+t+|+.e, jrejert} |etara. t|e a+mea a+t+|+.e jrejert .+|ae ler t|e .jec|l|ea a+t+|+.e +aa
jrejert. |\+mj|e.
:|||CI ||I|||:|||0|||I\('.+mj|e', '|.Na||Ceac+t'} . (I|e |sNc||Ceacat
jrejert cerre.jeaa. te t|e ejt|ea C0NC|I_NJ||_\||||:_NJ||, w||c| |.
ae.cr||ea +t t|e eaa el t||. c|+jter.}
||_||(a|_a+me[} |etara. t|e |aeat|l|er el t|e a+t+|+.e 6h_aame. |l ae a+me |. .jec|l|ea, t|e
|aeat|l|er el t|e carreat a+t+|+.e |. retaraea. |\+mj|e.
:|||CI ||_||('|a.eatare\er|.'}
||_N|M|(a|_|a[} |etara. t|e a+me el t|e a+t+|+.e w|t| t|e |aeat|l|er 6h_|6. |l ae |aeat|l|er |.
.jec|l|ea, t|e a+me el t|e carreat a+t+|+.e |. a|.j|+ea. |\+mj|e.
:|||CI ||_N|M|(} '|a.eatare\er|.'
|N||/_C0|(t+||e, |, ae} |etara. t|e a+me el t|e |aae\ea ce|ama |a t|e t+||e tah|e, ael|aea | t|e
|aae\ |aeat|l|er | +aa t|e je.|t|ea ae el t|e ce|ama |a t|e |aae\.
|N||/||0|||I\(e|j_|a, |aae\_a+me,
jrejert}
|etara. t|e a+mea |aae\ er .t+t|.t|c. jrejert .+|ae el + .jec|l|ea t+||e
|aeat|l|c+t|ea aam|er, |aae\ er .t+t|.t|c. a+me, +aa jrejert a+me.
0|I|CI_N|M|(e|j_|a} |etara. t|e a+me el t|e a+t+|+.e e|ject w|t| t|e |aeat|l|er eh[_|6. |\+mj|e.
:|||CI 0|I|CI_N|M|(+!1!I!+} 'jreaact.'
0|I|CI_||(e|j_a+me} |etara. t|e |aeat|l|er el t|e a+t+|+.e e|ject eh[_aame. |\+mj|e.
:|||CI 0|I|CI_||('jreaact.'} +!1!I!+
0|I|CI||0|||I\(e|j_|a,jrejert} |etara. t|e |alerm+t|ea +|eat t|e e|ject. lrem t|e carreat a+t+|+.e.
Cha pt er 4 : S QL Component s 87
ScaIar Dperators
Scalar opcrators arc uscd lor opcrations with scalar valucs. TransactSQL supports
numcric and 8oolcan opcrators as wcll as concatcnation.
Thcrc arc unary and binary arithmctic opcrators. Unary opcrators arc and (as
signs). 8inary arithmctic opcrators arc , , , /, and %. (Thc lirst lour binary opcrators
havc thcir rcspcctivc mathcmatical mcanings, whcrcas % is thc modulo opcrator.)
8oolcan opcrators havc two dillcrcnt notations dcpcnding on whcthcr thcy arc
applicd to bit strings or to othcr data typcs. Thc opcrators NT, AN, and R arc
applicd to all data typcs (cxccpt 8!T). Thcy arc dcscribcd in dctail in Chaptcr 6.
Thc bitwisc opcrators lor manipulating bit strings arc listcd hcrc, and xamplc 4.8
shows how thcy arc uscd:
Complcmcnt (i.c., NT)
& Conjunction ol bit strings (i.c., AN)
isjunction ol bit strings (i.c., R)
xclusivc disjunction (i.c., XR or xclusivc R)
XAMPL 4.8
~(1001001) - (0110110)
(11001001) ] (10101101) - (11101101)
(11001001) & (10101101) - (10001001)
(11001001) ^ (10101101) - (01100100)
Thc concatcnation opcrator can bc uscd to concatcnatc two charactcr strings or bit
strings.
CIobaI VariabIes
Global variablcs arc spccial systcm variablcs that can bc uscd as il thcy wcrc scalar
constants. TransactSQL supports many global variablcs, which havc to bc prcccdcd by
thc prclix . Thc lollowing tablc dcscribcs scvcral global variablcs. (For thc complctc
list ol all global variablcs, scc 8ooks nlinc.)
Var|ah|e lxp|aaat|ea
yyC0NN|CI|0N: |etara. t|e aam|er el |e|a +ttemjt. .|ace .t+rt|a t|e ..tem.
yyC|J_|J:\ |etara. t|e tet+| C|J t|me (|a aa|t. el m||||.eceaa.} a.ea .|ace .t+rt|a t|e ..tem.
yy|||0| |etara. t|e |alerm+t|ea +|eat t|e retara .+|ae el t|e |+.t e\ecatea Ir+a.+ct-:9| .t+temeat.
88 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Var|ah|e lxp|aaat|ea
yy|||NI|I\ |etara. t|e |+.t |a.ertea .+|ae ler t|e ce|ama w|t| t|e |||NI|I\ jrejert (.ee C|+jter }.
yy||N0|| |etara. t|e |aeat|l|er el t|e |+aa+e t|+t |. carreat| a.ea | t|e a+t+|+.e ..tem.
yy||N0J|0| |etara. t|e a+me el t|e |+aa+e t|+t |. carreat| a.ea | t|e a+t+|+.e ..tem.
yyM|/_C0NN|CI|0N: |etara. t|e m+\|mam aam|er el +cta+| ceaaect|ea. te t|e ..tem.
yy||0C|| |etara. t|e |aeat|l|er ler t|e .terea jreceaare carreat| |e|a e\ecatea.
yy|0\C0JNI |etara. t|e aam|er el rew. t|+t |+.e |eea +llectea | t|e |+.t Ir+a.+ct-:9| .t+temeat
e\ecatea | t|e ..tem.
yy:||\||N|M| |etr|e.e. t|e |alerm+t|ea ceacera|a t|e |ec+| a+t+|+.e .er.er. I||. |alerm+t|ea ceat+|a.,
+mea et|er t||a., t|e a+me el t|e .er.er +aa t|e a+me el t|e |a.t+ace.
yy:||| |etara. t|e |aeat|l|er el t|e .er.er jrece...
yy\||:|0N |etara. t|e carreat .er.|ea el t|e a+t+|+.e ..tem .eltw+re.
NULL VaIues
A NULL valuc is a spccial valuc that may bc assigncd to a column. This valuc is
normally uscd whcn inlormation in a column is unknown or not applicablc. For
cxamplc, in thc casc ol an unknown homc tclcphonc numbcr lor a company's cmploycc,
it is rccommcndcd that thc NULL valuc bc assigncd to thc home_telephone column.
Any arithmctic cxprcssion rcsults in a NULL il any opcrand ol that cxprcssion is itscll
a NULL valuc. Thcrclorc, in unary arithmctic cxprcssions (il A is an cxprcssion with a
NULL valuc), both A and A rcturn NULL. !n binary cxprcssions, il onc (or both) ol
thc opcrands A or 8 has thc NULL valuc, A 8, A 8, A 8, A / 8, and A 8 also
rcsult in a NULL. (Thc opcrands A and 8 havc to bc numcrical cxprcssions.)
!l an cxprcssion contains a rclational opcration and onc (or both) ol thc opcrands has
(havc) thc NULL valuc, thc rcsult ol this opcration will bc NULL. Hcncc, cach ol thc
cxprcssions A - 8, A ~ 8, A 8, and A ~ 8 also rcturns NULL.
!n thc 8oolcan AN, R, and NT, thc bchavior ol thc NULL valucs is spccilicd
by thc lollowing truth tablcs, whcrc T stands lor truc, U lor unknown (NULL), and F
lor lalsc. !n thcsc tablcs, lollow thc row and column rcprcscntcd by thc valucs ol thc
8oolcan cxprcssions that thc opcrator works on, and thc valuc whcrc thcy intcrscct
rcprcscnts thc rcsulting valuc.
|N| I J |
I I J |
J J J |
| | | |
0| I J |
I I I I
J I J J
| I J |
N0I
I |
J J
| I
Cha pt er 4 : S QL Component s 89
Any NULL valuc in thc argumcnt ol aggrcgatc lunctions A\G, SUM, MAX, M!N,
and CUNT is climinatcd bclorc thc rcspcctivc lunction is calculatcd (cxccpt lor thc
lunction CUNT()). !l a column contains only NULL valucs, thc lunction rcturns
NULL. Thc aggrcgatc lunction CUNT() handlcs all NULL valucs thc samc as
nonNULL valucs. !l thc column contains only NULL valucs, thc rcsult ol thc lunction
CUNT(!ST!NCT column_namc) is 0.
A NULL valuc has to bc dillcrcnt lrom all othcr valucs. For numcric data typcs,
thcrc is a distinction bctwccn thc valuc zcro and NULL. Thc samc is truc lor thc cmpty
string and NULL lor charactcr data typcs.
A column ol a tablc allows NULL valucs il its dclinition cxplicitly contains NULL.
n thc othcr hand, NULL valucs arc not pcrmittcd il thc dclinition ol a column
cxplicitly contains NT NULL. !l thc uscr docs not spccily NULL or NT NULL lor
a column with a data typc (cxccpt T!MSTAMP), thc lollowing valucs arc assigncd:
NULL !l thc ANS!_NULL_FLT_N option ol thc ST statcmcnt is sct to N
NOT NULL !l thc ANS!_NULL_FLT_FF option ol thc ST statcmcnt
is sct to N
!l thc ST statcmcnt isn't activatcd, a column will contain thc valuc NT NULL
by dclault. (Thc columns ol T!MSTAMP data typc can only bc dcclarcd as NT
NULL columns.)
Thcrc is also anothcr option ol thc ST statcmcnt: CNCAT_NULL_Y!LS_
NULL. This option inllucnccs thc concatcnation opcration with a NULL valuc so that
anything you concatcnatc to a NULL valuc will yicld NULL again. For instancc:
'San Francisco' NULL - NULL
ConcIusion
Thc basic lcaturcs ol TransactSQL consist ol data typcs, prcdicatcs, and lunctions.
ata typcs comply with data typcs ol thc ANS! SQL92 standard. TransactSQL
supports a varicty ol usclul systcm lunctions.
Thc ncxt chaptcr introduccs you to TransactSQL statcmcnts in rclation to SQL's
data dclinition languagc. This part ol TransactSQL compriscs all thc statcmcnts
nccdcd lor crcating, altcring, and rcmoving databasc objccts.
xercises
.4.1
Vhat is thc dillcrcncc bctwccn thc numcric data typcs !NT, SMALL!NT, and T!NY!NT:
90 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
.4.2
Vhat is thc dillcrcncc bctwccn thc data typcs CHAR and \ARCHAR: Vhcn should
you usc thc lattcr (instcad ol thc lormcr) and vicc vcrsa:
.4.3
How can you sct thc lormat ol a column with thc AT data typc so that its valucs
can bc cntcrcd in thc lorm 'yyyy/mm/dd':
!n thc lollowing two cxcrciscs, usc thc SLCT statcmcnt in thc Qucry ditor
componcnt window ol SQL Scrvcr Managcmcnt Studio to display thc rcsult ol all
systcm lunctions and global variablcs. (For instancc, SLCT host_id() displays thc
! numbcr ol thc currcnt host.)
.4.4
Using systcm lunctions, lind thc ! numbcr ol thc test databasc (xcrcisc 2.1).
.4.5
Using thc systcm variablcs, display thc currcnt vcrsion ol thc databasc systcm soltwarc
and thc languagc that is uscd by this soltwarc.
.4.6
Using thc bitwisc opcrators &, ], and ^, calculatc thc lollowing opcrations with thc bit
strings:
(11100101) & (01010111)
(10011011) ] (11001001)
(10110111) ^ (10110001)
.4.7
Vhat is thc rcsult ol thc lollowing cxprcssions: (A is a numcrical and 8 a logical cxprcssion.)
A NULL
NULL - NULL
8 R NULL
8 AN NULL
.4.8
Vhcn can you usc both singlc and doublc quotation marks to dclinc string and
tcmporal constants:
.4.9
Vhat is a dclimitcd idcntilicr and whcn do you nccd it:
In 7bis Cbapter
Creating Database Dbjects
Modifying Database Dbjects
Removing Database Dbjects
Iata Icllnltlon Ianguagc
Cbapter 5
Copyright 2008 by The McGraw-Hill Companies. Click here for terms of use.
92 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
T
his chaptcr dcscribcs all thc TransactSQL statcmcnts conccrning data
dclinition languagc (L). Thc L statcmcnts arc dividcd into thrcc
groups which arc discusscd in turn. Thc lirst group includcs statcmcnts that
crcatc objccts, thc sccond group includcs statcmcnts that modily thc structurc ol
objccts, and thc third group includcs statcmcnts that rcmovc objccts.
Creating Database Dbjects
Thc organization ol a databasc involvcs many dillcrcnt objccts. All objccts ol a databasc
can bc physical or logical. Thc physical objccts arc rclatcd to thc organization ol thc
data on thc physical dcvicc (disk). atabasc nginc's physical objccts arc lilcs and
lilcgroups. Logical objccts rcprcscnt a uscr's vicw ol a databasc. atabascs, tablcs,
columns, and vicws (virtual tablcs) arc cxamplcs ol logical objccts.
Thc lirst databasc objcct that has to bc crcatcd is a databasc itscll. atabasc nginc
managcs both systcm and uscr databascs. An authorizcd uscr can crcatc uscr databascs,
whilc systcm databascs arc gcncratcd during thc installation ol thc databasc systcm.
Systcm databascs arc
master
tempdb
model
msdb
resource
This chaptcr dcscribcs thc crcation, altcration, and rcmoval ol uscr databascs, whilc
all systcm databascs will bc covcrcd in dctail in Chaptcr 15.
Creation of a Database
Two basic mcthods arc uscd to crcatc a databasc. Thc lirst mcthod involvcs using
bjcct xplorcr in SQL Scrvcr Managcmcnt Studio (scc Chaptcr 3). Thc sccond
mcthod involvcs using thc TransactSQL statcmcnt CRAT ATA8AS. This
statcmcnt has thc lollowing gcncral lorm, thc dctails ol which arc discusscd ncxt:
CRAT ATA8AS db_namc
|N |PR!MARY| [ lilc_spcc1] ,...|
|LG N [lilc_spcc2] ,...|
|CLLAT collation_namc|
|FR [ATTACH ] ATTACH_R8U!L_LG ] |
Cha pt er 5 : Da t a Def i ni t i on L a ngua ge 93
|c/ ||e s,s|cs c/ ||e /cssc:|-:9| s|c|emes|s, ||e :cs|es||css ase1 c/e ||cse 1es:/||e1 |s ||e se:||cs :,s|cs
tcs|es||css |s t|c(|e/ !. /::c/1|s |c ||e :cs|es||css, c(||csc| ||ems c((ec/ |s |/c:|e|s, [ j. ||ems a/|||es |s
|/c:es, j }, cs1 /c||cae1 |, ... c/e ||ems ||c| :cs |e /e(ec|e1 cs, sam|e/ c/ ||mes.
db_name is thc namc ol thc databasc. Thc maximum sizc ol a databasc namc is 128
charactcrs. (Thc rulcs lor idcntilicrs dcscribcd in Chaptcr 4 apply to databasc namcs.)
Thc maximum numbcr ol databascs managcd by a singlc systcm is 32,767.
All databascs arc storcd in lilcs. Thcsc lilcs can bc cxplicitly spccilicd by thc systcm
administrator or implicitly providcd by thc systcm. !l thc N option cxists in thc
CRAT ATA8AS statcmcnt, all lilcs containing thc data ol a databasc arc
cxplicitly spccilicd.
|c|c|cse |s|se ases 1|s| /||es |c s|c/e 1c|c. |c:| 1|s| /||e :cs|c|ss 1c|c c/ c s|s|e 1c|c|cse. || |es ||emse||es
:cs |e c/cs|ce1 |s|c /||e/ca(s. |||e/ca(s (/c||1e ||e c|||||, |c 1|s|/||a|e 1c|c c|e/ 1|//e/es| 1|s| 1/||es cs1 |c
|c:| a( cs1 /es|c/e sa|se|s c/ ||e 1c|c|cse (ase/a| /c/ |e/, |c/e 1c|c|cses.
f ile_spec1 rcprcscnts a lilc spccilication, which includcs lurthcr options such as
thc logical namc ol thc lilc, thc physical namc, and thc sizc. Thc PR!MARY option
spccilics thc lirst (and most important) lilc that contains systcm tablcs and othcr
important intcrnal inlormation conccrning thc databasc. !l thc PR!MARY option is
omittcd, thc lirst lilc listcd in thc spccilication is uscd as thc primary lilc.
A login account ol atabasc nginc that is uscd to crcatc a databasc is callcd a
databasc owncr. A databasc can havc onc owncr, who always corrcsponds to a login
account namc. Thc login account, which is thc databasc owncr, has thc spccial namc
dbo. This namc is always uscd in rclation to a databasc it owns.
dbo uscs thc LG N option to dclinc onc or morc lilcs as thc physical dcstination
ol thc transaction log ol thc databasc. !l thc LG N option is not spccilicd, thc
transaction log ol thc databasc will still bc crcatcd bccausc cvcry databasc must havc at
lcast onc transaction log lilc. (atabasc nginc kccps a rccord ol cach changc it makcs
to thc databasc. Thc systcm kccps all thosc rccords, in particular bclorc and altcr valucs,
in onc or morc lilcs callcd thc transaction log. ach databasc ol thc systcm has its own
transaction log. Transaction logs arc discusscd in dctail in Chaptcr 13.)
Vith thc CLLAT option, you can spccily thc dclault collation lor thc databasc.
!l thc CLLAT option is not spccilicd, thc databasc is assigncd thc dclault collation
ol thc model databasc, which is thc samc as thc dclault collation ol thc databasc systcm.
94 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Thc FR ATTACH option spccilics that thc databasc is crcatcd by attaching an
cxisting sct ol opcrating systcm lilcs. !l this option is uscd, you havc to cxplicitly spccily
thc lirst primary lilc. Thc FR ATTACH_R8U!L_LG option spccilics that thc
databasc is crcatcd by attaching an cxisting sct ol opcrating systcm lilcs. (Attaching and
dctaching a databasc is dcscribcd latcr in this chaptcr.)
uring thc crcation ol a ncw databasc, atabasc nginc uscs thc model databasc as
a tcmplatc. Thc propcrtics ol thc model databasc can bc changcd to suit thc pcrsonal
conccption ol thc systcm administrator.
|/ ,ca |c|e c 1c|c|cse c|(e:| ||c| s|ca|1 es|s| |s ec:| ase/ 1c|c|cse, ,ca s|ca|1 :/ec|e ||c| c|(e:| |s ||e
me4e/ 1c|c|cse /|/s|.
xamplc 5.1 crcatcs a simplc databasc without any lurthcr spccilications. To cxccutc
this statcmcnt, typc it in thc Query Editor window ol SQL Scrvcr Managcmcnt
XAMPL 5.1
US mastcr,
CRAT ATA8AS samplc,
xamplc 5.1 crcatcs a databasc namcd sample. This concisc lorm ol thc CRAT
ATA8AS statcmcnt is possiblc, bccausc almost all options ol that statcmcnt havc
dclault valucs. Thc systcm crcatcs by dclault, two lilcs. Thc logical namc ol thc data lilc
is sample and its original sizc is 2M8. Similarly, thc logical namc ol thc transaction log
is sample_log and its original sizc is 1M8. (8oth sizc valucs, as wcll as othcr propcrtics
ol thc ncw databasc, dcpcnd on corrcsponding spccilications in thc model databasc.)
xamplc 5.2 crcatcs a databasc with cxplicit spccilications lor databasc and
transaction log lilcs.
XAMPL 5.2
US mastcr,
CRAT ATA8AS projccts
N (NAM-projccts_dat,
F!LNAM - 'C:\projccts.mdl',
S!Z - 10,
MAXS!Z - 100,
F!LGRVTH - 5)
Cha pt er 5 : Da t a Def i ni t i on L a ngua ge 95
LG N
(NAM-projccts_log,
F!LNAM - 'C:\projccts.ldl',
S!Z - 40,
MAXS!Z - 100,
F!LGRVTH - 10),
xamplc 5.2 crcatcs a databasc callcd projects. 8ccausc thc PR!MARY option
is omittcd, thc lirst lilc is assumcd as thc primary lilc. This lilc has thc logical namc
projects_dat and is storcd in thc lilc projects.mdf. Thc original sizc ol this lilc is
10M8. Additional portions ol 5M8 ol disk storagc arc allocatcd by thc systcm, il
nccdcd. !l thc MAXS!Z option is not spccilicd or is sct to UNL!M!T, thc lilc
will grow until thc disk is lull. (Thc K8, T8, and M8 sullixcs can bc uscd to spccily
kilobytcs, tcrabytcs, or mcgabytcs, rcspcctivclythc dclault is M8.)
Thcrc is also a singlc transaction log lilc with thc logical namc projects_log and thc
physical namc projects.ldf. All options ol thc lilc spccilication lor thc transaction log
havc thc samc namc and mcaning as thc corrcsponding options ol thc lilc spccilication
lor thc data lilc.
Using thc TransactSQL languagc, you can apply thc US statcmcnt to changc thc
databasc contcxt to thc spccilicd databasc. (Thc altcrnativc way is to sclcct thc databasc
namc in thc Database pulldown mcnu in thc toolbar ol SQL Scrvcr Managcmcnt Studio.)
Thc systcm administrator can assign a dclault databasc to a uscr by using thc CRAT
LG!N statcmcnt or thc ALTR LG!N Statcmcnt (scc also Chaptcr 12). !n this
casc, thc uscrs do not nccd to cxccutc thc US statcmcnt il thcy want to usc thcir dclault
databasc.
Creation of a Database Snapshot
Thc CRAT ATA8AS statcmcnt can also bc uscd to crcatc a databasc snapshot
ol an cxisting databasc (sourcc databasc). A databasc snapshot is transactionally
consistcnt with thc sourcc databasc as it cxistcd at thc timc ol thc snapshot's crcation.
Thc syntax lor thc crcation ol a snapshot is
CRAT ATA8AS databasc_snapshot_namc
N (NAM - logical_lilc_namc,
F!LNAM - 'os_lilc_namc') | ,...n |
AS SNAPSHT F sourcc_databasc_namc
As you can scc, il you want to crcatc a databasc snapshot, you havc to add thc AS
SNAPSHT F clausc in thc CRAT ATA8AS statcmcnt. xamplc 5.3
crcatcs a snapshot ol thc AdventureWorks databasc and storcs it in thc C:\tcmp data
dircctory. (You must crcatc this dircctory bclorc you start thc lollowing cxamplc.)
96 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
XAMPL 5.3
US mastcr,
CRAT ATA8AS AdvcnturVorks_snapshot
N (NAM - 'AdvcnturcVorks_ata' ,
F!LNAM - 'C:\tcmp\snapshot_8.mdl')
AS SNAPSHT F AdvcnturcVorks,
An cxisting databasc snapshot is a rcadonly copy ol thc corrcsponding databasc
that rcllccts thc point in timc whcn thc databasc is copicd. (For this rcason, you can
havc multiplc snapshots lor an cxisting databasc.) Thc snapshot lilc (in xamplc 5.3,
'C:\tcmp\snapshot_8.mdl ') contains only thc modilicd data that has changcd lrom
thc sourcc databasc. Thcrclorc, thc proccss ol crcating a databasc snapshot must includc
thc logical namc ol cach data lilc lrom thc sourcc databasc as wcll as ncw corrcsponding
physical namcs (scc xamplc 5.3).
Vhilc thc snapshot contains only modilicd data, thc disk spacc nccdcd lor cach
snapshot is just a small part ol thc ovcrall spacc rcquircd lor thc corrcsponding sourcc
databasc.
c :/ec|e ssc(s|c|s c/ c 1c|c|cse, ,ca see1 ||: 1|s| |c|ames, |e:case cs|, sa:| |c|ames sa((c/| ||e s(c/se
/||e |e:|sc|c, ||c| |s ase1 /c/ s|c/|s ssc(s|c|s.
atabasc snapshots arc usually uscd as a mcchanism to protcct data against uscr
crrors.
Attaching and Detaching Databases
All data ol a databasc can bc dctachcd and thcn attachcd to thc samc or anothcr
databasc scrvcr. ctaching and attaching a databasc should bc donc il you want to
movc thc databasc.
You can dctach a databasc lrom a databasc scrvcr by using thc sp_detach_db systcm
proccdurc. (Thc dctachcd databasc must bc in singlcuscr modc.)
To attach a databasc, usc cithcr thc CRAT ATA8AS statcmcnt or thc
sp_attach_db systcm proccdurc. Vhcn you attach a databasc, all data lilcs must bc
availablc. !l any data lilc has a dillcrcnt path lrom whcn thc databasc was lirst crcatcd,
you must spccily thc lilc's currcnt path.
Cha pt er 5 : Da t a Def i ni t i on L a ngua ge 97
CRA7 7AL: A asic Form
Thc CRAT TA8L statcmcnt crcatcs a ncw basc tablc with all corrcsponding
columns and thcir data typcs. Thc basic lorm ol thc CRAT TA8L statcmcnt is
CRAT TA8L tablc_namc
(col_namc1 typc1 |NT NULL] NULL|
|[, col_namc2 typc2 |NT NULL] NULL|] ...|)
3es|1es |cse |c||es, ||e/e c/e c|sc scme s(e:|c| ||s1s c/ |c||es sa:| cs |em(c/c/, |c||es cs1 ||eas (see t|c(|e/s
cs1 !!, /es(e:|||e|,.
table_name is thc namc ol thc crcatcd basc tablc. Thc maximum numbcr ol tablcs
pcr databasc is limitcd by thc numbcr ol objccts in thc databasc (thcrc can bc morc
than 2 billion objccts in a databasc, including tablcs, vicws, storcd proccdurcs, triggcrs,
and constraints). col_name1, col_name2,... arc thc namcs ol thc tablc columns. type1,
type2,... arc data typcs ol corrcsponding columns (scc Chaptcr 4).
|e scme c/ c 1c|c|cse c|(e:| :cs ese/c||, :cs|c|s /ca/ (c/|s |s ||e /c/m.
se/|e/_scme.1|_scme.s:|emc_scme.jjjc|(e:|_scme
eeject_aeme |s ||e scme c/ ||e 1c|c|cse c|(e:|. scbeme_aeme |s ||e scme c/ ||e s:|emc |c a||:| ||e
c|(e:| |e|css. setret_aeme cs1 4e_aeme c/e ||e scmes c/ ||e se/|e/ cs1 1c|c|cse |c a||:| ||e 1c|c|cse
c|(e:| |e|css. c||e scmes, :cm||se1 a||| ||e s:|emc scme, mas| |e as|ae a||||s ||e 1c|c|cse. :|m| |c/|,,
:c|ams scmes mas| |e as|ae a||||s ||e |c||e.
Thc lirst constraint that will bc discusscd in this book is thc cxistcncc and noncxistcncc
ol NULL valucs within a column. !l NT NULL is spccilicd, thc assignmcnt ol NULL
valucs lor thc column is not allowcd. (!n that casc, thc column may not contain nulls, and
il thcrc is a NULL valuc to bc inscrtcd, thc systcm rcturns an crror mcssagc.)
As alrcady statcd, a databasc objcct (in this casc, a tablc) is always crcatcd within
a schcma ol a databasc. A uscr can crcatc a tablc only in a schcma lor which shc has
ALTR pcrmissions. Any uscr in thc sysadmin, db_ddladmin, or db_owner rolc can
crcatc a tablc in any schcma. (Thc ALTR pcrmissions, as wcll as databasc and scrvcr
rolcs, arc discusscd in dctail in Chaptcr 12.)
Thc crcator ol a tablc must not bc its owncr. This mcans that you can crcatc a tablc
that bclongs to somconc clsc. Similarly, a tablc crcatcd with thc CRAT TA8L
statcmcnt must not bclong to thc currcnt databasc il somc othcr (cxisting) databasc
namc, togcthcr with thc schcma namc, is spccilicd as thc prclix ol thc tablc namc.
98 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Thc schcma to which a tablc bclongs has two possiblc dclault namcs. !l a tablc is
spccilicd without thc cxplicit schcma namc, thc systcm chccks lor a tablc namc in
thc corrcsponding dclault schcma. !l thc objcct namc cannot bc lound in thc dclault
schcma, thc systcm scarchcs in thc dbo schcma.
ca s|ca|1 c|ac,s s(e:|/, ||e |c||e scme |ce||e/ a||| ||e :c//es(cs1|s s:|emc scme. |c| ac, ,ca :cs
e||m|sc|e (css|||e cm||a|||es.
Tcmporary tablcs arc a spccial kind ol basc tablc. Thcy arc storcd in thc tempdb
databasc and arc automatically droppcd at thc cnd ol thc scssion. Thc propcrtics ol
tcmporary tablcs and cxamplcs conccrning thcm arc givcn in Chaptcr 6.
xamplc 5.4 shows thc crcation ol all tablcs ol thc sample databasc. (Thc sample
databasc should bc thc currcnt databasc.)
XAMPL 5.4
US samplc,
CRAT TA8L cmploycc (cmp_no !NTGR NT NULL,
cmp_lnamc CHAR(20) NT NULL,
cmp_lnamc CHAR(20) NT NULL,
dcpt_no CHAR(4) NULL),
CRAT TA8L dcpartmcnt(dcpt_no CHAR(4) NT NULL,
dcpt_namc CHAR(25) NT NULL,
location CHAR(30) NULL),
CRAT TA8L projcct (projcct_no CHAR(4) NT NULL,
projcct_namc CHAR(15) NT NULL,
budgct FLAT NULL),
CRAT TA8L works_on (cmp_no !NTGR NT NULL,
projcct_no CHAR(4) NT NULL,
job CHAR (15) NULL,
cntcr_datc AT NULL),
8csidcs thc data typc and thc nullability, thc column spccilication can contain thc
lollowing options:
FAULT clausc
!NT!TY propcrty
Thc FAULT clausc in thc column dclinition spccilics thc dclault valuc ol thc
columnthat is, whcncvcr a ncw row is inscrtcd into thc tablc, thc dclault valuc lor thc
Cha pt er 5 : Da t a Def i ni t i on L a ngua ge 99
particular column will bc uscd il thcrc is no valuc spccilicd lor it. A constant valuc,
such as thc systcm lunctions USR, CURRNT_USR, SSS!N_USR,
SYSTM_USR, CURRNT_T!MSTAMP, and NULL, among othcrs, can bc
uscd as thc dclault valucs.
A column with thc !NT!TY propcrty allows only intcgcr valucs, which arc
usually implicitly assigncd by thc systcm. ach valuc, which should bc inscrtcd in thc
column, is calculatcd by incrcmcnting thc last inscrtcd valuc ol thc column. Thcrclorc,
thc dclinition ol a column with thc !NT!TY propcrty contains (implicitly or
cxplicitly) an initial valuc and an incrcmcnt. This propcrty will bc discusscd in dctail in
thc ncxt chaptcr (scc xamplc 6.41).
3e:case |c|c|cse |s|se ese/c|es ||e |c|aes a||| ||e ||||| (/c(e/|,, ||ese |c|aes c/e c|ac,s 1|//e/es|,
e|es a|es ma|||(|e ase/s c/e c11|s /cas c| ||e scme ||me. ||s /ec|a/e |s |e/, ase/a| |s c ma|||ase/
es||/csmes|, a|e/e || |s a||e 1|//|:a|| /c/ cs c/1|sc/, (/c/cm |c ese/c|e as|ae same/|: |c|aes.
To closc this scction, xamplc 5.5 shows thc crcation ol a tablc with a column ol thc
SQL_\AR!ANT typc.
XAMPL 5.5
US samplc,
CRAT TA8L !tcm_Attributcs (
itcm_id !NT NT NULL,
attributc N\ARCHAR(30) NT NULL,
valuc SQL_\AR!ANT NT NULL,
PR!MARY KY (itcm_id, attributc) )
!n xamplc 5.5, thc tablc contains thc value column, which is ol typc SQL_\AR!ANT.
As you alrcady know lrom Chaptcr 4, thc SQL_\AR!ANT data typc can bc uscd to storc
valucs ol various data typcs at thc samc timc, such as numcric valucs, strings, and datc
valucs. Notc that in xamplc 5.5 thc SQL_\AR!ANT data typc is uscd lor thc column
valucs, bccausc dillcrcnt attributc valucs may bc ol dillcrcnt data typcs. For cxamplc, thc
sizc attributc storcs an intcgcr attributc valuc, and a namc attributc storcs a charactcr string
attributc valuc.
CRA7 7AL and DecIarative Integrity Constraints
nc ol thc most important lcaturcs that a 8MS must providc is a way ol maintaining
thc intcgrity ol data. Thc constraints, which arc uscd to chcck thc modilication or inscrtion
ol data, arc callcd integrity constraints. Thc task ol maintaining intcgrity constraints can
100 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
bc handlcd by thc uscr in application programs or by thc 8MS. Thc most important
bcnclits ol handling intcgrity constraints by thc 8MS arc thc lollowing:
!ncrcascd rcliability ol data
Rcduccd programming timc
Simplc maintcnancc
Using thc 8MS to dclinc intcgrity constraints incrcascs thc rcliability ol data
bccausc thcrc is no possibility that thc intcgrity constraints can bc lorgottcn by a
programmcr. (!l an intcgrity constraint is handlcd by application programs, all programs
conccrning thc constraint must includc thc corrcsponding codc. !l thc codc is omittcd
in onc application program, thc consistcncy ol data is compromiscd.)
An intcgrity constraint not handlcd by thc 8MS must bc dclincd in cvcry application
program that uscs thc data involvcd in thc constraint. !n contrast, thc samc intcgrity
constraint must bc dclincd only oncc il it is to bc handlcd by thc 8MS. Additionally,
applicationcnlorccd constraints arc usually morc complcx to codc than arc databasc
cnlorccd constraints.
!l an intcgrity constraint is handlcd by thc 8MS, thc modilication ol thc structurc
ol thc constraint must bc handlcd only oncc, in thc 8MS. Thc modilication ol a
structurc in application programs rcquircs thc modilication ol cvcry program that
involvcs thc corrcsponding codc.
Thcrc arc two groups ol intcgrity constraints handlcd by a 8MS:
cclarativc intcgrity constraints

triggcrs, scc Chaptcr 13)
Thc dcclarativc constraints arc dclincd using thc L statcmcnts CRAT
TA8L and ALTR TA8L. Thcy can bc columnlcvcl constraints or tablclcvcl
constraints. Columnlcvcl constraints, togcthcr with thc data typc and othcr column
propcrtics, arc placcd within thc dcclaration ol thc column, whilc tablclcvcl constraints
arc always dclincd at thc cnd ol thc CRAT TA8L or ALTR TA8L statcmcnt,
altcr thc dclinition ol all columns.
|e/e |s cs|, cse 1|//e/es:e |e|aees :c|ams-|e|e| :css|/c|s|s cs1 |c||e-|e|e| :css|/c|s|s. c :c|ams-|e|e|
:css|/c|s| :cs |e c((||e1 cs|, a(cs cse :c|ams, a|||e c |c||e-|e|e| :css|/c|s| :cs :c|e/ cse c/ mc/e :c|amss
c/ c |c||e.
Cha pt er 5 : Da t a Def i ni t i on L a ngua ge 101
ach dcclarativc constraint has a namc. Thc namc ol thc constraint can bc cxplicitly
assigncd using thc CNSTRA!NT option in thc CRAT TA8L statcmcnt or
thc ALTR TA8L statcmcnt. !l thc CNSTRA!NT option is omittcd, atabasc
nginc assigns an implicit namc lor thc constraint.
|s|s es(||:|| :css|/c|s| scmes |s s|/cs|, /e:cmmes1e1. |e sec/:| /c/ cs |s|e/||, :css|/c|s| :cs |e /ec||,
es|cs:e1 |/ cs es(||:|| scme /c/ c :css|/c|s| |s ase1.
All dcclarativc constraints can bc catcgorizcd into scvcral groups:
FAULT clausc
UN!QU clausc
PR!MARY KY clausc
CHCK clausc
FR!GN KY clausc and rclcrcntial intcgrity
Thc dclinition ol thc dclault valuc using thc FAULT clausc was shown carlicr in
this chaptcr (scc also xamplc 5.6). All othcr constraints arc dcscribcd in dctail in thc
lollowing scctions.
7he UNIQU CIause
Somctimcs morc than onc column or group ol columns ol thc tablc havc uniquc valucs
and thcrclorc can bc uscd as thc primary kcy. All columns or groups ol columns that
qualily to bc primary kcys arc callcd candidate keys. ach candidatc kcy is dclincd using
thc UN!QU clausc in thc CRAT TA8L or thc ALTR TA8L statcmcnt.
Thc UN!QU clausc has thc lollowing lorm:
|CNSTRA!NT c_namc|
UN!QU |CLUSTR ] NNCLUSTR| ([ col_namc1] ,...)
Thc CNSTRA!NT option in thc UN!QU clausc assigns an cxplicit namc to thc
candidatc kcy. Thc option CLUSTR or NNCLUSTR rclatcs to thc lact
that atabasc nginc always gcncratcs an indcx lor cach candidatc kcy ol a tablc. Thc
indcx can bc clustcrcdthat is, thc physical ordcr ol rows is spccilicd using thc indcxcd
ordcr ol thc column valucs. !l thc ordcr is not spccilicd, thc indcx is nonclustcrcd
102 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
(scc also Chaptcr 10). Thc dclault valuc is NNCLUSTR. col_name1 is a
column namc that builds thc candidatc kcy. (Thc maximum numbcr ol columns pcr
candidatc kcy is 16.)
xamplc 5.6 shows thc usc ol thc UN!QU clausc.
XAMPL 5.6
US samplc,
CRAT TA8L projccts (projcct_no CHAR(4) FAULT 'p1',
projcct_namc CHAR(15) NT NULL,
budgct FLAT NULL
CNSTRA!NT uniquc_no UN!QU (projcct_no)),
ach valuc ol thc project_no column ol thc projects tablc is uniquc, including
thc NULL valuc. ( Just as with any othcr valuc with a UN!QU constraint, il NULL
valucs arc allowcd on a corrcsponding column, thcrc can bc at most onc row with thc
NULL valuc lor that particular column.) !l an cxisting valuc should bc inscrtcd into
thc column project_no, thc systcm rcjccts it. Thc cxplicit namc ol thc constraint that is
dclincd in xamplc 5.6 is unique_no.
7he PRIMARY kY CIause
Thc primary key ol a tablc is a column or group ol columns whosc valucs arc dillcrcnt
in cvcry row. ach primary kcy is dclincd using thc PR!MARY KY clausc in thc
CRAT TA8L or thc ALTR TA8L statcmcnt.
Thc PR!MARY KY clausc has thc lollowing lorm:
|CNSTRA!NT c_namc|
PR!MARY KY |CLUSTR ] NNCLUSTR| ([col_namc1] ,...)
All options ol thc PR!MARY KY clausc havc thc samc mcaning as thc
corrcsponding options with thc samc namc in thc UN!QU clausc. !n contrast to
UN!QU, thc PR!MARY KY column must bc NT NULL, and its dclault valuc is
CLUSTR.
xamplc 5.7 shows thc spccilication ol thc primary kcy lor thc employee tablc ol
thc sample databasc.
ca |c|e |c 1/c( ||e em/e,ee |c||e (||0| /3|| em(|c,ee |e/c/e ,ca ese:a|e ||e /c||ca|s escm(|e.

Cha pt er 5 : Da t a Def i ni t i on L a ngua ge 103
XAMPL 5.7
US samplc,
CRAT TA8L cmploycc (cmp_no !NTGR NT NULL,
cmp_lnamc CHAR(20) NT NULL,
cmp_lnamc CHAR(20) NT NULL,
dcpt_no CHAR(4) NULL,
CNSTRA!NT prim_cmpl PR!MARY KY (cmp_no)),
Thc employee tablc is rccrcatcd and its primary kcy is dclincd in xamplc 5.7. Thc
primary kcy ol thc tablc is spccilicd using thc dcclarativc intcgrity constraint namcd
prim_empl. This intcgrity constraint is a tablclcvcl constraint, bccausc it is spccilicd
altcr thc dclinition ol all columns ol thc employee tablc.
xamplc 5.8 is cquivalcnt to xamplc 5.7, cxccpt lor thc spccilication ol thc primary
kcy ol thc employee tablc as a columnlcvcl constraint.
/c|s, ,ca |c|e |c 1/c( ||e em/e,ee |c||e (||0| /3|| em(|c,ee |e/c/e ,ca ese:a|e ||e /c||ca|s escm(|e.

XAMPL 5.8
US samplc,
CRAT TA8L cmploycc
(cmp_no !NTGR NT NULL CNSTRA!NT prim_cmpl PR!MARY KY,
cmp_lnamc CHAR(20) NT NULL,
cmp_lnamc CHAR(20) NT NULL,
dcpt_no CHAR(4) NULL),
!n xamplc 5.8, thc PR!MARY KY clausc bclongs to thc dcclaration ol thc
corrcsponding column, togcthcr with its data typc and nullability. For this rcason, it is
callcd a columnlcvcl constraint.
7he CHCk CIause
Thc check constraint spccilics conditions lor thc data inscrtcd into a column. ach row
inscrtcd into a tablc or cach valuc updating thc valuc ol thc column must mcct thcsc
conditions. Thc CHCK clausc is uscd to spccily chcck constraints. This clausc can
bc dclincd in thc CRAT TA8L or ALTR TA8L statcmcnt. Thc syntax ol thc
CHCK clausc is
|CNSTRA!NT c_namc|
CHCK |NT FR RPL!CAT!N| cxprcssion
104 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
expression must cvaluatc to a 8oolcan valuc (true or false) and can rclcrcncc any
columns in thc currcnt tablc (or just thc currcnt column il spccilicd as a column
lcvcl constraint), but no othcr tablcs. Thc CHCK clausc is not cnlorccd during a
rcplication ol thc data il thc option NT FR RPL!CAT!N cxists. (A databasc, or
a part ol it, is said to bc rcplicatcd il it is storcd at morc than onc sitc. Rcplication can
bc uscd to cnhancc thc availability ol data. Chaptcr 20 dcscribcs data rcplication.)
xamplc 5.9 shows how thc CHCK clausc can bc uscd.
XAMPL 5.9
US samplc,
CRAT TA8L customcr
(cust_no !NTGR NT NULL,
cust_group CHAR(3) NULL,
CHCK (cust_group !N ('c1', 'c2', 'c10'))),
Thc customer tablc that is crcatcd in xamplc 5.9 contains thc cust_group column
with thc corrcsponding chcck constraint. Thc databasc systcm rcturns an crror il thc
cust_group column, altcr a modilication ol its cxisting valucs or altcr thc inscrtion ol a
ncw row, would contain a valuc dillcrcnt lrom thc valucs in thc sct ('c1', 'c2', 'c10').
7he IORICN kY CIause
A foreign key is a column or group ol columns in onc tablc that contains valucs that
match thc primary kcy valucs in thc samc or anothcr tablc. ach lorcign kcy is dclincd
using thc FR!GN KY clausc combincd with thc RFRNCS clausc.
Thc FR!GN KY clausc has thc lollowing lorm:
|CNSTRA!NT c_namc|
||FR!GN KY| ([col_namc1] ,...)|
RFRNCS tablc_namc ([col_namc2],...)
|N LT [N ACT!N] CASCA ] ST NULL ] ST FAULT]|
|N UPAT [N ACT!N ] CASCA ] ST NULL ] ST FAULT]|
Thc FR!GN KY clausc dclincs all columns cxplicitly that bclong to thc lorcign
kcy. Thc RFRNCS clausc spccilics thc tablc namc with all columns that build
thc corrcsponding primary kcy. Thc numbcr and thc data typcs ol thc columns in thc
FR!GN KY clausc must match thc numbcr and thc corrcsponding data typcs ol
columns in thc RFRNCS clausc (and, ol coursc, both ol thcsc must match thc
numbcr and data typcs ol thc columns in thc primary kcy ol thc rclcrcnccd tablc).
Thc tablc that contains thc lorcign kcy is callcd thc referencing table, and thc tablc
that contains thc corrcsponding primary kcy is callcd thc parent table or referenced table.
xamplc 5.10 shows thc spccilication ol thc lorcign kcy in thc works_on tablc ol thc
sample databasc.
Cha pt er 5 : Da t a Def i ni t i on L a ngua ge 105
ca |c|e |c 1/c( ||e wetks_ea |c||e |e/c/e ,ca ese:a|e ||e /c||ca|s escm(|e.

XAMPL 5.10
US samplc,
CRAT TA8L works_on (cmp_no !NTGR NT NULL,
projcct_no CHAR(4) NT NULL,
job CHAR (15) NULL,
cntcr_datc AT NULL,
CNSTRA!NT prim_works PR!MARY KY(cmp_no, projcct_no),
CNSTRA!NT lorcign_works FR!GN KY(cmp_no)
RFRNCS cmploycc (cmp_no)),
Thc works_on tablc in xamplc 5.10 is spccilicd with two dcclarativc intcgrity
constraints: prim_works and foreign_works. 8oth constraints arc tablclcvcl constraints,
whcrc thc lormcr spccilics thc primary kcy and thc lattcr thc lorcign kcy ol thc works_on
tablc. Furthcr, thc constraint foreign_works spccilics thc employee tablc as thc parcnt
tablc and its emp_no column as thc corrcsponding primary kcy ol thc column with thc
samc namc in thc works_on tablc.
Thc FR!GN KY clausc can bc omittcd il thc lorcign kcy is dclincd as a
columnlcvcl constraint, bccausc thc column bcing constraincd is thc implicit column
"list" ol thc lorcign kcy, and thc kcyword RFRNCS is sullicicnt to indicatc what
kind ol constraint this is. Thc maximum numbcr ol FR!GN KY constraints in a
tablc is 63.
A dclinition ol thc lorcign kcys in tablcs ol a databasc imposcs thc spccilication ol
anothcr important intcgrity constraint: thc rclcrcntial intcgrity, dcscribcd ncxt.
ReferentiaI Integrity
A referential integrity cnlorccs inscrt and updatc rulcs lor thc tablcs with thc lorcign kcy
and thc corrcsponding primary kcy constraint. xamplcs 5.7 and 5.10 spccily two such
constraints: prim_empl and foreign_works. Thc RFRNCS clausc in xamplc 5.10
dctcrmincs thc employee tablc as thc parcnt tablc.
PossibIe ProbIems in ReIation to ReferentiaI Integrity
Thcrc arc lour cascs in which thc modilication ol thc valucs in thc lorcign kcy or
in thc primary kcy can causc problcms. All ol thcsc cascs will bc shown using thc
106 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
sample databasc. Thc lirst two cascs allcct modilications ol thc rclcrcncing tablc, whilc
thc last two conccrn modilications ol thc parcnt tablc.
Case 1
!nscrt a ncw row into thc works_on tablc with thc cmploycc numbcr 11111.
Thc inscrtion ol thc ncw row in thc rclcrcncing tablc works_on introduccs a ncw
cmploycc numbcr lor which thcrc is no matching cmploycc in thc parcnt tablc (employee).
!l thc rclcrcntial intcgrity lor both tablcs is spccilicd as is donc in xamplcs 5.7 and 5.10,
atabasc nginc rcjccts thc inscrtion ol a ncw row. For rcadcrs who arc lamiliar with thc
SQL languagc, thc corrcsponding TransactSQL statcmcnt is
US samplc,
!NSRT !NT works_on (cmp_no, ...)
\ALUS (11111, ...),
Case 2
Modily thc cmploycc numbcr 10102 in all rows ol thc works_on tablc. Thc ncw
numbcr is 11111.
!n Casc 2, thc cxisting valuc ol thc lorcign kcy in thc works_on tablc should bc rcplaccd
using thc ncw valuc, lor which thcrc is no matching valuc in thc parcnt tablc employee.
!l thc rclcrcntial intcgrity lor both tablcs is spccilicd as is donc in xamplcs 5.7 and 5.10,
thc databasc systcm rcjccts thc modilication ol thc rows in thc works_on tablc. Thc
corrcsponding TransactSQL statcmcnt is
US samplc,
UPAT works_on
ST cmp_no - 11111 VHR cmp_no - 10102,
Case 3
Modily thc cmploycc numbcr 10102 in thc corrcsponding row ol thc employee tablc.
Thc ncw numbcr is 22222.
!n Casc 3, thc cxisting valuc ol thc primary kcy in thc parcnt tablc and thc lorcign
kcy ol thc rclcrcncing tablc is modilicd only in thc parcnt tablc. Thc valucs in thc
rclcrcncing tablc arc unchangcd. Thcrclorc, thc systcm rcjccts thc modilication ol thc
row with thc cmploycc numbcr 10102 in thc employee tablc. Rclcrcntial intcgrity
rcquircs that no rows in thc rclcrcncing tablc (thc onc with thc FR!GN KY
clausc) can cxist unlcss a corrcsponding row in thc parcnt tablc (thc onc with thc
PR!MARY KY clausc) also cxists. thcrwisc, thc rows in thc parcnt tablc would
bc "orphancd." !l thc modilication dcscribcd abovc wcrc pcrmittcd, thcn rows in thc
Cha pt er 5 : Da t a Def i ni t i on L a ngua ge 107
works_on tablc having thc cmploycc numbcr 10102 would bc orphancd, and thc
systcm would rcjcct it. Thc corrcsponding TransactSQL statcmcnt is
US samplc,
UPAT cmploycc
ST cmp_no - 22222 VHR cmp_no - 10102,
Case 4
clctc thc row ol thc employee tablc with thc cmploycc numbcr 10102.
Casc 4 is similar to Casc 3. Thc dclction would rcmovc thc cmploycc lor which
matching rows cxist in thc rclcrcncing tablc. xamplc 5.11 shows thc dclinition ol
tablcs ol thc sample databasc with all cxisting primary kcy and lorcign kcy constraints.
(!l thc employee, department, project, and works_on tablcs alrcady cxist, drop thcm
lirst using thc RP TA8L table_name statcmcnt.)
XAMPL 5.11
US samplc,
CRAT TA8L dcpartmcnt(dcpt_no CHAR(4) NT NULL,
dcpt_namc CHAR(25) NT NULL,
location CHAR(30) NULL,
CNSTRA!NT prim_dcpt PR!MARY KY (dcpt_no)),
CRAT TA8L cmploycc (cmp_no !NTGR NT NULL,
cmp_lnamc CHAR(20) NT NULL,
cmp_lnamc CHAR(20) NT NULL,
dcpt_no CHAR(4) NULL,
CNSTRA!NT prim_cmp PR!MARY KY (cmp_no),
CNSTRA!NT lorcign_cmp FR!GN KY(dcpt_no) RFRNCS
dcpartmcnt(dcpt_no)),
CRAT TA8L projcct (projcct_no CHAR(4) NT NULL,
projcct_namc CHAR(15) NT NULL,
budgct FLAT NULL,
CNSTRA!NT prim_proj PR!MARY KY (projcct_no)),
CRAT TA8L works_on (cmp_no !NTGR NT NULL,
projcct_no CHAR(4) NT NULL,
job CHAR (15) NULL,
cntcr_datc AT NULL,
CNSTRA!NT prim_works PR!MARY KY(cmp_no, projcct_no),
CNSTRA!NT lorcign1_works FR!GN KY(cmp_no) RFRNCS
cmploycc(cmp_no),
CNSTRA!NT lorcign2_works FR!GN KY(projcct_no) RFRNCS
projcct(projcct_no)),
108 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
7he ON DL7 and ON UPDA7 Options
atabasc nginc can rcact dillcrcntly il thc valucs ol thc primary kcy ol a tablc
should bc modilicd or dclctcd. !l you try to updatc valucs ol a lorcign kcy, and thosc
modilications rcsult in inconsistcncics in thc corrcsponding primary kcy (scc Casc 1
and Casc 2 in thc prcvious scctions), thc databasc systcm will always rcjcct thc modilication
and will display a mcssagc similar to thc lollowing:
Scrvcr: Msg 547, Lcvcl 16, Statc 1, Linc 1
UPAT statcmcnt conllictcd with CLUMN FR!GN KY constraint
'FKcmploycc'. Thc conllict occurrcd in databasc 'samplc', tablc 'cmploycc',
column 'dcpt_no'. Thc statcmcnt has bccn tcrminatcd.
8ut il you try to modily thc valucs ol a primary kcy, and thcsc modilications rcsult in
inconsistcncics in thc corrcsponding lorcign kcy (scc Casc 3 and Casc 4 in thc prcvious
scctions), a databasc systcm could rcact vcry llcxibly. Gcncrally, thcrc arc lour options
lor how a databasc systcm can rcact:
NO ACTON Allows you to modily (updatc or dclctc) only thosc valucs ol thc
parcnt tablc that do not havc any corrcsponding valucs in thc lorcign kcy ol thc
rclcrcncing tablc.
CASCADE Allows you to modily (UPAT or LT) all valucs ol thc
lrom thc rclcrcncing tablc il thc corrcsponding valuc in thc primary kcy has bccn
updatcd, or thc wholc row with that valuc has bccn dclctcd lrom thc parcnt tablc.
SET NULL Allows you again to updatc or dclctc all valucs ol thc parcnt tablc.
lcad to data inconsistcncics in thc rclcrcncing tablc, thc databasc systcm scts
all corrcsponding valucs in thc lorcign kcy ol thc rclcrcncing tablc to NULL.
Similarly, il you want to dclctc thc row in thc parcnt tablc and thc dclction ol thc
valuc in thc primary kcy would lcad to data inconsistcncics, thc databasc systcm
inconsistcncics arc omittcd.
SET DEIAULT Analogous to thc ST NULL option, with onc cxccption:
all corrcsponding valucs in thc lorcign kcy arc sct to a dclault valuc. (bviously,
thc dclault valuc must still cxist in thc primary kcy ol thc parcnt tablc altcr
Cha pt er 5 : Da t a Def i ni t i on L a ngua ge 109
|e /cssc:|-:9| |csace sa((c/|s c|| /ca/ c||e/sc|||es.

xamplc 5.12 shows thc usc ol thc N LT and N UPAT options.
XAMPL 5.12
US samplc,
CRAT TA8L works_on1
(cmp_no !NTGR NT NULL,
projcct_no CHAR(4) NT NULL,
job CHAR (15) NULL,
cntcr_datc AT NULL,
CNSTRA!NT prim_works1 PR!MARY KY(cmp_no, projcct_no),
CNSTRA!NT lorcign1_works1 FR!GN KY(cmp_no)
RFRNCS cmploycc(cmp_no) N LT CASCA,
CNSTRA!NT lorcign2_works1 FR!GN KY(projcct_no)
RFRNCS projcct(projcct_no) N UPAT CASCA),
xamplc 5.12 crcatcs thc works_on1 tablc that uscs thc N LT CASCA
and N UPAT CASCA options. !l you load thc works_on1 tablc with thc
contcnt shown in Tablc 14, cach dclction ol a row in thc employee tablc will causc
thc additional dclction ol all rows in thc works_on1 tablc that havc thc corrcsponding
valuc in thc emp_no column. Similarly, cach updatc ol a valuc in thc project_no
column ol thc projcct tablc will causc thc samc modilication on all corrcsponding valucs
in thc project_no column ol thc works_on1 tablc.
Creating Dtber Database Dbjects
A rclational databasc contains not only basc tablcs that cxist in thcir own right but
also .ie.s, which arc virtual tablcs. Thc data ol a basc tablc cxists physicallythat
is, it is storcd on a diskwhilc a vicw is dcrivcd lrom onc or morc basc tablcs. Thc
CRAT \!V statcmcnt crcatcs a ncw vicw lrom onc or morc cxisting tablcs (or
vicws) using a SLCT statcmcnt, which is an inscparablc part ol thc CRAT
\!V statcmcnt. Sincc thc crcation ol a vicw always contains a qucry, thc CRAT
\!V statcmcnt bclongs to thc data manipulation languagc (ML) rathcr than to thc
data dclinition languagc (L). For this rcason, thc crcation and rcmoval ol vicws is
110 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
discusscd in Chaptcr 11, altcr thc prcscntation ol all TransactSQL statcmcnts lor
data modilication.
Thc CRAT !NX statcmcnt crcatcs a ncw index on a spccilicd tablc. Thc indiccs
arc primarily uscd to allow cllicicnt acccss to thc data storcd on a disk. Thc cxistcncc ol
an indcx can grcatly improvc thc acccss to data. !ndiccs, togcthcr with thc CRAT
!NX statcmcnt, arc discusscd in dctail in Chaptcr 10.
A stored procedure is an additional databasc objcct that can bc crcatcd using thc
corrcsponding CRAT PRCUR statcmcnt. (A storcd proccdurc is a spccial
kind ol scqucncc ol statcmcnts writtcn in TransactSQL, using thc SQL languagc and
proccdural cxtcnsions. Chaptcr 8 dcscribcs storcd proccdurcs in dctail.)
A trigger is a databasc objcct that spccilics an action as a rcsult ol an opcration.
This mcans that whcn a particular datamodilying action (modilication, inscrtion, or
dclction) occurs on a particular tablc, atabasc nginc automatically invokcs onc or
morc additional actions. Thc CRAT TR!GGR statcmcnt crcatcs a ncw triggcr.
Triggcrs arc dcscribcd in dctail in Chaptcr 14.
A synonym is a local databasc objcct that providcs a link bctwccn itscll and anothcr
objcct managcd by thc samc or a linkcd databasc scrvcr. Using thc CRAT SYNNYM
statcmcnt, you can crcatc a ncw synonym lor thc givcn objcct. xamplc 5. 13 shows thc usc
ol this statcmcnt.
XAMPL 5.13
US samplc,
CRAT SYNNYM prod
FR AdvcnturcVorks.Production.Product,
xamplc 5.13 crcatcs a synonym lor thc Product tablc in thc Production Schcma ol
thc AdventureWorks databasc. This synonym can bc uscd in ML statcmcnts, such as
SLCT, !NSRT, UPAT, and LT.
|e mc|s /ecscs |c ase s,scs,ms |s |c cm|| ||e ase c/ |es||, scmes |s |/| s|c|emes|s. /s ,ca c|/ec1, |sca,
||e scme c/ c 1c|c|cse c|(e:| :cs ese/c||, :cs|c|s /ca/ (c/|s. |s|/c1a:|s c (s|s|e-(c/| s,scs,m /c/ cs
c|(e:| ||c| |cs ||/ee c/ /ca/ (c/|s :cs sc|e ,ca ||me a|es |,(|s ||s scme.
A schema is a databasc objcct that includcs statcmcnts lor crcation ol tablcs, vicws,
and uscr privilcgcs. (You can think ol a schcma as a construct that collccts togcthcr
scvcral tablcs, corrcsponding vicws, and uscr privilcgcs.)
Cha pt er 5 : Da t a Def i ni t i on L a ngua ge 111
|c|c|cse |s|se |/ec|s ||e sc||cs c/ s:|emc ||e scme ac, || |s |/ec|e1 |s ||e /|:| :9| s|cs1c/1. |s ||e :9|
s|cs1c/1, c s:|emc |s 1e/|se1 cs c :c||e:||cs c/ 1c|c|cse c|(e:|s ||c| |s case1 |, c s|s|e (/|s:|(c| cs1 /c/ms
c s|s|e scmes(c:e. / scmes(c:e |s c se| c/ c|(e:|s ||c| :cssc| |c|e 1a(||:c|e scmes. |c/ escm(|e, |ac |c||es
:cs |c|e ||e scme scme cs|, |/ ||e, c/e |s se(c/c|e s:|emcs. |s ||e |e/s|css (/e||cas |c :9| :e/|e/ 1^^:,
1c|c|cse ase/s cs1 s:|emcs c/e |m(||:|||, :csse:|e1, ||c| |s, e|e/, 1c|c|cse ase/ |s ||e case/ c/ c s:|emc
||c| |cs ||e scme scme cs ||e ase/. /|sc, ||e t||/| :t||// s|c|emes| es|s|e1, |a| || 1|1 sc| c:|ac| |, :/ec|e c
s:|emc. (::|emc |s c |e/, |m(c/|cs| :cs:e(| |s ||e se:a/||, mc1e| c/ |c|c|cse |s|se. |c/ |||s /ecscs, ,ca :cs
/|s1 c 1e|c||e1 1es:/|(||cs c/ s:|emc |s t|c(|e/ !1.
Integrity Constraints and Domains
A domain is thc sct ol all possiblc lcgitimatc valucs that columns ol a tablc may contain.
Almost all 8MSs usc basc data typcs such as !NT, CHAR, and AT to dclinc
thc sct ol possiblc valucs lor a column. This mcthod ol cnlorcing "domain intcgrity" is
incomplctc, as can bc sccn lrom thc lollowing cxamplc.
Thc person tablc has a column, zip, that spccilics thc Z!P codc ol thc city in which
thc pcrson livcs. This column can bc dclincd using thc SMALL!NT or CHAR(5)
data typc. Thc dclinition with thc SMALL!NT data typc is inaccuratc, bccausc thc
SMALL!NT data typc contains all positivc and ncgativc valucs bctwccn 2
15
1 and
2
15
. Thc dclinition using CHAR(5) is cvcn morc inaccuratc, bccausc all charactcrs and
spccial signs can also bc uscd in such a casc. Thcrclorc, an accuratc dclinition ol Z!P
codcs rcquircs dclining an intcrval ol positivc intcgcrs bctwccn 00601 and 99950 and
assigning it to thc zip column.
CHCK constraints (dclincd in thc CRAT TA8L or ALTR TA8L
statcmcnt) can cnlorcc morc prccisc domain intcgrity bccausc thcir cxprcssions arc
llcxiblc, and thcy arc always cnlorccd whcn thc column is inscrtcd or modilicd.
Thc TransactSQL languagc providcs support lor domains by crcating alias data
typcs using thc CRAT TYP statcmcnt. Thc lollowing two scctions dcscribc alias
and CLR data typcs.
AIias Data 7ypes
An alias data typc is a spccial kind ol data typc that is dclincd by uscrs using thc
cxisting basc data typcs. Such a data typc can bc uscd with thc CRAT TA8L
statcmcnt to dclinc onc or morc columns in a databasc.
112 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Thc CRAT TYP statcmcnt is gcncrally uscd to crcatc an alias data typc. Thc
syntax ol this statcmcnt is as lollows:
CRAT TYP | typc_schcma_namc. | typc_namc
[ | FRM basc_typc | ( prccision | , scalc | ) | | NULL ] NT NULL | |
] | XTRNAL NAM asscmbly_namc | .class_namc | |]
xamplc 5.14 shows thc crcation ol an alias data typc using thc CRAT TYP
statcmcnt.
XAMPL 5.14
US samplc,
CRAT TYP zip
FRM SMALL!NT NT NULL,
xamplc 5.14 crcatcs an alias typc zip bascd on thc standard data typc CHAR(5).
This uscrdclincd data typc can now bc uscd as a data typc ol a tablc column, as shown
in xamplc 5.15.
/c|s, ,ca |c|e |c 1/c( ||e castemet |c||e (||0| /3|| :as|cme/ |e/c/e ,ca ese:a|e ||e /c||ca|s escm(|e.

XAMPL 5.15
US samplc,
CRAT TA8L customcr
(cust_no !NT NT NULL,
cust_namc CHAR(20) NT NULL,
city CHAR(20),
zip_codc Z!P,
CHCK (zip_codc 8TVN 601 AN 99950)),
xamplc 5.15 uscs thc ncw zip data typc to spccily a column ol thc customer tablc.
Thc valucs ol this column havc to bc constraincd to thc rcgion bctwccn 601 and 99950.
As can bc sccn lrom xamplc 5.15, this can bc donc using thc CHCK clausc.
ese/c||,, |c|c|cse |s|se |m(||:|||, :cs|e/|s |e|aees :cm(c||||e :c|amss c/ 1|//e/es| 1c|c |,(es. ||s |s |c||1
/c/ ||e c||cs 1c|c |,(es |cc.
Cha pt er 5 : Da t a Def i ni t i on L a ngua ge 113
SQL Scrvcr 2008 additionally supports thc crcation ol uscrdclincd tablc typcs.
xamplc 5.16 shows how you can usc thc CRAT TYP statcmcnt to crcatc such a
tablc typc.
XAMPL 5.16
US samplc,
CRAT TYP pcrson_tablc_t AS TA8L
( namc \ARCHAR(30), salary C!MAL(8,2)),
Thc uscrdclincd tablc typc callcd person_table_t has two columns: name and
salary. Such tablc typcs arc usually uscd in rclation to tablcvalucd paramctcrs (scc
Chaptcr 8).
CLR Data 7ypes
Thc CRAT TYP statcmcnt can also bc applicd to crcatc a uscrdclincd data typc
using .NT. !n this casc, thc implcmcntation ol a uscrdclincd data typc is dclincd in
a class ol an asscmbly in thc Common Languagc Runtimc (CLR). This mcans that
you can usc onc ol thc .NT languagcs likc C# or \isual 8asic to implcmcnt thc ncw
data typc. Furthcr dcscription ol thc uscrdclincd data typcs is outsidc thc scopc ol
this book.
Modifying Database Dbjects
Thc TransactSQL languagc supports changing thc structurc ol thc lollowing databasc
objccts, among othcrs:
atabasc
Tablc
Storcd proccdurc
\icw
Schcma
Triggcr
Thc lollowing two scctions dcscribc, in turn, how you can altcr a databasc and a
tablc. Thc modilication ol thc structurc ol cach ol thc last lour databasc objccts is
dcscribcd in Chaptcrs 8, 11, 12, and 14, rcspcctivcly.
114 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
AItering a Database
Thc ALTR ATA8AS statcmcnt changcs thc physical structurc ol a databasc. Thc
TransactSQL languagc allows you to changc thc lollowing propcrtics ol a databasc:
Changc thc namc ol thc databasc using thc sp_rename storcd proccdurc (scc thc
scction, "Altcring a Tablc")




Sct databasc options
Thc lollowing subscctions dcscribc thcsc dillcrcnt typcs ol databasc altcrations.
Adding or Removing Database IiIes, Log IiIes, or IiIegroups
Thc ALTR ATA8AS statcmcnt allows thc addition and rcmoval ol databasc lilcs.
Thc clauscs A F!L and RM\ F!L spccily thc addition ol a ncw lilc and
thc dclction ol an cxisting lilc, rcspcctivcly. (Additionally, a ncw lilc can bc assigncd to
an cxisting lilcgroup using thc T F!LGRUP option.)
xamplc 5.17 shows how a ncw databasc lilc can bc addcd to thc projects databasc.
XAMPL 5.17
US mastcr,
G
ALTR ATA8AS projccts
A F!L (NAM-projccts_dat1,
F!LNAM - 'C:\projccts1.mdl', S!Z - 10,
MAXS!Z - 100, F!LGRVTH - 5),
Thc ALTR ATA8AS statcmcnt in xamplc 5.17 adds a ncw lilc with thc
logical namc projects_dat1. !ts initial sizc is 10M8, and this lilc will grow using units
ol 5M8 until it rcachcs thc uppcr limit ol 100M8.
Thc RM\ F!L clausc rcmovcs onc or morc lilcs that bclong to an cxisting
databasc. Thc lilc can bc a data lilc or a log lilc. Thc lilc cannot bc rcmovcd unlcss it is
cmpty.
Log lilcs arc addcd in thc samc way as databasc lilcs. Thc only dillcrcncc is that you
usc thc A LG F!L clausc instcad ol A F!L.
Cha pt er 5 : Da t a Def i ni t i on L a ngua ge 115
Thc CRAT F!LGRUP clausc crcatcs a ncw lilcgroup, whilc LT
F!LGRUP rcmovcs an cxisting lilcgroup lrom thc systcm. Again, you cannot
rcmovc a lilcgroup unlcss it is cmpty.
Modifying IiIe or IiIegroup Properties
You can usc thc M!FY F!L clausc to changc thc lollowing lilc propcrtics:

M!FY F!L clausc
!ncrcasc thc valuc ol thc S!Z propcrty
Changc thc F!LNAM, MAXS!Z, or F!LGRVTH propcrty

Similarly, you can usc thc M!FY F!LGRUP clausc to changc thc lollowing
lilcgroup propcrtics:

F!LGRUP clausc


RAVR!T options
Setting Database Options
Thc ST clausc ol thc ALTR ATA8AS statcmcnt is uscd to sct dillcrcnt
databasc options. Somc options must bc sct to N or FF, but most ol thcm havc a list
ol possiblc valucs. ach databasc option has a dclault valuc, which is sct in thc model
databasc. Thcrclorc, you can altcr thc model databasc to changc thc dclault valucs ol
spccilic options.
All options that you can sct arc dividcd into scvcral groups. Thc most important
groups arc
Statc options
Auto options
SQL options
116 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Thc statc options control thc lollowing:
Uscr acccss to thc databasc (options arc S!NGL_USR, RSTR!CT_
USR, and MULT!_USR)

MRGNCY)

Thc auto options control, among othcr things, thc art ol thc databasc shutdown
(thc option AUT_CLS) and how indcx statistics arc built (thc options AUT_
CRAT_STAT!ST!CS and AUT_UPAT_STAT!ST!CS).
Thc SQL options control thc ANS! compliancc ol thc databasc and its objccts. All
SQL options can bc cditcd using thc databascpropcrty lunction and modilicd using thc
sp_dboption systcm proccdurc. Thc rccovcry options FULL, 8ULKLGG, and
S!MPL, inllucncc thc art ol databasc rccovcry.
AItering a 7abIe
Thc ALTR TA8L statcmcnt modilics thc schcma ol a tablc. Thc TransactSQL
languagc allows thc lollowing typcs ol altcration:
Add or drop onc or morc ncw columns
Modily column propcrtics
Add or rcmovc intcgrity constraints
nablc or disablc constraints
Rcnamc tablcs and othcr databasc objccts
Thc lollowing scctions dcscribc thcsc typcs ol changcs.
Adding or Dropping a New CoIumn
You can usc thc A clausc ol thc ALTR TA8L statcmcnt to add a ncw column to
thc cxisting tablc. nly onc column can bc addcd lor cach ALTR TA8L statcmcnt.
xamplc 5.18 shows thc usc ol thc A clausc.
XAMPL 5.18
US samplc,
ALTR TA8L cmploycc
A tclcphonc_no CHAR(12) NULL,
Cha pt er 5 : Da t a Def i ni t i on L a ngua ge 117
Thc ALTR TA8L statcmcnt in xamplc 5.18 adds thc column telephone_no to
thc employee tablc. atabasc nginc populatcs thc ncw column cithcr with NULL or
!NT!TY valucs or with thc spccilicd dclault. For this rcason, thc ncw column must
cithcr bc nullablc or must havc a dclault constraint.
|e/e |s sc ac, |c |sse/| c sea :c|ams |s c (c/||:a|c/ (cs|||cs |s ||e |c||e. |e :c|ams, a||:| |s c11e1 as|s ||e
/|| :|case, |s c|ac,s |sse/|e1 c| ||e es1 c/ ||e |c||e.
Thc RP CLUMN clausc providcs thc ability to drop an cxisting column ol thc
tablc, as shown in xamplc 5.19.
XAMPL 5.19
US samplc,
ALTR TA8L cmploycc
RP CLUMN tclcphonc_no,
Thc ALTR TA8L statcmcnt in xamplc 5.19 rcmovcs thc telephone_no
column, which was addcd to thc cmploycc tablc with thc ALTR TA8L statcmcnt
in xamplc 5.18.
Modifying CoIumn Properties
Thc TransactSQL languagc supports thc ALTR CLUMN clausc ol ALTR
TA8L to modily propcrtics ol an cxisting column. Thc lollowing column propcrtics
can bc modilicd:
ata typc
Nullability
xamplc 5.20 shows thc usc ol thc ALTR CLUMN clausc.
XAMPL 5.20
US samplc,
ALTR TA8L dcpartmcnt
ALTR CLUMN location CHAR(25) NT NULL,
Thc ALTR TA8L statcmcnt in xamplc 5.20 changcs thc prcvious propcrtics
(CHAR(30), nullablc) ol thc location column ol thc department tablc to ncw
propcrtics (CHAR(25), not nullablc).
118 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Adding or Removing Integrity Constraints
A ncw intcgrity constraint can bc addcd to a tablc using thc ALTR TA8L statcmcnt
and its option callcd A CNSTRA!NT. xamplc 5.21 shows how you can usc thc
A CNSTRA!NT clausc in rclation to a chcck constraint.
XAMPL 5.21
US samplc,
CRAT TA8L salcs
(ordcr_no !NTGR NT NULL,
ordcr_datc AT NT NULL,
ship_datc AT NT NULL),
ALTR TA8L salcs
A CNSTRA!NT ordcr_chcck CHCK(ordcr_datc - ship_datc),
Thc CRAT TA8L statcmcnt in xamplc 5.21 crcatcs thc sales tablc with two
columns ol thc AT data typc: order_date and ship_date. Thc subscqucnt ALTR
TA8L statcmcnt dclincs an intcgrity constraint namcd order_check, which comparcs
both ol thc valucs and displays an crror mcssagc il thc shipping datc is carlicr than thc
ordcr datc.
xamplc 5.22 shows how you can usc thc ALTR TA8L statcmcnt to additionally
dclinc thc primary kcy ol a tablc.
XAMPL 5.22
US samplc,
ALTR TA8L salcs
A CNSTRA!NT primaryk_salcs PR!MARY KY(ordcr_no),
Thc ALTR TA8L statcmcnt in xamplc 5.22 dcclarcs thc primary kcy lor thc
sales tablc.
ach intcgrity constraint can bc rcmovcd using thc RP CNSTRA!NT clausc ol
thc ALTR TA8L statcmcnt, as shown in xamplc 5.23.
XAMPL 5.23
US samplc,
ALTR TA8L salcs
RP CNSTRA!NT ordcr_chcck,
Thc ALTR TA8L statcmcnt in xamplc 5.23 rcmovcs thc CHCK constraint
callcd order_check, spccilicd in xamplc 5.21.
Cha pt er 5 : Da t a Def i ni t i on L a ngua ge 119
ca :cssc| ase ||e /||| /3|| s|c|emes| |c mc1|/, c 1e/|s|||cs c/ cs |s|e/||, :css|/c|s|. |s |||s :cse, ||e
:css|/c|s| mas| |e /e-:/ec|e1||c| |s, 1/c((e1 cs1 ||es c11e1 a||| ||e sea 1e/|s|||cs.
nabIing or DisabIing Constraints
As prcviously statcd, an intcgrity constraint always has a namc that can bc cxplicitly
dcclarcd using thc CNSTRA!NT option or implicitly dcclarcd by thc systcm. Thc
namc ol all (implicitly or cxplicitly) dcclarcd constraints lor a tablc can bc vicwcd using
thc systcm proccdurc sp_helpconstraint.
A constraint is cnlorccd by dclault during luturc inscrt and updatc opcrations.
Additionally, thc cxisting valucs in thc column(s) arc chcckcd against thc constraint.
thcrwisc, a constraint that is crcatcd with thc V!TH NCHCK option is disablcd
in thc sccond casc. !n othcr words, il you usc thc V!TH NCHCK option, thc
constraint will bc applicd only to luturc inscrt and updatc opcrations. (8oth options,
V!TH CHCK and V!TH NCHCK, can bc applicd only with thc CHCK and
FR!GN KY constraints.)
xamplc 5.24 shows how you can disablc all cxisting constraints lor a tablc.
XAMPL 5.24
US samplc,
ALTR TA8L salcs
NCHCK CNSTRA!NT ALL,
!n xamplc 5.24, thc kcyword ALL is uscd to disablc all thc constraints on thc
sales tablc.
|se c/ ||e |0t||t| c(||cs |s sc| /e:cmmes1e1. /s, :css|/c|s| ||c|c||css ||c| c/e sa((/esse1 mc, :case /a|a/e
a(1c|es |c /c||.
Renaming Database Ob|ects
Thc sp_rename systcm proccdurc modilics thc namc ol an cxisting tablc (and any othcr
cxisting databasc objcct, such as databasc, vicw, or storcd proccdurc). xamplcs 5.25
and 5.26 show thc usc ol this systcm proccdurc.
120 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
XAMPL 5.25
US samplc,
XC sp_rcnamc Cobjnamc - dcpartmcnt, Cncwnamc - subdivision
xamplc 5.25 rcnamcs thc department tablc to subdivision.
XAMPL 5.26
US samplc,
XC sp_rcnamc Cobjnamc - 'salcs.ordcr_no' , Cncwnamc - ordcrnumbcr
xamplc 5.26 rcnamcs thc order_no column in thc sales tablc. !l thc objcct to
bc rcnamcd is a column in a tablc, thc spccilication must bc in thc lorm table_
name.column_name.
|c sc| ase ||e s_teaeme s,s|em (/c:e1a/e, |e:case :|cs|s c|(e:| scmes :cs |s/|aes:e c||e/ 1c|c|cse
c|(e:|s ||c| /e/e/es:e ||em. |/c( ||e c|(e:| cs1 /e-:/ec|e || a||| ||e sea scme.
Removing Database Dbjects
All TransactSQL statcmcnts that arc uscd to rcmovc a databasc objcct havc thc
lollowing gcncral lorm:
RP objcct_typc objcct_namc
ach CRAT objcct statcmcnt has thc corrcsponding RP objcct statcmcnt.
Thc statcmcnt
RP ATA8AS databasc1 [, ...]
rcmovcs onc or morc databascs. This mcans that all traccs ol thc databasc arc rcmovcd
lrom your databasc systcm.
nc or morc tablcs can bc rcmovcd lrom a databasc with thc lollowing statcmcnt:
RP TA8L tablc_namc1 [, ...]
All data, indiccs, and triggcrs bclonging to thc rcmovcd tablc arc also droppcd. (!n
contrast, all vicws that arc dclincd using thc droppcd tablc arc not rcmovcd.) nly thc
uscr with thc corrcsponding privilcgcs can rcmovc a tablc.
Cha pt er 5 : Da t a Def i ni t i on L a ngua ge 121
!n addition to ATA8AS and TA8L, objects in thc RP statcmcnt can bc,
among othcrs, thc lollowing:
TYP
SYNNYM
PRCUR
!NX
\!V
TR!GGR
SCHMA
Thc statcmcnts RP TYP and RP SYNNYM drop a typc and a synonym,
rcspcctivcly. Thc rcst ol thc statcmcnts arc dcscribcd in dillcrcnt chaptcrs: RP
PRCUR in Chaptcr 8, RP !NX in Chaptcr 10, RP \!V in
Chaptcr 11, RP SCHMA in Chaptcr 12 and RP TR!GGR in Chaptcr 14.
ConcIusion
Thc TransactSQL languagc supports many data dclinition statcmcnts that crcatc,
altcr, and rcmovc databasc objccts. Thc lollowing databasc objccts, among othcrs, can
bc crcatcd and rcmovcd using thc CRAT objcct and thc RP objcct statcmcnt,
rcspcctivcly:
atabasc
Tablc
Schcma
\icw
Triggcr
Storcd proccdurc
!ndcx
A structurc ol all databasc objccts in thc prcccding list can bc altcrcd using
thc ALTR object statcmcnt. Notc that thc ALTR TA8L statcmcnt is thc
122 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
only standardizcd statcmcnt lrom this list. All othcr ALTR object statcmcnts arc
TransactSQL cxtcnsions to thc SQL standard.
Thc ncxt chaptcr addrcsscs thc data manipulation statcmcnt callcd SLCT.
xercises
.5.1
Using thc CRAT ATA8AS statcmcnt, crcatc a ncw databasc namcd test_db
with cxplicit spccilications lor databasc and transaction log lilcs. Thc databasc lilc with
thc logical namc test_db_dat is storcd in thc lilc C:\tmp\tcst_db.mdl and thc initial
sizc is 5M8, thc maximum sizc is unlimitcd, and thc lilc growth is cight pcrccnt. Thc
log lilc callcd test_db_log is storcd in thc lilc C:\tmp\tcst_db_log.ldl and thc initial
sizc is 2M8, thc maximum sizc is 10M8, and thc lilc growth is 500K8.
.5.2
Using thc ALTR ATA8AS statcmcnt, add a ncw log lilc to thc test_db databasc.
Thc log lilc is storcd in thc lilc C:\tmp\cmp_log.ldl and thc initial sizc ol thc lilc is
2M8, with growth ol 2M8 and an unlimitcd maximum sizc.
.5.3
Using thc ALTR ATA8AS statcmcnt, changc thc lilc sizc ol thc test_db databasc
to 10M8.
.5.4
!n xamplc 5.4, thcrc arc somc columns ol thc lour crcatcd tablcs dclincd with thc
NT NULL spccilication. For which column is this spccilication rcquircd and lor
which is it not rcquircd:
.5.5
Vhy arc thc columns dept_no and project_no in xamplc 5.4 dclincd as CHAR
valucs (and not as numcrical valucs):
.5.6
Crcatc thc tablcs customers and orders with thc lollowing columns. (o not dcclarc
thc corrcsponding primary and lorcign kcys.)
Cha pt er 5 : Da t a Def i ni t i on L a ngua ge 123
ccstemers er6ers
ca.temer|a c|+r(!} aet aa|| eraer|a |ateer aet aa||
cemj+aa+me .+rc|+r(+} aet aa|| ca.temer|a c|+r(!} aet aa||
ceat+cta+me c|+r(1} aa|| eraera+te a+te aa||
+aare.. .+rc|+r(} aa|| .||jjeaa+te a+te aa||
c|t c|+r(!} aa|| lre||t meae aa||
j|eae c|+r(z+} aa|| .||ja+me .+rc|+r(+} aa||
l+\ c|+r(z+} aa|| .||j+aare.. .+rc|+r(} aa||
a+at|t |ateer aa||
.5.7
Using thc ALTR TA8L statcmcnt, add a ncw column namcd shipregion to thc
orders tablc. Thc liclds should bc nullablc and contain intcgcrs.
.5.8
Using thc ALTR TA8L statcmcnt, changc thc data typc ol thc column shipregion
lrom !NTGR to CHARACTR with lcngth 8. Thc liclds may contain NULL
valucs.
.5.9
clctc thc lormcrly crcatcd column shipregion.
.5.10
cscribc cxactly what happcns il a tablc is dclctcd with thc RP TA8L statcmcnt.
.5.11
Rccrcatc thc tablcs customers and orders, cnhancing thcir dclinition with all primary
and lorcign kcys constraints.
.5.12
Using SQL Scrvcr Managcmcnt Studio, try to inscrt a ncw row into thc orders tablc
with thc lollowing valucs:
(10, 'ord01', gctdatc(), gctdatc(), 100.0, 'Vindstar', 'ccan', 1).
Vhy isn't that working:
124 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
.5.13
Using thc ALTR TA8L statcmcnt, add thc currcnt systcm datc and timc as thc
dclault valuc to thc orderdate column ol thc orders tablc.
.5.14
Using thc ALTR TA8L statcmcnt, crcatc an intcgrity constraint that limits thc
possiblc valucs ol thc quantity column in thc orders tablc to valucs bctwccn 1 and 30.
.5.15
isplay all intcgrity constraints lor thc orders tablc.
.5.16
clctc thc primary kcy ol thc customers tablc. Vhy isn't that working:
.5.17
clctc thc intcgrity constraint callcd prim_empl dclincd in xamplc 5.7.
.5.18
Rcnamc thc city column ol thc customers tablc. Thc ncw namc is town.
In 7bis Cbapter
SLC7 Statement:
A asic Form
Subqueries
SLC7 Statement: Dtber
CIauses and Functions
7emporary 7abIes
1oin Dperator
CorreIated Subqueries
7abIe xpressions

Cbapter 6
Copyright 2008 by The McGraw-Hill Companies. Click here for terms of use.
126 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de

n this chaptcr you will lcarn how to usc thc SLCT statcmcnt to pcrlorm
rctricvals. This chaptcr dcscribcs cvcry clausc in this statcmcnt and givcs numcrous
cxamplcs using thc sample databasc to dcmonstratc thc practical usc ol cach clausc.
Altcr that, aggrcgatc lunctions and thc sct opcrators, as wcll as computcd columns and
tcmporary tablcs will bc introduccd. Thc sccond part ol thc chaptcr tclls you morc about
complcx qucrics. This part introduccs thc join opcrator. !n addition to looking at all
lorms ol thc join opcrator, which is thc most important opcrator lor rclational databasc
systcms, thc chaptcr discusscs corrclatcd subqucrics and thc X!STS lunction. Thc cnd
ol thc chaptcr dcscribcs common tablc cxprcssions, togcthcr with thc APPLY opcrator.
SLC7 Statement: A asic Form
and tbe WHR CIause
Thc TransactSQL languagc has onc basic statcmcnt lor rctricving inlormation lrom a
databasc: thc SLCT statcmcnt. Vith this statcmcnt, it is possiblc to qucry inlormation
lrom onc or morc tablcs ol a databasc (or cvcn lrom multiplc databascs). Thc rcsult ol a
SLCT statcmcnt is anothcr tablc, also known as a result set.
Thc simplcst lorm ol thc SLCT statcmcnt contains a SLCT list with a FRM
clausc. (All othcr clauscs arc optional.) This lorm ol thc SLCT statcmcnt has thc
lollowing syntax:
SLCT | ALL ]!ST!NCT| column_list
FRM [tablc1 |tab_alias1| ] ,...
table1 is a namc ol thc tablc lrom which inlormation is rctricvcd. tab_alias1 providcs
an alias lor thc namc ol thc corrcsponding tablc. An alias is anothcr namc lor thc
corrcsponding tablc and can bc uscd as a shorthand way ol rclcrring to thc tablc or as
a way to rclcr to two logical instanccs ol thc samc physical tablc. on't worry, this will
bccomc clcarcr as cxamplcs arc prcscntcd.
column_list contains onc or morc ol thc lollowing spccilications:

table2.*)

column_name |AS| column_heading, which is a way to rcplacc
thc namc ol a column or to assign a ncw namc to an cxprcssion
An cxprcssion
A systcm or an aggrcgatc lunction
Cha pt er 6 : Quer i es 127
|s c11|||cs |c ||e (/e:e1|s s(e:|/|:c||css, ||e/e c/e c||e/ c(||css ||c| a||| |e (/eses|e1 |c|e/ |s |||s :|c(|e/.

A SLCT statcmcnt can rctricvc cithcr ccrtain columns or rows lrom a tablc.
Thc lirst opcration is callcd SELECT list (or projection), and thc sccond onc is callcd
selection. Thc combination ol both opcrations is also possiblc in a SLCT statcmcnt.
3e/c/e ,ca s|c/| |c ese:a|e ae/|es |s |||s :|c(|e/, (|ecse /e-:/ec|e ||e es||/e sem/e 1c|c|cse.

xamplc 6.1 shows thc simplcst rctricval lorm with thc SLCT statcmcnt.
XAMPL 6.1
Gct lull dctails ol all dcpartmcnts:
US samplc,
SLCT dcpt_no, dcpt_namc, location
FRM dcpartmcnt,
Thc rcsult is
6ept_ae 6ept_aame |ecat|ea
a |e.e+rc| |+||+.
az |cceaat|a :e+tt|e
a1 M+r|et|a |+||+.
Thc SLCT statcmcnt in xamplc 6.1 rctricvcs all rows and all columns lrom
thc department tablc. !l you includc all columns ol a tablc in a SLCT list (as in
xamplc 6.1), you can usc as shorthand, but this notation is not rccommcndcd. Thc
column namcs scrvc as column hcadings ol thc rcsulting output.
WHR CIause
Thc simplcst lorm ol thc SLCT statcmcnt, dcscribcd in thc prcvious scction, is not
vcry usclul lor qucrics. !n practicc, thcrc arc always scvcral morc clauscs in a SLCT
128 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
statcmcnt than in thc statcmcnt shown in xamplc 6.1. Thc lollowing is thc syntax ol
a SLCT statcmcnt that rclcrcnccs a tablc, with (almost) all possiblc clauscs:
SLCT sclcct_list
|!NT ncw_tablc_|
FRM tablc
|VHR scarch_condition|
|GRUP 8Y group_by_cxprcssion|
|HA\!NG scarch_condition|
|RR 8Y ordcr_cxprcssion |ASC ] SC| |,
|e :|cases |s ||e :|||t s|c|emes| mas| |e a/|||es |s ||e s,s|c:||:c| c/1e/ ||es |s ||e (/e:e1|s s,s|cs/c/
escm(|e, ||e |0|| 3 :|case mas| :cme c/|e/ ||e |||| :|case cs1 |e/c/e ||e |/l|| :|case. (|e ||0
:|case |s sc| s|s|/|:cs| cs ||e c||e/ :|cases. |c/ |||s /ecscs || a||| |e 1|s:asse1 |c|e/.
This scction starts with thc dclinition ol thc VHR clausc. ltcn, it is ncccssary
to dclinc onc or morc conditions that limit thc sclcctcd rows. Thc VHR clausc
spccilics a 8oolcan cxprcssion (an cxprcssion that rcturns a valuc ol TRU or FALS)
that is tcstcd lor cach row to bc rcturncd (potcntially). !l thc cxprcssion is truc, thcn thc
row is rcturncd, il it is lalsc, it is discardcd.
xamplc 6.2 shows thc usc ol thc VHR clausc.
XAMPL 6.2
Gct thc namcs and numbcrs ol all dcpartmcnts locatcd in allas:
US samplc,
SLCT dcpt_namc, dcpt_no
FRM dcpartmcnt
VHR location - 'allas',
Thc rcsult is
6ept_aame 6ept_ae
|e.e+rc| a
M+r|et|a a1
Cha pt er 6 : Quer i es 129
!n addition to thc cqual sign, thc VHR clausc can contain othcr comparison
opcrators, including thc lollowing:
<> (er !} aet ea+|
< |e.. t|+a
> re+ter t|+a
> re+ter t|+a er ea+|
< |e.. t|+a er ea+|
!> aet re+ter t|+a
!< aet |e.. t|+a
xamplc 6.3 shows thc usc ol a comparison opcrator in thc VHR clausc.
XAMPL 6.3
Gct thc last and lirst namcs ol all cmployccs with cmploycc numbcrs - 15000:
US samplc,
SLCT cmp_lnamc, cmp_lnamc
FRM cmploycc
VHR cmp_no ~- 15000,
Thc rcsult is
emp_|aame emp_faame
:m|t| M+tt|ew
|+rr|mere Ie|a
I+me. I+me.
Me.er :||||
An cxprcssion can also bc a part ol thc condition in thc VHR clausc, as
xamplc 6.4 shows.
XAMPL 6.4
Gct thc projcct namcs lor all projccts with a budgct ~ 60000 /. Thc currcnt ratc ol
cxchangc is 0.51 / pcr 81.
US samplc,
SLCT projcct_namc
FRM projcct
VHR budgct0.51 ~ 60000,
130 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Thc rcsult is
pre[ect_aame
|je||e
Mercar
Comparisons ol strings (that is, valucs ol data typcs CHAR, \ARCHAR, NCHAR
or N\ARCHAR) arc cxccutcd in accordancc with thc collating scqucncc (in cllcct thc
"sort ordcr" spccilicd whcn atabasc nginc was installcd). !l two strings arc comparcd
using ASC!! codc (or any othcr codc), cach ol thc corrcsponding (lirst, sccond, third,
and so on) charactcrs will bc comparcd. nc charactcr is lowcr in priority than thc othcr
il it appcars in thc codc tablc bclorc thc othcr onc. Two strings ol dillcrcnt lcngths arc
comparcd altcr thc shortcr onc is paddcd at thc right with blanks, so that thc lcngth
ol both strings is cqual. Numbcrs comparc algcbraically. \alucs ol tcmporal data typcs
(such as AT, T!M, and ATT!M) comparc in chronological ordcr.
tc|amss a||| |/ c/ |//| 1c|c |,(es :cssc| |e ase1 |s ||e |||| :|case, es:e(| a||| ||e |||| cs1 |: ||||
c(e/c|c/s.
8ooIean Operators
VHR clausc conditions can cithcr bc simplc or contain multiplc conditions.
Multiplc conditions can bc built using thc 8oolcan opcrators AN, R, and NT.
Thc bchavior ol thcsc opcrators has bccn dcscribcd in Chaptcr 4 using truth tablcs.
!l two conditions arc conncctcd by thc AN opcrator, rows arc rctricvcd lor which
both conditions arc truc. !l two conditions arc conncctcd by thc R opcrator, all rows
ol a tablc arc rctricvcd in which cithcr thc lirst or thc sccond condition (or both) is truc,
as shown in xamplc 6.5.
XAMPL 6.5
Gct thc cmploycc numbcrs lor all cmployccs that work lor cithcr projcct p1 or projcct
p2 (or both):
US samplc,
SLCT projcct_no, cmp_no
FRM works_on
VHR projcct_no - 'p1'
R projcct_no - 'p2',
Cha pt er 6 : Quer i es 131
Thc rcsult is
pre[ect_ae emp_ae
j z
jz z!1+
jz 1
jz z1+
j 1
j z!!
jz z!!
j z1+
Thc rcsult ol xamplc 6.5 contains somc duplicatc valucs ol thc emp_no column. !l
this rcdundant inlormation is to bc climinatcd, thc !ST!NCT option should bc uscd,
as shown hcrc:
US samplc,
SLCT !ST!NCT cmp_no
FRM works_on
VHR projcct_no - 'p1'
R projcct_no - 'p2',
!n this casc, thc rcsult is
emp_ae
1
z
1
z!1+
z!!
z1+
tc|amss a||| |/ c/ |//| 1c|c |,(es :cssc| |e /e|/|e|e1 a||| ||e ||:||t c(||cs.

132 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Notc that thc !ST!NCT option can bc uscd only oncc in a SLCT list, and it
must prcccdc all column namcs in that list. Thcrclorc, xamplc 6.6 is .rong.
XAMPL 6.6 (XAMPL DF AN ILLCAL S7A7MN7)
US samplc,
SLCT cmp_lnamc, !ST!NCT cmp_no
FRM cmploycc
VHR cmp_lnamc - 'Moscr',
Thc rcsult is
Scrvcr: Msg 156, Lcvcl 15, Statc 1, Linc 1
!ncorrcct syntax ncar thc kcyword '!ST!NCT'.
|es ||e/e |s mc/e ||cs cse :c|ams |s ||e :|||t ||s|, ||e ||:||t :|case 1|s(|c,s c|| /cas a|e/e ||e
:cm||sc||cs c/ :c|amss |s 1|s||s:|.
Thc VHR clausc may includc any numbcr ol thc samc or dillcrcnt 8oolcan
opcrations. You should bc awarc that thc thrcc 8oolcan opcrations havc dillcrcnt
prioritics lor cvaluation: thc NT opcration has thc highcst priority, AN is cvaluatcd
ncxt, and thc R opcration has thc lowcst priority. !l you do not pay attcntion to thcsc
dillcrcnt prioritics lor 8oolcan opcrations, you will gct uncxpcctcd rcsults, as xamplc
6.7 shows.
XAMPL 6.7
US samplc,
SLCT cmp_no, cmp_lnamc, cmp_lnamc
FRM cmploycc
VHR cmp_no - 25348 AN cmp_lnamc - 'Smith'
R cmp_lnamc - 'Matthcw' AN dcpt_no - 'd1',
SLCT cmp_no, cmp_lnamc, cmp_lnamc
FRM cmploycc
VHR ((cmp_no - 25348 AN cmp_lnamc - 'Smith')
R cmp_lnamc -'Matthcw') AN dcpt_no - 'd1',
Cha pt er 6 : Quer i es 133
Thc rcsult is
emp_ae emp_faame emp_|aame
z!1+ M+tt|ew :m|t|
emp_ae emp_faame emp_|aame
As thc rcsults ol xamplc 6.7 show, thc two SLCT statcmcnts display two
dillcrcnt rcsult scts. !n thc lirst SLCT statcmcnt, thc systcm cvaluatcs both AN
opcrators lirst (lrom thc lclt to thc right), thcn thc R opcrator is cvaluatcd. !n thc
sccond SLCT statcmcnt, thc usc ol parcnthcscs changcs thc opcration cxccution,
with all cxprcssions within parcnthcscs bcing cxccutcd lirst, in scqucncc lrom lclt
to right. As you can scc, thc lirst statcmcnt rcturncd onc row, whilc thc sccond onc
rcturncd zcro rows.
Thc cxistcncc ol scvcral 8oolcan opcrations in a VHR clausc complicatcs thc
corrcsponding SLCT statcmcnt and makcs it crror pronc. !n such cascs, thc usc
ol parcnthcscs is highly rccommcndcd, cvcn il thcy arc not ncccssary. Thc rcadability
ol such SLCT statcmcnts will bc grcatly improvcd, and possiblc crrors can bc
avoidcd. Hcrc is thc lirst SLCT statcmcnt lrom xamplc 6.7, modilicd using thc
rccommcndcd lorm:
US samplc,
SLCT cmp_no, cmp_lnamc, cmp_lnamc
FRM cmploycc
VHR (cmp_no - 25348 AN cmp_lnamc - 'Smith')
R (cmp_lnamc - 'Matthcw' AN dcpt_no - 'd1'),
Thc third 8oolcan opcrator, NT, changcs thc logical valuc ol thc corrcsponding
condition. Thc truth tablc lor NT in Chaptcr 4 shows that thc ncgation ol thc truc
valuc is lalsc and vicc vcrsa, thc ncgation ol thc NULL valuc is also NULL.
xamplc 6.8 shows thc usc ol thc NT opcrator.
XAMPL 6.8
Gct thc cmploycc numbcrs and lirst namcs ol all cmployccs who do not bclong to thc
dcpartmcnt d2:
US samplc
SLCT cmp_no, cmp_lnamc
FRM cmploycc
VHR NT dcpt_no - 'd2',
134 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Thc rcsult is
emp_ae emp_|aame
z!1+ :m|t|
z Ieae.
1 |+rr|mere
z!! Me.er
!n this casc, thc NT opcrator can bc rcplaccd by thc comparison opcrator ~ (not
cqual).
||s |cc| ases ||e c(e/c|c/ <> (|ss|ec1 c/ /= |c /emc|s :css|s|es| a||| ||e /|:| :9| s|cs1c/1.

IN and 87WN Operators
An !N opcrator allows thc spccilication ol two or morc cxprcssions to bc uscd lor a
qucry scarch. Thc rcsult ol thc condition rcturns truc il thc valuc ol thc corrcsponding
column cquals onc ol thc cxprcssions spccilicd by thc !N prcdicatc.
XAMPL 6.9
Gct all thc columns lor cvcry cmploycc whosc cmploycc numbcr cquals 29346, 28559,
or 25348:
US samplc,
SLCT cmp_no, cmp_lnamc, cmp_lnamc
FRM cmploycc
VHR cmp_no !N (29346, 28559, 25348),
Thc rcsult is
emp_ae emp_faame emp_|aame
z!1+ M+tt|ew :m|t|
z1+ I+me. I+me.
z!! :|||| Me.er
An !N opcrator is cquivalcnt to a scrics ol conditions, conncctcd with onc or morc
R opcrators. (Thc numbcr ol R opcrators is cqual to thc numbcr ol cxprcssions
lollowing thc !N opcrator minus onc.)
Cha pt er 6 : Quer i es 135
Thc !N opcrator can bc uscd togcthcr with thc 8oolcan opcrator NT, as shown in
xamplc 6.10. !n this casc, thc qucry rctricvcs rows that do not includc any ol thc listcd
valucs in thc corrcsponding columns.
XAMPL 6.10
Gct all columns lor cvcry cmploycc whosc cmploycc numbcr is ncithcr 10102 nor 9031:
US samplc,
SLCT cmp_no, cmp_lnamc, cmp_lnamc, dcpt_no
FRM cmploycc
VHR cmp_no NT !N (10102, 9031),
Thc rcsult is
emp_ae emp_faame emp_|aame 6ept_ae
z!1+ M+tt|ew :m|t| a1
1 Ie|a |+rr|mere a
z1+ I+me. I+me. az
z! |||e |+a.e| az
z!! :|||| Me.er a
!n contrast to thc !N opcrator, which spccilics cach individual valuc, thc 8TVN
opcrator spccilics a rangc, which dctcrmincs thc lowcr and uppcr bounds ol qualilying
valucs. xamplc 6.11 providcs an cxamplc.
XAMPL 6.11
Gct thc namcs and budgcts lor all projccts with a budgct bctwccn 895,000 and
8120,000, inclusivc:
US samplc,
SLCT projcct_namc, budgct
FRM projcct
VHR budgct 8TVN 95000 AN 120000,
Thc rcsult is
pre[ect_aame hc6qet
|je||e z.
0em|a| !.
136 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Thc 8TVN opcrator scarchcs lor all valucs in thc rangc inclusivcly, that is,
qualilying valucs can bc bctwccn or equal to thc lowcr and uppcr boundary valucs.
Thc 8TVN opcrator is logically cqual to two individual comparisons, which arc
conncctcd with thc 8oolcan opcrator AN. xamplc 6.11 is cquivalcnt to xamplc 6.12.
XAMPL 6.12
US samplc,
SLCT projcct_namc, budgct
FRM projcct
VHR budgct ~- 95000 AN budgct - 120000,
Likc thc 8TVN opcrator, thc NT 8TVN opcrator can bc uscd to
scarch lor column valucs that do not lall within thc spccilicd rangc. Thc 8TVN
opcrator can also bc applicd to columns with charactcr and datc valucs.
Thc two SLCT statcmcnts in xamplc 6.13 show a qucry that can bc writtcn in
two dillcrcnt, but cquivalcnt ways.
XAMPL 6.13
Gct thc namcs ol all projccts with a budgct lcss than 8100,000 and grcatcr than
8150,000:
US samplc,
SLCT projcct_namc
FRM projcct
VHR budgct NT 8TVN 100000 AN 150000,
Thc rcsult is
pre[ect_aame
0em|a|
Mercar
Using comparison opcrators, thc qucry looks dillcrcnt:
US samplc,
SLCT projcct_namc
FRM projcct
VHR budgct 100000 R budgct ~ 150000,
Cha pt er 6 : Quer i es 137
/|||ca| ||e |s||s| (|/cs|s c/ ||e /ea|/emes|s, e| ||e scmes c/ c|| (/c(e:|s a||| |a1e|s ||c| c/e |ess ||cs
,!^^,^^^ cs1 /ec|e/ ||cs ,!:^,^^^, saes|s ||e ase c/ ||e /|| c(e/c|c/ |s ||e se:cs1 :|||t s|c|emes|
(/eses|e1 |s |scm(|e .!?, ||e |c|:c| mecs|s c/ ||e ae/, 1emcs1s ||e ase c/ ||e 0| c(e/c|c/, |e:case |/ ,ca
ase /|| |ss|ec1 c/ 0|, ,ca a||| e| sc /esa||s c| c||. (|e /ecscs |s ||c| ||e/e :cssc| |e c |a1e| ||c| |s c| ||e
scme ||me |ess ||cs ,!^^,^^^ cs1 /ec|e/ ||cs ,!:^,^^^. |e/e/c/e, ||e se:cs1 ae/, |s ||e escm(|e s|cas c
(css|||e (/c||em ||c| :cs c((ec/ |e|aees |s||s| (|/cs|s c/ cs ese/:|se cs1 ||s |c|:c| mecs|s.
Queries InvoIving NULL VaIues
A NULL in thc CRAT TA8L statcmcnt spccilics that a spccial valuc callcd
NULL (which usually rcprcscnts unknown or not applicablc valucs) is allowcd in thc
column. Thcsc valucs dillcr lrom all othcr valucs in a databasc. Thc VHR clausc
ol a SLCT statcmcnt gcncrally rcturns rows lor which thc comparison cvaluatcs to
truc. Thc conccrn thcn rcgarding qucrics is, how will comparisons involving NULL
valucs bc cvaluatcd in thc VHR clausc:
All comparisons with NULL valucs will rcturn lalsc (cvcn whcn prcccdcd by NT).
To rctricvc thc rows with NULL valucs in thc column, TransactSQL includcs thc
opcrator lcaturc !S NULL. This spccilication in a VHR clausc ol a SLCT
statcmcnt has thc lollowing gcncral lorm:
column !S |NT| NULL
xamplc 6.14 shows thc usc ol thc !S NULL opcrator.
XAMPL 6.14
Gct cmploycc numbcrs and corrcsponding projcct numbcrs lor cmployccs with
unknown jobs who work on projcct p2:
US samplc,
SLCT cmp_no, projcct_no
FRM works_on
VHR projcct_no - 'p2'
AN job !S NULL,
Thc rcsult is
emp_ae pre[ect_ae
1 jz
z1+ jz
138 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
8ccausc all comparisons with NULL valucs rcturn lalsc, xamplc 6.15 shows
syntactically corrcct, but logically incorrcct, usagc ol NULL.
XAMPL 6.15
US samplc,
SLCT projcct_no, job
FRM works_on
VHR job ~ NULL,
Thc rcsult is
pre[ect_ae [eh
Thc condition
column !S NT NULL
is cquivalcnt to thc condition
NT (column !S NULL)
Thc systcm lunction !SNULL allows a display ol thc spccilicd valuc as substitution
lor NULL (scc xamplc 6.16).
XAMPL 6.16
US samplc,
SLCT cmp_no, !SNULL(job, 'Job unknown') AS task
FRM works_on
VHR projcct_no - 'p1',
Thc rcsult is
emp_ae task
z |a+|.t
1 M+a+er
z!! Ie| aa|aewa
z1+ C|er|
xamplc 6.16 uscs a column hcading callcd task lor thc job column.
Cha pt er 6 : Quer i es 139
LIk Operator
L!K is an opcrator that is uscd lor pattcrn matching, that is, it comparcs column
valucs with a spccilicd pattcrn. Thc data typc ol thc column can bc any charactcr or
datc. Thc gcncral lorm ol thc L!K opcrator is
column |NT| L!K 'pattcrn'
pattern may bc a string or datc constant or cxprcssion (including columns ol tablcs)
and must bc compatiblc with thc data typc ol thc corrcsponding column. For thc
spccilicd column, thc comparison bctwccn thc valuc in a row and thc pattcrn cvaluatcs
to truc il thc column valuc matchcs thc pattcrn cxprcssion.
Ccrtain charactcrs within thc pattcrncallcd wildcard charactcrshavc a spccilic
intcrprctation. Two ol thcm arc
% (percent sign) Spccilics any scqucncc ol zcro or morc charactcrs
_ (underscore) Spccilics any singlc charactcr
xamplc 6.17 shows thc usc ol thc wildcard charactcrs % and _.
XAMPL 6.17
Gct thc lirst and last namcs and numbcrs ol all cmployccs whosc lirst namc contains
thc lcttcr a as thc sccond charactcr:
US samplc,
SLCT cmp_lnamc, cmp_lnamc, cmp_no
FRM cmploycc
VHR cmp_lnamc L!K '_a',
Thc rcsult is
emp_faame emp_|aame emp_ae
M+tt|ew :m|t| z!1+
I+me. I+me. z1+
!n addition to thc pcrccnt sign and thc undcrscorc, TransactSQL supports othcr
charactcrs that havc a spccial mcaning whcn uscd with thc L!K opcrator. Thcsc
charactcrs (|, ], and ^) arc dcmonstratcd in xamplcs 6.18 and 6.19.
140 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
XAMPL 6.18
Gct lull dctails ol all dcpartmcnts whosc location bcgins with a charactcr in thc rangc C
through F:
US samplc,
SLCT
FRM dcpartmcnt
VHR location L!K '|CF|',
Thc rcsult is
6ept_ae 6ept_aame |ecat|ea
a |e.e+rc| |+||+.
a1 M+r|et|a |+||+.
As shown in xamplc 6.18, thc squarc brackcts, | |, dclimit a rangc or list ol
charactcrs. Thc ordcr in which charactcrs appcar in a rangc is dclincd by thc collating
scqucncc, which is dctcrmincd during thc systcm installation.
Thc charactcr ^ spccilics thc ncgation ol a rangc or a list ol charactcrs. This charactcr
has this mcaning only within a pair ol squarc brackcts, as shown in xamplc 6.19.
XAMPL 6.19
Gct thc numbcrs and lirst and last namcs ol all cmployccs whosc last namc docs not
bcgin with thc lcttcr J, K, L, M, N, or O and whosc lirst namc docs not bcgin with thc
lcttcr E or Z:
US samplc,
SLCT cmp_no, cmp_lnamc, cmp_lnamc
FRM cmploycc
VHR cmp_lnamc L!K '|^J|'
AN cmp_lnamc L!K '|^Z|',
Thc rcsult is
emp_ae emp_faame emp_|aame
z!1+ M+tt|ew :m|t|
1 Ie|a |+rr|mere
Cha pt er 6 : Quer i es 141
Thc condition
column NT L!K 'pattcrn'
is cquivalcnt to thc condition
NT (column L!K 'pattcrn')
xamplc 6.20 shows thc usc ol thc L!K opcrator (togcthcr with NT).
XAMPL 6.20
Gct lull dctails ol all cmployccs whosc lirst namc docs not cnd with thc charactcr n:
US samplc,
SLCT cmp_no, cmp_lnamc, cmp_lnamc
FRM cmploycc
VHR cmp_lnamc NT L!K 'n',
Thc rcsult is
emp_ae emp_faame emp_|aame
z!1+ M+tt|ew :m|t|
z1+ I+me. I+me.
z! |||e |+a.e|
1 ||.+ |ertea|
z!! :|||| Me.er
Any ol thc wildcard charactcrs (, _, |, |, or ^) cncloscd in squarc brackcts stands lor
itscll. An cquivalcnt lcaturc is availablc through thc SCAP option. Thcrclorc, both
SLCT statcmcnts in xamplc 6.21 havc thc samc mcaning.
XAMPL 6.21
US samplc,
SLCT projcct_no, projcct_namc
FRM projcct
VHR projcct_namc L!K '|_|',
SLCT projcct_no, projcct_namc
FRM projcct
VHR projcct_namc L!K '!_' SCAP '!',
142 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Thc rcsult is
pre[ect_ae pre[ect_aame
pre[ect_ae pre[ect_aame
8oth SLCT statcmcnts scarch lor thc undcrscorc as an actual charactcr in thc
column project_name. !n thc lirst SLCT statcmcnt, this scarch is cstablishcd
by cnclosing thc sign _ in squarc brackcts. Thc sccond SLCT statcmcnt uscs a
charactcr (in xamplc 6.21, thc charactcr !) as an cscapc charactcr. Thc cscapc charactcr
ovcrridcs thc mcaning ol thc undcrscorc as thc wildcard charactcr and lcavcs it to bc
intcrprctcd as an ordinary charactcr. (Thc rcsult contains no rows bccausc thcrc arc no
projcct namcs that includc thc undcrscorc charactcr.)
|e :9| s|cs1c/1 cs|, sa((c/|s ||e ase c/ , _, cs1 ||e |:t/|| c(e/c|c/. |c/ |||s /ecscs, |/ cs, a|| 1:c/1
:|c/c:|e/ mas| s|cs1 /c/ ||se|/, as|s ||e |:t/|| c(e/c|c/ |ss|ec1 c/ c (c|/ c/ sac/e |/c:|e|s |s /e:cmmes1e1.
Subqueries
All prcvious cxamplcs in this chaptcr contain comparisons ol column valucs with an
cxprcssion, constant, or sct ol constants. Additionally, thc TransactSQL languagc ollcrs
thc ability to comparc column valucs with thc rcsult ol anothcr SLCT statcmcnt.
Such construct, whcrc onc or morc SLCT statcmcnts arc ncstcd in thc VHR
clausc ol anothcr SLCT statcmcnt, is callcd a subquery. Thc lirst SLCT
statcmcnt ol a subqucry is callcd thc outer queryin contrast to thc inner query, which
dcnotcs thc SLCT statcmcnt(s) uscd in a comparison. Thc inncr qucry will bc
cvaluatcd lirst, and thc outcr qucry rcccivcs thc valucs ol thc inncr qucry.
/s |sse/ ae/, :cs c|sc |e ses|e1 |s cs ||:||, |||/|, c/ ||||| s|c|emes|, a||:| a||| |e 1|s:asse1 |c|e/ |s
|||s |cc|.
Thcrc arc two typcs ol subqucrics:
Scllcontaincd
Corrclatcd
Cha pt er 6 : Quer i es 143
!n a scllcontaincd subqucry, thc inncr qucry is logically cvaluatcd cxactly oncc. A
corrclatcd subqucry dillcrs lrom a scllcontaincd onc in that its valuc dcpcnds upon
a variablc lrom thc outcr qucry. Thcrclorc, thc inncr qucry ol a corrclatcd subqucry is
logically cvaluatcd cach timc thc systcm rctricvcs a ncw row lrom thc outcr qucry. This
scction shows cxamplcs ol scllcontaincd subqucrics. Thc corrclatcd subqucry will bc
discusscd latcr in thc chaptcr, togcthcr with thc join opcration.
A scllcontaincd subqucry can bc uscd with thc lollowing opcrators:
Comparison opcrators
!N opcrator
ANY or ALL opcrator
Subqueries and Comparison Dperators
xamplc 6.22 shows thc scllcontaincd subqucry that is uscd with thc opcrator -.
XAMPL 6.22
Gct thc lirst and last namcs ol cmployccs who work in thc Rcscarch dcpartmcnt:
US samplc,
SLCT cmp_lnamc, cmp_lnamc
FRM cmploycc
VHR dcpt_no -
(SLCT dcpt_no
FRM dcpartmcnt
VHR dcpt_namc - 'Rcscarch'),
Thc rcsult is
emp_faame emp_|aame
Ie|a |+rr|mere
:|||| Me.er
Thc inncr qucry ol xamplc 6.22 is logically cvaluatcd lirst. That qucry rcturns thc
numbcr ol thc rcscarch dcpartmcnt (d1). Thus, altcr thc cvaluation ol thc inncr qucry,
thc subqucry in xamplc 6.22 can bc rcprcscntcd with thc lollowing cquivalcnt qucry:
US samplc
SLCT cmp_lnamc, cmp_lnamc
FRM cmploycc
VHR dcpt_no - 'd1',
144 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
A subqucry can bc uscd with othcr comparison opcrators, too. Any comparison
opcrator can bc uscd, providcd that thc inncr qucry rcturns cxactly onc row. This is
obvious bccausc comparison bctwccn particular column valucs ol thc outcr qucry and
a sct ol valucs (as a rcsult ol thc inncr qucry) is not possiblc. Thc lollowing scction
shows how you can handlc thc casc in which thc rcsult ol an inncr qucry contains a
sct ol valucs.
Subqueries and tbe IN Dperator
Thc !N opcrator allows thc spccilication ol a sct ol cxprcssions (or constants) that arc
subscqucntly uscd lor thc qucry scarch. This opcrator can bc applicd to a subqucry lor
thc samc rcasonthat is, whcn thc rcsult ol an inncr qucry contains a sct ol valucs.
xamplc 6.23 shows thc usc ol thc !N opcrator in a subqucry.
XAMPL 6.23
Gct lull dctails ol all cmployccs whosc dcpartmcnt is locatcd in allas:
US samplc,
SLCT
FRM cmploycc
VHR dcpt_no !N
(SLCT dcpt_no
FRM dcpartmcnt
VHR location - 'allas'),
Thc rcsult is
emp_ae emp_faame emp_|aame 6ept_ae
z!1+ M+tt|ew :m|t| a1
z |aa Ieae. a1
1 Ie|a |+rr|mere a
z!! :|||| Me.er a
ach inncr qucry may contain lurthcr qucrics. This typc ol subqucry is callcd a
subqucry with multiplc lcvcls ol ncsting. Thc maximum numbcr ol inncr qucrics in a
subqucry dcpcnds on thc amount ol mcmory atabasc nginc has lor cach SLCT
statcmcnt. !n thc casc ol subqucrics with multiplc lcvcls ol ncsting, thc systcm lirst
cvaluatcs thc inncrmost qucry and rcturns thc rcsult to thc qucry on thc ncxt ncsting
lcvcl, and so on. Finally, thc outcrmost qucry cvaluatcs thc linal outcomc.
xamplc 6.24 shows thc qucry with multiplc lcvcls ol ncsting.
Cha pt er 6 : Quer i es 145
XAMPL 6.24
Gct thc last namcs ol all cmployccs who work on thc projcct Apollo:
US samplc,
SLCT cmp_lnamc
FRM cmploycc
VHR cmp_no !N
(SLCT cmp_no
FRM works_on
VHR projcct_no !N
(SLCT projcct_no
FRM projcct
VHR projcct_namc - 'Apollo')),
Thc rcsult is
emp_|aame
Ieae.
I+me.
|ertea|
Me.er
Thc inncrmost qucry in xamplc 6.24 cvaluatcs to thc project_no valuc p1. Thc
middlc inncr qucry comparcs this valuc with all valucs ol thc project_no column in thc
works_on tablc. Thc rcsult ol this qucry is thc sct ol cmploycc numbcrs: (10102, 29346,
9031, 28559). Finally, thc outcrmost qucry displays thc corrcsponding last namcs lor thc
sclcctcd cmploycc numbcrs.
Subqueries and ANY and ALL Dperators
Thc opcrators ANY and ALL arc always uscd in combination with onc ol thc
comparison opcrators. Thc gcncral syntax ol both opcrators is
column_namc opcrator |ANY ] ALL| qucry
whcrc operator stands lor a comparison opcrator and query is an inncr qucry.
Thc ANY opcrator cvaluatcs to truc il thc rcsult ol thc corrcsponding inncr qucry
contains at lcast onc row that satislics thc comparison. Thc kcyword SM is thc
synonym lor ANY. xamplc 6.25 shows thc usc ol thc ANY opcrator.
146 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
XAMPL 6.25
Gct thc cmploycc numbcrs, projcct numbcrs, and job namcs lor cmployccs who havc
not spcnt thc most timc on onc ol thc projccts:
US samplc
SLCT !ST!NCT cmp_no, projcct_no, job
FRM works_on
VHR cntcr_datc ~ ANY
(SLCT cntcr_datc
FRM works_on)
Thc rcsult is
emp_ae pre[ect_ae [eh
z! j1 |a+|.t
1 j M+a+er
1 j1 C|er|
z j1 M+a+er
1 jz NJ||
z!1+ jz C|er|
z!! j NJ||
z!! jz C|er|
z1+ j C|er|
z1+ jz NJ||
ach valuc ol thc enter_date column in xamplc 6.25 is comparcd with all valucs
ol this column. For all datcs ol thc column, cxccpt thc oldcst onc, thc comparison is
cvaluatcd to truc at lcast oncc. Thc row with thc oldcst datc docs not bclong to thc
rcsult bccausc thc comparison docs not cvaluatc to truc in any casc. !n othcr words,
thc cxprcssion "cntcr_datc ~ ANY (SLCT cntcr_datc FRM works_on)" is truc il
thcrc arc any (onc or morc) rows in thc works_on tablc with a valuc ol thc enter_date
column lcss than thc valuc ol enter_date lor thc currcnt row. This will bc truc lor all
but thc carlicst valuc ol thc enter_date column.
Thc ALL opcrator cvaluatcs to truc il thc cvaluation ol thc tablc column in thc inncr
qucry rcturns all valucs ol that column.
Cha pt er 6 : Quer i es 147
|c sc| ase /| cs1 /|| c(e/c|c/s| ||e/, ae/, as|s /| c/ /|| :cs |e |e||e/ /c/ma|c|e1 a||| ||e |/| ::
/as:||cs, a||:| |s es(|c|se1 |c|e/ |s |||s :|c(|e/ (see ||e se:||cs :a|ae/|es cs1 ||e |/|:: |as:||cs .
/11|||csc||,, ||e semcs||: mecs|s c/ ||e /| c(e/c|c/ :cs |e ecs||, :cs/ase1 a||| ||e semcs||: mecs|s c/
||e /|| c(e/c|c/, cs1 ||:e |e/sc.
SLC7 Statement: Dtber CIauses and Functions
Thc lollowing subscctions dcscribc thc rcst ol thc clauscs that can bc uscd in a qucry
as wcll as aggrcgatc lunctions and sct opcrators. First, wc discuss thc GRUP 8Y
clausc and show scvcral cxamplcs with thc clausc and thc aggrcgatc lunctions, which
arc supportcd by TransactSQL. Altcr that, thc HA\!NG and RR 8Y clauscs arc
introduccd. This scction also discusscs thc !NT!TY propcrty and thc cxisting sct
opcrators (UN!N, !NTRSCT and XCPT).
CRDUP Y CIause
Thc GRUP 8Y clausc dclincs onc or morc columns as a group such that all rows
within any group havc thc samc valucs lor thosc columns. xamplc 6.26 shows thc
simplc usc ol thc GRUP 8Y clausc.
XAMPL 6.26
Gct all jobs ol thc cmployccs:
US samplc,
SLCT job
FRM works_on
GRUP 8Y job,
Thc rcsult is
[eh
NJ||
|a+|.t
C|er|
M+a+er
148 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
!n xamplc 6.26, thc GRUP 8Y clausc builds dillcrcnt groups lor all possiblc
valucs (NULL, too) appcaring in thc job column.
|e/e |s c /es|/|:||cs /ec/1|s ||e ase c/ :c|amss |s ||e |0|| 3 :|case. |c:| :c|ams c((ec/|s |s ||e :|||t
||s| c/ ||e ae/, mas| c|sc c((ec/ |s ||e |0|| 3 :|case. ||s /es|/|:||cs 1ces sc| c((|, |c :css|cs|s cs1 |c
:c|amss ||c| c/e (c/| c/ cs c/ec|e /as:||cs. (//ec|e /as:||css c/e es(|c|se1 |s ||e ses| sa|se:||cs. ||s
mc|es sesse, |e:case cs|, :c|amss |s ||e |0|| 3 :|case c/e ac/cs|ee1 |c |c|e c s|s|e |c|ae /c/ ec:| /ca(.
A tablc can bc groupcd by any combination ol its columns. xamplc 6.27 shows thc
grouping ol rows ol thc works_on tablc using two columns.
XAMPL 6.27
Group all cmployccs using thcir projcct numbcrs and jobs:
US samplc,
SLCT projcct_no, job
FRM works_on
GRUP 8Y projcct_no, job,
Thc rcsult is
pre[ect_ae [eh
j |a+|.t
j C|er|
j M+a+er
j NJ||
jz NJ||
jz C|er|
j1 |a+|.t
j1 C|er|
j1 M+a+er
Thc rcsult ol xamplc 6.27 shows that thcrc arc ninc groups with dillcrcnt
combinations ol projcct numbcrs and jobs. Thc only two groups that contain morc than
onc row arc
jz C|er| z!1+, z!!
jz NJ|| 1, z1+
Cha pt er 6 : Quer i es 149
Thc scqucncc ol thc column namcs in thc GRUP 8Y clausc nccd not corrcspond
to thc scqucncc ol thc namcs in thc SLCT list.
tc|amss a||| ||e |/ (c/ |//| 1c|c |,(e :cssc| |e ase1 |s ||e |0|| 3 :|case.

Aggregate Functions
Aggrcgatc lunctions arc lunctions that arc uscd to gct summary valucs. All aggrcgatc
lunctions can bc dividcd into scvcral groups:
Convcnicnt aggrcgatc lunctions
Statistical aggrcgatc lunctions

Analytic aggrcgatc lunctions
Thc lirst thrcc typcs arc dcscribcd in thc lollowing scctions, whilc analytic aggrcgatc
lunctions arc cxplaincd in dctail in Chaptcr 24.
Convenient Aggregate Iunctions
Thc TransactSQL languagc supports six aggrcgatc lunctions:
M!N
MAX
SUM
A\G
CUNT
CUNT_8!G
All aggrcgatc lunctions opcratc on a singlc argumcnt, which can bc cithcr a
column or an cxprcssion. (Thc only cxccption is thc sccond lorm ol thc CUNT
and CUNT_8!G lunctions: CUNT() and CUNT_8!G().) Thc rcsult ol cach
aggrcgatc lunction is a constant valuc, which is displaycd in a scparatc column ol
thc rcsult.
150 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Thc aggrcgatc lunctions appcar in thc SLCT list, which can includc a GRUP
8Y clausc. !l thcrc is no GRUP 8Y clausc in thc SLCT statcmcnt, and thc
SLCT list includcs at lcast onc aggrcgatc lunction, thcn no simplc columns can
bc includcd in thc SLCT list (othcr than as argumcnts ol an aggrcgatc lunction).
Thcrclorc, xamplc 6.28 is .rong.
XAMPL 6.28 (XAMPL DF AN ILLCAL S7A7MN7)
US samplc,
SLCT cmp_lnamc, M!N(cmp_no)
FRM cmploycc,
Thc emp_lname column ol thc employee tablc must not appcar in thc SLCT list
ol xamplc 6.28, bccausc it is not thc argumcnt ol an aggrcgatc lunction. n thc othcr
hand, all column namcs that arc not argumcnts ol an aggrcgatc lunction may appcar in
thc SLCT list il thcy arc uscd lor grouping.
Thc argumcnt ol an aggrcgatc lunction can bc prcccdcd by onc ol two kcywords:
ALL !ndicatcs that all valucs ol a column arc to bc considcrcd (ALL is dclault
valuc)
DSTNCT liminatcs duplicatc valucs ol a column bclorc thc aggrcgatc
lunction is applicd
MIN and MAX Aggregate Functions Thc aggrcgatc lunctions M!N and MAX
computc thc lowcst and highcst valucs in thc column, rcspcctivcly. !l thcrc is a
VHR clausc, thc M!N and MAX lunctions rcturn thc lowcst or highcst ol valucs
lrom sclcctcd rows. xamplc 6.29 shows thc usc ol thc aggrcgatc lunction M!N.
XAMPL 6.29
Gct thc lowcst cmploycc numbcr:
US samplc,
SLCT M!N(cmp_no) AS min_cmploycc_no
FRM cmploycc,
Thc rcsult is
m|a_emp|eee_ae
z!
Cha pt er 6 : Quer i es 151
Thc rcsult ol xamplc 6.29 is not uscr lricndly. For instancc, thc namc ol thc
cmploycc with thc lowcst numbcr is not known. As alrcady shown, thc cxplicit
spccilication ol thc emp_name column in thc SLCT list is not allowcd. To rctricvc
thc namc ol thc cmploycc with thc lowcst cmploycc numbcr, usc a subqucry, as shown
in xamplc 6.30, whcrc thc inncr qucry contains thc SLCT statcmcnt ol thc
prcvious cxamplc.
XAMPL 6.30
Gct thc numbcr and thc last namc ol thc cmploycc with thc lowcst cmploycc numbcr:
US samplc,
SLCT cmp_no, cmp_lnamc
FRM cmploycc
VHR cmp_no -
(SLCT M!N(cmp_no)
FRM cmploycc),
Thc rcsult is
emp_ae emp_|aame
z! |+a.e|
xamplc 6.31 shows thc usc ol thc aggrcgatc lunction MAX.
XAMPL 6.31
Gct thc cmploycc numbcr ol thc managcr who was cntcrcd last in thc works_on tablc:
US samplc,
SLCT cmp_no
FRM works_on
VHR cntcr_datc -
(SLCT MAX(cntcr_datc)
FRM works_on
VHR job - 'Managcr'),
Thc rcsult is
emp_ae
z
152 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Thc argumcnt ol thc lunctions M!N and MAX can also bc a string valuc or a
datc. !l thc argumcnt has a string valuc, thc comparison bctwccn all valucs will bc
providcd using thc actual collating scqucncc. For all argumcnts ol tcmporal data typcs,
thc carlicst datc spccilics thc lowcst valuc and thc latcst datc thc highcst valuc in thc
column.
Thc !ST!NCT option cannot bc uscd with thc aggrcgatc lunctions M!N and
MAX. All NULL valucs in thc column that arc thc argumcnt ol thc aggrcgatc lunction
M!N or MAX arc always climinatcd bclorc M!N or MAX is applicd.
SUM Aggregate Function Thc aggrcgatc lunction SUM calculatcs thc sum ol thc
valucs in thc column. Thc argumcnt ol thc lunction SUM must bc numcric. xamplc 6.32
shows thc usc ol thc SUM lunction.
XAMPL 6.32
Calculatc thc sum ol all budgcts ol all projccts:
US samplc,
SLCT SUM (budgct) sum_ol_budgcts
FRM projcct,
Thc rcsult is
scm_ef_hc6qets
+!
Thc aggrcgatc lunction in xamplc 6.32 groups all valucs ol thc projccts' budgcts
and dctcrmincs thcir total sum. For this rcason, thc qucry in xamplc 6.32 (as docs cach
analog qucry) implicitly contains thc grouping lunction. Starting with SQL Scrvcr 2008,
you can writc thc grouping lunction cxplicitly in thc qucry shown in xamplc 6.32 with
thc GRUP 8Y clausc, as shown in xamplc 6.33.
XAMPL 6.33
SLCT SUM (budgct) sum_ol_budgcts
FRM projcct
GRUP 8Y(),
Thc usc ol thc ncw syntax lor thc GRUP 8Y clausc is rccommcndcd, bccausc it
dclincs a grouping cxplicitly. (Chaptcr 25 dcscribcs scvcral othcr GRUP 8Y lcaturcs
that arc ncw in SQL Scrvcr 2008.)
Cha pt er 6 : Quer i es 153
Thc usc ol thc !ST!NCT option climinatcs all duplicatc valucs in thc column
bclorc thc lunction SUM is applicd. Similarly, all NULL valucs arc always climinatcd
bclorc SUM is applicd.
AVC Aggregate Function Thc aggrcgatc lunction A\G calculatcs thc avcragc ol thc
valucs in thc column. Thc argumcnt ol thc lunction A\G must bc numcric. All NULL
valucs arc climinatcd bclorc thc lunction A\G is applicd. xamplc 6.34 shows thc usc
ol thc A\G aggrcgatc lunction.
XAMPL 6.34
Calculatc thc avcragc ol all budgcts with an amount grcatcr than 8100,000:
US samplc,
SLCT A\G(budgct) avg_budgct
FRM projcct
VHR budgct ~ 100000,
Thc rcsult is
aq_hc6qet
!1z!
CDUN7 and CDUN7_IC Aggregate Functions Thc aggrcgatc lunction CUNT
has two dillcrcnt lorms:
CUNT (|!ST!NCT| col_namc)
CUNT ()
Thc lirst lorm calculatcs thc numbcr ol valucs in thc col_name column. Vhcn thc
!ST!NCT kcyword is uscd, all duplicatc valucs arc climinatcd bclorc CUNT is
applicd. This lorm ol CUNT docs not count NULL valucs lor thc column.
xamplc 6.35 shows thc usc ol thc lirst lorm ol thc aggrcgatc lunction CUNT.
XAMPL 6.35
Count all dillcrcnt jobs in cach projcct:
US samplc,
SLCT projcct_no, CUNT(!ST!NCT job) job_count
FRM works_on
GRUP 8Y projcct_no,
154 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Thc rcsult is
pre[ect_ae [eh_cecat
j +
jz
j1 1
As can bc sccn lrom thc rcsult ol xamplc 6.35, all NULL valucs arc climinatcd
bclorc thc lunction CUNT(!ST!NCT job) is applicd. (Thc sum ol all valucs in thc
column is 8 instcad ol 11.)
Thc sccond lorm ol thc lunction CUNT, CUNT(), counts thc numbcr ol rows
in thc tablc. r il thcrc is a VHR clausc in thc SLCT statcmcnt, it rcturns thc
numbcr ol rows lor which thc VHR condition is truc. !n contrast to thc lirst lorm
ol thc lunction CUNT, thc sccond lorm docs not climinatc NULL valucs, bccausc
this lunction opcratcs on rows and not on columns. xamplc 6.36 shows thc usc ol
CUNT().
XAMPL 6.36
Gct thc numbcr ol cach job in all projccts:
US samplc,
SLCT job, CUNT() job_count
FRM works_on
GRUP 8Y job,
Thc rcsult is
[eh [eh_cecat
NJ|| 1
|a+|.t z
C|er| +
M+a+er z
Thc CUNT_8!G lunction is analogous to thc CUNT lunction. Thc only
dillcrcncc bctwccn thcm is in rclation to thcir rcturn valucs: CUNT_8!G always
rcturns a valuc ol thc 8!G!NT data typc, whilc thc CUNT lunction always rcturns a
valuc ol thc !NTGR data typc.
Cha pt er 6 : Quer i es 155
StatisticaI Aggregate Iunctions
Thc lollowing aggrcgatc lunctions bclong to thc group ol statistical aggrcgatc lunctions:
VAR Computcs thc variancc ol all thc valucs listcd in a column or cxprcssion.
VARP Computcs thc variancc lor thc population ol all thc valucs listcd in a
column or cxprcssion.
STDEV Computcs thc standard dcviation ol all thc valucs listcd in a column
corrcsponding variancc.)
STDEVP Computcs thc standard dcviation lor thc population ol all thc valucs
listcd in a column or cxprcssion.
xamplcs conccrning statistical aggrcgatc lunctions will bc shown in Chaptcr 24.
User-Defined Aggregate Iunctions
atabasc nginc also supports thc implcmcntation ol uscrdclincd aggrcgatc lunctions.
Using thcsc lunctions, you can implcmcnt and dcploy aggrcgatc lunctions that do not
bclong to aggrcgatc lunctions supportcd by thc systcm. Thcsc lunctions arc a spccial
casc ol uscrdclincd lunctions, which will bc dcscribcd in dctail in Chaptcr 8.
HAVINC CIause
Thc HA\!NG clausc dclincs thc condition that is thcn applicd to groups ol rows.
Hcncc, this clausc has thc samc mcaning to groups ol rows that thc VHR clausc
has to thc contcnt ol thc corrcsponding tablc. Thc syntax ol thc HA\!NG clausc is
HA\!NG condition
whcrc condition contains aggrcgatc lunctions or constants.
xamplc 6.37 shows thc usc ol thc HA\!NG clausc with thc aggrcgatc lunction
CUNT().
XAMPL 6.37
Gct projcct numbcrs lor all projccts cmploying lcwcr than lour pcrsons:
US samplc,
SLCT projcct_no
FRM works_on
GRUP 8Y projcct_no
HA\!NG CUNT() 4,
156 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Thc rcsult is
pre[ect_ae
j1
!n xamplc 6.37, thc systcm uscs thc GRUP 8Y clausc to group all rows according
to cxisting valucs in thc project_no column. Altcr that, it counts thc numbcr ol rows in
cach group and sclccts thosc groups with thrcc or lcwcr rows.
Thc HA\!NG clausc can also bc uscd without aggrcgatc lunctions, as shown in
xamplc 6.38.
XAMPL 6.38
Group rows ol thc works_on tablc by job and climinatc thosc jobs that do not bcgin
with thc lcttcr M:
US samplc,
SLCT job
FRM works_on
GRUP 8Y job
HA\!NG job L!K 'M',
Thc rcsult is
[eh
M+a+er
Thc HA\!NG clausc can also bc uscd without thc GRUP 8Y clausc, although
doing so is uncommon in practicc. !n such a casc, all rows ol thc cntirc tablc bclong to a
singlc group.
tc|amss a||| ||e |/ (c/ |//| 1c|c |,(e :cssc| |e ase1 a||| ||e |/l|| :|case.

DRDR Y CIause
Thc RR 8Y clausc dclincs thc particular ordcr ol thc rows in thc rcsult ol a qucry.
This clausc has thc lollowing syntax:
RR 8Y [|col_namc ] col_numbcr |ASC ] SC||] , ...
Cha pt er 6 : Quer i es 157
Thc col_name column dclincs thc ordcr. col_number is an altcrnativc spccilication,
which idcntilics thc column by its ordinal position in thc scqucncc ol all columns in thc
SLCT list (1 lor thc lirst column, 2 lor thc sccond onc, and so on). ASC indicatcs
asccnding ordcr and SC indicatcs dcsccnding ordcr, with ASC as thc dclault valuc.
|e :c|amss |s ||e 0|||| 3 :|case see1 sc| c((ec/ |s ||e :|||t ||s|. |cae|e/, ||e 0|||| 3 :c|amss mas|
c((ec/ |s ||e :|||t ||s| |/ :|||t ||:||t |s s(e:|/|e1. /|sc, |||s :|case mc, sc| /e/e/es:e :c|amss //cm |c||es
||c| c/e sc| ||s|e1 |s ||e ||0/ :|case.
As thc syntax ol thc RR 8Y clausc shows, thc ordcr critcrion may contain morc
than onc column, as shown in xamplc 6.39.
XAMPL 6.39
Gct dcpartmcnt numbcrs and cmploycc namcs lor cmployccs with cmploycc numbcrs
20000, in asccnding ordcr ol last and lirst namcs:
US samplc,
SLCT cmp_lnamc, cmp_lnamc, dcpt_no
FRM cmploycc
VHR cmp_no 20000
RR 8Y cmp_lnamc, cmp_lnamc,
Thc rcsult is
emp_faame emp_|aame 6ept_ae
Ie|a |+rr|mere a
||.+ |retea| az
|||e |+a.e| az
|aa Ieae. a1
!t is also possiblc to idcntily thc columns in thc RR 8Y clausc by thc ordinal
position ol thc column in thc SLCT list. Thc RR 8Y clausc in xamplc 6.39
could bc writtcn in thc lollowing lorm:
RR 8Y 2,1
Thc usc ol column numbcrs instcad ol column namcs is an altcrnativc solution, il
thc ordcr critcrion contains any aggrcgatc lunction. (Thc othcr way is to usc column
hcadings, which thcn appcar in thc RR 8Y clausc.) Howcvcr, using column
158 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
namcs rathcr than numbcrs in thc RR 8Y clausc is rccommcndcd to rcducc thc
dilliculty ol maintaining thc qucry il any columns nccd to bc addcd or dclctcd lrom thc
SLCT list. xamplc 6.40 shows thc usc ol column numbcrs.
XAMPL 6.40
For cach projcct numbcr, gct thc projcct numbcr and thc numbcr ol all cmployccs, in
dcsccnding ordcr ol thc cmploycc numbcr:
US samplc,
SLCT projcct_no, CUNT() cmp_quantity
FRM works_on
GRUP 8Y projcct_no
RR 8Y 2 SC
Thc rcsult is
pre[ect_ae emp_caat|t
j +
jz +
j1 1
Thc TransactSQL languagc ordcrs NULL valucs at thc bcginning ol all valucs il thc
ordcr is asccnding, and at thc cnd ol all valucs il thc ordcr is dcsccnding.
tc|amss a||| ||e |/ (c/ |//| 1c|c |,(e :cssc| |e ase1 |s ||e 0|||| 3 :|case.

SLC7 Statement and IDN7I7Y Property
Columns with numcric data typcs, such as T!NY!NT, SMALL!NT, !NT, and
8!G!NT, can havc thc !NT!TY propcrty. atabasc nginc gcncratcs valucs lor
such columns scqucntially, starting with an initial valuc. Thcrclorc, you can usc thc
!NT!TY propcrty to lct thc systcm gcncratc uniquc numcric valucs lor thc tablc
column ol your choicc.
ach tablc can havc only onc column with thc !NT!TY propcrty. Thc tablc
owncr can spccily thc starting numbcr and thc incrcmcnt valuc, as shown in
xamplc 6.41.
Cha pt er 6 : Quer i es 159
XAMPL 6.41
US samplc,
CRAT TA8L product
(product_no !NTGR !NT!TY(10000,1) NT NULL,
product_namc CHAR(30) NT NULL,
pricc MNY),
SLCT !NT!TYCL
FRM product
VHR product_namc - 'Soap',
Thc rcsult could bc
pre6cct_ae
!
Thc product tablc is crcatcd lirst in xamplc 6.41. This tablc has thc column
product_no with thc !NT!TY propcrty. Thc valucs ol thc product_no column arc
automatically gcncratcd by thc systcm, bcginning with 10000 and incrcmcnting by 1 lor
cvcry subscqucnt valuc: 10000, 10001, 10002, and so on.
Somc systcm lunctions and variablcs arc rclatcd to thc !NT!TY propcrty.
xamplc 6.41 uscd thc !NT!TYCL variablc. As can bc sccn lrom thc rcsult sct
ol xamplc 6.41, this variablc automatically rclcrs to thc column with thc !NT!TY
propcrty.
To lind out thc starting valuc and thc incrcmcnt ol thc column with thc !NT!TY
propcrty, you can usc thc !NT_S and !NT_!NCR lunctions, rcspcctivcly:
SLCT !NT_S('product'), !NT_!NCR('product)'
As you alrcady know, thc systcm automatically scts idcntity valucs. !l you want to
supply your own valucs lor particular rows, you must sct thc !NT!TY_!NSRT
option to N bclorc thc cxplicit valuc will bc inscrtcd:
ST !NT!TY_!NSRT tablc_namc N
3e:case ||e |||||_||:|| c(||cs :cs |e ase1 |c s(e:|/, cs, |c|aes /c/ c :c|ams a||| ||e ||||| (/c(e/|,,
||||| 1ces sc| ese/c||, es/c/:e as|aesess. |se ||e |||9|| c/ |||//| || :css|/c|s| /c/ |||s |cs|.
160 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
!l you inscrt valucs altcr thc !NT!TY_!NSRT option is sct to N, thc systcm
prcsumcs that thc ncxt valuc is thc incrcmcntcd valuc ol thc highcst valuc that cxists in
thc tablc at that momcnt.
Set Dperators
!n addition to thc opcrators dcscribcd in thc prcvious scctions, thcrc arc thrcc sct
opcrators supportcd in thc TransactSQL languagc:
UN!N
!NTRSCT
XCPT
UNION Set Operator
Thc rcsult ol thc union ol two scts is thc sct ol all clcmcnts appcaring in cithcr or both
ol thc scts. Accordingly, thc union ol two tablcs is a ncw tablc consisting ol all rows
appcaring in cithcr or both ol thc tablcs.
Thc gcncral lorm ol thc UN!N opcrator is
sclcct_1 UN!N |ALL| sclcct_2 [|UN!N |ALL| sclcct_3|]...
select_1, select_2,... arc SLCT statcmcnts that build thc union. !l thc ALL option
is uscd, all rcsulting rows, including duplicatcs, arc to bc displaycd. Thc ALL option has
thc samc mcaning with thc UN!N opcrator as it has in thc SLCT list. Thcrc is
only onc dillcrcncc: thc ALL option is thc dclault in thc SLCT list, but it must bc
spccilicd with thc UN!N opcrator to display all rcsulting rows, including duplicatcs.
Thc sample databasc in its original lorm is not suitablc lor a dcmonstration ol thc
UN!N opcrator. For this rcason, this scction introduccs a ncw tablc, employee_enh,
which is idcntical to thc cxisting employee tablc, up to thc additional domicile column.
Thc domicile column contains thc placc ol rcsidcncc ol cvcry cmploycc.
Thc ncw employee_enh tablc has thc lollowing lorm:
emp_ae emp_faame emp_|aame 6ept_ae 6em|c||e
z!1+ M+tt|ew :m|t| a1 :+a |atea|e
z |aa Ieae. a1 |ea.tea
1 Ie|a |+rr|mere a :+a |atea|e
z1+ I+me. I+me. az :e+tt|e
1 |||e |erte|| az |ert|+aa
z! |||.+ ||m az I+cem+
z!! :|||| Me.er a |ea.tea
Cha pt er 6 : Quer i es 161
Crcation ol thc employee_enh tablc providcs an opportunity to show thc usc ol thc
!NT clausc ol thc SLCT statcmcnt. SLCT !NT has two dillcrcnt parts:
First, it crcatcs thc ncw tablc with thc columns corrcsponding to thc columns listcd in
thc SLCT list. Sccond, it inscrts thc cxisting rows ol thc original tablc into thc ncw
tablc. (Thc namc ol thc ncw tablc appcars with thc !NT clausc, and thc namc ol thc
sourcc tablc appcars in thc FRM clausc ol thc SLCT statcmcnt.)
xamplc 6.42 shows thc crcation ol thc employee_enh tablc.
XAMPL 6.42
US samplc,
SLCT cmp_no, cmp_lnamc, cmp_lnamc, dcpt_no
!NT cmploycc_cnh
FRM cmploycc,
ALTR TA8L cmploycc_cnh
A domicilc CHAR(25) NULL,
!n xamplc 6.42, SLCT !NT gcncratcs thc employee_enh tablc and inscrts all
rows lrom thc initial tablc (employee) into thc ncw onc. Finally, thc ALTR TA8L
statcmcnt appcnds thc domicile column to thc employee_enh tablc.
Altcr thc cxccution ol xamplc 6.42, thc domicile column contains no valucs. Thc
valucs can bc addcd using SQL Scrvcr Managcmcnt Studio (scc Chaptcr 3) or thc
lollowing UPAT statcmcnts:
US samplc,
UPAT cmploycc_cnh ST domicilc - 'San Antonio'
VHR cmp_no - 25348,
UPAT cmploycc_cnh ST domicilc - 'Houston'
VHR cmp_no - 10102,
UPAT cmploycc_cnh ST domicilc - 'San Antonio'
VHR cmp_no - 18316,
UPAT cmploycc_cnh ST domicilc - 'Scattlc'
VHR cmp_no - 29346,
UPAT cmploycc_cnh ST domicilc - 'Portland'
VHR cmp_no - 9031,
UPAT cmploycc_cnh ST domicilc - 'Tacoma'
VHR cmp_no - 2581,
UPAT cmploycc_cnh ST domicilc - 'Houston'
VHR cmp_no - 28559,
162 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
xamplc 6.43 shows thc union ol thc tablcs employee_enh and department.
XAMPL 6.43
US samplc,
SLCT domicilc
FRM cmploycc_cnh
UN!N
SLCT location
FRM dcpartmcnt,
Thc rcsult is
6em|c||e
:+a |atea|e
|ea.tea
|ert|+aa
I+cem+
:e+tt|e
|+||+.
Two tablcs can bc conncctcd with thc UN!N opcrator il thcy arc compatiblc
with cach othcr. This mcans that both thc SLCT lists must havc thc samc numbcr
ol columns, and thc corrcsponding columns must havc compatiblc data typcs. (For
cxamplc, !NT and SMALL!NT arc compatiblc data typcs.)
Thc ordcring ol thc rcsult ol thc union can bc donc only il thc RR 8Y clausc
is uscd with thc last SLCT statcmcnt, as shown in xamplc 6.44. Thc GRUP 8Y
and HA\!NG clauscs can bc uscd with thc particular SLCT statcmcnts, but not
with thc union itscll.
XAMPL 6.44
Gct thc cmploycc numbcr lor cmployccs who cithcr bclong to dcpartmcnt d1 or
cntcrcd thcir projcct bclorc 1/1/2007, in asccnding ordcr ol cmploycc numbcr:
US samplc,
SLCT cmp_no
FRM cmploycc
VHR dcpt_no - 'd1'
UN!N
SLCT cmp_no
Cha pt er 6 : Quer i es 163
FRM works_on
VHR cntcr_datc '01.01.2007'
RR 8Y 1,
Thc rcsult is
emp_ae
1
z
1
z!!
z1+
|e |||0| c(e/c|c/ sa((c/|s ||e /|| c(||cs. |||0| a||| /|| mecss ||c| 1a(||:c|es c/e sc| /emc|e1 //cm ||e
/esa|| se|.
Thc R opcrator can bc uscd instcad ol thc UN!N opcrator il all SLCT
statcmcnts conncctcd by onc or morc UN!N opcrators rclcrcncc thc samc tablc.
!n this casc, thc sct ol thc SLCT statcmcnts is rcplaccd through onc SLCT
statcmcnt with thc sct ol R opcrators.
IN7RSC7 and XCP7 Set Operators
Thc two othcr sct opcrators arc !NTRSCT, which spccilics thc intcrscction, and
XCPT, which dclincs thc dillcrcncc opcrator. Thc intcrscction ol two tablcs is thc sct
ol rows bclonging to both tablcs. Thc dillcrcncc ol two tablcs is thc sct ol all rows, whcrc
thc rcsulting rows bclong to thc lirst tablc but not to thc sccond onc. xamplc 6.45 shows
thc usc ol thc !NTRSCT opcrator.
XAMPL 6.45
US samplc,
SLCT cmp_no
FRM cmploycc
VHR dcpt_no - 'd1'
!NTRSCT
SLCT cmp_no
FRM works_on
VHR cntcr_datc '01.01.2008',
164 Mi c r os of t S QL S er v er 2 00 8: A 8egi nner ' s Gui de
Thc rcsult is
emp_ae
1
z!!
/cssc:|-:9| 1ces sc| sa((c/| ||e ||||:|t c(e/c|c/ a||| ||e /|| c(||cs. (|e scme |s |/ae /c/ ||e |/t||
c(e/c|c/.
xamplc 6.46 shows thc usc ol thc XCPT sct opcrator.
XAMPL 6.46
US samplc,
SLCT cmp_no
FRM cmploycc
VHR dcpt_no - 'd3'
XCPT
SLCT cmp_no
FRM works_on
VHR cntcr_datc ~ '01.01.2008',
Thc rcsult is
emp_ae
z
z!1+
ca s|ca|1 |e cac/e ||c| ||e ||/ee se| c(e/c|c/s |c|e 1|//e/es| (/|c/|||es /c/ e|c|ac||cs. ||e ||||:|t c(e/c|c/
|cs ||e |||es| (/|c/||,, |/t|| |s e|c|ac|e1 ses|, cs1 ||e |||0| c(e/c|c/ |cs ||e |caes| (/|c/||,. |/ ,ca 1c sc| (c,
c||es||cs |c ||ese 1|//e/es| (/|c/|||es, ,ca a||| e| ases(e:|e1 /esa||s a|es ,ca ase se|e/c| se| c(e/c|c/s |ce||e/.
CAS xpressions
!n databasc application programming, it is somctimcs ncccssary to modily thc
rcprcscntation ol data. For instancc, a pcrson's gcndcr can bc codcd using thc valucs 1,
2, and 3 (lor lcmalc, malc, and child, rcspcctivcly). Such a programming tcchniquc can

Vous aimerez peut-être aussi