zoukankan      html  css  js  c++  java
  • MySQL学习笔记——外键约束条件

    外键有4种约束

      1、restrict:立即检查外键约束

        限制,如果子表引用了附表的某个字段,如外键的引用,那么就不允许直接删除父表的该值。如果想要删除,就得先删除子表再删除父表。

      2、cascade:

        级联,update或者delete父表,子表中引用了该值所对应的记录也会被自动同步update或delete

      3、no action:

        如果子表引用了附表的某个字段,如外键的引用,那么就不允许直接删除父表的该值。如果想要删除,就得先删除子表再删除父表。

      4、set null:

        在父表上update或者delete记录时,子表中引用了该值所对应的记录将会被设置为 null,但前提是该值不能设置为 not null。

    创建示例:

    create table Employee(
        emp_id int(20)  primary key,
        emp_depid int(20) not null,
        emp_pasid int(20) not null,
        constraint fk_emp_dep foreign key(emp_depid) references department(dep_id) on delete cascade on update cascade,
        constraint fk_emp_pas foreign key(emp_pasid) references PasswordTable(pwd_id) on delete cascade on update cascade
    );

    constraint 外键名 foreign key(表内要关联的字段) references 要关联的父表(要关联的父表中的关联字段) on delete/update 约束 on delete/update 约束

    mysql创建表时不手动设置约束将会自动设置为 restrict

  • 相关阅读:
    Codeforces Round #545 (div 1.)
    THUSC 2017 大魔法师
    loj #6216. 雪花挂饰
    [NOI Online #2 提高组]涂色游戏
    [NOI Online #2 提高组]子序列问题
    [NOI Online #1 入门组]跑步
    备战noip week7
    [NOI Online #3 提高组]优秀子序列
    20201017校测
    springboot基于maven多模块项目搭建(直接启动webApplication)
  • 原文地址:https://www.cnblogs.com/lucky-jun/p/14179630.html
Copyright © 2011-2022 走看看