To restore the Northwind database, right-click the Northwind node in ObjectExplorer, than click Tasks ➤Restore➤Database….. In SSMSE Object Explorer, right-click the Northwind node and cl
Trang 116. Now try to execute the query in the SQL edit window (or try anything to access theinstance, if you’ve already shut the window) There will be no Results window, andthe Messages window will report an error, as in Figure 2-16.
C H A P T E R 2 ■ G E T T I N G TO K N O W O U R TO O L S 27
Figure 2-14.A database diagram
Figure 2-15.Stopping an SSE instance
Trang 217. To start SQLEXPRESS, right-click the instance node in Object Explorer, click Start,and click Yes in the confirmation message box Click the Northwind node andretry the query It should work fine.
18. To restore the Northwind database, right-click the Northwind node in ObjectExplorer, than click Tasks ➤Restore➤Database… You’ll see the window inFigure 2-17
19. Click OK and you’ll get the message box in Figure 2-18 You can’t restore theNorthwind database, because the kind of restore you’re doing requires exclusiveaccess, and SSMSE is currently connected to Northwind through the query Click
OK to close the message box, then close the SQL edit window (click No whenprompted to save it)
20. Click OK again in the Restore Database window You should get the message boxshown in Figure 2-19 To see that the database has been restored, try to expandthe Database Diagrams node under Northwind You saved a diagram earlier, butonce again the plus sign disappears, since the database diagram was stored inNorthwind in step 14, but you backed up the database in steps 7 and 8, so you’verestored Northwind to a state before the diagram was saved The same holds truefor any data in any SSE database
C H A P T E R 2 ■ G E T T I N G TO K N O W O U R TO O L S
28
Figure 2-16.Error on trying to query a stopped SSE instance
777Xch02final.qxd 11/18/06 3:43 PM Page 28
Trang 3C H A P T E R 2 ■ G E T T I N G TO K N O W O U R TO O L S 29
Figure 2-17.Restoring the Northwind database
Figure 2-18.Restore-failure message box
Figure 2-19.Message box stating the Northwind database is restored
Trang 4Configuring VCSE
VCSE offers a significant subset of Visual Studio 2005 functionality, limited to C# gramming and not including templates for building ASP.NET applications (for whichanother Express IDE, Visual Web Developer 2005 Express, is available) We assume sinceyou know C# that you have some experience with either Visual Studio or VCSE, so we’llonly cover the specific things you should do with VCSE to make the examples in thisbook easiest to work with
pro-Let’s do these few simple things with VCSE:
1. To open VCSE, click Start ➤All Programs ➤Microsoft Visual C# 2005 Express tion You should see the screen shown in Figure 2-20
Edi-C H A P T E R 2 ■ G E T T I N G TO K N O W O U R TO O L S
30
Figure 2-20.Opening VCSE
777Xch02final.qxd 11/18/06 3:43 PM Page 30
Trang 52. VCSE is a highly configurable IDE that each user typically adapts to personal erences We use it in a rather vanilla way and don’t at all try to exploit its manyfeatures that can significantly enhance developer productivity, because whatseems the best way to one user may seem the worst way to another But, we docustomize the configuration a bit, because we want to control how we indent codeand where we store our VCSE solutions You don’t have to do this, but here’s what
pref-we did, if you’d like to be consistent Click Tools ➤Options… to open the window,
C H A P T E R 2 ■ G E T T I N G TO K N O W O U R TO O L S 31
Figure 2-21.Opening VCSE Options
Trang 65. You’ll need a connection to the SSE Northwind database, so let’s create one ClickView ➤Other Windows ➤Database Explorer to open Database Explorer (which isvery similar to SSMSE’s Object Explorer) Notice in Figure 2-24 that there are noData Connections nodes.
C H A P T E R 2 ■ G E T T I N G TO K N O W O U R TO O L S
32
Figure 2-22.Changing the default folder for VCSE projects
Figure 2-23.Setting VCSE tabs and indentations
777Xch02final.qxd 11/18/06 3:43 PM Page 32
Trang 76. Right-click the Data Connections node and click Add Connection… The AddConnection window appears, as in Figure 2-25 Note that the data source is a SQL Server database file and will be accessed through SqlClient(the NET dataprovider for SQL Server, to be covered in Chapter 4) The connection will useWindows Authentication, meaning any user who can log in to the server machinecan connect to the Northwind database.
C H A P T E R 2 ■ G E T T I N G TO K N O W O U R TO O L S 33
Figure 2-24.Database Explorer with no connections
Trang 87. You need to specify a database to connect to Browse to the Northwind database
in C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\northwnd.mdf, andselect it Then click Test Connection A message box should appear telling you
“Test connection succeeded.” Click OK to close it Click OK to save the tion A northwnd.mdf node will appear in Database Explorer Expand the node,and you’ll see nodes in Figure 2-26 that are very similar to the ones in Figure 2-14for SSMSE
connec-C H A P T E R 2 ■ G E T T I N G TO K N O W O U R TO O L S
34
Figure 2-25.Add Connection
Figure 2-26.An expanded connection in Database Explorer
777Xch02final.qxd 11/18/06 3:43 PM Page 34
Trang 98. Expand the Tables node and right-click the Employees node You’ll see the menu
in Figure 2-27 Database Explorer basically offers the same features for databaseaccess and management as SSMSE’s Object Explorer, but it doesn’t offer adminis-tration functions for managing instances VCSE’s New Query has a slightly differ-ent format from SSMSE’s New Query, but it’s as powerful VCSE’s Show Table Datahas the same format and functionality as SSMSE’s Open Table
Using BOL
BOL is the official source of SQL Server documentation The documentation for SQL
Server Express is part of BOL BOL is a hypertext application that is both a blessing and
a bane for SQL Server users It’s an enormously rich source of tutorials as well as
refer-ence information, but despite the fact that it’s extensively indexed, finding what you need
to know can sometimes be quite frustrating
C H A P T E R 2 ■ G E T T I N G TO K N O W O U R TO O L S 35
Figure 2-27.An expanded table menu in Database Explorer
Trang 10We’ll only point out a few things to help you get started:
1. To open BOL, click Start ➤All Programs ➤Microsoft SQL Server 2005 ➤mentation and Tutorials ➤SQL Server Books Online You should see the screen
Docu-in Figure 2-28
2. There are five nodes in the Contents tree The third one leads to SSE-specific umentation, but unless you plan to use SSE exclusively as a production server, youprobably won’t find this very useful What you’ll find most useful is the first node,SQL Server 2005 Books Online, so expand it Then expand the SQL Server Lan-guage Reference node and the Transact-SQL Reference node within it Scroll up
doc-to the doc-top, and you’ll see a screen as in Figure 2-29 All T-SQL language nents are listed alphabetically
compo-C H A P T E R 2 ■ G E T T I N G TO K N O W O U R TO O L S
36
Figure 2-28.Opening BOL
777Xch02final.qxd 11/18/06 3:43 PM Page 36
Trang 113. Click the Index tab below the Contents tree You’ll see the screen in Figure 2-30.
The “Look for:” text box is where you specify what to search for Above it is a tered by:” drop-down Click the down arrow and click SQL Server Express, to keepsearch results to a minimum (since SSE doesn’t support most of the other topics inthe list)
“Fil-C H A P T E R 2 ■ G E T T I N G TO K N O W O U R TO O L S 37
Figure 2-29.T-SQL Language Reference Table of Contents
Trang 124. The index redisplays entries as you type, so if you enter “sel” (it’s not case tive), you’ll see the screen in Figure 2-31.
sensi-C H A P T E R 2 ■ G E T T I N G TO K N O W O U R TO O L S
38
Figure 2-30.BOL Index
777Xch02final.qxd 11/18/06 3:43 PM Page 38
Trang 135. Click on the “SELECT INTO statement” entry and you’ll see an Index Results dow at the bottom of the screen offering alternatives to the first topic, which isdisplayed in the window above it (see Figure 2-32) Filtering helps reduce yourhaving to deal with alternatives that may not be relevant to your needs, but alter-natives do frequently occur Clicking on an entry in the Index Results windowdisplays that topic Now you’re able to search BOL as well as the rest of us Goodluck!
win-C H A P T E R 2 ■ G E T T I N G TO K N O W O U R TO O L S 39
Figure 2-31.BOL Index entries for “sel”
Trang 14In this chapter, we covered just enough about SSMSE to get you familiar with the kinds
of things you’ll do with it later in this book Among other things, we showed how to cute T-SQL, back up and restore a database, and create database diagrams We thenshowed you how to configure a few VCSE options to be consistent with the ones we use
exe-in writexe-ing our example programs exe-in VCSE We also showed you the basics of usexe-ing BOL.Now that your tools are installed and configured, you can start learning how to dodatabase programming by learning the basics of T-SQL
C H A P T E R 2 ■ G E T T I N G TO K N O W O U R TO O L S
40
Figure 2-32.Alternative BOL Index results
777Xch02final.qxd 11/18/06 3:43 PM Page 40
Trang 15HOW TO RECOVER NORTHWIND, WHEN ALL ELSE FAILS
If you need to recover Northwind and can’t do it in SSMSE (see Figure 2-17), follow these steps:
1 In SSMSE Object Explorer, right-click the Northwind node and click Delete The Delete Object
win-dow will appear Check “Close existing connections” and click OK The Northwind database will
be dropped from the SSE instance
2 Right-click the SSE instance at the top of Object Explorer, click Restart, and click Yes when
prompted for confirmation
3 In C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data, delete any files ing with northwnd
start-4 Re-create the Northwind database by running instnwnd.sqlwith sqlcmd, as described inChapter 1
C H A P T E R 2 ■ G E T T I N G TO K N O W O U R TO O L S 41
Trang 17If you’ve ever worked with relational databases, you’ve probably used SQL SQL is the
international standard database language You can use SQL to create, retrieve, change,
or delete data (and a lot of other things)
The formal definition of SQL comes from the American National Standards Institute(ANSI) It’s the same as the international standard specified by the International Organi-
zation for Standardization (ISO) T-SQL is the dialect of SQL provided by SQL Server
Since we’re using SSE, we’ll focus on T-SQL rather than standard SQL, but unless we’re
discussing a feature not found in standard SQL, we’ll refer to both simply as “SQL.” We’ll
use “standard SQL” when referring specifically to the ISO/ANSI version of the language
Each database vendor offers its own implementation of SQL, which conforms at some
level to the standard but typically extends it T-SQL does just that, and some of the SQL
used in this book may not work if you try it with a database server other than SQL Server
43
C H A P T E R 3
■ ■ ■
Trang 18■ Tip Relational database terminology is often confusing For example, neither the meaning nor the nunciation of SQL is crystal clear IBM invented the language back in the 1970s and called it StructuredEnglish Query Language (SEQUEL), changing it shortly thereafter to Structured Query Language (SQL) toavoid conflict with another vendor’s product SEQUEL and SQL were both pronounced “sequel.” When theISO/ANSI standard was adopted, it referred to the language simply as “database language SQL” and wassilent on whether this was an acronym and how it should be pronounced Today, two pronunciations areused In the Microsoft and Oracle worlds (as well as many others), it’s pronounced “sequel.” In the DB2 and MySQL worlds (among others), it’s pronounced “ess cue ell.” We’ll follow the most reasonable practice.We’re working in a Microsoft environment, so we’ll pronounce SQL as “sequel.”
pro-Retrieving Data
A SQL query retrieves data from a database Data is stored as rows in tables Rows are
composed of columns In its simplest form, a query consists of two parts:
• A SELECTlist, where the columns to be retrieved are specified
• A FROMclause, where the table or tables to be accessed are specified
■ Tip We’ve written SELECTand FROMin capital letters, simply to indicate they’re SQL keywords SQL isn’tcase sensitive, and keywords are typically written in lowercase in code In T-SQL, queries are called SELECT
statements, but the ISO/ANSI standard clearly distinguishes “queries” from “statements.” The distinction is
conceptually important A query is an operation on a table that produces a table as a result; statements may (or may not) operate on tables and don’t produce tables as results Further, subqueries can be used in both
queries and statements So, we’ll typically call queries “queries” instead of SELECTstatements Call querieswhatever you prefer, but keep in mind that queries are special elements of SQL
Performing Simple Queries
Using two keywords, SELECTand FROM, here’s the simplest possible query that gets all thedata from the Employeestable:
select
*from
employees
C H A P T E R 3 ■ I N T R O D U C I N G S Q L
44
777Xch03final.qxd 11/18/06 3:40 PM Page 44
Trang 19The asterisk (*) means you want to select all the columns in the table If you runthis query against the Northwind database, you’ll get all the rows and columns in the
Employeestable
■ Tip Although most of the SQL you’ll see in this book is short and sweet, statements, and especially
queries, can be complex and require many lines (in extreme cases, hundreds) of code Formatting SQL as
carefully as you format C# code is an excellent coding practice
Try It Out: Running a Simple Query
We’ll use SSMSE to submit queries, since it’s a convenient interface to SQL Server Here’s
how to submit a query to retrieve all employee data:
1. Open SSMSE and select the Northwind database (see Figure 3-1)
2. Click New Query A tabbed window opens, in which you can enter SQL Note thatthe status bar below the window shows that you’re connected to the Northwinddatabase (see Figure 3-2)
C H A P T E R 3 ■ I N T R O D U C I N G S Q L 45
Figure 3-1.Selecting a database to query
Trang 203. Enter the query as shown in Figure 3-3, then click Execute (or press F5 or selectQuery ➤Execute).
C H A P T E R 3 ■ I N T R O D U C I N G S Q L
46
Figure 3-2.A SQL edit window
Figure 3-3.Writing a query
777Xch03final.qxd 11/18/06 3:40 PM Page 46
Trang 214. A Results window should open, as in Figure 3-4 Note that the status bar indicatesthe query was successful and shows how many rows (nine) were retrieved.
How It Works
You asked the database to return the data for all columns, which is exactly what has
hap-pened If you scroll to the right, you’ll find all the columns in the Employeestable
Most of the time, you should limit queries to only relevant columns When you selectcolumns you don’t need, you waste resources To select columns explicitly, enter the col-
umn names after the SELECTkeyword (see Figure 3-5):
select
employeeid,firstname,lastnamefrom
employees
This query selects all the rows from the Employeestable but only the EmployeeId,
FirstName, and LastNamecolumns (Use the splitter between the query and result windows
to expand/contract them.)
C H A P T E R 3 ■ I N T R O D U C I N G S Q L 47
Figure 3-4.Query Results window
Trang 22Using the WHERE Clause
Queries can haveWHEREclauses The WHEREclause allows you to specify criteria for ing rows This clause can be complex, but we’ll stick to a simple example for now Thesyntax for our example is
select-WHERE <column1> <operator> <column2>
where <operator>is a comparison operator (for example, =,<>,>, or <) See Table 3-1,which lists the T-SQL comparison operators
Try It Out: Refining Your Query
To refine your query:
1. Add the following WHEREclause to the query in Figure 3-5:
wherecountry = 'USA'
2. Run the query by pressing F5, and you should see results as in Figure 3-6
C H A P T E R 3 ■ I N T R O D U C I N G S Q L
48
Figure 3-5.Selecting specific columns
777Xch03final.qxd 11/18/06 3:40 PM Page 48
Trang 23■ Caution SQL keywords and table and column names aren’t case sensitive, but string literals (enclosed
in single quotes) are So, use 'USA', not 'usa', for this example
How It Works
The new query means: Return the data for columns EmployeeID,FirstName, and LastName
from the Employeestable, but only for rows where the Countrycolumn equals USA
Using Comparison Operators in a WHERE Clause
You can use a number of different comparison operators in a WHEREclause (see Table 3-1)
C H A P T E R 3 ■ I N T R O D U C I N G S Q L 49
Figure 3-6.Using aWHEREclause
Trang 24Table 3-1.Comparison Operators
> Greater than EmployeeID > 1
<= Less than or equal to EmployeeID <= 1
>= Greater than or equal to EmployeeID >= 1=
<>,!= Not equal to EmployeeID <> 1
!< Not less than EmployeeID !< 1
!> Not greater than EmployeeID !> 1
■ Tip As mentioned earlier, every database vendor has its own implementation of SQL This discussion isspecific to T-SQL; for example, standard SQL doesn’t have the !=operator and calls <>the not equals oper- ator In fact, standard SQL calls the expressions in a WHEREclause predicates; we’ll use that term because
predicates are either true or false, but other expressions don’t have to be If you work with another version
of SQL, please refer to its documentation for specifics
In addition to these operators, the LIKEoperator (see Table 3-2) allows you to matchpatterns in character data As with all SQL character data, strings must be enclosed insingle quotes (')
Table 3-2.The LIKEOperator
LIKE Allows you to specify a pattern WHERE Title LIKE 'Sales%' selects all rows where
the Title column contains a value that starts with the wordSales followed by zero or more
Trang 25Table 3-3.Wildcard Characters
Wildcard Description
% Any combination of characters.
_ Any one character WHERE Title LIKE '_ales' selects all rows where the Title
column equals Aales, aales, Bales, bales, and so on.
[ ] A single character within a range [a-d] or set [abcd] WHERE Title LIKE '[bs]ales'
selects all rows where the Title column equals either the word bales or sales.
[^] A single character not within a range [^a-d] or set [^abcd].
Sometimes it’s useful to select rows where a value is unknown When no value hasbeen assigned to a column, the column is NULL (This isn’t the same as a column that con-
tains the value 0or a blank.) To select a row with a column that’s NULL, use the IS NULL
operator (see Table 3-4)
Table 3-4.The IS [NOT] NULLOperator
IS NULL Allows you to select rows where WHERE Region IS NULL returns all rows where
a column has no value Region has no value.
IS NOT NULL Allows you to select rows where WHERE Region IS NOT NULL returns all rows
a column has a value where Region has a value.
■ Note You must use the IS NULLand IS NOT NULLoperators (collectively called the null predicate
in standard SQL) to select or exclude NULLcolumn values, respectively The following is a valid query but
always produces zero rows:SELECT * FROM employees WHERE region = NULL, because nothing
"equals" NULL (not even another NULL) If you change =to IS, the query will return rows where
regions have no value
To select values in a range or in a set, you can use the BETWEENand INoperators (seeTable 3-5)
Table 3-5.The BETWEENand INOperators
BETWEEN True if a value is within a range WHERE extension BETWEEN 400 AND 500 returns
the rows where Extension is between 400 and
500, inclusive.
IN True if a value is in a list The list WHERE city IN ('Seattle', 'London') returns
can be the result of a subquery the rows where City is either Seattle or London
C H A P T E R 3 ■ I N T R O D U C I N G S Q L 51
Trang 26Combining Predicates
Quite often you’ll need to use more than one predicate to filter your data You can use thelogical operators shown in Table 3-6
Table 3-6.SQL Logical Operators
AND Combines two expressions, evalu- WHERE (title LIKE 'Sales%' AND lastname
ating the complete expression as ='Peacock') true only if both are true.
NOT Negates a Boolean value WHERE NOT (title LIKE 'Sales%' AND lastname
= 'Peacock')
OR Combines two expressions, evalu- WHERE (title = 'Peacock' OR title = 'King')
ating the complete expression as true if either is true.
When you use these operators, it’s often a good idea to use parentheses to clarify theconditions In complex queries, this may be absolutely necessary
Sorting Data
After you’ve filtered the data you want, you can sort the data by one or more columnsand in a certain direction Since tables are unsorted by definition, the order in whichrows are retrieved by a query is unpredictable To impose an ordering, use the ORDER BY
clause:
ORDER BY <column> [ASC | DESC] {, n}
The <column>is the column that should be used to sort the result The {, n}syntaxmeans you can specify any number of columns separated by commas The result will besorted in the order in which you specify the columns
The following are the two sort directions:
• ASC: Ascending (1, 2, 3, 4, and so on)
• DESC: Descending (10, 9, 8, 7, and so on)
If you omit the ASCor DESCkeywords, the sort order defaults to ASC
C H A P T E R 3 ■ I N T R O D U C I N G S Q L
52
777Xch03final.qxd 11/18/06 3:40 PM Page 52