foreign key + InnoDB + set null

  • 親の列がdelete/updateされたときに子の列がnullに変更される
    • 参照している列にnot nullが入ってると set null 利かないので注意
drop database if exists hoge;
create database hoge;

use hoge;

create table main (
       id    int auto_increment primary key,
       name  char(5),
       unique key (name)
) type=InnoDB;

create table sub (
      subid int auto_increment primary key,
      subname  char(5),
      foreign key (subname) references main (name) on delete set null 
) type=InnoDB;