我希望执行以下操作,
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;
但我希望使用触发器,因为我必须这样做很长一段时间,所以我编写了以下代码,但由于提到的错误它不起作用。
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.
任何帮助!