Solved

Auto Incrementing fields in db2

Posted on 2007-03-29
3
14,533 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 37

Accepted Solution

by:
momi_sabag earned 500 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 45

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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

726 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