数据库MySQL —— 约束

目录

一、概述

 二、约束演示

三、外键约束

1. 概念

2. 语法

3. 删除 / 更新行为


一、概述

  1. 概念:约束是作用于表中字段上的规则,用于限制存储在表中的数据。
  2. 目的:保证数据库中数据的正确、有效性和完整性。
  3. 分类:
约束 描述 关键字
非空约束 限制该字段的数据不能为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(7) 如果没有指定该值,默认为1 DEFAULT
gender 性别 char(7)
create table user(
    id int primary key auto_increment comment '主键',
    name varchar(10) not null unique comment '姓名',
    age int check ( age>= 0 && age <= 120 ) comment '年龄',
    status char(1) default '1' comment '状态',
    gender char(1) comment '年龄'
)comment '用户表';

-- 添加数据
insert into  user(id, name, age, status, gender)
values (1,'Tom',19,'1','男'),(2,'Rose',25,'0','女');

 报错判断:

  • 唯一约束(ID唯一):

  • 非空约束(Name不能为空):

  •  检查约束(年龄范围):

三、外键约束

1. 概念

  • 外键用来让两张表的数据之间建立连接,从而保证数据的一致性和完整性。

 注意:目前上述的两张表,在数据库层面,并未建立外键关联,所以是无法保证数据的一致性和完整性的。

2. 语法

  • 添加外键:

         ALTER TABLE 表名 ADD CONSTRANT 外键名称 FOREIGN KEY(外键字段名) REFERENCES  主表(主表列名) ;

-- 建表
create table emp(
    id int primary key auto_increment comment 'ID',
    name varchar(50) not null unique comment '姓名',
    age int comment '年龄',
    salary int  comment '薪资',
    entrydate date comment '入职时间',
    managerid int comment '直属领导id',
    dept_id int comment '部门id'
)comment '员工表';

create table dept(
    id int auto_increment comment 'ID' primary key ,
    name varchar(50) not null comment '部门名称'
)comment '部门表';

-- 添加数据
insert into dept(id, name)
values (1,'研发部'),(2,'市场部'),(3,'财务部'),(4,'销售部'),(5,'总经办');

insert into emp(id, name, age, salary, entrydate, managerid, dept_id) 
values (1,'金庸',66,'总裁',20000,'2000-01-01',null,5),
       (2,'张无忌',20,'项目经理',12500,'2005-12-05',1,1),
       (3,'杨逍',33,'开发',8400,'2000-11-03',2,1),
       (4,'韦一笑',48,'开发',11000,'2002-02-05',2,1),
       (5,'常同春',43,'开发',10500,'2004-09-07',3,1);
-- 添加外键
alter table emp add constraint fk_emp_dept_id 
      foreign key (dept_id) reference dept(id);
  • 删除外键:ALTER TABLE 表名 DROP FOREIGN KEY 外键名称;
-- 删除外键
alter table emp drop foreign key fk_emp_dept_id;

3. 删除 / 更新行为

行为 说明
NO ACTION 当在父表中删除/更新对应记录时,首先检查该记录是否有对应外键,如果有则不允许删除/更新。(与RESTRICT一致)
RESTRICT 当在父表中删除/更新对应记录时,首先检查该记录是否有对应外键,如果有则不允许删除/更新。(与NOACTION一致)
CASCADE 当在父表中删除/更新对应记录时,首先检查该记录是否有对应外键,如果有,则也删除/更新外键在子表中的记录。
SET NULL 当在父表中删除对应记录时,首先检查该记录是否有对应外键,如果有则设置子表中该外键值为null(这就要求该外键允许取nul)。
SET DEFAULT 父表有变更时,子表将外键列设置成一个默认的值(Innodb不支持)

语法:ALTER TABLE 表名 ADD CONSTRAINT 外键名称 FOREIGN KEY (外键字段) REERENCES 主表名 (主表字段名) ON UPDATE CASOADE ON DELETE CASCAOE;

-- 外键的删除和更新行为
-- cascade
alter table emp add constraint fk_emp_dept_id
    foreign key (dept_id) references dept(id)
        on update cascade on delete cascade ;
-- set null
alter table emp add constraint fk_emp_dept_id
    foreign key (dept_id) references dept(id)
        on update set null on delete set null ;

猜你喜欢

转载自blog.csdn.net/hdakj22/article/details/129696296