Reputation: 3
Let's say I have an application that runs and stores its objects in 3 different tables (A, B and C)
.Visual context
What I want to do is :
- To keep the efficiency of an int PK
for sorting and searching the tables.
- When a customer calls the customer service by giving it's ID being able to identify if that ID comes from table A, B or C
.
Here are the potential solutions I came up with :
local id
(for the table) that is an int
and a global id
(for the public) that is local_id + A, B or C
as a suffix. So 10C
is for the id 10
in table C
.PK
of table A
starts from 1 million; table B => 2 millions
;... Other options looked at :
3. UUID
and GID
don't answer the need since I won't be able to know in which table the ID
goes.
My favorite is option 1 since it's not heavy to implement and scalable.
And you what would you do?
EDIT : Please consider that columns in A, B and C are completely different. If it wasn't the case I wouldn't be asking the question.
Upvotes: 0
Views: 1912
Reputation: 12959
I would suggest option 2 little differently. I would suggest you to keep three integer sequences: Seq_A, Seq_B, Seq_C. These sequences will have different ranges:
Have the logic in such a way: The Id should concatenation of: TableName + ID.
Upvotes: 1
Reputation: 2116
The best practice is not to organize your data this way. You should not have three tables with unique primary keys. This will lead to lots of trouble, not the least of which is concurrency issues.
You didn't explain why you have three tables that seem to have similar data, but there should be much better solutions: Perhaps having all the records in one table with a column that indicates data A, B, or C
? Explain your application in more detail if you want a better suggestion.
Upvotes: 2
Reputation: 521178
Actually, a UUID combined with a junction table which relates the ID to the table in which it appears is one option here. The definition for this junction table might look like:
CREATE TABLE id_map(id binary(16), table_name varchar(36));
But note that being able to get the table name from the above given a UUID won't be too useful when writing queries. A more relational way of doing this would be to setup primary foreign key relationships between the A, B, and C tables. Also, it might actually make more sense here to just have a single table, with a single ID column. Whether you do that depends on whether the structure of the three tables be very similar or not.
Upvotes: 0