53

How to view the table structure in DB2 database

22 Answers 22

69

I got the answer from the sysibm.syscolumns

Select distinct(name), ColType, Length from Sysibm.syscolumns where tbname = 'employee';
5
  • 4
    It may differ by version as it seems. We are running V7R1 and table is SYSIBM.COLUMNS. Also columns are different, so i uses DSPFFD FILE(SYSIBM/COLUMNS) to find correct columns
    – Piro
    Aug 15, 2013 at 8:18
  • 2
    Added 'order by COLNO' because the output order is not the same as the actual column order. Select distinct(name), COLNO, ColType, Length from Sysibm.syscolumns where tbname = 'EMPLOYEE' order by COLNO
    – devXen
    Aug 30, 2015 at 15:22
  • This worked for me on V7R1 and it selects for a specific library: select COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGth, NUMERIC_PRECISION from SYSIBM/COLUMNS where TBNAME = 'TABLE' and TABLE_SCHEMA='LIBRARY';
    – KC Baltz
    Apr 8, 2016 at 16:52
  • table name is case sensitive for me.
    – user117499
    Jan 5, 2019 at 19:38
  • NULLS can be added to know mandatory columns
    – Krishna
    Aug 18, 2021 at 11:40
28

Generally it's easiest to use DESCRIBE.

DESCRIBE TABLE MYSCHEMA.TABLE

or

DESCRIBE INDEXES FOR MYSCHEMA.TABLE SHOW DETAIL

etc.

See the documentation: DESCRIBE command

5
  • Thanks for your answer. But i couldn't get the result Jun 7, 2010 at 6:50
  • 1
    What platform are you using? Based on your comments, I suspect it may be zOS. Jun 9, 2010 at 16:51
  • 3
    I tried executing DESCRIBE TABLE statement but there is error: 11:42:25 [DESCRIBE - 0 row(s), 0.000 secs] [Error Code: -104, SQL State: 42601] An unexpected token "TABLE" was found following "DESCRIBE ". Expected tokens may include: "JOIN <joined_table>".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.13.127 ... 1 statement(s) executed, 0 row(s) affected, exec/fetch time: 0.000/0.000 sec [0 successful, 0 warnings, 1 errors] I am running this on DBVisualizer. Please advise
    – SSC
    Jun 12, 2014 at 6:43
  • 2
    IBM DB2 is not responding to this query ! It says- Error : DB21033E The command is not valid for this DB2 database server. Feb 10, 2015 at 6:16
  • @ssc describe here is db2 command not sql statement. if you are trying through ibm_studio you will hit this error Mar 16, 2021 at 14:05
9

In DB2, enter on db2 command prompt.

  db2  =>  describe  table MyTableName
7

Also the following command works:

describe SELECT * FROM table_name;

Where the select statement can be replaced with any other select statement, which is quite useful for complex inserts with select for example.

6

How to view the table structure in db2 database

Open db2 command window, connect to db2 with following command.

> db2 connect to DATABASE_NAME USER USERNAME USING PASSWORD

Once you connected successfully, issue the following command to view the table structure.

> db2 "describe select * from SCHEMA_NAME.TABLE_NAME"

The above command will display db2 table structure in tabular format.

Note: Tested on DB2 Client 9.7.11

5

FOR TABLE DESCRIPTION IN IBM DB2 10.7 VERSION I TRIED THIS AND IT WORKED FINE

SELECT NAME,COLTYPE,NULLS,LONGLENGTH FROM SYSIBM.SYSCOLUMNS where TBcreator =SCHEMANAME and TBNAME =TABLENAME;
4

1.use db2 describe table

  db2 describe table tabschema.tabname

2.use db2 describe output

  db2 "describe select * from tabschema.tabname"

3.use db2look utility

  db2look -d dbname -e -t tabname

4.find rows in db2 syscat

  db2 "Select * from syscat.columns wher tabname='' and tabschema =''"
3

php example for iSeries (as400) db2, yes this worked!

$i5 = db2_connect($database, $user, $password, array("i5_lib"=>"qsys2"));

$querydesc = "select * from qsys2.syscolumns where table_schema = '".$library."' and table_name = '".$table_name."' ";

$result = db2_exec($i5, $querydesc);

also if you just want to list all tables with their descriptions

$query = "select TABLE_NAME, TABLE_TEXT from systables where table_schema = '$library' ";

$result = db2_exec($i5, $query);
3

You can Get the table meta data using this query

SELECT * FROM SYSIBM.COLUMNS WHERE TABLE_NAME = 'ASTPCLTEXT';
1

to get all tables: (You may want to restrict schema to your schema)

select * from syscat.tables

to get all columns: (where tabname = your_tabname)

select * from syscat.columns
1
  • I couldn't get the result for the above given. Jun 7, 2010 at 6:48
1

Control Center already got the feature of that. It's just below the table list.

enter image description here

1
  • I wanted to get this in a console rather than in control center Jan 31, 2018 at 14:27
1

Use the below to check the table description for a single table

DESCRIBE TABLE Schema Name.Table Name

