zoukankan      html  css  js  c++  java
  • 级联删除与更新的例子

    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 on update 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'

    --更新A表中的id为'15',那么,B表内的userId也自动更新成了‘15’

    update a

    set id='15'

    where id='11'

  • 相关阅读:
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    星空雅梦
    MySQL-数据库三范式
    去除IDEA中xml黄色背景
    git查看commit提交记录详情
    spring-定时任务<task:scheduled-tasks>
  • 原文地址:https://www.cnblogs.com/La5DotNet/p/2572324.html
Copyright © 2011-2022 走看看