我正在研究删除存储在邻接表模型中的树的子树的方法。我设置了这段代码,它使用 ON DELETE CASCADE 删除了一个完整的树。当我将max_设置为 > 14 的值时,MariaDB (Windows) 会抛出此错误:
得到错误 193 ' mydb
。tree
, 约束外tree_ibfk_1
键 ( parentid
) 引用tree
( nodeid
) ON DELETE CASCADE' 从 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