• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 14997
  • Last Modified:

Auto Incrementing fields in db2

I'm new to db2. I have the free version of it and what I'm trying to do is create a table that has auto incrementing row in it for the primary key.

Also do any one have any good sites for information out side of ibm.com.

Thank you,

thomas
0
thomasbonham
Asked:
thomasbonham
1 Solution
 
momi_sabagCommented:
Hi Thomas

you can find all the udb documentation here
http://publib.boulder.ibm.com/infocenter/db2help/index.jsp
it's called the udb information center

auto incremented values in udb can be achived using an identity column with the attribute generated always or generated by default
either way, db2 will insert values for you into the column in ascending order
you can read more about it here (also samples)
http://publib.boulder.ibm.com/infocenter/db2luw/v8//topic/com.ibm.db2.udb.doc/ad/c0007006.htm

if you need further clarification just ask
momi
0
 
thomasbonhamAuthor Commented:
Thank you,

That works.

Thomas
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi Thomas,

Just to add a bit to Momi's thoughts, the IBM documentation is about as good as there is on the web.  If anything it's too complete as I sometimes find it tough to find exactly what I need without hunting through a list of possibilites.

From the first link, expand "Reference" in the left frame.  Then expand "SQL".  It's all there.


"Auto Increment" is a term used by several vendors.  MySQL uses the term and allows for this type of column.  If you've used a MySQL auto-increment field, you know that you must define the column as auto-increment and define the column as a primary key.  (Not very friendly.)  DB2 recognizes an auto-increment (IDENTITY) column as the primary key without the need for additional constructs.  Here's a quick example:

-- MySQL

create table MyTable
(
  id   int(11) not null auto_increment,
  primary key (id)
);

-- DB2

create table MyTable
(
  id integer generated always as identity
);

Note that DB2 allows you to define the identity column as "generated always" or "generated by default".  Generated always means just that -- DB2 assigns the value.  Generated by default means that you can provide the column value when you insert a row.  (This is particularly helpful when you're building tables that must live happily with existing data.)  if you don't assign a value to the column during an insert DB2 will generate it for you.  In either case, DB2 verifies that the new value for the column is unique.


Good Luck,
Kent
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now