Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

auto generated unique Id?

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to generate unique Id through SQL query for any field?

Thanks.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*any* field? What are you trying to do -- what is your requirement?
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • 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.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • 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.
 
Scott Selikoff
author
Saloon Keeper
Posts: 4033
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • 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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic