Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL:在自己的触发器中更新表_Mysql_Sql_Triggers_Sql Update_Database Integrity - Fatal编程技术网

MySQL:在自己的触发器中更新表

MySQL:在自己的触发器中更新表,mysql,sql,triggers,sql-update,database-integrity,Mysql,Sql,Triggers,Sql Update,Database Integrity,显然,MySQL有一个非常恼人的限制,即不能在为同一个表定义的触发器中更新表。 我使用的是MySQL版本5.1,出现错误:“无法更新存储函数/触发器中的表,因为调用此函数/触发器的语句已经在使用它” 我所拥有的是: create table folder( id int unsigned not null auto_increment PRIMARY KEY , name varchar(100) not null , parentId int unsigned not

显然,MySQL有一个非常恼人的限制,即不能在为同一个表定义的触发器中更新表。
我使用的是MySQL版本5.1,出现错误:“无法更新存储函数/触发器中的表,因为调用此函数/触发器的语句已经在使用它”

我所拥有的是:

create table folder(
    id int unsigned not null auto_increment PRIMARY KEY ,
    name varchar(100) not null ,
    parentId int unsigned not null
) ;
这是一个分层的文件夹结构。文件夹有名称,可能还有父文件夹(如果没有,则
parentId
为零)。
删除文件夹时,我需要更改其中所有子文件夹的
parentId
,以便它们不会成为不存在文件夹的子文件夹

这相当简单(几乎微不足道):

然而,MySQL不允许这样一个简单的触发器,因为正如我前面所说的,您不能在它自己的触发器内更新一个表

有没有办法通过某种方式模拟触发器的效果来实现这种触发器

注意:请不要建议按顺序发送这两个语句(删除和更新)。如果没有其他办法,这显然是最后的解决办法

编辑:

我正在使用MyISAM引擎(出于性能原因),因此无法使用外键。

您不能添加一个在删除时设置为NULL(或默认值)
的外键。

更新
默认值
仍未实现;
设置为空
是唯一选项…
所以你会有类似的

create table folder(
id int unsigned not null auto_increment PRIMARY KEY ,
name varchar(100) not null ,
parentId int unsigned null ,
FOREIGN KEY(parentId) REFERENCES folder(id) ON UPDATE CASCADE ON DELETE SET NULL      
) ;

我使用的是不支持外键的MyISAM引擎。这是一个高流量的网站,所以MyISAM是性能原因所必需的。
create table folder(
id int unsigned not null auto_increment PRIMARY KEY ,
name varchar(100) not null ,
parentId int unsigned null ,
FOREIGN KEY(parentId) REFERENCES folder(id) ON UPDATE CASCADE ON DELETE SET NULL      
) ;