For a store
I have many store_offers
which is a one-to-many relationship.
However, for a table
create table store (
id bigserial primary key
);
I can use a single primary key id
(SQLfiddle):
create table store_offer (
id bigserial primary key,
store_id bigint not null,
constraint fk__store_offer__store
foreign key (store_id)
references store(id)
);
or a composite primary key (id, store_id)
(SQLFiddle):
create table store_offer (
id bigserial not null,
store_id bigint not null,
constraint fk__store_offer__store
foreign key (store_id)
references store(id),
primary key(id, store_id)
);
My question is "what does make more sense here?". Imho the composite key should be the correct way to do it since a store_offer
is actually "bound" to as store
. But one can argue that this is already the case since the first version has a foreign key. On the other hand a store_offer
primary key actually must not change once it's created. You have to create a new store_offer
and delete the old one if you want discard one. But you cannot simply change store_id
in the second approach.
So what is the correct answer here?