1

新規挿入時に親テーブルに生成された値を子テーブルに挿入したい。

推定、

create table head (
head_id int primary key auto_increment,
table_type varchar(60) );

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

create table table2 (
t2_id int primary key,
name varchar(60) ,
foreign key (t2_id) references head(head_id)
);

3 つのテーブル head と table1、table2 を作成します。

table1またはtable2に値を挿入するたびに、

insert into table1 (name) values('Hi');

head と table1 はこれらの値を挿入し、

select * from head;
id      table_type
.
.
5       table1

select * from table1;

t1_id      name
.
.
5          hi

ヘッド テーブルは自動で ID を増やしていることに注意してください。これは、子テーブル table1 に挿入されます。それを行う方法は、次のトリガーを試しましたが、機能しません。

DELIMITER $$
CREATE
    TRIGGER `t1` BEFORE INSERT
    ON `table1`
    FOR EACH ROW BEGIN
        insert into head (table_type) values ('table1');
        insert into table1 (t1_id,name) values (NEW.head(id),NEW.table1(name));
    END

$$

どんな助けでも大歓迎です。

4

0 に答える 0