Единственная причина для использования ассоциативной сущности, которую вы предлагаете, заключается в том, что вы хотите, чтобы отношения между объектами обслуживания были множественными.Если это то, что вам нужно, то вариант 1 - это то, что вы делаете.Схема будет выглядеть примерно так:
create table MaintenanceProgram
(
ProgramCode int not null ,
... -- other non-key attributes here
primary key ( ProgramCode ) ,
)
create table MaintenanceItem
(
ProgramCode int not null ,
MaintenanceCode int not null ,
... -- other non-key attributes here
primary key ( ProgramCode , MaintenanceCode ) ,
foreign key ( ProgramCode )
references MaintenanceProgram ( ProgramCode ) ,
)
create table MaintenanceItemMap
(
ProgramCode int not null ,
ParentMaintenanceCode int not null ,
ChildMaintenanceCode int not null ,
primary key ( ProgramCode , ParentMaintenanceCode , ChildMaintenanceCode ) ,
foreign key ( ProgramCode , ParentMaintenanceCode )
references MaintenanceItem ( ProgramCode , MaintenanceCode ) ,
foreign key ( ProgramCode , ChildMaintenanceCode )
references MaintenanceItem ( ProgramCode , MaintenanceCode ) ,
check ( ParentMaintenanceCode != ChildMaintenanceCode ) ,
)
Это гарантирует, что все связанные элементы обслуживания совместно используют один и тот же программный код и что элемент обслуживания не может быть сопоставлен самому себе (ограничение проверки).
ОднакоВаша постановка проблемы относится к родительским / дочерним отношениям, которые больше похожи на иерархию / дерево.В этом случае схема, которую вы захотите, будет выглядеть примерно так:
create table MaintenanceProgram
(
ProgramCode int not null ,
... -- other non-key attributes here
primary key ( ProgramCode ) ,
)
create table MaintenanceItem
(
ProgramCode int not null ,
MaintenanceCode int not null ,
ParentMaintenanceCode int null ,
... -- other non-key attributes here
primary key ( ProgramCode , MaintenanceCode ) ,
foreign key ( ProgramCode )
references MaintenanceProgram ( ProgramCode ) ,
foreign key ( ProgramCode , ParentMaintenanceCode )
references MaintenanceItem ( ProgramCode , MaintenanceCode ) ,
check ( MaintenanceCode != ParentMaintenanceCode or ParentMaintenanceCode is null ) ,
)
Выше сказано, что каждый элемент обслуживания связан с одной программой обслуживания;наоборот, что каждая программа обслуживания имеет ноль или более элементов обслуживания.
Кроме того, что каждый элемент обслуживания имеет ноль или 1 родительский элемент обслуживания, что должно относиться к одной и той же программе обслуживания.
Проверочное ограничение говорит, что данный элемент обслуживания не может быть его собственным родителем.