外部制約の 1 つを削除できますが、それをトリガーに置き換えます。
DROP TABLE Supevision
DROP TABLE Staff
CREATE TABLE Staff(
Id INT NOT NULL PRIMARY KEY
)
GO
CREATE TABLE Supevision (
SupervisorId INT NOT NULL FOREIGN KEY REFERENCES Staff(Id) ON UPDATE CASCADE ON DELETE CASCADE,
SupervisedId INT NOT NULL --FOREIGN KEY REFERENCES Staff(Id) ON UPDATE CASCADE ON DELETE CASCADE,
PRIMARY KEY (SupervisorId, SupervisedId)
)
GO
CREATE TRIGGER DELETEStuff
ON Staff
FOR DELETE
AS
BEGIN
DELETE sv
FROM deleted d
INNER JOIN Supevision sv
ON d.Id = sv.SupervisedId
DELETE sv
FROM deleted d
INNER JOIN Supevision sv
ON d.Id = sv.SupervisorId
END
GO
CREATE TRIGGER UPDATEStuff
ON Staff
FOR UPDATE
AS
BEGIN
UPDATE Supevision
SET SupervisedId = inserted.id
FROM inserted, deleted
WHERE Supevision.SupervisedId = deleted.Id
UPDATE Supevision
SET SupervisorId = inserted.id
FROM inserted, deleted
WHERE Supevision.SupervisorId = deleted.Id
END
GO
INSERT INTO Staff (ID)
VALUES (1), (2), (3), (4), (5), (6), (7)
INSERT INTO Supevision (SupervisorId, SupervisedId)
VALUES (1, 2), (2, 3), (3, 4), (5, 6), (6, 7)
DELETE FROM Staff WHERE ID = 3
UPDATE Staff SET Id = 60 WHERE Id=6
SELECT * FROM Supevision
SELECT * FROM Staff