暂无图片
GBase8s如何在有外键关系的表中删除数据
我来答
分享
棉花糖
2021-01-22
GBase8s如何在有外键关系的表中删除数据
暂无图片 5M

GBase8s如何在有外键关系的表中删除数据

我来答
添加附件
收藏
分享
问题补充
1条回答
默认
最新
棉花糖

答:有两种方式:
方式一:先删除外键表中的数据,再删除当前数据
举例:
drop table if exists t1;
drop table if exists t2;
create table t1(id int primary key , s1 float);
create table t2(id int , id1 int ,s1 float,foreign key(id1) references t1(id) );
insert into t1 values(1,20);
insert into t1 values(2,30);
insert into t2 values(1,1,50);
–如果你直接删除t1的数据,则报错
delete t1;
692: Key value for constraint (root.u143_565) is still being referenced.
Error in line 1
Near character position 8

–需要先删除t2表中的数据,才能删除t1表中的数据
delete t2;
delete t1;

方式二:可以通过外键设置级联删除
–在设置外键时,设置级联删除
drop table t2;
drop table t1;
create table t1(id int primary key , s1 float);
create table t2(id int , id1 int ,s1 float,foreign key(id1) references t1(id) on delete cascade);
insert into t1 values(1,20);
insert into t1 values(2,30);
insert into t2 values(1,1,50);
–此时删除t1的数据,则删除t1及t2中数据

delete t1;
2 row(s) deleted.
select * from t2;
id id1 s1
No rows found.

暂无图片 评论
暂无图片 有用 0
回答交流
提交
问题信息
请登录之后查看
邀请回答
暂无人订阅该标签,敬请期待~~
暂无图片墨值悬赏