Professional Documents
Culture Documents
html
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),
1 of 14 3/29/2010 3:14 PM
DB2 Basics http://users.sdsc.edu/~jrowley/db2/howto.html
HIREDATE DATE,
SALARY DECIMAL(7,2),
COMM DECIMAL(7,2),
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
2 of 14 3/29/2010 3:14 PM
DB2 Basics http://users.sdsc.edu/~jrowley/db2/howto.html
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
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
3 of 14 3/29/2010 3:14 PM
DB2 Basics http://users.sdsc.edu/~jrowley/db2/howto.html
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:
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:
4 of 14 3/29/2010 3:14 PM
DB2 Basics http://users.sdsc.edu/~jrowley/db2/howto.html
At a minimum, you should consider restricting access to the SYSCAT.DBAUTH, SYSCAT.TABAUTH, SYSCAT.PACKAGEAUTH,
SYSCAT.INDEXAUTH, SYSCAT.COLAUTH, and SYSCAT.SCHEMAAUTH catalog views. This would prevent information on user privileges, which could
be used to target an authorization name for break-in, becoming available to everyone with access to the database. The following statement makes the view
available to every authorization name:
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:
5 of 14 3/29/2010 3:14 PM
DB2 Basics http://users.sdsc.edu/~jrowley/db2/howto.html
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.
6 of 14 3/29/2010 3:14 PM
DB2 Basics http://users.sdsc.edu/~jrowley/db2/howto.html
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.
7 of 14 3/29/2010 3:14 PM
DB2 Basics http://users.sdsc.edu/~jrowley/db2/howto.html
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
8 of 14 3/29/2010 3:14 PM
DB2 Basics http://users.sdsc.edu/~jrowley/db2/howto.html
State = 0x0000
Detailed explanation:
Normal
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
9 of 14 3/29/2010 3:14 PM
DB2 Basics http://users.sdsc.edu/~jrowley/db2/howto.html
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
10 of 14 3/29/2010 3:14 PM
DB2 Basics http://users.sdsc.edu/~jrowley/db2/howto.html
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
11 of 14 3/29/2010 3:14 PM
DB2 Basics http://users.sdsc.edu/~jrowley/db2/howto.html
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
12 of 14 3/29/2010 3:14 PM
DB2 Basics http://users.sdsc.edu/~jrowley/db2/howto.html
On server:
dbadm authority:
select
TABNAME,DELETEAUTH,INSERTAUTH,SELECTAUTH from
syscat.tabauth
to group ugrad
Db2 -f filename.clp
13 of 14 3/29/2010 3:14 PM
DB2 Basics http://users.sdsc.edu/~jrowley/db2/howto.html
Db2 +c -v +t infile .. The option can be prefixed by a + sign or turned on by a letter with a -sign
14 of 14 3/29/2010 3:14 PM