当前位置:Gxlcms > 数据库问题 > mysql-表关联和约束

mysql-表关联和约束

时间:2021-07-01 10:21:17 帮助过:30人阅读

表关联和约束
表关系:
一对一 字段设置不可重复unique
一对多 普通外键关联
多对多 中间表关联

 

创建表关联--外键:
1
create table 时,在最后面加入
constraint name foreign key(ziduan) referncese 表(字段)
关联索引名 从表字段 主表字段
2
alter table person add constraint dept_fk foreign key(dept_id) references dept(id)[on delete cascade on update cascade];

通过外键名称解除外键约束

alter table person drop foreign key dept_fk;#此时索引还存在,也需要手动解除


外键约束:
1.
从表约束字段填入内容必须是主表内存在的
2.
从表引用记录,主表不可删除(默认),可修改非引用字段内容
3.设置可删除
on delete cascade on updare cascade

主表删除更新记录,从表引用记录同步更新删除

on delete set null on update set null

主表删除更新记录,从表为null

mysql-表关联和约束

标签:表关联   从表   修改   通过   const   null   str   foreign   关联   

人气教程排行