• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

DB2 and automated PK genearation..

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi..
I'm using DB2 for a project.. I want to be able to genearate an automated PK which can just be a serial number like 1, 2,3.....
So I want to know that how can i do that with DB2
Secondly, When i insert I want to know what was the PK that was genearted.. so how can i do that.. like i'll execute the insert statement for example with a persons details.. a record is inserted and a pk is generated.. so how do i get that pk value..

Thanks..
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use an identity column.

Candle has a short example. (search for "identity" in the list of features.)
 
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that this feature became available with DB2 version 7. DB2 5.2 and 6.1 have no method of automated key generation.
 
Saniya Ansari
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks you guys.. one more part that i'm trying to figure out is that how do i get that key that was generated back.. bcoz usually in select we'll give the primary key and select a record.. i can i suppose make a query and put the rest of the fields for example name, address, phone, email to match with the record i just inserted and get its id but this sounds like a less efficient method.. are there any other ways someone can suggest...
the question is agand if i'm using an automated pk generated by db, after insert how do i get this pk back right after i have inserted the record...
Thankyou..
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic