Tải bản đầy đủ (.pdf) (10 trang)

SQL VISUAL QUICKSTART GUIDE- P4 ppsx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (336.84 KB, 10 trang )

Microsoft SQL Server
Microsoft SQL Server is a commercial
DBMS that supports very large databases
and numbers of transactions. It runs on
only Microsoft Windows operating systems
and is complex enough to require a full-
time database administrator (DBA) to run
and maintain it.
Learn about SQL Server products at
www.microsoft.com/sql
and download a
free 180-day trial copy of SQL Server or a
(permanently) free copy of SQL Server
Express Edition.
This book covers Microsoft SQL Server 2008
but also includes tips for 2000 and 2005.
To determine which version of Microsoft
SQL Server you’re running, run the SQL
Server command-line command
osql -E
-Q "SELECT @@VERSION;"
or run the query
SELECT SERVERPROPERTY('ProductVersion');
or
SELECT @@VERSION;
.
✔ Tip

You can use the
SET ANSI_DEFAULTS ON
option to make SQL Server conform to


standard SQL more closely.
10
Chapter 1
Microsoft SQL Server
SQL Server 2000, 2005,
and 2008
If you’re upgrading from SQL Server 2000
to 2005/2008, here are a few things to
know about running SQL programs:

SQL Server 2005 and later support
some standard SQL features that 2000
doesn’t (such as the
EXCEPT
and
INTERSECT
operators, described in
Chapter 9), but most of the SQL
examples in this book will run the
same in 2000, 2005, and 2008. If an
example doesn’t run in all versions,
look for a DBMS Tip.

SQL Server 2005/2008’s SQL Server
Management Studio Query Editor
replaces 2000’s SQL Query Analyzer.

SQL Server 2005/2008’s
sqlcmd
command-line tool replaces 2000’s

osql
. The
sqlcmd
tool has many of
the same command-line options as
osql
(and
osql
is still available in
2005/2008 for backward compati-
bility). Run
sqlcmd -?
to show the
syntax summary.

SQL Server Express Edition is a free,
easy-to-use, lightweight version of
SQL Server 2005/2008. SQL Server
Management Studio Express is a com-
panion graphical management tool,
available as a separate download or
bundled with SQL Server Express
Edition.
SQL Server 2000
To run SQL programs in SQL Server 2000,
use the SQL Query Analyzer graphical tool
or the
osql
command-line tool.
To use SQL Query Analyzer:

1.
On the Windows desktop, choose Start >
All Programs > Microsoft SQL Server >
Query Analyzer.
2.
In the Connect to SQL Server dialog box,
select the server and authentication
mode; then click OK.
3.
On the toolbar (near the top edge of the
window), select a database in the drop-
down list (Figure 1.16).
4.
To run SQL interactively, type or paste an
SQL statement in the query window.
or
To run an SQL script, choose File > Open
(or press Ctrl+Shift+P); navigate to and
select the script file; then click Open.
5.
Choose Query > Execute (or press F5).
SQL Query Analyzer displays the results
in the bottom pane (Figure 1.17).
✔ Tip

You also can run
isqlw
at a command
prompt to launch SQL Query Analyzer.
11

DBMS Specifics
Microsoft SQL Server
Figure 1.16 SQL Query
Analyzer uses the
selected database to
resolve references in
your SQL statements.
Figure 1.17 The results of a
SELECT
statement in SQL Query Analyzer.
To use the osql command-line tool
interactively:
1.
At a command prompt, type:
osql -E -d
dbname
The
-E
option tells SQL Server to use a
trusted connection instead of requesting
a password. dbname is the name of the
database to use.
2.
Type an SQL statement. The statement
can span multiple lines. Terminate it
with a semicolon (;) and then press Enter.
3.
Type
go
and then press Enter to display

the results (Figure 1.18).
To use the osql command-line tool in
script mode:
1.
At a command prompt, type:
osql -E -d
dbname
-n -i
sql_script
The
-E
option tells SQL Server to use a
trusted connection instead of requesting
a password. dbname is the name of the
database to use. The
-n
option suppresses
numbering and prompt symbols (
>
) in the
output. sql_script is a text file containing
SQL statement(s) and can include an
absolute or relative pathname.
2.
Press Enter to display the results (Figure
1.19).
12
Chapter 1
Microsoft SQL Server
Figure 1.18 The same

SELECT
statement
in
osql
interactive mode.
Figure 1.19 The same
SELECT
statement in
osql
script mode.
To exit the osql command-line tool:

Type
exit
or
quit
and then press Enter.
To show osql command-line options:

At a command prompt, type
osql -?
and
then press Enter.
✔ Tips

If SQL Server makes you specify a user
name and password instead of using a
trusted connection, replace the
-E
option

with
-U
login_id
. login_id is your user
name.
osql
will prompt you for your
password.

If SQL Server is running on a remote
network computer, add the option
-S
server
to specify the SQL Server
instance to connect to. Ask your DBA
for the connection parameters. (The
-S
option also works for local connections,
when SQL Server is running on your
own PC rather than on some server
elsewhere.)
13
DBMS Specifics
Microsoft SQL Server
SQL Server 2005/2008
To run SQL programs in SQL Server 2005
and 2008, use the SQL Server Management
Studio graphical tool or the
sqlcmd
command-line tool.

To use SQL Server Management
Studio:
1.
On the Windows desktop, choose Start >
All Programs > Microsoft SQL Server >
SQL Server Management Studio.
In SQL Server Express Edition, the pro-
gram is named SQL Server Management
Studio Express.
2.
In the Connect to Server dialog box,
select the server and authentication
mode; then click Connect.
3.
In Object Explorer (the left pane),
expand the Databases folder of the server
that you’re using and then select a data-
base (Figure 1.20).
If Object Explorer isn’t visible, choose
View > Object Explorer (or press F8).
4.
To run SQL interactively, click
New Query (on the toolbar) or right-click
the database (in Object Explorer) and
choose New Query; then type or paste an
SQL statement in the empty tab that
appears in the right pane.
or
To run an SQL script, choose File >
Open > File (or press Ctrl+O); navigate to

and select the script file; then click Open.
The file’s contents appear in a new tab in
the right pane.
5.
Click Execute (on the toolbar) or
choose Query > Execute (or press F5).
SQL Server displays the results in the
bottom pane (Figure 1.21).
14
Chapter 1
Microsoft SQL Server
Figure 1.20 SQL Server Management Studio
uses the selected database to resolve
references in your SQL statements.
Figure 1.21 The results of a
SELECT
statement in
SQL Server Management Studio.
To use the sqlcmd command-line tool
interactively:
1.
At a command prompt, type:
sqlcmd -d
dbname
dbname is the name of the database
to use.
2.
Type an SQL statement. The statement
can span multiple lines. Terminate it
with a semicolon (;) and then press Enter.

3.
Type
go
and then press Enter to display
the results (Figure 1.22).
To use the sqlcmd command-line tool
in script mode:
1.
At a command prompt, type:
sqlcmd -d
dbname
-i
sql_script
dbname is the name of the database to
use. sql_script is a text file containing
SQL statement(s) and can include an
absolute or relative pathname.
2.
Press Enter to display the results
(Figure 1.23).
To exit the sqlcmd command-line tool:

Type
exit
or
quit
and then press Enter.
To show sqlcmd command-line
options:


At a command prompt, type
sqlcmd -?
and then press Enter.
continues on next page
15
DBMS Specifics
Microsoft SQL Server
Figure 1.22 The same
SELECT
statement in
sqlcmd
interactive mode.
Figure 1.23 The same
SELECT
statement in
sqlcmd
script mode.
✔ Tips

sqlcmd
tries to use a trusted connection
by default. If instead you have to specify
a user name and password, add the
option
-U
login_id
. login_id is your user
name.
sqlcmd
will prompt you for your

password.

If SQL Server is running on a remote
network computer, add the option
-S
server
to specify the SQL Server
instance to connect to. Ask your DBA
for the connection parameters. (The
-S
option also works for local connec-
tions, when SQL Server is running on
your own PC rather than on some server
elsewhere.)
16
Chapter 1
Microsoft SQL Server
Oracle
Oracle Database is a commercial DBMS that
supports very large databases and numbers
of transactions. It runs on many operating
systems and hardware platforms and is com-
plex enough to require a full-time database
administrator (DBA) to run and maintain it.
Learn about Oracle products at
www.oracle.com
and download Oracle
Express Edition (XE)—a free, starter version
of Oracle Database. Documentation is at
www.oracle.com/technology/documentation

.
This book covers Oracle 11g but also
includes tips for 10g, 9i, and 8i. The version
of Oracle that you’re running is displayed in
the initial “Connected to” message that
appears when you log on to SQL*Plus (or run
the query
SELECT banner FROM v$version;
).
To run SQL programs, use the SQL*Plus
(
sqlplus
) command-line tool.
✔ Tip

To open a command prompt in
Windows, choose Start > All Programs >
Accessories > Command Prompt.
17
DBMS Specifics
Oracle
To use the sqlplus command-line
tool interactively:
1.
At a command prompt, type:
sqlplus
user/password@dbname
user is your Oracle user name, password
is your password, and dbname is the
name of the database to connect to. For

security, you can omit the password and
instead type:
sqlplus
user@dbname
SQL*Plus will prompt you for your pass-
word.
2.
Type an SQL statement. The statement
can span multiple lines. Terminate it
with a semicolon (;) and then press Enter
to display the results (Figure 1.24).
To use the sqlplus command-line
tool in script mode:

At a command prompt, type:
sqlplus
user/password@dbname

@sql_script
user is your Oracle user name, password
is your password, dbname is the name of
the database to connect to, and
sql_script is a text file containing SQL
statement(s) and can include an absolute
or relative pathname. For security, you
can omit the password, and instead type:
sqlplus
user@dbname @sql_script
SQL*Plus will prompt you for your pass-
word (Figure 1.25).

18
Chapter 1
Oracle
Figure 1.24 The results of a
SELECT
statement in
sqlplus
interactive mode.
Figure 1.25 The same
SELECT
statement in
sqlplus
script mode.
To exit the sqlplus command-line
tool:

Type
exit
or
quit
and then press Enter.
To show sqlplus command-line
options:

At a command prompt, type
sqlplus -H
and then press Enter.
This command displays a few pages that
speed by. To view one page at a time,
type

sqlplus -H | more
and then press
Enter. Tap the spacebar to advance pages
(Figure 1.26).
✔ Tips

If you’re running Oracle locally, you can
use the user name
system
and the pass-
word you specified when you created the
database:
sqlplus system@
dbname
If you’re connecting to a remote Oracle
database, ask your DBA for the connec-
tion parameters.

An alternative way to open SQL*Plus in
Windows: Choose Start > All Programs >
Oracle > Application Development >
SQL Plus.
19
DBMS Specifics
Oracle
Figure 1.26 The
sqlplus
help screen.

×