DB2

DB2 Cheat Sheet

Db2 is a family of data management products, including database servers, developed by IBM. It initially supported the relational model but was extended to support object–relational features and non-relational structures like JSON and XML.

Database Management

Create a database (default drive)DB2 CREATE DB <dbName>
Create a database on a specific driveDB2 CREATE DB <dbName> ON <Drive>
Drop a database (with caution)DB2 DROP DB <dbName>
Connect to a databaseDB2 CONNECT TO <dbName>
Disconnect from a databaseDB2 DISCONNECT FROM <dbName>

Instance Management

Create a new instanceDB2ICRT <NewInstanceName>
Drop an instance (with caution)DB2IDROP <InstanceName>
Start an instanceDB2START <InstanceName>
Stop an instanceDB2STOP <InstanceName>

DCL

Grant on a table:GRANT SELECT, INSERT ON TABLE tbl1 TO user
Grant execution on a stored procedure:GRANT EXECUTE ON PROCEDURE prc1(INT, DATE) TO USER jdoe
GRANT EXECUTE ON SPECIFIC PROCEDURE mypr TO GROUP admins
Revoke on a table:REVOKE DELETE ON TABLE mytable FROM recur

DDL

Create a schema:CREATE SCHEMA sch1
Create a table specifying the primary key:CREATE TABLE tbl1 (col1 CHAR(1) NOT NULL PRIMARY KEY)
CREATE TABLE tbl2 (col1 INT NOT NULL, col2 DATE NOT NULL, PRIMARY KEY (col1, col2))
Create a table specifying tablespaces:CREATE TABLE tbl3 (col1 INT NOT NULL, col2 CHAR(1)) IN ts1 INDEX IN ts2
Create a table specifying schema:CREATE TABLE sch1.tbl4 (col1 INT)
Create a table with auto auto-incremental column:CREATE TABLE tbl5 (col1 INT NOT NULL GENERATED AS IDENTITY)
Create a table like another one:CREATE TABLE tbl6 LIKE tbl1 IN ts1 INDEX IN ts2
Comment on table and column:COMMENT ON TABLE tbl1 IS 'Comment in table'
COMMENT ON COLUMN tbl1.col1 IS 'Description of the field'
Declare a temporary table (session schema):DECLARE GLOBAL TEMPORARY TABLE tmp1 (col1 INT, col2 DATE) ON COMMIT PRESERVE ROWS
Create a global temporary tablespace:CREATE GLOBAL TEMPORARY TABLE tmp2 (col1 INT)
Create an index:CREATE INDEX idx1 ON tbl2 (col2)
Create a unique index:CREATE UNIQUE INDEX idx2 ON tbl5 (col)
Drop an index:DROP INDEX idx1
Add a column (requires Reorg table):ALTER TABLE tbl1 ADD COLUMN col3 timestamp
Change nullability:ALTER TABLE tbl1 ALTER COLUMN col3 SET NOT NULL
Drop nullability:ALTER TABLE tbl1 ALTER COLUMN col3 DROP NOT NULL
Rename a column:ALTER TABLE tbl1 RENAME COLUMN col3 TO new3
Drop column:ALTER TABLE tbl1 DROP COLUMN new3
Create a primary key constraint:ALTER TABLE tbl5 ADD CONSTRAINT pkt5 PRIMARY KEY (col1)
Drop primary key:ALTER TABLE tbl5 DROP PRIMARY KEY
Add identity:ALTER TABLE tbl2 ALTER col1 SET GENERATED ALWAYS AS IDENTITY
Restart identity:ALTER TABLE tbl2 ALTER col1 RESTART WITH 1
Drop identity:ALTER TABLE tbl2 ALTER col1 DROP IDENTITY
Create a foreign key:ALTER TABLE tbl5 ADD CONSTRAINT fkt5 FOREIGN KEY (col1) REFERENCES tbl11 (col1)
Create a check constraint:ALTER TABLE tbl1 ADD CONSTRAINT chk CHECK (col1 in ('a', 'b', 'c'))
Enforce a constraint:ALTER TABLE tbl1 ALTER CHECK chk ENFORCED
Not enforce a constraint:ALTER TABLE tbl5 ALTER FOREIGN KEY fkt5 NOT ENFORCED
Change the granularity of the locks:ALTER TABLE tbl1 LOCKSIZE TABLE
Drop a table:DROP TABLE tbl1
Rename a table:RENAME TABLE tbl2 TO table2
Truncate a table:TRUNCATE TABLE tbl1 IMMEDIATE
Create a sequence:CREATE SEQUENCE seq AS INTEGER
Restart sequence:ALTER SEQUENCE seq RESTART WITH 15
Create a stored procedure:CREATE OR REPLACE PROCEDURE prc1 (IN val INT, OUT ret DATE) SPECIFIC mypr BEGIN SET ret = (SELECT col2 FROM tbl2 WHERE col1 = val); END @
Create a trigger:CREATE TRIGGER cp_val AFTER INSERT ON tbl1 REFERENCING NEW AS n FOR EACH ROW INSERT INTO tbl2 VALUES (n.col1, n.col2)
Create a view:CREATE VIEW vw1 AS SELECT col2 FROM tbl

General

Semi-colon separated sentencesdb2 -t
At sign-separated sentences (when there is SQL PL code)db2 -td@
Define a terminator character--#SET TERMINATOR @
List all databases (aliases)LIST DB DIRECTORY
Connect to a database (alias)CONNECT TO mydb
Disconnect from a databaseTERMINATE
Get the current timestamp from the environment (registry values).VALUES CURRENT TIMESTAMP
Get the current user from the environment (registry values).VALUES CURRENT USER
Get the current database from the environment (registry values).VALUES CURRENT SERVER
List all tablesLIST TABLES FOR SCHEMA myuser
Change current schemaSET CURRENT SCHEMA otherschema
Change the isolation level (RR, RS, CS, UR)SET ISOLATION RR
List all tablespaces with their statusLIST TABLESPACES
Describe the structure of the tableDESCRIBE TABLE tbl1
Describe the result of a queryDESCRIBE SELECT * FROM tbl1
Get help for a Db2 command? command
Get help for a SQL code (SQLXXXX)? SQLXXXX
Get help for a SQLstate (YYYYY)? YYYYY