D
D
Dark_Dante2017-09-29 12:12:03
MySQL
Dark_Dante, 2017-09-29 12:12:03

Whether to put primary key on the linking table?

Hello, I vaguely understand sql unfortunately, and therefore the question arose.
There are three tables
59ce0df025c36074272262.png
. Is it necessary to set the primary_key in the brands_state table to the id_brand and id_group fields (id_group and id_brand integer of course, there is a little wrong on the diagram)?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
d-stream, 2017-09-29
@d-stream

Without a deep understanding, it is worth asking yourself an immutable rule "every table must have a primary key." In the future, someday it will be possible to reach the nirvana of a situation where pk should not be.

E
egor_nullptr, 2017-09-29
@egor_nullptr

Necessarily.

D
Denis Holub, 2017-09-29
@denman1985

I still don’t understand why everyone recommends setting the primary key to the brands_state table if it is a link table.
There are two foreign keys.
I would not bet (and I don’t bet on such pk link tables, they are not needed in them).
If pk is an attempt to make the relationship unique (so that duplicates are not generated), then this is a question for the application logic, which mindlessly duplicates rows consisting of two foreign key fields, and not for SQL constraints.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question