私は同じ結果、すなわちデフォルト/優先値のオプションとm:nの関係を達成すると信じている2つのスキーマを以下に示します。どちらか一方を使用する理由はありますか?あなたが最初に自分の名前を知らなくても、人を追加することはできません2番目のスキーマでm:nを1つの省略可能なデフォルト値でモデリングする方法?
スキーマ#1
CREATE TABLE people (
id serial,
primary key (id)
);
CREATE TABLE names (
id serial,
first_name text not null,
last_name text not null,
primary key (id)
);
CREATE TABLE person_has_name (
person_id integer not null references people (id),
name_id integer not null references names (id),
is_default boolean not null default false,
primary key (person_id, name_id)
);
スキーマ#2
CREATE TABLE people (
id serial,
default_name_id integer references names (id),
primary key (id)
);
-- this table has not changed
CREATE TABLE names (
id serial,
first_name text not null,
last_name text not null,
primary key (id)
);
CREATE TABLE person_has_name (
person_id integer not null references people (id),
name_id integer not null references names (id),
primary key (person_id, name_id)
);
申し訳ありませんが、FKは持つべきではありませんnullでない制約 –
idを追加すると、最初のスキーマperson_has_nameにperson_idごとにdefault = trueが1つだけ存在するように制約が必要になります。この理由とあなたが私がスキーマ#2を好むと述べている理由もあります。 –
しかし、スキーマ#2には独自の問題があります。 person_has_nameテーブルにdefault_name_idも存在するはずですか?それはできない可能性があります。 –