How To/sql-db/

Get table info in DB2 IBM Database

Information about table and columns

to get information about tables and columns in DB2 database you can use those queries:

DESCRIBE TABLE TABLENAME;

generates:

describe table TABLENAME

Nazwa                          Schemat   Nazwa
kolumny            typu        typu                         Długość  Skala Puste
------------------------------ --------- ------------------ -------- ----- ------
PARAM_ID                       SYSIBM    BIGINT                    8     0 Nie
NAME                           SYSIBM    VARCHAR                 128     0 Nie
VALUE                          SYSIBM    VARCHAR                 255     0 Nie
CLASSNAME                      SYSIBM    VARCHAR                 255     0 Nie
CATENTRY_ID                    SYSIBM    BIGINT                    8     0 Nie
OPTCOUNTER                     SYSIBM    SMALLINT                  2     0 Tak
DESCRIBE indexes FOR TABLE TABLENAME;
SELECT name, coltype, length, remarks  FROM SYSIBM.SYSCOLUMNS WHERE tbname='TABLENAME';

MySQL's SHOW CREATE TABLE equivalent in DB2

There is no MySQL's SHOW CREATE TABLE equivalent in IBM DB2 database but you can use command line tool "db2look" that comes with IBM DB2 Client.

db2look -d DBNAME -e -t "TABLE_NAME" -o "c:\output.sql" -i USERNAME -w USERPASSWORD

This will create output like:

CONNECT TO DBNAME USER USERNAME;

------------------------------------------------
-- Instrukcje DDL dla tabeli "USERNAME"."TABLE_NAME"
------------------------------------------------

CREATE TABLE "USERNAME"."TABLE_NAME"  (
    "UPDATE_ID" CHAR(7) NOT NULL ,
    "NAME" CHAR(11) NOT NULL ,
    "SERIAL" DECIMAL(7,0) NOT NULL ,
    "ACCOUNT" CHAR(1) ,
    "ORDNUM" CHAR(1) )
   IN "DB_KORRESP" ;

-- Instrukcje DDL dla klucza podstawowego w tabeli "USERNAME"."TABLE_NAME"

ALTER TABLE "USERNAME"."TABLE_NAME"
 ADD PRIMARY KEY
  ("UPDATE_ID");