GBase 8s
运维管理
问答
GBase8s如何在有外键关系的表中删除数据?
发表于2025-12-08 16:03:0871次浏览11个评论
为提高效率,提问时请提供以下信息,问题描述清晰可优先响应。
【GBase版本】:GBase8s
【操作系统】:
【CPU】:
【问题描述】*:GBase8s如何在有外键关系的表中删除数据?
悬赏:400吉币
回答被发帖人采纳后可获得赏金
评论
登录后才可以发表评论
路路路发表于 7个月前
赞
GBase用户31099发表于 7个月前
555
GBase用户31099发表于 7个月前
888
GBase用户21182发表于 7个月前
先删子表关联数据,再删主表(按外键依赖顺序)
大力发表于 7个月前
先删子表关联数据,再删主表(按外键依赖顺序)
崔哥发表于 3个月前
春日游,杏花吹满头。陌上谁家年少足风流?妾拟将身嫁与一生休。纵被无情弃,不能羞。
茵陈发表于 2个月前
学习下。
GBase用户51934发表于 2个月前
坐等大佬解析。
热门帖子
- 12025-12-01浏览数:182764
- 22023-05-09浏览数:25062
- 42023-09-25浏览数:18526
- 52020-05-11浏览数:17529
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;
2、可以通过外键设置级联删除
--在设置外键时,设置级联删除
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.