Suppose we have a table of jobs like so:
- job (id, number);
Suppose we have a customer request to track job times and we create a table like so:
- (A) job_timer (id, job_id, timestamp);
but we have a choice of how we want to tie it to job
table, so we can also:
- (B) job_timer (id, job_number, timestamp);
Suppose that job_number is UNIQUE
.
I am conditioned to make foreign keys based on id, so A) job_id
would be the way I have seen it done. But, customers are looking up jobs by job number, and it will save me and database some work if I do a lookup directly by B) job_number
. But should I do so?
By more work when using job_id
I mean, i.e. given job number I only need to use job_timer
table. when given job_id
I need to tie in both tables - more cognitive programming work, more database work.
Note, a similar question Foreign Key to non-primary key addresses UNIQUE-ness of non-primary key but I don't believe it addressed my issue.
The original table (job
) is part of legacy codebase where both fields id
and number
are utilized throughout the code extensively, and in this sense I have a "split primary key" condition. Weeding that out will be prohibitive due to lack of full test coverage. Why the number
field was created and also why it was made to be a varchar are good questions. I am sure there must've been a reason at some point.
I'm looking for something like "yes, go ahead it's totally fine, there is no best practice in this case", or "no, if you do this you might come across issues X, Y, Z in the future".