[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Auto Incrementing fields in db2

Posted on 2007-03-29
3
Medium Priority
?
14,777 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 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

649 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