Professional Documents
Culture Documents
source /home/db2/db2user/db2env
The changes become effective after your next login or in newly opened xterms. See if everything is okay
by typing in2
$ :::::
type ::::
db2
db2 is /usr/IBMdb2/V7.1/bin/db2
$
$ ::::
db2
(c) Copyright IBM Corporation 1993,2000
Command Line Processor for DB2 SDK 7.1.0
... and some messages more ...
db2 =>
You see the DB2 command prompt, db2 =>. At this command prompt, you can type in SQL queries
and some DB2 specific database commands. Exit the command line interface with
db2 =>
where username is your Unix login name. You will get your database password in the lecture. As in SQL,
all commands and identifiers are case-insensitive.
Before you end your work with the DB2 database, you have to disconnect from the database:
db2 => :::::::::::
disconnect::::::::::
infosys
DB20000I The SQL DISCONNECT command completed successfully.
db2 =>
After that, you can exit the command line interface with terminate.
2.2.1 Schemata
In the lecture you saw simple SQL queries like SELECT * FROM Kurs. “Real” SQL (i. e. the lastest ANSI
standard), however, provides the mechanism of ‘schemata’. A schema can be seen as a ‘namespace’ or (in
Java terms) ‘package’. Different tables can exist with the same name in different schemata.
Every table (in fact, every database object), that is not in your current schema, must be specified with
a fully qualified name. A database object is fully qualified by the schema name and a dot in front of the
object identifier, like nobody.kurs. (I put the ‘KursDB’ database into the ‘nobody’ schema.)
Example:
SELECT *
FROM nobody.Teilnehmer
WHERE Ort = ’Ulm’
If you know you’ll be working in a specific schema for a while, you can set your current schema for this
database session with
db2 => :::
set::::::::::
current ::::::::
schema ::
=::::::::::::::
schema-name
From now on, the schema schema-name will be assumed if you do not fully qualify your database objects.
Example:
db2 => :::
set::::::::::
current ::::::::
schema ::
=::::::::
nobody
DB20000I The SQL command completed successfully.
db2 => :::::::
select ::*::::::
from::::::
kurs :::::::
where ::::::
titel:::
= ::::::::::::::::
’Datenbanken’
KURSNR TITEL
------ --------------------
I09 Datenbanken
1 record(s) selected.
db2 =>
You may make the option -t by setting the Unix environment variable DB2OPTIONS to ‘-t’ (e. g. in your
.bashrc.username file).
1. The DB2 Command Reference Manual is the reference to all these commands. Most of the commands
that are listed there, however, will not be available to you, as they are intended for the database
administrator. Some commands can be executed from the Unix command line, others from within the
DB2 command line interface. You can find the Command Reference Manual on the course website.
2. If you type in a question mark (?) within the command line interface, you’ll get a list of all DB2
commands. You get specific help for a single command by typing in ‘? command-name ’. Don’t forget
the space between the question mark and the command you want to have documented.
• Work with an open editor in parallel. Edit your queries in a file that you have open in your favorite
editor. To execute a query, simply use the copy/paste mechanism of your X11 system. Mark the
query text in your editor and paste it into the command line interface with the middle mouse button.
It is probably convenient to turn on the semicolon as a statement termination character for this (see
above).
• State your query on db2’s command line. The db2 utility has the ability to keep your database
connection open during its invocations. As long as you haven’t terminated or disconnected, you
can exit db2 and re-start it again without losing your database connection. To exit the db2 utility
without closing your connection, use the command quit.
If you state database commands on the shell command line when you invoke db2 these commands will
be executed and db2 will quit immediately. The following session log demonstrates this feature.
$ ::::
db2 ::::::::::
"connect:::
to::::::::::
infosys :::::
user:::::::::::
teubner"
Enter current password for teubner:
$ ::::
db2 :::::::::
"select ::*::::::
from::::::::::::::
nobody.kurs :::::::
where ::::::::
kursnr ::
= ::::::::
’G08’"
KURSNR TITEL
------ --------------------
G08 Grundlagen I
1 record(s) selected.
$ ::::
db2
(c) Copyright IBM Corporation 1993,2000
Command Line Processor for DB2 SDK 7.1.0
...
db2 => :::::::
select ::::::::::
count(*)::::::
from::::::::::::::
nobody.kurs
1
-----------
4
1 record(s) selected.
I started db2 here several times, sometimes with a query specified on the command line, once in
interactive mode.
Caution! Be careful with your shell’s command line expansion! If you do not properly quote your
query (as seen above), you will get strange results. (If you don’t understand anything here, read it as
“Always put your SQL query into double quotes (") when you specify it on the Unix command line.”)
• Use the db2batch utility. There’s a second utility from IBM, called db2batch. Intended to measure
runtime statistics, you can execute a whole file with SQL queries in batch mode. In your query file,
separate your queries with semicolons; comments start with two minus signs (--). See the DB2
Command Reference Manual or invoke db2batch with the option -h to see the options available for
the db2batch program.