次のデータベース テーブルがあると仮定します。
create table Names (
Id INT IDENTITY NOT NULL,
Name NVARCHAR(100) not null,
ParentNameId INT null,
primary key (Id)
)
create index IX_Name on Names (Name)
alter table Names
add constraint FK_NameNames
foreign key (ParentNameId)
references Names
これにより、階層名の定義が可能になります。各名前には、1 つの親名と任意の数の子名を含めることができます。
「a:b:c」のような修飾名に対応するレコードを検索したいと考えています。ここで、各名前はコロンで区切られています。私は現在、結合を使用してそうしています:
select
Id
from
Names names0
inner join Names names1 on names0.ParentNameId = names1.Id
inner join Names names2 on names1.ParentNameId = names2.Id
where
names0.Name = 'a' and
names1.Name = 'b' and
names2.Name = 'c' and
names0.ParentNameId is null
私が疑問に思っているのは、データの非正規化や特定の DBMS への強い依存を伴わない、より効率的な方法があるかどうかということです。
ありがとう