[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Auto Incrementing fields in db2

Posted on 2007-03-29
3
Medium Priority
?
14,894 Views
Last Modified: 2008-09-03
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
Comment
Question by:thomasbonham
3 Comments
 
LVL 37

Accepted Solution

by:
momi_sabag earned 1500 total points
ID: 18821812
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
 

Author Comment

by:thomasbonham
ID: 18822712
Thank you,

That works.

Thomas
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 18822751
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
Creating a Cordova application which allow user to save to/load from his Dropbox account the application database.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question