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;

总结

相关推荐
DKPT2 小时前
JVM栈溢出和堆溢出哪个先满?
java·开发语言·jvm·笔记·学习
Rock_yzh5 小时前
AI学习日记——参数的初始化
人工智能·python·深度学习·学习·机器学习
今天只学一颗糖8 小时前
Linux学习笔记--insmod 命令
linux·笔记·学习
charlie1145141918 小时前
精读C++20设计模式:行为型设计模式:中介者模式
c++·学习·设计模式·c++20·中介者模式
楼田莉子8 小时前
Qt开发学习——QtCreator深度介绍/程序运行/开发规范/对象树
开发语言·前端·c++·qt·学习
摩羯座-185690305948 小时前
爬坑 10 年!京东店铺全量商品接口实战开发:从分页优化、SKU 关联到数据完整性闭环
linux·网络·数据库·windows·爬虫·python
Le1Yu9 小时前
2025-10-7学习笔记
java·笔记·学习
im_AMBER9 小时前
Web 开发 21
前端·学习
又是忙碌的一天9 小时前
前端学习day01
前端·学习·html
月白风清江有声9 小时前
安装适用于 GPU的NVIDIA显卡驱动及Linux GUI 应用
学习