![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi, I have a question regarding sequences as primary keys. I have some example tables each having an integer as a primary key. create sequence vegetable_id_seq ; CREATE TABLE vegetable ( id INTEGER PRIMARY KEY default nextval ('vegetable_id_seq'), name varchar (255) not null ); create sequence fruit_id_seq ; CREATE TABLE fruit ( id INTEGER PRIMARY KEY default nextval ('fruit_id_seq'), name varchar (255) not null ); create sequence meat_id_seq ; CREATE TABLE meat ( id INTEGER PRIMARY KEY default nextval ('meat_id_seq'), name varchar (255) not null ); My question is: Is it better to use separate sequence counters (as in the example) or is it better to share a sequence counter among all (or some) tables? The advantage of sharing a sequence counter is that it stores redundancy information in the ID number itself which can aid in debugging code. |
|
For example, if you have a function which compares two 'meat' objects generated from the information in meat table, one can verify by looking at the ID values that both objects are really genuine 'meat' objects as both values can only occur in the meat table. |
|
When using separate sequence counters this is not possible as ID numbers are not unique and it is much harder to find code that accidentally mixes up ID values. I personally would tend to using a shared sequence but I don't have enough experience with databases to make a good judgement about this. |
#3
| ||||
| ||||
|
|
I'm not sure I understand this: a number is a number. If you want to "store" additional information in a number you need to use a particular numbering scheme (such as "all odd numbers mean X"). Choosing between a single and multiple sequences won't help you there. |
|
- higher risk of running out of numbers |
|
- potential bottleneck (synchronization on one vs. multiple locks) |
|
- no per table control over numbering, i.e. you might want to wrap numbering for one table but not for another |
#4
| |||
| |||
|
|
My question is: Is it better to use separate sequence counters (as in the example) or is it better to share a sequence counter among all (or some) tables? |
|
I personally would tend to using a shared sequence but I don't have enough experience with databases to make a good judgment about this. |
![]() |
| Thread Tools | |
| Display Modes | |
| |