join the below tables to check the table description for a multiple tables, join with the table id syscat.tables and syscat.columns

You can also check the details of indexes on the table using the below command describe indexes for table . show detail

1

if you're using Aqua Data studio, simply write select * from table_name and instead of pressing execute,, press ctrl +D .

You shall be able to see the description for the table

0

The easiest way as many have mentioned already is to do a DESCRIBE TABLE

However you can also get some the same + additional information from

db2> SELECT * SYSCAT.TABLES

db2> SELECT * FROM SYSCAT.COLUMNS

I usually use SYSCAT.COLUMNS to find the related tables in the database where I already know the column name :)

Another good way if you want to get the DDL of a particular table or the whole database is to use the db2look

# db2look -d *dbname* -t *tablename* > tablestructure.out

This will generate the ".out" file for you which will contain the particular table's DDL script.

# db2look -d *dbname* -e > dbstructure.out

This will generate the entire database's DDL as a single script file, this is usually used to replicate the database, "-e" is to indicate that one wants to export DDL suitable recreate exact same setup in a new database.

Hope this can help someone looking for such answers :)

0

I am running DB2/LINUXX8664 10.5.3 and describe select * from schema_name.table_name works for me.

However, describe table schema_name.table_name fails with this error:

SQL0100W No row was found for FETCH, UPDATE or DELETE; or the result of a query is an empty table. SQLSTATE=02000

0
drop view lawmod9t.vdesc

create view lawmod9t.vDesc as select 
       upper(t.table_cat) as Catalog, 
       upper(t.table_schem) as Schema, 
       upper(t.table_name) as table, 
       t.table_text as tableDesc, 
       c.system_column_name as colname_short, 
       c.column_name as colname_long, 
       c.column_text as coldesc, 
       c.Type_Name as type, 
       c.column_Size as size
from sysibm.SQLColumns c
inner join sysibm.sqltables t
on c.table_schem = t.table_schem
and c.table_name = t.table_name

select * from vdesc where table = 'YPPPOPL'
0

I am using Aquadata Studio 12.0.23, which is several versions short of the newest. So your experience may be better than mine. I found that the best way to get an overview was to use the ERD generator. It took a couple of hours, since normalization was not a concept used in the design of this database almost 30 years ago. I was able to get definitions for all of the objects in a few hours, with a file for each.

0

The OP doesn't mention if this is DB2/400 being discussed, but I found that the only way I could get the table structure including the column name descriptions was to use DSPFFD.

DSPFFD FILE(TBNAME) OUTPUT(*OUTFILE) OUTFILE(SOMELIB/TBDESC)

This puts the description of TBNAME in a table called TBDESC in the SOMELIB library. You can then query that with:

select * from SOMELIB/TBDESC

3
  • The only way? Wow! Someone needs to dig into the SQL some more ;-) Try some of the suggestions in prior answers; admittedly some are not going to function on the DB2 for IBM i. Mostly because the authors failed to preface with "For DB2 LUW", unlike this answer did, by so nicely starting with essentially "For DB2 for IBM i". But typically the responses that will have directed to use of the SQL catalogs for obtaining the descriptive information [e.g. from COLUMNS or SYSCOLUMNS; typically VIEWs with metadata for DB2 objects], those should be similar across all DB2 variants.
    – CRPence
    Oct 12, 2016 at 23:13
  • The other ways work with the exception of the column name description. This was the only method I found that gave me that. I'm not saying there aren't others because I stopped looking when I found what I needed.
    – KC Baltz
    Oct 15, 2016 at 0:56
  • Depends on what is meant by "column name description", I suppose. But the VIEW SYSCOLUMNS in QSYS2 has defined, a COLUMN_TEXT FOR LABELTEXT column, that is derived from the data DBITXT of the System Database Cross Reference file QADBIFLD in QSYS that should be the same as the data obtained from the WHFTXT column of the system-supplied record format QWHDRFFD of the model-file QADSPFFD in QSYS; i.e. the dynamically maintained information for that "column name description" can be obtained directly from the catalog rather than having to use the Display File Field Descriptions (DSPFFD)
    – CRPence
    Oct 15, 2016 at 5:00
0

The Db2 catalogs hold lots of information about table structure. You can query them to find (most) things out about the structure of a table.

For example, you can generate (approximate) DDL for Db2 LUW tables from the catalog with SQL such as contained in this view from the IBM samples

https://github.com/IBM/db2-samples/blob/master/db_library/views/db_table_quick_ddl.sql

0

One way to get list of columns and their detail using SQL editor is:

SELECT * FROM QSYS2.SYSCOLUMNS WHERE SYS_TNAME ='YOURTABLE';

Describe works for MariaDB, but not for IBM or at least I do not know.

0
describe output select * from TABLE(WLM_GET_WORKLOAD_OCCURRENCE_ACTIVITIES('',-2))
-1

Follow this simple steps:

  1. Select the Browsers window.
  2. Extract (expand) it.
  3. Select and extract (expand) the table list.
  4. Select the required table and extract (expand) it.
  5. On double click the code option, it opens the table structure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.