1

私は次のことをしたい、

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.

どんな助けでも!

4

0 に答える 0