MySQL でクロージャ テーブルを使用して階層をモデル化するときに、兄弟ノードの名前に一意の制約を適用する方法を考えています。
これは私のスキーマです:
create table spaces (
id int not null,
name varchar(50) not null,
parent int not null,
primary key (id),
foreign key (parent) references spaces(id),
unique key (name, parent)
)
create table space_paths (
ancestor int not null,
descendant int not null,
depth int not null,
primary key (ancestor, descendant),
foreign key (ancestor) references spaces(id),
foreign key (descendant) references spaces(id)
)
このスキーマでは、spaces
テーブルに一意の制約を使用して、同じ名前の兄弟がないことを確認しています。
このアプローチの欠点は、space_paths
テーブルにカプセル化された階層メタデータが非正規化されることです。parent
つまり、テーブル内のフィールドとspaces
テーブル内のパスの一貫性を手動で管理する必要があるということですspace_paths
。
非正規化することなく、データベースが兄弟間で一意の名前の制約を適用するようにスキーマを再設計する方法はありますか?