• 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

auto generated unique Id?

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to generate unique Id through SQL query for any field?

Thanks.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
*any* field? What are you trying to do -- what is your requirement?
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeff Albrechtsen:
*any* field? What are you trying to do -- what is your requirement?



Suppose, user_id is one of the column in a table and that is generated by system (not entered by user) and it should be unique...

How can I achieve this?

Thanks.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depends on the Database....

In Oracle, I use Sequences.

In most other databases, I have a UNIQUE_SEQUENCE_ID table that functions as a sequence -- it has SEQUENCE_NAME and NEXT_AVAILABLE_KEY fields. The application layer then obtains the keys from this table. Just watch out for concurrency issues when doing this.
 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It does depend on the database as Joel said, and each database has its own special way of handing unique auto-generated keys. Its common though, to avoid using such a database-provided solution since it is not often portable (nor is it scalable), and to focus on manual-application controlled solutions. This also helps for distributed environment where keys may need to be unique accross multiple databases.

In general for small applications, use the database solution, but if its a large scale application, write your own unique key generator. If you do though, performance is something you should be aware of since you wouldn't want the system querying the database for every single generated key (more likely you'd want a cached set of data and update the database at set intervals).
 
reply
    Bookmark Topic Watch Topic
  • New Topic