外键约束

简述:

  外键约束的作用就是限制字段的值必须为另一个表所包含的值。

在创建表格时约束:
有如下表:
    create table department(Id int not null primary key auto_increment,partName         varchar(15) not null );

    create table person(pId int not null primary key auto_increment,pName varchar(30)         not null ,age int not null,email varchar(50) not null,PartId int not null,constraint department_person_t1 foreign key (partId) references department(Id));

在表格已存在时创建外键约束:
    alter table person add constraint department_person_t1 foreign key (partId) references department(Id);

删除外键约束:
    alter table person drop foreign key department_person_t1;
原文地址:https://www.cnblogs.com/dontgiveup/p/9372542.html