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

  • 相关阅读:
    快速幂取模
    程序人生系列之新闻发布系统 0105
    JavaWeb之博客系统(四)
    [转]树状数组
    题目:免费午餐
    题目:删数问题
    题目:三元组
    题目:分子团
    题目:[汪老师结婚]婚礼上的袭击
    题目:[SBN号码]
  • 原文地址:https://www.cnblogs.com/lucky-jun/p/14179630.html
Copyright © 2011-2022 走看看