René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

Referential integrity with MySql

create table parent_table (
  id      int         not null auto_increment,
  txt     varchar(20) not null unique        ,
  primary key (id) 
) 
type = innodb;
create table child_table (
  parent_id int         not null,
  txt       varchar(20)         ,
  index (parent_id)             ,
  foreign key (parent_id) references parent_table (id)
) type = innodb;
insert into parent_table (txt) values ('foo');
set @p_id = last_insert_id();
insert into child_table (parent_id,txt) 
values (@p_id, 'foo 1');

insert into child_table (parent_id,txt) 
values (@p_id, 'foo 2');

insert into child_table (parent_id,txt) 
values (@p_id, 'foo 3');
insert into parent_table (txt) values ('bar');
set @p_id = last_insert_id();

insert into child_table (parent_id,txt) 
values (@p_id, 'bar 1');
insert into parent_table (txt) values ('baz');
select p.txt, c.txt from
  parent_table p left join 
  child_table  c on p.id=c.parent_id;