zoukankan      html  css  js  c++  java
  • SQL级联删除——删除主表同时删除从表——同时删除具有主外键关系的表

    create table a
    (
    id  varchar(20) primary key,
    password varchar(20) not null
    )

    create table b
    (
    id int identity(1,1)  primary key,
    name varchar(50) not null,
    userId varchar(20),
    foreign key (userId) references a(id) on delete cascade
    )
    表B创建了外码userId 对应A的主码ID,声明了级联删除
    测试数据:
    insert a values ('11','aaa')
    insert a values('23','aaa')
    insert b values('da','11')
    insert b values('das','11')
    insert b values('ww','23')
    删除A表内id为‘11’的数据,发现B表内userId 为“11”也被数据库自动删除了,这就是级联删除
    delete a where id='11'

  • 相关阅读:
    csp-s模拟110
    csp-s模拟109
    留念
    csp-s 2019 游记
    HEOI2020
    CSP-S2019记
    堆积的$TIPS$
    低错复习
    倍增并查集
    4.26
  • 原文地址:https://www.cnblogs.com/fengyingwang/p/3391106.html
Copyright © 2011-2022 走看看