(very Urgent)How will I generate Database independent Unique ids for setting the primary keys?
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Eldo_Itteera
Posted On:   Tuesday, September 4, 2001 10:46 PM




I want to generate unique ids in the database table without the user intervention. This id should be incremented each time a record gets inserted. we do not want use "SELECT max(id)" sql command for getting the max value each time ,because this will cause a performance degradation.


Can we use any other class file to do the same.



Please send us a sample of the same.


Thanks

Re: (very Urgent)How will I generate Database independent Unique ids for setting the primary keys?

Posted By:   Holly_Wyman  
Posted On:   Saturday, February 16, 2002 06:03 AM

Hy Eldo,



you might use the remote ip address and the system time in milliseconds and convert them into a String. I usually do so when implementing my own Session-classes.



Holly

Re: (very Urgent)How will I generate Database independent Unique ids for setting the primary keys?

Posted By:   Marc_Flament  
Posted On:   Friday, November 23, 2001 06:24 AM

Use the CREATE SEQUENCE statement to create a sequence, which is a database object from which multiple users may generate unique integers. You can use sequences to automatically generate primary key values.

You can create a sequence with :

CREATE SEQUENCE
schema.your_sequence_name start with 1
increment by 1;




to select the next id from this sequence :

select your_sequence_name.NEXTVAL from DUAL;

or to select the current val :

select your_sequence_name.CURRVAL from DUAL;



you can use next id directly in your insert statement

insert EMPLOYEE(ID,NAME)values(your_sequence_name.NEXTVAL,"JOHNN");


or you can use it a WHERE clause too.


Good luck.

Re: (very Urgent)How will I generate Database independent Unique ids for setting the primary keys?

Posted By:   AlessandroA_Garbagnati  
Posted On:   Tuesday, September 4, 2001 11:00 PM

Hi,

There are few solutions, and based on the type of technology (i.e.: EJB) you need to use you could choose the better one.


With an J2EE you can apply one of the common patterns that you can find on the net. Interesting examples are CMP Primary Key Sequence Generated By Bean Self Cache or HIGH/LOW Singleton+Session Bean Universal Object ID Generator or Sequence and CMP or even GUIDs without Singletons and Databases.


These are all good solutions for a J2EE environment, but the logic could be applied also to a not-J2EE environment. The simplest solution could be:

create a simple table with just one single row and one single field. Then every time you need a new Id, you select the value from the table and then you update it. You can speed up the process in many ways (threading solution for the update, caching the values, etc). Once I saw a system that was providing an alphanumeric ID that was the current time in millis plus a counter... very simple 'no database' solution.

Take a look at those patterns and you can have great suggestions.

About | Sitemap | Contact