我正在研究删除邻接表模型中存储的树的子树的方法。我设置了此代码,该代码使用ON DELETE CASCADE删除了完整的树。当我将 max _ 设置为值> 14时,MariaDB(Windows)会引发以下错误:
出现错误193'mydb
。tree
,约束tree_ibfk_1
,外键(parentid
)参考tree
(nodeid
)删除级联来自InnoDB
是我的代码还是数据库的限制?
DROP TABLE IF EXISTS tree;
DROP PROCEDURE IF EXISTS prepTree;
CREATE TABLE tree (
name varchar(255) NOT NULL,
nodeid INT NOT NULL AUTO_INCREMENT,
parentid INT NULL,
PRIMARY KEY(nodeid),
FOREIGN KEY(parentid) REFERENCES tree(nodeid) ON DELETE CASCADE
) ENGINE=InnoDB;
DELIMITER // ;
CREATE PROCEDURE prepTree()
BEGIN
DECLARE i INT;
DECLARE max_ INT;
SET max_ = 15; -- >14 won't work
INSERT INTO tree(name, parentid) VALUES("root", NULL);
SET i = 1;
WHILE i <= max_ DO
INSERT INTO tree(name, parentid) VALUES("child", i);
SET i = i + 1;
END WHILE;
END //
DELIMITER ; //
CALL prepTree;
DELETE FROM tree WHERE name = "root";
SELECT * FROM tree; --table will be empty on success
答案 0 :(得分:2)
我知道它同样适用于MariaDB。
15.8.1.6 InnoDB and FOREIGN KEY Constraints
...
参考操作
...
- ...级联操作的嵌套深度不能超过15级。
...