title: How to insert value from parent table into child table on insertion into child table
body: <p>I want to insert a value in child table which is generated in parent table on new insertion.</p>
  
  <p>Assume, </p>
  
  <pre><code>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)
  );
  </code></pre>
  
  <p>Creates 3 tables head and table1, table2.</p>
  
  <p>I want whenever i insert values in table1 or table2,</p>
  
  <pre><code>insert into table1 (name) values('Hi');
  </code></pre>
  
  <p>the head and table1 insert these values,</p>
  
  <pre><code>select * from head;
  id      table_type
  .
  .
  5       table1
  
  select * from table1;
  
  t1_id      name
  .
  .
  5          hi
  </code></pre>
  
  <p>note that head table is auto increasing the id. which is inserted into child table table1.
  How to do that i tried following trigger but its not working.</p>
  
  <pre><code>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
  
  $$
  </code></pre>
  
  <p>Any help would be appreciated.</p>