Professional Documents
Culture Documents
Start an instance
As an instance owner on the host running db2, issue the following command
$ db2start
$ db2stop
Create a table
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:
CREATE UNIQUE INDEX PH ON EMPLOYEE (PHONENO DESC)
Alter tablespace
By 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 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:
ALTER TABLE EMPLOYEE ADD MIDINIT CHAR(1) NOT NULL WITH DEFAULT ADD
HIREDATE DATE ADD WORKDEPT CHAR(3)
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:
GRANT SELECT ON EMPLOYEE TO GROUP HERON
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:
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:
One of the following: sysadm , dbadm load authority on the database and
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 previous load replace operation)
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.
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 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
Number of containers = 1
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
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
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:
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