Mysql学习(七)——约束

文章目录

  • 四、约束
    • [4.1 概述](#4.1 概述)
    • [4.2 约束演示](#4.2 约束演示)
    • [4.3 外键约束](#4.3 外键约束)
  • 总结

四、约束

4.1 概述

  • 概念:约束是作用于表中字段上的规则,用于限制存储在表中的数据。
  • 目的:保证数据库中数据的正确、有效性和完整性。
  • 分类:

4.2 约束演示

  • 根据需求,完成表的创建
sql 复制代码
create table test1(
    id int primary key auto_increment comment '主键',
    name varchar(10) not null unique comment '姓名',
    age int check ( age>0 and age<=120 ) comment '年龄',
    status char(1) default '1' comment '状态',
    gender char(1) comment '性别'
) comment '用户表';

4.3 外键约束

  • 概念:外键用来让两张表的数据之间建立连接,从而保证数据的一致性和完整性。
  • 注意:目前上述的两张表,在数据库层面并未建立外键关联,所以是无法保证数据的一致性和完整性的。

  • 语法:

sql 复制代码
-- 创建表时添加外键
create table 表名(
	字段名 数据类型,
    ...
    [constraint] [外键名称] foreign key (外键字段名) references 主表(主表列名)
);
-- 添加字段约束时添加外键
alter table 表名 add constraint 外键名称 foreign key (外键字段名) references 主表(主表列名);
-- 删除外键
alter table 表名 drop foreign key 外键名称;
  • 案例在员工表的部门id与部门表的id添加外键
sql 复制代码
-- 案例在员工表的部门id与部门表的id添加外键
create table dept(
    id int primary key auto_increment comment 'ID',
    name varchar(58) not null comment '部门名称'
) comment '部门表';
insert into dept(id, name) VALUES (1,'研发部'),(2,'市场部'),(3,'财务部'),(4,'销售部'),(5,'总经办');
select * from dept;

create table emp (
    id int primary key auto_increment comment 'ID',
    name varchar(50) not null comment '姓名',
    age int comment '年龄',
    job varchar(20) comment '职位',
    salary int comment '薪资',
    entrydate date comment '入职时间',
    managerid int comment '直属领导ID',
    dept_id int comment '部门ID'
)comment '员工表';
insert into emp(name, age, job, salary, entrydate, managerid, dept_id) VALUES ('金庸',66,'总裁',20000,'2000.1.1',null,5),
                                                                              ('张无忌',20,'项目经理',12500,'2005.12.05',1,1),
                                                                              ('杨逍',33,'开发',8400,'2000.11.3',2,1),
                                                                              ('韦一笑',48,'开发',11000,'2002.2.5',2,1),
                                                                              ('常遇春',43,'开发',10500,'2004.9.7',3,1),
                                                                              ('小昭',19,'程序员鼓励师',6600,'2004.10.12',2,1);
select * from emp;
-- 添加外键约束
alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id);
  • 删除/更新行为
  • 语法:
sql 复制代码
alter table 表名 add constraint 外键名称 foreign key (外键字段) references 主表名(主表字段名) on update cascade on delete cascade;

总结

相关推荐
不是起点的终点20 小时前
【实战】Python 一键生成数据库说明文档(对接阿里云百炼 AI,输出 Word 格式)
数据库·python·阿里云
2301_813599551 天前
Go语言怎么做秒杀系统_Go语言秒杀系统实战教程【实用】
jvm·数据库·python
NCIN EXPE1 天前
redis 使用
数据库·redis·缓存
MongoDB 数据平台1 天前
为编码代理引入 MongoDB 代理技能和插件
数据库·mongodb
极客on之路1 天前
mysql explain type 各个字段解释
数据库·mysql
代码雕刻家1 天前
MySQL与SQL Server的基本指令
数据库·mysql·sqlserver
lThE ANDE1 天前
开启mysql的binlog日志
数据库·mysql
yejqvow121 天前
CSS如何控制placeholder文字的颜色_使用--placeholder伪元素
jvm·数据库·python
handler011 天前
从源码到二进制:深度拆解 Linux 下 C 程序的编译与链接全流程
linux·c语言·开发语言·c++·笔记·学习
电子云与长程纠缠1 天前
UE5 两种方式解决Decal Actor贴花拉伸问题
学习·ue5·游戏引擎