This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes JDBC and the fly likes which way to generate primary key for table ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "which way to generate primary key for table ?" Watch "which way to generate primary key for table ?" New topic
Author

which way to generate primary key for table ?

Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
I have a complicated multithread database applicaton. I need to generate unique primary key for table. The data type is "Long". Now I am thinking to use system time to act as id.

I want to know, except the system time, do we have some options ? The random API has some build-in method ?

Thanks
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Using system time in reverse order to millisecond precision is probably good enough for most applications, if a little wasteful. Why use long if a smaller datatype (e.g. int) will do, especially if you may have hundreds of thousands, or even millions, of them - that's a lot of wasted bytes. Another alternative is to use the sequencing or identity generation capabilities of many RDBMS, though this is not without its problems (through Java).

Best practice is not simple. You can find an example of a Sequencer class if you download the examples from O'Reilly's Java Enterprise Best Practices.

Jules
Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
Originally posted by Julian Kennedy:
Using system time in reverse order to millisecond precision

Thanks, Jules. what do you mean "in reverse order " ? why we need "reverse order ".

Thanks
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
dont know really why jules said for the reverse order but i can guess it.

watch this.

pk_column
---------
20040101
20040102
20040103
20040104
20040105
20040106
20040107

here you get the change after checking the 7 characters right. not that much fast.

now watch the same in reverse order.

pk_column
---------
01012004
02012004
03012004
04012004
05012004
06012004
07012004

this will make a positive difference.

In some of DB not sure about all, but specifically in ORACLE we have the concept of reverse indexing. this is the same as reverse indexing.
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Of course in Adeel's example the "dates" would actually be stored as numbers, not strings (varchars) so any comparison wouldn't be character by character...

My only reason for suggesting reverse order (it doesn't make any difference for uniqueness) is that if you put the date in reverse order then you can compare and sort on that field to find earlier or later entries. In other words, the later the date the higher the number. That doesn't work if, for example you were to use DDMMYYYY format, e.g. 31012004 (Jan) is greater than 01022004 (Feb) but is an earlier date. It might be useful and there's no extra cost.

Jules
Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
I got it. Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: which way to generate primary key for table ?
 
Similar Threads
Inserting prrimary key into other table as foreign key.
Hibernate and Primary Key
Retrieving Primary key
Database Design issue
newbie question on database