真好啊又活了一天 2022-07-21 10:42 采纳率: 0%
浏览 5

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

GBase8s如何在有外键关系的表中删除数据?有几种方式,分别是什么?

  • 写回答

1条回答 默认 最新

  • weixin_52822475 2022-07-21 13:39
    关注

    有两种方式:
    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.

    评论

报告相同问题?

问题事件

  • 创建了问题 7月21日

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序