C
C
Crunchor2019-12-02 17:23:16
PostgreSQL
Crunchor, 2019-12-02 17:23:16

Is it correct to put foreign keys on the ID field?

There are 2 tables
Table 1
id - automatically generated field
field1 - unique table field
Table 2
id - automatically generated field
table1_field1 - link to a unique table field 1. Not unique in the table 2.
field2 - unique table field
How to properly organize a relationship between tables?
Will there be a difference between the foreign key of the table1_field1 field of table 2 on the id and field1 fields of table 1?
id is numeric and field1 is a string field. Could it matter?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Sundukov, 2019-12-03
@Crunkor

Apparently your id is a surrogate PK. If there is a unique field1 field, then it makes sense to keep a surrogate PK in the table only for the sake of human convenience, otherwise it is completely unnecessary. Table 2 is in a similar situation.
If one-to-many, then table1_field1 is sufficient.
What difference are we talking about? In a wording like yours, no, it won't.
field1 isn't a UUID by any chance? In most cases, there will be no difference. All the same on them to hang up an index.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question