52

How to view the table structure in DB2 database

royhowie
  • 11,075
  • 14
  • 50
  • 67
Ambat bhath
  • 1,425
  • 1
  • 12
  • 23

22 Answers22

68

I got the answer from the sysibm.syscolumns

Select distinct(name), ColType, Length from Sysibm.syscolumns where tbname = 'employee';
flipchart
  • 6,548
  • 4
  • 28
  • 53
Ambat bhath
  • 1,425
  • 1
  • 12
  • 23
  • 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 '13 at 08: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 '15 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 08 '16 at 16:52
  • table name is case sensitive for me. –  Jan 05 '19 at 19:38
  • NULLS can be added to know mandatory columns – Krishna Aug 18 '21 at 11:40
27

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

Ian Bjorhovde
  • 10,916
  • 1
  • 28
  • 25
  • Thanks for your answer. But i couldn't get the result – Ambat bhath Jun 07 '10 at 06:50
  • 1
    What platform are you using? Based on your comments, I suspect it may be zOS. – Ian Bjorhovde Jun 09 '10 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 ".. 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 '14 at 06:43
  • 2
    IBM DB2 is not responding to this query ! It says- Error : DB21033E The command is not valid for this DB2 database server. – Arunchunaivendan Feb 10 '15 at 06:16
  • @ssc describe here is db2 command not sql statement. if you are trying through ibm_studio you will hit this error – TheBeginner Mar 16 '21 at 14:05
9

In DB2, enter on db2 command prompt.

  db2  =>  describe  table MyTableName
DJo
  • 2,133
  • 4
  • 30
  • 46
binhn
  • 99
  • 1
  • 1
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.

Ajinkya
  • 22,324
  • 33
  • 110
  • 161
Boris
  • 71
  • 1
  • 3
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

fabfas
  • 2,200
  • 1
  • 21
  • 21
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;
Vinit
  • 95
  • 2
  • 7
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 =''"
emmanuel
  • 9,607
  • 10
  • 25
  • 38
Andrew
  • 41
  • 1
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);
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
captainhero70
  • 694
  • 6
  • 8
3

You can Get the table meta data using this query

SELECT * FROM SYSIBM.COLUMNS WHERE TABLE_NAME = 'ASTPCLTEXT';
mhasan
  • 3,703
  • 1
  • 18
  • 37
R K
  • 31
  • 1
1

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

enter image description here

zawhtut
  • 8,335
  • 5
  • 52
  • 76
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

Dhanish Jose
  • 739
  • 1
  • 8
  • 19
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
Peter Miehle
  • 5,984
  • 2
  • 38
  • 55
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 :)

Faisal
  • 442
  • 5
  • 13
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

richardsun
  • 3,245
  • 1
  • 18
  • 22
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'
D. Kermott
  • 1,613
  • 17
  • 24
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.

Joseph Morgan
  • 163
  • 1
  • 9
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

KC Baltz
  • 1,498
  • 1
  • 13
  • 22
  • 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 '16 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 '16 at 00: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 '16 at 05: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

Paul Vernon
  • 3,818
  • 1
  • 10
  • 23
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.

Noor
  • 1
  • 2
0
describe output select * from TABLE(WLM_GET_WORKLOAD_OCCURRENCE_ACTIVITIES('',-2))
Ritu Gupta
  • 2,249
  • 1
  • 18
  • 11
-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.
MarmiK
  • 5,639
  • 6
  • 40
  • 49
Ambat bhath
  • 1,425
  • 1
  • 12
  • 23