如何在具有fk约束的情况下从多表中删除? [英] How can i delete from the multi table with fk constraint?

查看:87
本文介绍了如何在具有fk约束的情况下从多表中删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,假设我有2张桌子.

Ok, let's say i have 2 tables.

tbl_1

1.id_tbl1(主键)

1.id_tbl1 (primary key)

2.name

tbl_2

1.id_tbl2(主键)

1.id_tbl2 (primary key)

2.id_tbl1(外键)

2.id_tbl1 (foreign key)

3.name

PS如果我想删除数据tbl_1,那么 id_tbl1 as fk 也将在tbl_2中删除 ... * 如何使用php?

P.S If i want to delete data tbl_1 then the id_tbl1 as fk will also be deleted in tbl_2 ... *how can work with php?

推荐答案

定义外键时需要使用ON DELETE CASCADE.见下文:

You need to use ON DELETE CASCADE when defining the foreign key. See below:

create table tbl_1 (
  id_tbl1 int primary key not null,
  name varchar(10)
);

create table tbl_2 (
  id_tbl2 int primary key not null,
  id_tbl1 int,
  constraint fk1 foreign key (id_tbl1) 
    references tbl_1 (id_tbl1) on delete cascade
);

请参见数据库小提琴.

这篇关于如何在具有fk约束的情况下从多表中删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