Graphical tools do have a few advantages, though: ◆ Full clipboard support for cut, copy, and paste ◆ Boundless horizontal and vertical scrolling ◆ Column widths that you can change by d
Trang 1What You’ll Need
To replicate this book’s examples on your
own computer, you’ll need:
◆ A text editor
◆ The sample database
◆ A database management system
A text editor Typing short or ad-hoc
interactive SQL statements at a prompt is
convenient, but you’ll want to store nontrivial
SQL programs in text files A text editor is a
program that you use to open, create, and
edit text files, which contain only printable
letters, numbers, and symbols—no fonts,
formatting, invisible codes, colors, graphics,
or any of the clutter usually associated with
a word processor Every operating system
includes a free text editor Windows has
Notepad, Unix has vi and emacs, and Mac
OS X has TextEdit, for example By
conven-tion, SQL files have the filename extension
.sql, but you can use txt (or any extension)
if you prefer
✔ Tips
■ Windows users might want to forgo
Notepad for a better alternative such
as TextPad ($30 U.S.; www.textpad.com),
EditPlus ($30 U.S.; www.editplus.com),
or Vim (free; www.vim.org)
■ You can type SQL programs in a word
processor such as Microsoft Word and
save them as text-only files, but that
The sample database Most examples in
this book use the same database, described
in “The Sample Database” in Chapter 2
To build the sample database, follow the instructions in “Creating the Sample Database” in Chapter 2 If you’re working with a production-server DBMS, you might need permission from your database admin-istrator to run SQL programs that create and update data and database objects
A database management system How do
you get SQL? You don’t—you get a DBMS that understands SQL and feed it an SQL program The DBMS runs your program and displays the results, as described in the next chapter
Introduction
Trang 2You need a database management system to run SQL programs You can have your own private copy of a DBMS running on your desktop (local) computer, or you can use a shared DBMS over a network In the latter case, you use your desktop computer to con-nect to a DBMS server running on another machine The computer where the DBMS is
running is called a host.
Because this book is about SQL and not DBMSs, I won’t rehash the instructions for installing and configuring database software This evasion might seem like a brush-off at first glance, but setting up a DBMS varies by vendor, product, version, edition, and oper-ating system All DBMSs come with exten-sive installation, administration, reference, and tutorial documentation (To give you an idea, just the installation manual for Oracle runs more than 300 pages.)
DBMS Specifics
1
Trang 3Running SQL Programs
In this chapter, I’ll describe how to run SQL
programs on these DBMSs:
◆ Microsoft Access 2007
◆ Microsoft SQL Server 2008
◆ Oracle 11g
◆ IBM DB2 9.5
◆ MySQL 5.1
◆ PostgreSQL 8.3
These systems are the most popular
com-mercial and open-source DBMSs I tested
the SQL examples in this book with the
indi-cated releases The examples will work with
later versions but not necessarily with earlier
ones SQL-standard conformance usually
improves in successive releases
Microsoft Access’s graphical interface lets
you run only one SQL statement at a time
The other systems, all DBMS servers, let you
run SQL programs in interactive mode or
script mode In interactive mode, you type
individual SQL statements at a command
prompt and view the results of each
state-ment separately, so input and output are
interleaved In script mode (also called batch
mode), you save your entire SQL program in
a text file (called a script or a batch file), and
a command-line tool takes the file, executes
the program, and returns the results without
your intervention I use the sample database
Chapter 1
The Command Line
Most database professionals prefer to submit commands and SQL scripts through
a DBMS’s command-line environment rather than mousing around the menus and windows of a graphical front-end (Database administrators don’t add 1,000 users by pointing and clicking.) If you’re new to DBMSs, you might find the com-mand line to be cryptic and intimidating, but experience will show you its power, simplicity, and speed Graphical tools do have a few advantages, though:
◆ Full clipboard support for cut, copy, and paste
◆ Boundless horizontal and vertical scrolling
◆ Column widths that you can change
by dragging with the mouse
◆ Better history of the commands and results
Listing 1.1 This file, named listing0101.sql , contains
a simple SQL SELECT statement, which I’ll use to query the sample database in subsequent DBMS examples.
SELECT au_fname, au_lname FROM authors
ORDER BY au_lname;
Listing
Trang 4✔ Tips
■ When you specify the name of an SQL file in script mode, you can include an absolute or relative pathname (see the sidebar in this section)
■ To run a command-line tool from any particular directory (folder), your path must include the directory that actually
contains the tool A path is a list of
direc-tories that the OS searches for programs For some DBMSs, the installer handles the path details; for others, you must add the tool’s directory to your path yourself
To view your path, type path(Windows) or echo $PATH(Unix or Mac OS X Terminal)
at a command prompt To change your path, add the directory in which the tool resides to the path environment variable
Search Help for environment variable
(Windows), or modify the path command
in your login initialization file, usually named bash_login,.bashrc, cshrc, login, profile, or shrc(Unix or Mac OS X)
Pathnames
A pathname specifies the unique
loca-tion of a directory or file in a filesystem
hierarchy An absolute pathname specifies
a location completely, starting at the
top-most node of the directory tree, called
the root A relative pathname specifies a
location relative to the current (or
work-ing) directory In Windows, an absolute
path starts with a backslash (\) or with a
drive letter followed by a colon and a
backslash In Unix or Mac OS X Terminal,
an absolute path starts with a slash (/)
C:\Program Files\Microsoft SQL Server
(Windows) and /usr/local/bin/mysql
(Unix) are absolute paths, for example
scripts\listing0101.sql(Windows)
anddoc/readme.txt(Unix) are relative
paths Absolute pathnames for files and
folders on a network also can begin with
a double backslash and server name
(\\someserver, for example) If a
path-name contains spaces, surround the
entire pathname with double quotes
Pathname commonly is shortened to path.
Although the difference is obvious from
context, I’ll use pathname to prevent
con-fusion with the PATHenvironment variable
Trang 5Chapter 1
Other DBMSs
FileMaker Pro (www.filemaker.com) is a desktop database program that supports a subset
of SQL You can use the SQL Query Builder tool or the Execute SQL script step to run SQL statements
Sybase (www.sybase.com) is a commercial server DBMS Sybase and Microsoft once had an agreement for sharing source code, and their DBMSs were almost identical Years ago, each company went its own way with its own product The shared heritage, however, means that almost all the SQL examples that work in Microsoft SQL Server will work in Sybase Adaptive Server as well
Teradata (www.teradata.com) is a commercial server DBMS that supports huge databases and numbers of transactions The Teradata SQL dialect largely supports ANSI SQL, so you’ll
be able to run most of the examples in this book with few or no changes
Firebird (www.firebirdsql.org) is an open-source DBMS descended from Borland’s
InterBase DBMS It’s free, supports large databases and numbers of transactions, has high conformance with ANSI SQL, and runs on many operating systems and hardware platforms
SQLite (www.sqlite.org) is an open-source DBMS database engine It’s free, supports large databases and numbers of transactions, has moderate conformance with ANSI SQL, and runs
on many operating systems and hardware platforms Applications that access SQLite data-bases read and write directly from the database files on disk, with no intermediary server
SAS (www.sas.com) is a commercial statistical and data-warehousing system Even though SAS isn’t a relational DBMS, you can use ANSI or DBMS-specific SQL to import and export SAS data via PROC SQL or SAS/Access A SAS dataset is equivalent to an SQL table, an observation to an SQL row, and a variable to an SQL column
You can find more information and useful links at http://en.wikipedia.org/wiki/
Comparison_of_relational_database_management_systems, “Comparison of Relational Database Management Systems.”
Trang 6Microsoft Access
Microsoft Access is a commercial desktop DBMS that supports small and medium-size databases Learn about Access at
www.microsoft.com/office/accessand download a free 60-day trial copy
This book covers Microsoft Access 2007 but also includes tips for 2000, 2002 (also known
as Access XP), and 2003 To determine which version of Access you’re running, in Access 2003 or earlier, choose Help > About Microsoft Office Access In Access 2007 or later, choose Microsoft Office button >
Access Options > Resources (in the left pane) > About
In Access, you must turn on ANSI-92 SQL syntax to run many of the examples in this book
To turn on ANSI-92 SQL syntax for a database:
1. In Access, open the database if necessary
2. In Access 2003 or earlier, choose Tools > Options > Tables/Queries tab
or
In Access 2007 or later, choose Microsoft Office Button > Access Options > Object Designers (in the left pane)
3. Below SQL Server Compatible Syntax (ANSI 92), check This Database
(Figure 1.1).
4. Click OK
Access closes, compacts, and then reopens the database before the new set-ting takes effect You may see a few warn-ings, depending on your security settings
ANSI-89 vs ANSI-92 SQL
Be careful switching between ANSI-89
(the default for Access) and ANSI-92 SQL
syntax mode The modes aren’t
compati-ble, so you should pick a mode when you
create a database and never change it
The range of data types, reserved words,
and wildcard characters differs by mode,
so SQL statements created in one mode
might not work in the other The older
ANSI-89 standard is limited compared
with ANSI-92, so you should choose
ANSI-92 syntax for new databases For
more information, see “SQL Standards
and Conformance” in Chapter 3
If you’re using Access as a
front-end to query a Microsoft SQL
Server database, you must use ANSI-92
syntax If you’re using Access 97 or
earli-er, you’re stuck with ANSI-89
Figure 1.1 Check this box to turn on ANSI-92 SQL
syntax mode for the open database.
Trang 7If you’re a casual Access user, you’ve
proba-bly used the query design grid to create a
query When you create a query in Design
View, Access builds the equivalent SQL
statement behind the scenes for you You
can view, edit, and run the SQL statement in
SQL View
To run an SQL statement in
Access 2000, 2002, or 2003:
1. Open a database, or press F11 to switch
to the Database window for the open
database
2. In the Database window, click Queries
(below Objects), and then click New in
the toolbar (Figure 1.2).
3. In the New Query dialog box, click
Design View, and then click OK
(Figure 1.3).
4. Without adding tables or queries, click
Close in the Show Table dialog box
(Figure 1.4).
5 Choose View > SQL View (Figure 1.5).
Chapter 1
Figure 1.2 On the toolbar, click the New
button to create a new query.
Figure 1.3 Select Design View to skip the
hand-holding wizards.
Figure 1.4 You don’t need to add tables graphically
because the SQL statement specifies the tables.
Figure 1.5 SQL View
hides the graphical
Trang 86. Type or paste an SQL statement
(Figure 1.6).
7. To run the SQL statement, click on the toolbar or choose Query > Run
(Figure 1.7).
Access displays the results of a SELECT
statement (Figure 1.8) but blocks or
executes other types of SQL statements, with or without warning messages, depending on your settings
Figure 1.6 Enter an SQL
statement
Figure 1.7 and run it.
Figure 1.8 Access displays the results of
a SELECT statement.
Trang 9To run an SQL statement in
Access 2007:
1. Open a database
2. On the ribbon, choose Create tab > Other
group > Query Design (Figure 1.9).
3. Without adding tables or queries, click
Close in the Show Table dialog box
(Figure 1.10).
4. On the ribbon, choose Design tab >
Results group > SQL View (Figure 1.11).
Chapter 1
Figure 1.9 Query Design
lets you skip the hand-holding wizards.
Figure 1.11 SQL View hides the
graphical query grid and instead shows a text editor where you can type or paste an SQL statement.
Figure 1.10 You don’t need to add tables graphically
because the SQL statement specifies the tables.
Trang 105. Type or paste an SQL statement
(Figure 1.12).
6. On the ribbon, choose Design tab >
Results group > Run (Figure 1.13).
Access displays the results of a SELECT
statement (Figure 1.14) but blocks or
executes other types of SQL statements, with or without warning messages, depending on your settings
✔ Tips
■ You can run only a single SQL statement through an Access Query object To run multiple statements, use multiple Query objects or a host language such as Visual Basic or C#
■ To display a list of existing
queries in Access 2007 or later,
press F11 to show the Navigation pane (on the left), click the menu at the top of the pane and choose Object Type, and then click the menu again and choose
Queries (Figure 1.15).
Figure 1.13 and run it.
Figure 1.12 Enter an SQL statement
Figure 1.14 Access displays the results of a
SELECT statement.
Figure 1.15 The Navigation
pane, new in Access 2007, replaces the Database window in earlier Access versions (refer to Figure 1.2).