0

これを実装しようとしていますが、循環パスまたは複数のカスケード パスが発生し、問題の場所と解決方法がわかりません。

CREATE TABLE Staff(
    Id INT NOT NULL PRIMARY KEY
)

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)
)
4

2 に答える 2

1

これを試してください:

表:スタッフ

CREATE TABLE IF NOT EXISTS `staff` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `name` varchar(15) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT

表:監督

CREATE TABLE IF NOT EXISTS `supevision` (
  `details_id` int(5) NOT NULL AUTO_INCREMENT,
  `SupervisorId` int(5) NOT NULL,
  `SupervisedId` int(5) NOT NULL,
  `is` int(1) NOT NULL DEFAULT '1',
  PRIMARY KEY (`details_id`),
  KEY `SupervisorId` (`SupervisorId`),
  KEY `SupervisedId` (`SupervisedId`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT

クエリ:

ALTER TABLE `supevision`
  ADD CONSTRAINT `supevision_ibfk_2` FOREIGN KEY (`SupervisedId`) REFERENCES `staff` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `supevision_ibfk_1` FOREIGN KEY (`SupervisorId`) REFERENCES `staff` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
于 2013-06-25T08:20:52.283 に答える
0

外部制約の 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 
于 2013-06-25T08:06:12.713 に答える