• 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

which way to generate primary key for table ?

 
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 798
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
JulianInactive KennedyInactive
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 798
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it. Thanks.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic