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):

相关推荐
计算机–阿拉升学长17 分钟前
基于SpringBoot的番茄种植水肥一体化管理系统 — 系统设计与实现
java·spring boot·后端·mysql·vue·javaweb
计算机毕设定制辅导-无忧学长1 小时前
《基于SpringBoot的肩周炎健康管理系统设计与实现》
java·vue.js·spring boot·mysql·肩周炎健康管理系统
Decay__1 小时前
PostgreSQL 高可用与容灾架构 —— 流复制、Patroni、Keepalived+VIP、读写分离与两地三中心
运维·数据库·sql·postgresql
梅雅达编程笔记1 小时前
开发机能跑,服务器上跑不起来:MYD 考试系统配置设计与上线清单
mysql·nginx·flask·gunicorn·毕设项目·部署上线
Escalating_xu11 小时前
【MySQL】事务管理(上):从 ACID 到提交回滚,把一次转账真正做完整
android·数据库·mysql
不喝水就会渴13 小时前
「不想上班」实战系列:密码卡证保险箱 —— 主密钥存入 Asset,密文落库 RDB【鸿蒙心迹】
数据库·oracle
西泽XIZ913 小时前
微信小程序网络与数据库交互实践:从后端环境搭建到数据渲染
网络·数据库·微信小程序·小程序
TiDB 社区干货传送门15 小时前
平凯数据库携手东软医保共筑智慧医保,构建可靠、安全、开放的数据底座
数据库·tidb·国产数据库
YYYing.17 小时前
【Mysql剑指offer系列 (七) 】MySQL性能调优和架构
数据库·mysql·八股