Professional Documents
Culture Documents
Start an instance
As an instance owner on the host running db2, issue the following command
$ db2start
$ db2stop
$ db2
Create a table
NAME VARCHAR(9),
HIREDATE DATE,
SALARY DECIMAL(7,2),
COMM DECIMAL(7,2),
http://users.sdsc.edu/~jrowley/db2/howto.html Page 1 of 11
DB2 Basics 10/4/09 2:18 PM
A simple version:
Create a schema
If a user has SYSADM or DBADM authority, then the user can create a schema with any valid name. When
a database is created, IMPLICIT_SCHEMA authority is granted to PUBLIC (that is, to all users). The
following example creates a schema for an individual user with the authorization ID 'joe'
Create an alias
The following SQL statement creates an alias WORKERS for the EMPLOYEE table:
You do not require special authority to create an alias, unless the alias is in a schema other than the one
owned by your current authorization ID, in which case DBADM authority is required.
Create an Index:
The physical storage of rows in a base table is not ordered. When a row is inserted, it is placed in the most
convenient storage location that can accommodate it. When searching for rows of a table that meet a
particular selection condition and the table has no indexes, the entire table is scanned. An index optimizes
data retrieval without performing a lengthy sequential search. The following SQL statement creates a
non-unique index called LNAME from the LASTNAME column on the EMPLOYEE table, sorted in
ascending order:
The following SQL statement creates a unique index on the phone number column:
Drop a database:
Alter tablespace
Adding a Container to a DMS Table Space You can increase the size of a DMS table space (that is, one
created with the MANAGED BY DATABASE clause) by adding one or more containers to the table
http://users.sdsc.edu/~jrowley/db2/howto.html Page 2 of 11
DB2 Basics 10/4/09 2:18 PM
space. The following example illustrates how to add two new device containers (each with 40 000 pages) to
a table space on a UNIX-based system:
You can reuse the containers in an empty table space by dropping the table space but you must COMMIT
the DROP TABLESPACE command, or have had AUTOCOMMIT on, before attempting to reuse the
containers. The following SQL statement creates a new temporary table space called TEMPSPACE2:
Once TEMPSPACE2 is created, you can then drop the original temporary table space TEMPSPACE1 with
the command: DROP TABLESPACE TEMPSPACE1
When a new column is added to an existing table, only the table description in the system catalog is
modified, so access time to the table is not affected immediately. Existing records are not physically altered
until they are modified using an UPDATE statement. When retrieving an existing row from the table, a null
or default value is provided for the new column, depending on how the new column was defined. Columns
that are added after a table is created cannot be defined as NOT NULL: they must be defined as either NOT
NULL WITH DEFAULT or as nullable. Columns can be added with an SQL statement. The following
statement uses the ALTER TABLE statement to add three columns to the EMPLOYEE table:
GrantPermissions by Users
The following example grants SELECT privileges on the EMPLOYEE table to the user HERON:
The following example grants SELECT privileges on the EMPLOYEE table to the group HERON:
http://users.sdsc.edu/~jrowley/db2/howto.html Page 3 of 11
DB2 Basics 10/4/09 2:18 PM
If a privilege has been granted to both a user and a group with the same name, you must specify the
GROUP or USER keyword when revoking the privilege. The following example revokes the SELECT
privilege on the EMPLOYEE table from the user HERON:
The first example will delete only the records with emplno field = 001 The second example deletes all the
records
Import Command
Requires one of the following options: sysadm, dbadm, control privileges on each participating table or
view, insert or select privilege, example:
http://users.sdsc.edu/~jrowley/db2/howto.html Page 4 of 11
DB2 Basics 10/4/09 2:18 PM
Load Command:
Requires the following auithority: sysadm, dbadm, or load authority on the database:
You may have to specify the full path of testfile in single quotes
Authorization Level:
sysadm
dbadm
INSERT privilege on the table when the load utility is invoked in INSERT mode, TERMINATE mode
(to terminate a previous load insert operation), or RESTART mode (to restart a previous load insert
operation)
INSERT and DELETE privilege on the table when the load utility is invoked in REPLACE mode,
TERMINATE mode (to terminate a previous load replace operation), or RESTART mode (to restart a
INSERT privilege on the exception table, if such a table is used as part of the load operation.
Caveat:
If you are performing a load operation and you CTRL-C out of it, the tablespace is left in a load pending
state. The only way to get out of it is to reload the data with a terminate statement
Db2 list tablespaces show detail will display the tablespace is in a load pending state.
http://users.sdsc.edu/~jrowley/db2/howto.html Page 5 of 11
DB2 Basics 10/4/09 2:18 PM
If you break out of the load illegally (ctrl-c), the tablespace is left load pending.
To correct:
This will return the table to it's original state and roll back the entries that you started loading.
If you try to reset the tablespace with quiesce, it will not work . It's an integrety issue
DB2BATCH- command
Reads SQL statements from either a flat file or standard input, dynamically prepares and describes the
statements and returns an answer set: Authorization: sysadmin .and Required Connection -None..eg
Describes the access plan selection for static SQL statements in packages that are stored in the DB2
common server systems catalog. Given the database name, package name ,package creator abd section
number the tool interprets and describes the information in these catalogs.
db2licm -a db2entr.lic
Generates the updates statements required to make the catalog statistics of a test database match those of a
production. It is advantageous to have a test system contain asubset of your production system's data.
This tool queries the system catalogs of a database and outputs a tablespace n table index, and column
http://users.sdsc.edu/~jrowley/db2/howto.html Page 6 of 11
DB2 Basics 10/4/09 2:18 PM
information about each table in that database Authorization: Select privelege on system catalogs Required
Fname -e -m -c -r -h
where -s : generate a postscript file, -g a graph , -a for all users in the database, -t limits output to a
particular tablename, -p plain text format , -m runs program in mimic mode, examples:
Tablespace ID = 0
Name = SYSCATSPACE
State = 0x0000
Detailed explanation:
Normal
http://users.sdsc.edu/~jrowley/db2/howto.html Page 7 of 11
DB2 Basics 10/4/09 2:18 PM
Number of containers = 1
Authorization - none , Required connection none, syntax db2tbst tabpespace-state:The state value is part of
the output of list tablespaces example
Defining this environment variable with the database you want to connect to automatically connects you to
the database . example setenv db2dbdft sample will allow you to connect to sample by default.
db2 starts the command line processor. The clp is used to execute database utilities, sql statements and
online help. It offers a variety of command options and can be started in :
MAXFILOP = 64 2 - 9150
tcpip ..... not started up properly Check the DB2COMM variable if it it is set
http://users.sdsc.edu/~jrowley/db2/howto.html Page 8 of 11
DB2 Basics 10/4/09 2:18 PM
db2stop
db2start
Connections to db fails:
Move the db2diag.log from the sqllib/db2dump directory to some other working directory ( mv db2diag.log
<some other working directory )
db2stop
db2start
db2trc on -l 8000000 -e 10
db2trc off
http://users.sdsc.edu/~jrowley/db2/howto.html Page 9 of 11
DB2 Basics 10/4/09 2:18 PM
On server: db2 => revoke connect , createtab, bindadd on database from public
SQL1060N User "TECHSTU " does not have the CONNECT privilege. SQLSTATE=08004
On server:
Now on the client, I can connect as a student, list tables but not select. I
To prevent this:
On server
On server:
dbadm authority:
http://users.sdsc.edu/~jrowley/db2/howto.html Page 10 of 11
DB2 Basics 10/4/09 2:18 PM
select
TABNAME,DELETEAUTH,INSERTAUTH,SELECTAUTH from
syscat.tabauth
to group ugrad
Db2 -f filename.clp
Db2 +c -v +t infile .. The option can be prefixed by a + sign or turned on by a letter with a -sign
http://users.sdsc.edu/~jrowley/db2/howto.html Page 11 of 11