March 27, 2008

how to create auto id with IBM-DB2 database

Assume, that i have a table (in example "QUESTION") and i have a column with integer data type .
Then i want to have auto-id in that column (in example "QST_ID") .

That's two ways , there are via sql-operation and via-wizard based on IBM-DB2 Interface ..
1. via SQL

CREATE TABLE "QUESTION" ( "QST_ID" INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY
(START WITH 1, INCREMENT BY 1, NO CACHE )
, "QST_DESCRIPTION" VARCHAR (150) NOT NULL );

2. via Wizard

  • First, right-click on 'Tables' tab to create a table











  • Then, fill the name of your table (i named it with "USER")











  • Specified the column that will be added to the table











  • then we make a column with auto-id, please check carefully in the red rectangular ... we click the Identity radiobox, then specified Intitial value, and Increment value ...













  • If done, click finish to end











So, we have a table "QUESTION" with two columns, QST_ID and QST_DESCRIPTION .
with the auto-id generated in QST_ID column

No comments: