Solved

Auto Incrementing fields in db2

Posted on 2007-03-29
3
14,185 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 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:Kdo
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Join & Write a Comment

SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
Creating and Managing Databases with phpMyAdmin in cPanel.
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…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now