My Blog List

Tuesday, March 12, 2013

'cascade on delete' in mysql


what mean cascade on delete ??

this means if parent record deleted, the child records referencing parent pk also deleted


create table tb_parent (
 id int not null,
fd varchar(100) not null,
primary key (id)
)ENGINE = INNODB;

create table tb_child(
id int not null,
pid int default null,
fd varchar(100) default null,
primary key (id),
key ix_parentid(pid),
constraint child_ibfk_1 foreign key (pid) references tb_parent (id) on delete cascade
) ENGINE = INNODB;

insert into tb_parent values (1, 'parent-1'), (2, 'parent-2');

insert into tb_child values(100, 1, 'childe-100');


No comments:

Post a Comment