我有一个收集人员和公司信息的简单地址簿。所以我有以下 3 个表格,然后是电子邮件、电话号码、链接到联系人的地址等其他表格:
CREATE TABLE [dbo].[Contact] (
[ID] INT IDENTITY (1, 1) NOT NULL,
...
CONSTRAINT [PK_Contatto] PRIMARY KEY CLUSTERED ([ID] ASC)
);
CREATE TABLE [dbo].[Person] (
[ID] INT IDENTITY (1, 1) NOT NULL,
[IdContact] INT NOT NULL,
[IdCompany] INT NULL,
...
CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED ([ID] ASC),
CONSTRAINT [FK_Person_Company] FOREIGN KEY ([IdCompany]) REFERENCES [dbo].[Company] ([ID]) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT [FK_Person_Contact] FOREIGN KEY ([IdContact]) REFERENCES [dbo].[Contact] ([ID]) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE [dbo].[Company] (
[ID] INT IDENTITY (1, 1) NOT NULL,
[IdContact] INT NOT NULL,
...
CONSTRAINT [PK_Company] PRIMARY KEY CLUSTERED ([ID] ASC),
CONSTRAINT [FK_Company_Contact] FOREIGN KEY ([IdContact]) REFERENCES [dbo].[Contact] ([ID]) ON DELETE CASCADE ON UPDATE CASCADE
);
基本上我希望如果有人删除一个联系人,与之关联的个人/公司也将被删除。此外,如果有人删除了与某人关联的公司,则必须将 person 的 IdCompany 设置为 null。
由于“创建循环或创建附加传播路径”而无法创建的约束是 FK_Person_Contact。
我错过了什么?