绕过错误代码:1442。无法更新调用此存储函数/触发器的语句所使用的表

时间:2013-03-26 12:25:24

标签: mysql delimiter

我希望做到以下几点,

drop database ABC;
create database ABC;
use ABC;

create table head (
head_id int primary key auto_increment,
head_name varchar(55)
);

create table table1 (
t1_id int primary key,
t1_name varchar(55),
foreign key (t1_id) references head(head_id)
);


insert into head(head_name) values ('table1');
insert into table1(t1_id,t1_name) select max(head_id),'HI' from head;


select * from head;
select * from table1;

但是我希望使用触发器,因为我需要长时间的heiriechy,所以我写了下面的代码,但由于提到的错误它没有工作。

drop database ABC;
create database ABC;
use ABC;

create table head (
head_id int primary key auto_increment,
head_name varchar(55)
);

create table table1 (
t1_id int primary key,
t1_name varchar(55),
foreign key (t1_id) references head(head_id)
);

delimiter $$
CREATE
    TRIGGER `t1` BEFORE INSERT
    ON `table1`
    FOR EACH ROW BEGIN
        insert into head (head_name) values ('table1');
    END
$$
delimiter ;

insert into table1(t1_id,t1_name) select max(head_id),'HI' from head;

select * from head;
select * from table1;

以上触发器给我错误,

Error Code: 1442. Can't update table 'head' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.

任何帮助!

0 个答案:

没有答案