首页 > 数据库 >MySQL中表的约束

MySQL中表的约束

时间:2023-01-11 15:23:49浏览次数:34  
标签:comment 中表 外键 约束 key MySQL department null id

概念

约束是作用于表中字段上的规则,用于限制存储在表中的数据。

目的

保证数据库中数据的正确、有效性和完整性。

分类

约束 描述 关键字
非空约束 限制该字段的数据不能为null not null
唯一约束 保证该字段的所有数据都是唯一、不重复的 unique
主键约束 主键是一行数据的唯一标识,要求非空且唯一 primary key
默认约束 保存数据时,如果未指定该字段的值,则采用默认值 default
检查约束(8.0.16版本 之后) 保证字段值满足某一个条件 check
外键约束 用来让两张表的数据之间建立连接,保证数据的一致 性和完整性 foreign key

演示

案例需求: 根据需求,完成表结构的创建。需求如下:

字段名 字段含 义 字段类型 约束条件 约束关键字
id ID唯一 标识 int 主键,并且自动增长 primary key, auto_increment
name 姓名 varchar(10) 不为空,并且唯一 not null , unique
age 年龄 int 大于0,并且小于等 于120 check
status 状态 char(1) 如果没有指定该值, 默认为1 default
gender 性别 char(1)
create table student (
    id int primary key auto_increment comment 'ID唯一标识',
    name varchar(20) not null unique comment '姓名',
    age int check ( age>0 && age<=120 ) comment '年龄',
    status char(1) default '1' comment '状态',
    gender char(1) comment '性别'
) comment '学生表';

外键约束

/*演示数据:*/
create table department
(
    id   int auto_increment comment 'ID' primary key,
    name varchar(50) not null comment '部门名称'
) comment '部门表';
insert into department (id, name)
values (1, '研发部'),
       (2, '市场部');

create table employee
(
    id            int auto_increment comment 'ID' primary key,
    name          varchar(50) not null comment '姓名',
    entrydate     date comment '入职时间',
    department_id int comment '部门ID'
) comment '员工表';
insert into employee (id, name, entrydate, department_id)
values (1, '张三', '2023-01-05', 1),
       (2, '李四', '2023-01-06', 2),
       (3, '王五', '2023-01-07', 2),
       (4, '赵六', '2023-01-08', 2);

添加外键

create table 表名(
	字段名 数据类型,
	...
[constraint] [外键名称] foreign key (外键字段名) references 主表 (主表列名)
)
/*案例:*/
create table employee
(
    id            int auto_increment comment 'ID' primary key,
    name          varchar(50) not null comment '姓名',
    entrydate     date comment '入职时间',
    department_id int comment '部门ID',
    constraint fk_employee_department_id foreign key (department_id) references department(id)
) comment '员工表';
alter table 表名 add constraint 外键名称 foreign key (外键字段名) references 主表(主表列名);
/*案例:*/
alter table employee add constraint fk_employee_department_id foreign key (department_id) references department(id);

删除外键

alter table 表名 drop foreign key 外键名称;
/*案例:*/
alter table employee drop foreign key fk_employee_department_id;

删除/更新行为

添加了外键之后,再删除父表数据时产生的约束行为,我们就称为删除或更新行为。具体的删除/更新行为有以下几种:

