Forums Register Login

auto generated unique Id?

+Pie Number of slices to send: Send
How to generate unique Id through SQL query for any field?

Thanks.
+Pie Number of slices to send: Send
*any* field? What are you trying to do -- what is your requirement?
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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).
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1369 times.
Similar Threads
Generating a unique id
Make and use bean in same jsp
Primary key
Individual Cell Padding in Table
Javascript Dynamic Dropdown Box validation
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 04:37:24.