# # Inserting into a parent, child, grand child tables # ### PARENT TABLE ######### drop table x; create table x (pk int primary key auto_increment, name text )engine=innodb; ### CHILD TABLE ######### drop table y; create table y (pk int primary key auto_increment, name text, fk int references x (pk) )engine=innodb; ### GRAND CHILD TABLE ######### drop table z; create table z (pk int primary key auto_increment, name text, fk int references y (pk) )engine=innodb; ### INSERT INTO PARENT ######### insert into x values(null,'1'); ### INSERT INTO CHILD ######### insert into y values(null,'11', (select pk from x where name='1')); insert into y values(null,'12', (select pk from x where name='1')); ### INSERT INTO GRAND CHILD ######### insert into z values(null,'111',(select pk from y where name='11')); insert into z values(null,'112',(select pk from y where name='11')); insert into z values(null,'121',(select pk from y where name='12')); insert into z values(null,'122',(select pk from y where name='12')); select * from x; select * from y; select * from z;