JimmyWang_Jl 2021-12-23 10:30 采纳率: 0%
浏览 33
已结题

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

使用GBase8s数据库,想知道GBase 8s 如何在有外键关系的表中删除数据

  • 写回答

2条回答 默认 最新

  • JimmyWang_Jl 2021-12-23 10:30
    关注

    有两种方式:
    方式一:先删除外键表中的数据,再删除当前数据
    举例:
    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.

    评论

报告相同问题?

问题事件

  • 系统已结题 12月31日
  • 创建了问题 12月23日

悬赏问题

  • ¥15 CBF预处理数据归一化的时候报错了如下图
  • ¥15 yolo v5中labelimg的作用
  • ¥15 国赛c题2021,没有理解这一串代码的意思,这样报错该怎么解决(语言-matlab)
  • ¥15 一、执行完中断程序后如何继续运行,二、中断结束后如何跳过中断触发前的点位(LOW点不要继续运行,可以运行UP点)(关键词-程序运行)
  • ¥15 if为什么跳过if 直接执行else 中文
  • ¥200 解决登录微信老版本限制封号问题
  • ¥15 mysql中时间处理问题
  • ¥20 讲解此音频放大电路原理及关键部分
  • ¥15 rtsp 转 m3u8 执行后卡在Decoding VUI
  • ¥20 微信小程序转发链接问题,已禁止转发仍能转发的场景