-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
292 additions
and
292 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,21 @@ | ||
# 1.1 数据库基础 | ||
# 1.1 数据库 | ||
|
||
## 1.1.5 主键 | ||
## 表 | ||
|
||
主键就像人的身份证。 | ||
![表结构.drawio](https://csnotes.oss-cn-beijing.aliyuncs.com/photos/%E8%A1%A8%E7%BB%93%E6%9E%84.drawio.png) | ||
|
||
- 任意两行都不具有相同的主键值 | ||
- 每个行都必须有一个主键值(主键列不允许 NULL 值) | ||
页是被管理的,在内存中被 BufferManager 管理,在磁盘中被磁盘空间管理器管理。 | ||
|
||
```mysql | ||
create database 数据库名;//创建数据库 | ||
show databases;//展示数据库 | ||
show tables;//展示数据库里有多少表 | ||
show columns from 表名;//打印一张表 | ||
页是 IO 的基本单位。 | ||
|
||
``` | ||
## 页 | ||
|
||
记录有定长的和不定长的。 | ||
|
||
## Page Header | ||
|
||
页面头部。 | ||
|
||
## 紧凑型 | ||
|
||
## 非紧凑型 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,11 @@ | ||
# 1.2 数据库范式 | ||
# 1.2 MySQL 语法 | ||
|
||
## 第一范式 1NF | ||
MySQL 使用三值逻辑 —— TRUE, FALSE 和 UNKNOWN。任何与 NULL 值进行的比较都会与第三种值 UNKNOWN 做比较。这个“任何值”包括 NULL 本身!这就是为什么 MySQL 提供 IS NULL 和 IS NOT NULL 两种操作来对 NULL 特殊判断。 | ||
|
||
确保数据库字段的原子性。 | ||
|
||
例如,假设有一个员工信息表,如果将员工的姓名、地址和电话号码存储在同一列中,则这个表不符合第一范式。正确的做法是,将员工的姓名、地址和电话号码分别存储在三个单独的列中,以确保数据的单一性。 | ||
|
||
## 第二范式 2NF | ||
对于 SQL 表,用于计算字符串中字符数的最佳函数是 CHAR_LENGTH(str),它返回字符串 str 的长度。 | ||
|
||
换句话说,如果数据库表是符合第二范式的,那么它的每一行都必须仅通过主键字段来标识。这确保了数据的独立性和可维护性,避免了错误的数据冗余。 | ||
|
||
例如,假设有一个员工信息表,如果在表中存储了员工的编号,姓名和所属部门名称,则这个表不符合第二范式。正确的做法是,将员工的编号、姓名和部门编号分别存储在三个不同的表中,以确保数据的独立性。 | ||
|
||
## 第三范式 3NF | ||
另一个常用的函数 LENGTH(str) 在这个问题中也适用,因为列 content 只包含英文字符,没有特殊字符。否则,LENGTH() 可能会返回不同的结果,因为该函数返回字符串 str 的字节数,某些字符包含多于 1 个字节。 | ||
|
||
以字符 '¥' 为例:CHAR_LENGTH() 返回结果为 1,而 LENGTH() 返回结果为 2,因为该字符串包含 2 个字节。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1 @@ | ||
# 1.3 字符集 | ||
|
||
## 字符编码 | ||
|
||
- 固定长度编码方案 | ||
|
||
表示不同的字符所需要的字节数量是相同的。ASCII、UCS2 | ||
|
||
- 可变长度编码方案 | ||
|
||
表示不同的字符所需要的字节数量是相同的。UTF-8、GB2312 | ||
|
||
### utf8mb4 | ||
|
||
|
||
|
||
## MySQL 的字符串类型 | ||
|
||
| 类型 | 最大长度 | 存储空间要求 | 含义 | | ||
| ---------- | -------- | ------------ | ---------------- | | ||
| CHAR(M) | M 个字符 | MxW 字节 | 固定长度的字符串 | | ||
| VARCHAR(M) | M 个字符 | L+1 或 L+2 字节 | 可变长度的字符串 | | ||
|
||
L 表示字符串内容占用字节数。 | ||
# 1.3 查询 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
# 1.4 数据库概念 | ||
# 1.4 连接 | ||
|
||
数据库:本质是一个文件。 | ||
|
||
DBMS:数据库管理系统,操纵和管理数据库的大型软件 | ||
|
||
SQL:数据库查询语言 | ||
- 内连接(INNER JOIN)只返回两个表中都有匹配的行。 | ||
- 左连接(LEFT JOIN)返回左表(这里是 Employees 表)的所有行,如果右表(这里是 EmployeeUNI 表)中没有匹配的行,则结果集中的对应列将为 NULL。 | ||
|
||
![维恩连接](https://i0.wp.com/blog.jooq.org/wp-content/uploads/2016/07/venn-join1.png?w=700&ssl=1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,16 @@ | ||
# 1.5 最大行大小 | ||
# 1.5 LIKE 操作符 | ||
|
||
## 百分号(%)通配符 | ||
|
||
%表示任何字符出现任意次数 | ||
|
||
## 下划线(_)通配符 | ||
|
||
_表示任意字符出现一次 | ||
|
||
::: tip | ||
|
||
注意,LIKE 中'\\\_'表示'_' | ||
|
||
::: | ||
|
||
The internal representation of a table has a maximum row size of 65,535 bytes, even if the storage engine is capable of supporting larger rows. |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,19 @@ | ||
# 2.1 一条 SQL 语句是如何执行的? | ||
# 2.1 数据库基础 | ||
|
||
## 2.1.5 主键 | ||
|
||
主键就像人的身份证。 | ||
|
||
- 任意两行都不具有相同的主键值 | ||
- 每个行都必须有一个主键值(主键列不允许 NULL 值) | ||
|
||
```mysql | ||
create database 数据库名;//创建数据库 | ||
show databases;//展示数据库 | ||
show tables;//展示数据库里有多少表 | ||
show columns from 表名;//打印一张表 | ||
|
||
``` | ||
|
||
|
||
数据库不直接执行 SQL。 | ||
|
||
SQL 被转换成抽象语法树。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,16 @@ | ||
# 2.2 InnoDB 行格式 | ||
# 2.2 数据库范式 | ||
|
||
以记录为单位向表中插入数据,这些记录在磁盘上的存放形式也被称为行格式。 | ||
## 第一范式 1NF | ||
|
||
四种行格式:分为 | ||
确保数据库字段的原子性。 | ||
|
||
例如,假设有一个员工信息表,如果将员工的姓名、地址和电话号码存储在同一列中,则这个表不符合第一范式。正确的做法是,将员工的姓名、地址和电话号码分别存储在三个单独的列中,以确保数据的单一性。 | ||
|
||
## 第二范式 2NF | ||
|
||
![记录.drawio](https://csnotes.oss-cn-beijing.aliyuncs.com/photos/%E8%AE%B0%E5%BD%95.drawio.png) | ||
换句话说,如果数据库表是符合第二范式的,那么它的每一行都必须仅通过主键字段来标识。这确保了数据的独立性和可维护性,避免了错误的数据冗余。 | ||
|
||
## 2.2.1 COMPACT 行格式 | ||
例如,假设有一个员工信息表,如果在表中存储了员工的编号,姓名和所属部门名称,则这个表不符合第二范式。正确的做法是,将员工的编号、姓名和部门编号分别存储在三个不同的表中,以确保数据的独立性。 | ||
|
||
![COMPACT 行格式.drawio](https://csnotes.oss-cn-beijing.aliyuncs.com/photos/COMPACT%E8%A1%8C%E6%A0%BC%E5%BC%8F.drawio.png) | ||
## 第三范式 3NF | ||
|
||
## 2.2.2 DYNAMIC 行格式 | ||
|
||
数据库从磁盘中读写以块为单位 因为根据局部性原理。 | ||
|
||
|
||
|
||
记录被存放在数据页中。 | ||
|
||
![数据页.drawio](https://csnotes.oss-cn-beijing.aliyuncs.com/photos/%E6%95%B0%E6%8D%AE%E9%A1%B5.drawio.png) | ||
|
||
那么记录怎样排序呢 | ||
|
||
## 分组 | ||
|
||
Infimum 伪记录单独分为 1 个组 | ||
|
||
Supremum 伪记录所在分组记录条数只能在 1~8 条之间 | ||
|
||
其余分组的记录条数只能在 4~8 条之间。 | ||
|
||
每个组有一个组长和几个组员。组长的 n_owned 值记录本组成员个数。 | ||
|
||
![数据页详细.drawio](https://csnotes.oss-cn-beijing.aliyuncs.com/photos/n_owned.drawio.png) | ||
|
||
![槽](https://csnotes.oss-cn-beijing.aliyuncs.com/photos/%E6%A7%BD.png) | ||
|
||
|
||
|
||
![索引.drawio (1)](https://csnotes.oss-cn-beijing.aliyuncs.com/photos/%E7%B4%A2%E5%BC%95.drawio%20(1).png) | ||
|
||
## 主键索引 | ||
|
||
按照主键的大小对用户记录和数据页进行排序,记录用单向链表进行连接,数据页用双向链表进行连接。 | ||
|
||
主键索引是根据主键查数据 | ||
|
||
二级索引是根据 A 列查主键,再根据主键查数据 | ||
|
||
联合索引是根据 A、B 两列查主键,再根据主键查数据 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,24 @@ | ||
# 2.3 为什么数据库 VARCHAR 字段上限是 16383? | ||
# 2.3 字符集 | ||
|
||
## 字符编码 | ||
|
||
- 固定长度编码方案 | ||
|
||
表示不同的字符所需要的字节数量是相同的。ASCII、UCS2 | ||
|
||
- 可变长度编码方案 | ||
|
||
表示不同的字符所需要的字节数量是相同的。UTF-8、GB2312 | ||
|
||
### utf8mb4 | ||
|
||
|
||
|
||
## MySQL 的字符串类型 | ||
|
||
| 类型 | 最大长度 | 存储空间要求 | 含义 | | ||
| ---------- | -------- | ------------ | ---------------- | | ||
| CHAR(M) | M 个字符 | MxW 字节 | 固定长度的字符串 | | ||
| VARCHAR(M) | M 个字符 | L+1 或 L+2 字节 | 可变长度的字符串 | | ||
|
||
L 表示字符串内容占用字节数。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# 2.4 数据库概念 | ||
|
||
数据库:本质是一个文件。 | ||
|
||
DBMS:数据库管理系统,操纵和管理数据库的大型软件 | ||
|
||
SQL:数据库查询语言 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# 2.5 最大行大小 | ||
|
||
The internal representation of a table has a maximum row size of 65,535 bytes, even if the storage engine is capable of supporting larger rows. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
# 3.1 数据库数据结构 | ||
# 3.1 一条 SQL 语句是如何执行的? | ||
|
||
B+ 树但是在原有基础上将叶结点的单链表改成双链表。 | ||
数据库不直接执行 SQL。 | ||
|
||
SQL 被转换成抽象语法树。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# 3.2 InnoDB 行格式 | ||
|
||
以记录为单位向表中插入数据,这些记录在磁盘上的存放形式也被称为行格式。 | ||
|
||
四种行格式:分为 | ||
|
||
|
||
|
||
![记录.drawio](https://csnotes.oss-cn-beijing.aliyuncs.com/photos/%E8%AE%B0%E5%BD%95.drawio.png) | ||
|
||
## 3.2.1 COMPACT 行格式 | ||
|
||
![COMPACT 行格式.drawio](https://csnotes.oss-cn-beijing.aliyuncs.com/photos/COMPACT%E8%A1%8C%E6%A0%BC%E5%BC%8F.drawio.png) | ||
|
||
## 3.2.2 DYNAMIC 行格式 | ||
|
||
数据库从磁盘中读写以块为单位 因为根据局部性原理。 | ||
|
||
|
||
|
||
记录被存放在数据页中。 | ||
|
||
![数据页.drawio](https://csnotes.oss-cn-beijing.aliyuncs.com/photos/%E6%95%B0%E6%8D%AE%E9%A1%B5.drawio.png) | ||
|
||
那么记录怎样排序呢 | ||
|
||
## 分组 | ||
|
||
Infimum 伪记录单独分为 1 个组 | ||
|
||
Supremum 伪记录所在分组记录条数只能在 1~8 条之间 | ||
|
||
其余分组的记录条数只能在 4~8 条之间。 | ||
|
||
每个组有一个组长和几个组员。组长的 n_owned 值记录本组成员个数。 | ||
|
||
![数据页详细.drawio](https://csnotes.oss-cn-beijing.aliyuncs.com/photos/n_owned.drawio.png) | ||
|
||
![槽](https://csnotes.oss-cn-beijing.aliyuncs.com/photos/%E6%A7%BD.png) | ||
|
||
|
||
|
||
![索引.drawio (1)](https://csnotes.oss-cn-beijing.aliyuncs.com/photos/%E7%B4%A2%E5%BC%95.drawio%20(1).png) | ||
|
||
## 主键索引 | ||
|
||
按照主键的大小对用户记录和数据页进行排序,记录用单向链表进行连接,数据页用双向链表进行连接。 | ||
|
||
主键索引是根据主键查数据 | ||
|
||
二级索引是根据 A 列查主键,再根据主键查数据 | ||
|
||
联合索引是根据 A、B 两列查主键,再根据主键查数据 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# 3.3 为什么数据库 VARCHAR 字段上限是 16383? |
Oops, something went wrong.