当前位置:网站首页>MySQL数据库约束与表的设计
MySQL数据库约束与表的设计
2022-08-05 22:45:00 【如风暖阳】


️前言️
本文主要介绍MySQL数据库中一些常用的约束,以及表的设计方法。
博客主页:【如风暖阳】
精品Java专栏【JavaSE】、【备战蓝桥】、【JavaEE初阶】、【MySQL】、【数据结构】
欢迎点赞收藏留言评论私信必回哟本文由 【如风暖阳】 原创,首发于 CSDN
博主将持续更新学习记录收获,友友们有任何问题可以在评论区留言

内容导读

MySQL数据库约束与表的设计
1.数据库约束
1.1 约束类型
- NOT NULL - 指示某列不能存储 NULL 值。
- UNIQUE - 保证某列的每行必须有唯一的值。
- DEFAULT - 规定没有给列赋值时的默认值。
- PRIMARY KEY - NOT NULL 和 UNIQUE 的结合。确保某列(或两个列多个列的结合)有唯一标识,有助于更容易更快速地找到表中的一个特定的记录。
- FOREIGN KEY - 保证一个表中的数据匹配另一个表中的值的参照完整性。
1.2 NULL约束
创建表时,指定某列不为空
drop table if exists student;
create table student(id int not null ,name varchar(20));

1.3 UNIQUE:唯一约束
指定某列为唯一的、不重复的。
drop table if exists student;
create table student(id int unique ,name varchar(20));

1.4 DEFAULT:默认值约束
指定插入数据时,name列为空,默认值unknown
drop table if exists student;
create table student(id int unique ,name varchar(20) default 'unknown');

1.5 PRIMARY KEY:主键约束
指定某列为主键,作为标识需要保证唯一性(主键primary key其实就是unique和not null的结合)
drop table if exists student;
create table student(id int primary key ,name varchar(20));

对于整数类型的主键,常搭配自增auto_increment来使用,默认每次加一。
drop table if exists student;
create table student(id int primary key auto_increment,name varchar(20));
insert into student values (null,'张三'),(null,'李四');
select * from student;

1.6 FOREIGN KEY:外键约束
外键将子表与父表关联起来,且被关联的父表的列必须是primary key或者unique
语法:
foreign key (字段名) references 父表(列);
比如两张表:学生表和班级表
- 班级表classes:id为主键
drop table if exists classes;
create table classes (id int primary key auto_increment,name varchar(20));
- 学生表student:一个学生对应一个班级,一个班级对应多个学生。id为主键,classes_id为外键,关联班级表id。
drop table if exists student;
create table student (id int primary key auto_increment,name varchar(20),classes_id int,foreign key (classes_id) references classes(id));
必须保证学生表里的每个记录,班级id(classes_id)必须在班级表中存在;
也就是说父表(classes)对子表(student)产生了约束,同时子表也会对父表产生约束,在删除父表中的某条记录时,如果该记录被子表引用,也无法删除(比如在classes表中有班级id 3被子表student 中的某一个学生引用,则若想直接删除父表中3这条记录,则无法直接删除)。
2.表的设计
在项目实际生产时,我们可能面对的是多个实体,所以在着手建立数据库之前,需要先根据实体的关系来完成表的设计,所以引出来下边的三大范式。
2.1 一对一
人与其账户一对一

2.2 一对多
一个班级包含多个学生,但一个学生只能属于一个班级

2.3 多对多
学生表和课程表
一个学生可以选多门课程,一个课程可以被多个学生选择
两个表多对多时常常需要第三个中间表来做关联。

️最后的话️
总结不易,希望uu们不要吝啬你们的哟(^U^)ノ~YO!!如有问题,欢迎评论区批评指正

边栏推荐
- 60: Chapter 5: Develop admin management services: 13: Develop new functions of [Add/Modify Friendship Links, Interfaces]; (Add data to MongoDB) (To operate the Dao layer interface of MongoDB, you must
- 从零开始配置 vim(8)——文件类型检测
- How to use debug to debug projects in IDEA step-by-step detailed tutorial
- Nanoprobes丨EnzMet 用于免疫组织化学 (IHC) 和印迹
- Day11:二叉树---->满~、完全~、堆
- [debian]cockpit 报错Cannot refresh cache whilst offline
- Merge Sort
- Where can I get a secure low-commission account channel
- leetcode:291. 单词规律
- Selection Sort
猜你喜欢

关于 SAP UI5 floating footer 显示与否的单步调试以及使用 SAP UI5 的收益

APS Solutions for Furniture Industry

动态内存管理

宝塔实测-电商ERP进销存系统源码

CAN-Oe channel configuration method

60:第五章:开发admin管理服务:13:开发【新增/修改友情链接,接口】的新增功能;(向MongoDB中,新增数据)(操作MongoDB的Dao层接口,得继承MongoRepository接口;)

亿级流量系统架构之如何设计高容错分布式计算系统

LeetCode 每日一题——623. 在二叉树中增加一行

过孔设计得越小就越好吗?

S32K1xx 系列安全手册
随机推荐
视觉slam学习|基础篇02
ESXI7.0 host installation Synology DS3617xs
[debian]cockpit 报错Cannot refresh cache whilst offline
On how to download m3u8/ts videos in web pages
S32K1xx 系列安全手册
Nanoprobes丨Nanogold 标记条带的凝胶染色
60: Chapter 5: Develop admin management services: 13: Develop new functions of [Add/Modify Friendship Links, Interfaces]; (Add data to MongoDB) (To operate the Dao layer interface of MongoDB, you must
error: ‘getCurrentCUDAStream’ is not a member of ‘at::cuda’ cudaStream_t stream = at::cuda::getCurre
How the Internet of Things is Driving Agriculture
[Flask] Deploy flask using gevent
【flask】模块拆分の使用红图
如何优雅的消除系统重复代码
树莓派红外控制空调
[ESP32]ESP8266连接小爱同学控制继电器二
What are the common nested queries in MySQL
计数排序(Counting Sort)
有关CRT密码反编译问题
js监听退出全屏事件
2022-8-5 第六组 ptz 集合与树
选择排序(Selection Sort)