In the minimal reproducible example below, table C is invalid as it results in a cycle. I understand I can simplify C by removing the constraints, however the number of triggers required after goes up significantly if I want to preserve the cascading behavior from anywhere in the hierarchy (the actual use case more tables).
Is that the only option here, or is there a better way to achieve the automated removal of the entry in C when any dependency above it is removed? I would prefer not to split table B.
CREATE TABLE A
(
Id UNIQUEIDENTIFIER NOT NULL DEFAULT (newsequentialid()) PRIMARY KEY,
Name NVARCHAR(256) NOT NULL
);
CREATE TABLE B
(
Id UNIQUEIDENTIFIER NOT NULL DEFAULT (newsequentialid()) PRIMARY KEY,
AId UNIQUEIDENTIFIER NOT NULL,
Name NVARCHAR(256) NOT NULL,
CONSTRAINT FK_B_A FOREIGN KEY (AId) REFERENCES A (Id) ON DELETE CASCADE
);
CREATE TABLE C
(
Id UNIQUEIDENTIFIER NOT NULL DEFAULT (newsequentialid()) PRIMARY KEY,
PrimaryId UNIQUEIDENTIFIER NOT NULL,
SecondaryId UNIQUEIDENTIFIER NOT NULL,
CONSTRAINT FK_C_PrimaryId FOREIGN KEY (PrimaryId) REFERENCES B (Id),
CONSTRAINT FK_C_SecondaryId FOREIGN KEY (SecondaryId) REFERENCES B (Id)
);