4.mysql表约束 及 mysql库表设计范式

一:表的约束

1.1概念:

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

注意:约束是作用于表中字段上的,可以在创建表/修改表时添加约束。
  • 分类
约束 描述
非空约束 限制该字段的数据不能为null
唯一约束 保证该字段所有数据唯一且不重复
主键约束 主键是一行数据的唯一标识,要求非空且唯一
默认约束 保存数据时未指定字段值,则采用默认值
检查约束 保证字段值满足某一个条件
外键约束 让两张表的数据之间建立联系,保证数据的一致性和完整性
  • 案例
bash 复制代码
1.查看未建表前的数据库
mysql> show tables;
+--------------------+
| Tables_in_it_heima |
+--------------------+
| employer           |
| score              |
+--------------------+

2.创建表(含约束)
mysql> create table user(
    -> id int auto_increment primary key 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"性别"
    -> );
Query OK, 0 rows affected, 1 warning (0.05 sec)

3.再次查看数据库,发现表已经创建成功
mysql> show tables;
+--------------------+
| Tables_in_it_heima |
+--------------------+
| employer           |
| score              |
| user               |
+--------------------+

1.2语法:

1.3非空约束:

1.4唯一约束:

1.5列描述---comment:

1.6主键约束---not null unique:

1.7主键自增:

1.8默认约束:

1.9外键约束:

  • 概念
bash 复制代码
外键用来让两张表的数据之间建立连接,从而保证数据的一致性和完整性
子表/从表:含有外键的表被称为子表;
父表/主表:外键关联的表被称为父表。
  • 语法
bash 复制代码
添加外键:
alter table 表名 add constraint 外键名称 foreign key(外键字段名) references 主表(主表列名); 

删除外键:
alter table 表名 drop foreign key 外键名称;

外键删除/更新行为:
行为 说明
no action/restrict 系统默认:父表有外键连接时,不允许删除/更新
cascade 有外键连接时,父表删除/更新,也删除/更新子表中的记录
set null 有外键连接时,父表删除/更新,设置子表外键值为null
  • 示例
bash 复制代码
要求如下:
1.创建部门表
2.创建员工表
3.将员工的部门id与部门表id外键连接
4.删除外键
5.重新建立外键约束,并指定删除/更新行为
bash 复制代码
1.创建部门表,并输入数据
create table dep(
    id int auto_increment comment 'ID' primary key,
    name varchar(50) not null comment '部门名称'
)comment '部门表';
  输入部门数据:
insert into dep (id, name) values (1, '研发部'), (2, '市场部'), (3, '财务部'), (4, '销售部'), (5, '总经办');

2.创建员工表,并输入数据
create table emp(
    id int auto_increment comment 'ID' primary key,
    name varchar(50) not null comment '姓名',
    age int comment '年龄',
    job varchar(20) comment '职位',
    salary int comment '薪资',
    entrydate date comment '入职时间',
    managerid int comment '直属领导ID',
    dep_id int comment '部门ID'
)comment '员工表';
  输入员工数据:
insert into emp (id, name, age, job, salary, entrydate, managerid, dep_id) values
(1, '金庸', 66, '总裁', 200000, '2000-01-01', null, 5),
(2, '张无忌', 20, '项目经理', 125000, '2005-12-05', 1, 1),
(3, '杨逍', 33, '开发', 84000, '2000-11-03', 2, 1),
(4, '韦一笑', 48, '开发', 110000, '2002-02-05', 2, 1),
(5, '常遇春', 43, '开发', 105000, '2004-09-07', 3, 1),
(6, '小昭', 19, '程序员鼓励师', 6600, '2004-10-12', 2, 1);

3.创建emp表dep_id与dep表id外键
alter table emp add constraint fk_emp_dep_id foreign key(dep_id) references dep(id);

4.删除外键
alter table emp drop foreign key fk_emp_dep_id;

5-1.重新建立外键,并指定删除/更新行为:cascade
alter table emp add constraint fk_emp_dep_id foreign key(dep_id) references dep(id) on update cascade on delete cascade;
5-2.重新建立外键,并指定删除/更新行为:set null
alter table emp add constraint fk_emp_dep_id foreign key(dep_id) references dep(id) on update set null on delete set null;

1.10检查约束:

1.11删除表的约束:

1.12存储引擎:

总结---完整的建表语句:

练习:

二:mysql库表设计---范式

2.1概念:

2.2第一范式(1NF):

2.3第二范式(2NF):

2.4第三范式(3NF):

相关推荐
leoZ231几秒前
2026-09-08-mysql-57-init-walkthrough
前端·javascript·数据库·vue.js·opencv·mysql·adb
云飞云共享云桌面5 分钟前
钣金制造研发优化:不采购传统工作站,实现 SolidWorks 团队共用服务器开展三维建模
运维·服务器·网络·数据库·3d·制造
4SAPI16 分钟前
2026年大模型API接入选型指南:企业与个人用户的架构、稳定性与成本考量
大数据·开发语言·数据库·人工智能·架构·php
她说..28 分钟前
MySQL 与 Java 的 JSON 数据处理
java·mysql·json·springboot
xixiaoyunya30 分钟前
只备份数据远远不够:服务器系统备份的完整方案与实践
服务器·网络·数据库
大模型丫丫1 小时前
LangGraph + MCP(Model Context Protocol)完整讲解
java·开发语言·数据库
我不是程序员三三1 小时前
怎么监控电脑?企业办公终端监控建设实战与合规要点
服务器·数据库·电脑
这个DBA有点耶1 小时前
时间序列数据库选型2026:5款主流产品深度对比与场景适配
大数据·数据库·程序人生·架构·时序数据库·dba·数据库管理员
砚底藏山河1 小时前
拉数管道设计:从一次性脚本到可重启的数据流水线(魔码量化实战 #01)
java·数据库·python·金融·maven
2601_966377131 小时前
运营商密评密改落地实践:透明加密解决密码应用合规与安全痛点
数据库·安全·oracle