行为 说明
no action 当在父表中删除/更新对应记录时,首先检查该记录是否有对应外键,如果有则不允许删除/更新。 (与restrict 一致) 默认行为
restrict 当在父表中删除/更新对应记录时,首先检查该记录是否有对应外键,如果有则不允许删除/更新。 (与no action 一致) 默认行为
cascade 当在父表中删除/更新对应记录时,首先检查该记录是否有对应外键,如果有,则也删除/更新外键在子表中的记录。
set null 当在父表中删除对应记录时,首先检查该记录是否有对应外键,如果有则设置子表中该外键值为null(这就要求该外键允许取null)。
set default 父表有变更时,子表将外键列设置成一个默认的值 (Innodb不支持)
alter table 表名 add constraint 外键名称 foreign key (外键字段) references 主表名(主表字段名) on update cascade on delete cascade;
/*案例:*/
alter table employee add constraint fk_employee_department_id foreign key (department_id) references department(id) on update cascade on delete cascade;
update department set id=6 where name='市场部'; -- 子表employee中的department_id也跟着修改了。
delete from department where id=6; -- 子表employee中的department_id=6的记录也被删除了。*/
/*在一般的业务系统中,不会修改一张表的主键值。*/
alter table 表名 add constraint 外键名称 foreign key (外键字段) references 主表名(主表字段名) on update set null on delete set null;
/*案例:*/
alter table employee add constraint fk_employee_department_id foreign key (department_id) references department(id) on update set null on delete set null;
delete from department where id=2; -- 子表employee中的department_id=2的记录的department_id=null*/

标签:comment,中表,外键,约束,key,MySQL,department,null,id
From: https://www.cnblogs.com/yellow-mokey/p/17043130.html

相关文章

  • MySQL中的group by字段理解
    若groupby后只跟了一个字段,相信大家都很好理解,就是将该字段值相同的分到一个组里;但当groupby后要是跟了多个字段,也许就会发懵了。其实,很简单,最重要的一点就是要理解g......
  • 避免 MySQL 插入重复数据的 4 种方式
    最常见的方式就是为字段设置主键或唯一索引,当插入重复数据时,抛出错误,程序终止,但这会给后续处理带来麻烦,因此需要对插入语句做特殊处理,尽量避开或忽略异常,下面我简单介绍一......
  • MySql基础-笔记2 -数据库创建、删除、选择等操作
    在MySql数据库基础1-Windows下安装配置图文教程的基础上,我们来了解如何对数据库进行操作,比如常见的创建数据库、删除数据库、选择数据库等;(目录)1、连接数据库简单的方......
  • Docker 打包MySQL (带数据源打包) 并加载打包后镜像运行
    前言mysql镜像的数据默认都在/var/lib/mysql目录下,我们修改默认的数据库的数据位置就行,不要放在/var/lib/mysql下面。操作1.创建mysql源数据备份目录mkdir/mysqldata......
  • mysql 备份定时任务
    #!/bin/bashrq=`date+%Y-%m-%d-%H`#日期#数据库信息host=127.0.0.1user=rootpassword=xxxdbname=script#放在这个目录path=/usr/local/backups/sqlmysqldump-h......
  • mysql导出表数据
    -T表-B备份数据库-t线程数-r多少行-c压缩输出文件--less-locking在InnoDB表使用最小的锁表时间导出表结构mydumper-h127.0.0.1-uroot-p*-Btest-t......
  • 实训体会--swing和mysql的使用
    结构设计思想:前端界面和后端数据库通过一个中间件操作。中间件就像一个中间助手,每一个前端界面通过一个具象的中间助手进行操作。最大的设计错误:中间件不是一个具象的实......
  • MySql树形结构(多级菜单)查询设计方案
    背景又很久没更新了,很幸运地新冠引发了严重的上呼吸道感染,大家羊过后注意休息和防护工作中(尤其是传统项目中)经常遇到这种需要,就是树形结构的查询(多级查询),常见的场景有:......
  • python操作mysql数据库,增删查改等需要执行后加commit()
    p1:关于commit方法第一感觉是这个方法只用来提交“数据”,比如插入数据、更新数据需要在execute()后面跟上一个commit();现在看来,commit()方法需要跟在增(insert)、删(delete)、......
  • MySQL UPDATE:修改数据-更新数据-在原有表基础上增加列--python
    使用UPDATE语句修改单个表,语法格式为:UPDATE<表名>SET字段1=值1[,字段2=值2…][WHERE子句][ORDERBY子句][LIMIT子句]语法说明如下:<表名>:用于指定要......