You are on page 1of 3

Db2 Cheat Sheet for Development Cheat Sheet

by Andres Gomez Casanova (angoca) via cheatography.com/73273/cs/18485/

Db2 logo General (cont) License

List all tables​paces with their status This work is licensed under a Creative
LIST TABLES​PACES Commons Attrib​uti​on-​Sha​reAlike 4.0 Intern​‐
Describe the estructure of the table ational License.
DESCRIBE TABLE mytable
Describe the result of a query DDL
DESCRIBE SELECT * FROM mytable Create a schema
Get help for a Db2 command CREATE SCHEMA sch1
? command Create a table specifying primary key
Get help for a SQL code (SQLXXXX) or CREATE TABLE tbl1 (col1 CHAR(1)
SQLstate (YYYYY) NOT NULL PRIMARY KEY)
? SQLXXX CREATE TABLE tbl2 (col1 INT NOT
? YYYYY NULL, col2 DATE NOT NULL,
PRIMARY KEY (col1, col2))
General DCL
Create a table specifying tables​paces
Execution of a file in the console (db2clp) Grant on a table CREATE TABLE tbl3 (col1 INT NOT
 Semi-colon separated sentences: GRANT SELECT, INSERT ON TABLE NULL, col2 CHAR(1)) IN ts1 INDEX
db2 -t tbl1 TO user IN ts2
 At sign separated sentences (when there Grant execution on a stored procedure Create a table specifying schema
is SQL PL code): GRANT EXECUTE ON PROCEDURE CREATE TABLE sch1.tbl4 (col1
db2 -td@ prc1(INT, DATE) TO USER jdoe INT)
Define a terminator character GRANT EXECUTE ON SPECIFIC Create a table with auto increm​ental column
--#SET TERMINATOR @ PROCEDURE mypr TO GROUP admins CREATE TABLE tbl5 (col1 INT NOT
List all databases (aliases) Revoke on a table NULL GENERATED AS IDENTITY)
LIST DB DIRECTORY REVOKE DELETE ON TABLE mytable Create a table like another one
Connect to a database (alias) FROM recur CREATE TABLE tbl6 LIKE tbl1 IN
CONNECT TO mydb ts1 INDEX IN ts2
Disconnect from a database Source Comment on table and column
CONNECT RESET COMMENT ON TABLE tbl1 IS
Created by: Andres Gomez Casanova
TERMI​NATE 'Comment in table'
(@angoca)
Get values from the enviro​nment (registry
Version: 2019-08-04 COMMENT ON COLUMN tbl1.col1 IS
values)
Get the most recent version at 'Descr​iption of the field'
* Current timestamp
https://github.com/angoca/db2-cheat-sheet/ Declare a temporary table (session
VALUES CURRENT TIMESTAMP
schema)
 Connected user
DECLARE GLOBAL TEMPORARY TABLE
VALUES CURRENT USER
tmp1 (col1 INT, col2 DATE) ON
 Current database
COMMIT PRESERVE ROWS
VALUES CURRENT SERVER
Create a global temporary tablespace
List all tables
CREATE GLOBAL TEMPORARY TABLE
LIST TABLES
tmp2 (col1 INT)
LIST TABLES FOR SCHEMA myuser
Create an index
LIST TABLES FOR ALL
CREATE INDEX idx1 ON tbl2 (col2)
Change current schema
SET CURRENT SCHEMA others​chema
Change the isolation level
SET ISOLATION RR

By Andres Gomez Casanova Published 8th January, 2019. Sponsored by CrosswordCheats.com


(angoca) Last updated 6th August, 2019. Learn to solve cryptic crosswords!
cheatography.com/angoca/ Page 1 of 3. http://crosswordcheats.com
angoca.users.sf.net
Db2 Cheat Sheet for Development Cheat Sheet
by Andres Gomez Casanova (angoca) via cheatography.com/73273/cs/18485/

DDL (cont) DDL (cont) DML

Create a unique index ALTER TABLE tbl1 ADD CONSTRAINT Insert values on a table
CREATE UNIQUE INDEX idx2 ON tbl5 chk CHECK (col1 in ('a', 'b', INSERT INTO tbl3 VALUES (2, 'b')
(col1) 'c')) INSERT INTO tbl3 VALUES (3,
Drop an index Enforce a constraint 'c'), (4, 'd'), (5, 'e') --
DROP INDEX idx1 ALTER TABLE tbl1 ALTER CHECK chk Atomic
Add a column (requires Reorg table) ENFORCED Insert certain columns
ALTER TABLE tbl1 ADD COLUMN col3 Not enforce a constraint INSERT INTO tbl1 (col1) VALUES
timestamp ALTER TABLE tbl5 ALTER FOREIGN (6)
Change nullab​ility KEY fkt5 NOT ENFORCED Insert values from a select
ALTER TABLE tbl1 ALTER COLUMN Change the granul​arity of the locks INSERT INTO tbl6 SELECT col1
col3 SET NOT NULL ALTER TABLE tbl1 LOCKSIZE TABLE FROM tbl1
Drop nullab​ility Drop a table Insert in temporary table
ALTER TABLE tbl1 ALTER COLUMN DROP TABLE tbl1 INSERT INTO sessio​n.tmp1 VALUES
col3 DROP NOT NULL Rename a table (1)
Rename a column RENAME TABLE tbl2 TO table2 Update fields
ALTER TABLE tbl1 RENAME COLUMN Truncate a table UPDATE tbl3 SET col1 = 5, mycol2
col3 TO new3 TRUNCATE TABLE tbl1 IMMEDIATE = 'e' -–all table
Drop column Create a sequence UPDATE tbl3 SET col2 = 'd' WHERE
ALTER TABLE tbl1 DROP COLUMN CREATE SEQUENCE seq AS INTEGER col1 = 7
new3 Restart sequence Merge (upsert)
Create a primary key constraint ALTER SEQUENCE seq RESTART WITH MERGE INTO tbl3 AS t USING
ALTER TABLE tbl5 ADD CONSTRAINT 15 (SELECT col1 FROM tbl1) s ON
pkt5 PRIMARY KEY (col1) Create a stored procedure (t.col1 = s.col1) WHEN MATCHED
Drop primary key CREATE OR REPLACE PROCEDURE prc1 THEN UPDATE SET col2 = 'X' WHEN
ALTER TABLE tbl5 DROP PRIMARY (IN val INT, OUT ret DATE) NOT MATCHED THEN INSERT VALUES
KEY SPECIFIC mypr BEGIN SET ret = (10, 'X')
Add identity (SELECT col2 FROM tbl2 WHERE Delete rows
ALTER TABLE tbl2 ALTER col1 SET col1 = val); END @ DELETE FROM tbl1 -–all table
GENERATED ALWAYS AS IDENTITY Create a trigger DELETE FROM tbl1 WHERE col1 > 5
Restart identity CREATE TRIGGER cp_val AFTER Export
ALTER TABLE tbl2 ALTER col1 INSERT ON tbl1 REFERE​NCING NEW EXPORT TO myfile OF DEL SELECT *
RESTART WITH 1 AS n FOR EACH ROW INSERT INTO FROM tbl1
Drop identity tbl2 VALUES (n.col1, n.col2) Import
ALTER TABLE tbl2 ALTER col1 DROP Create a view IMPORT FROM myfile OF DEL INSERT
IDENTITY CREATE VIEW vw1 AS SELECT col2 INTO mytable1
Create a foreign key FROM tbl1 Cursor
ALTER TABLE tbl5 ADD CONSTRAINT DECLARE cur1 CURSOR FOR SELECT *
fkt5 FOREIGN KEY (col1) FROM tbl1
REFERENCES tbl11 (col1) Load
Create a check constraint LOAD FROM myfile OF DEL INSERT
INTO tbl1
LOAD FROM cur1 OF CURSOR INSERT
INTO tbl1

By Andres Gomez Casanova Published 8th January, 2019. Sponsored by CrosswordCheats.com


(angoca) Last updated 6th August, 2019. Learn to solve cryptic crosswords!
cheatography.com/angoca/ Page 2 of 3. http://crosswordcheats.com
angoca.users.sf.net
Db2 Cheat Sheet for Development Cheat Sheet
by Andres Gomez Casanova (angoca) via cheatography.com/73273/cs/18485/

DML (cont) Queries (cont)

Query the status of the load in a table Query the database catalog
LOAD QUERY TABLE tbl1 SELECT * FROM SYSCAT.TA​BLES
Set integrity SELECT * FROM SYSCAT.TA​BAUTH
SET INTEGRITY FOR tbl1 IMMEDIATE SELECT * FROM SYSCAT.RO​UTINES
CHECKED
Ingest TCL
INGEST FROM FILE myfile FORMAT
Commit changes
DELIMITED INSERT INTO tbl1 COMMIT
Get the next value from a sequence Create a savepoint
VALUES NEXT VALUE FOR seq SAVEPOINT sp1 ON ROLLBACK RETAIN
INSERT INTO tbl3 (col1) VALUES CURSORS
(NEXT VALUE FOR seq) Undo changes until savepoint
ROLLBACK TO SAVEPOINT sp1
Queries Undo changes
Put a lock at table level ROLLBACK
LOCK TABLE tbl1 IN EXCLUSIVE
MODE
Execute a query without regard of commit
rows
SELECT * FROM tbl1 WITH UR --
RR,R​S,CS
Execute a query with only 5 rows
SELECT * FROM tbl1 FETCH FIRST 5
ROWS ONLY
Perform a query to a dummy table (dual)
SELECT 'Any string' FROM
SYSIBM.SY​SDU​MMY1
Perform a query calling a function
SELECT HEX(col2) FROM tbl5
Call a function
VALUES HEX('A​nyT​ext')
Perform a cast
VALUES CAST('123' AS INTEGER)
Concat​enate
VALUES 'AnyText' || 5
VALUES 'AnyText' concat 5
Escape a single quote in a text field
VALUES 'Sinead o''Con​nor'

By Andres Gomez Casanova Published 8th January, 2019. Sponsored by CrosswordCheats.com


(angoca) Last updated 6th August, 2019. Learn to solve cryptic crosswords!
cheatography.com/angoca/ Page 3 of 3. http://crosswordcheats.com
angoca.users.sf.net

You might also like