Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unique primary key generator

 
Dmitry Shekhter
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
For the project i'm working on, i need to insert data to fill
the tables in the database schema, what i'm trying to figure out is what is an efficient way to generate unique primary keys for each of the tables, as each table has to have a unique primary key, but that data is not going to be provided to my database bean... These primary don't have to be meaningful, they are just to uniquely identify each record and support relationships between the tables in the schema.
I have some ideas, but i was hoping that someone who has already went through this would share the pattern.
Thanks in advance,
Dmitry
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many databases have database-specific ways to do this. SQL Server can autonumber primary keys. Oracle has sequences to generate numbers. Etcetera.
If you don't want to be database-specific, use some type of service to generate these numbers (can be a class, bean, EJB, RMI or Corba callable service, whatever) in conjunction with a "nextnum" table. For best efficiency, if strictly consecutive numbers are not necessary, you grab a range of numbers from the nextnum table and only go back to the database once you've exhausted that range. With this technique you can have multiple, distributed number generators.
- Peter
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic