Alternatively, if Control Center is open, choose Tools > Command Editor or click.. To start the db2command-line processor in Windows: ◆ At a command prompt, type db2cmdand then press Ent
Trang 1IBM DB2
IBM DB2 is a commercial DBMS that sup-ports very large databases and numbers of transactions It runs on many operating sys-tems and hardware platforms and is com-plex enough to require a full-time database administrator (DBA) to run and maintain it Learn about DB2 products at
www.ibm.com/db2and download a free 90-day trial copy of DB2 or a (permanently) free copy of IBM DB2 Express-C
This book covers DB2 9.5 but also includes tips for earlier versions, back to 8.0 To deter-mine which version of DB2 you’re running, run the DB2 command-line command db2level
or run the query SELECT service_level FROM SYSIBMADM.ENV_INST_INFO;
To run SQL programs, use the Command
Center graphical tool in DB2 8.x, the
Command Editor graphical tool in DB2 9.x,
or the db2command-line processor (CLP)
Trang 2To use Command Center (DB2 8. x):
1. Start Command Center
This procedure varies by platform
In Windows, for example, choose Start > All Programs > IBM DB2 > Command Line Tools > Command Center In Unix and Linux, use the command db2cctr Alternatively, if Control Center is open, choose Tools > Command Center or click
2. Choose the command-type SQL Statements and DB2 CLP Commands
3. Click the Interactive tab
4. In the Database Connection box, click , expand the object tree until you find the Databases folder, select a database, and then click OK
5. To run SQL interactively, in the Command box, type or paste an SQL
statement (Figure 1.27).
or
To run an SQL script, click the Scripts tab, choose Script > Import, navigate to and select the script file, and then click OK
6. Click or press Ctrl+Enter
or
Choose Interactive > Execute or Script > Execute
Interactive-SQL results appear in the
Results tab (Figure 1.28) SQL-script
Command Center.
Trang 3To use Command Editor (DB2 9. x):
1. Start Command Editor
This procedure varies by platform
In Windows, for example, choose
Start > All Programs > IBM DB2 >
[db2_copy_name] > Command Line
Tools > Command Editor In Unix and
Linux, use the command db2ce
Alternatively, if Control Center is open,
choose Tools > Command Editor or
click
2. On the Commands tab, choose a
data-base in the Target list
If the desired database doesn’t appear in
the Target list, click Add (to the right of
the list), choose a database, and then
click OK
3. To run SQL interactively, type or paste an
SQL statement in the top box of the
Commands tab (Figure 1.29).
or
To run an SQL script, choose Selected >
Open (or press Ctrl+O), navigate to and
select the script file, and then click OK
4. Click or press Ctrl+Enter
or
Choose Selected > Execute
Command Editor displays the results in
the Query Results tab (Figure 1.30) The
bottom box of the Commands tab shows
query-processing information
Command Editor.
Trang 4For technical reasons involving parent and
child processes, (only) Microsoft Windows
users must start the db2command-line
processor with a special preliminary step
To start the db2command-line
processor in Windows:
◆ At a command prompt, type db2cmdand then press Enter
or
For DB2 8.x, choose Start > All Programs >
IBM DB2 > Command Line Tools >
Command Window
or
For DB2 9.x, choose Start > All Programs > IBM DB2 > [db2_copy_name] >
Command Line Tools > Command
Window
A new DB2 CLP command-prompt win-dow appears
✔ Tips
■ You must use the DB2 CLP window for
alldb2commands (described next) If
you try to run db2at a regular Windows
command prompt, DB2 responds with
the error “Command line environment
not initialized.”
■ If you launch a new DB2 CLP window via the db2cmdcommand, you can close the
original command-prompt window
■ In the DB2 CLP window, change (cd)
your working directory if necessary
before you run db2commands
Trang 5To use the db2command-line
processor interactively:
1. At a command prompt, type
db2 -t
and then press Enter The -toption
tells db2that a semicolon (;) terminates
statements
Thedb2 =>prompt appears
2. At the db2prompt, type
connect to dbname;
and then press Enter dbname is the
name of the database to use
3. 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.31).
✔ Tip
■ Alternatively, you can avoid the db2 =>
prompt by typing commands and SQL
statements right on the command line
For example:
db2 connect to books
db2 SELECT * FROM authors
If you omit the -toption, as here, don’t
terminate commands and SQL
state-ments with a semicolon
interactive mode.
Trang 6To use the db2command-line processor in script mode:
1. At a command prompt, type:
db2 connect to dbname
dbname is the name of the database
to use
2. At a command prompt, type:
db2 -t -f sql_script
sql_script is a text file containing SQL
statement(s) and can include an absolute
or relative pathname The -toption tells db2that a semicolon (;) terminates statements
3. Press Enter to display the results
(Figure 1.32).
✔ Tips
■ In step 2, add the option -vif you want
to echo the contents of sql_script in the
output
■ An alternative script tool is db2batch
mode.
Trang 7To exit the db2command-line tool:
◆ At the db2prompt, type quit;and then
press Enter (Omit the semicolon if
you didn’t use the -toption when you
started db2.)
To show db2command-line options:
◆ At a command prompt, type db2 ?and
then press Enter
This command displays a few pages that
speed by quickly To view one page at a
time, instead type db2 ? | moreand then
press Enter Tap the spacebar to advance
pages (Figure 1.33).
✔ Tips
■ To get help while you’re at a db2 =>
prompt, type ?;and then press Enter
(Omit the semicolon if you didn’t use
the -toption when you started db2.)
■ DB2 for Linux, Unix, and Windows
(“LUW” in IBM-speak) differs somewhat
from DB2 on other platforms This
sec-tion covers LUW DB2 If you’re running
non-LUW DB2 (on z/OS or OS/390, for
example), your commands might differ
Trang 8MySQL (pronounced my-es-kyoo-el) is an
open-source DBMS that supports large data-bases and numbers of transactions MySQL
is known for its speed and ease of use It’s free for personal use and runs on many operating systems and hardware platforms
You can download it at www.mysql.com This book covers MySQL 5.1 but also includes tips for earlier versions, back to 4.0
To determine which version of MySQL you’re running, run the MySQL command-line command mysql -Vor run the query
SELECT VERSION();
To run SQL programs, use the mysql
command-line tool
✔ Tip
■ To open a command prompt in Windows, choose Start > All Programs > Accessories > Command Prompt
To use the mysqlcommand-line tool interactively:
1. At a command prompt, type:
mysql -h host -u user -p dbname
host is the host name, user is your MySQL user name, and dbname is the name of
the database to use MySQL will prompt you for your password (for a passwordless user, either omit the -poption or press Enter at the password prompt)
2. Type an SQL statement The statement
interactive mode.
Trang 9To use the mysqlcommand-line tool
in script mode:
1. At a command prompt, type:
mysql -h host -u user -p -t
➞ dbname < sql_script
host is the host name, user is your
MySQL user name, and dbname is the
name of the database to use MySQL
will prompt you for your password (for
a passwordless user, either omit the -p
option or press Enter at the password
prompt) The -toption formats the
results as a table; omit this option if you
want tab-delimited output dbname is
the name of the database to use The <
redirection operator reads from the file
sql_script, which 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.35).
mode.
Trang 10To exit the mysqlcommand-line tool:
◆ Type quitor\qand then press Enter
To show mysqlcommand-line options:
◆ At a command prompt, type mysql -?
and then press Enter
This command displays a few pages that speed by To view one page at a time, type mysql -? | moreand then press Enter Tap the spacebar to advance pages
(Figure 1.36).
✔ Tips
■ If MySQL is running on a remote net-work computer, ask your database administrator (DBA) for the connection parameters If you’re running MySQL locally (that is, on your own computer),
then set host tolocalhost, set user to
root, and use the password you assigned
torootwhen you set up MySQL
■ As an alternative to the command prompt, you can use the graphical tools
at www.mysql.com/products/tools
■ You can learn more about open-source software at www.opensource.org