上传文件至 mysql-MD
This commit is contained in:
parent
3cf5a36fb8
commit
eea3f7d9f5
|
@ -0,0 +1,412 @@
|
|||
<h1><center>数据库基础</center></h1>
|
||||
|
||||
**作者:行癫(盗版必究)**
|
||||
|
||||
------
|
||||
|
||||
## 一:数据库简介
|
||||
|
||||
#### 1.数据库技术构成
|
||||
|
||||
数据库系统 DBS
|
||||
|
||||
数据库管理系统(DataBase Management System, DBMS)
|
||||
|
||||
SQL(RDS): ORACLE、 MySQL、MariaDB、SQL-SERVER
|
||||
|
||||
NoSQL: Redis、MongoDB、Memcache
|
||||
|
||||
数据库管理员(DBA)
|
||||
|
||||
SQL语言(Structured Query Language 即结构化查询语言)
|
||||
|
||||
DDL语句 数据库定义语言: 数据库、表、视图、索引、存储过程、函数, CREATE ALTER //开发人员
|
||||
|
||||
DML语句 数据库操纵语言: 插入数据INSERT、删除数据DELETE,DROP、更新数据UPDATE //开发人员
|
||||
|
||||
DQL语句 数据库查询语言: 查询数据 SELECT
|
||||
|
||||
DCL语句 数据库控制语言: 例如控制用户的访问权限GRANT、REVOKE
|
||||
|
||||
数据访问技术
|
||||
|
||||
ODBC PHP <.php>
|
||||
|
||||
JDBC JAVA <.jsp>
|
||||
|
||||
settings.py python <.py>
|
||||
|
||||
#### 2.数据库部署
|
||||
|
||||
##### 版本
|
||||
|
||||
mysql:8.0
|
||||
|
||||
mysql:5.7
|
||||
|
||||
##### 网址
|
||||
|
||||
www.mysql.com
|
||||
|
||||
##### yum安装
|
||||
|
||||
下载mysql的yum仓库
|
||||
|
||||
```shell
|
||||
[root@xingdian ~]# wget https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm
|
||||
```
|
||||
|
||||
安装mysql的yum仓库
|
||||
|
||||
```shell
|
||||
[root@xingdian ~]# rpm -ivh https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm
|
||||
```
|
||||
|
||||
修改安装版本:
|
||||
|
||||
```shell
|
||||
方法一:命令方式
|
||||
[root@xingdian ~]# yum repolist all | grep mysql
|
||||
[root@xingdian ~]# yum -y install yum-utils
|
||||
[root@xingdian ~]# yum-config-manager --enable mysql57-community
|
||||
[root@xingdian ~]# yum-config-manager --disable mysql80-community
|
||||
|
||||
方法二:修改文件
|
||||
[root@xingdian ~]# vim /etc/yum.repos.d/mysql-community.repo
|
||||
```
|
||||
|
||||
安装mysql
|
||||
|
||||
```shell
|
||||
[root@xingdian ~]# yum -y install mysql mysql-server
|
||||
```
|
||||
|
||||
查看初始密码
|
||||
|
||||
```shell
|
||||
[root@xingdian ~]# grep 'password' /var/log/mysqld.log
|
||||
2019-07-13T15:14:31.176905Z 1 [Note] A temporary password is generated for root@localhost: k12zPB1r;2Ta
|
||||
```
|
||||
|
||||
修改密码
|
||||
|
||||
```shell
|
||||
[root@xingdian ~]# mysqladmin -u root -p'k12zPB1r;2Ta' password 'QianFeng@123'
|
||||
密码:大小写有特殊字符数字
|
||||
```
|
||||
|
||||
##### 编译安装
|
||||
|
||||
准备编译所需的环境
|
||||
|
||||
```shell
|
||||
[root@xingdian ~]# yum -y install ncurses ncurses-devel openssl-devel bison gcc gcc-c++ make cmake
|
||||
```
|
||||
|
||||
准备编译所需的安装包
|
||||
|
||||
```shell
|
||||
[root@xingdian ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.39.tar.gz
|
||||
[root@xingdian ~]# tar xf mysql-boost-5.7.39.tar.gz
|
||||
```
|
||||
|
||||
![image-20220919132739384](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20220919132739384.png)
|
||||
|
||||
![image-20220919132806056](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20220919132806056.png)
|
||||
|
||||
![image-20220919132826328](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20220919132826328.png)
|
||||
|
||||
![](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20220919133151912.png)
|
||||
|
||||
清理系统残留并创建新用户【可选操作】
|
||||
|
||||
```shell
|
||||
删除:
|
||||
[root@xingdian ~]# userdel -r mysql
|
||||
[root@xingdian ~]# yum -y remove mariadb mariadb-libs mariadb-server mariadb-devel
|
||||
[root@xingdian ~]# rm -rf /etc/my*
|
||||
[root@xingdian ~]# rm -rf /var/lib/mysql
|
||||
[root@xingdian ~]# rm -rf /var/log/mysql*
|
||||
创建:
|
||||
[root@xingdian ~]# groupadd mysql
|
||||
[root@xingdian ~]# useradd -r -g mysql -s /bin/nolgin mysql
|
||||
```
|
||||
|
||||
配置
|
||||
|
||||
```shell
|
||||
[root@mysql-5.7.26 ~]# cmake . \
|
||||
-DWITH_BOOST=boost_1_59_0/ \
|
||||
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
|
||||
-DSYSCONFDIR=/etc \指定安装目录配置文件的位置,默认就是etc
|
||||
-DMYSQL_DATADIR=/usr/local/mysql/data \数据目录 错误日志文件
|
||||
-DINSTALL_MANDIR=/usr/share/man \ 帮助文档的目录
|
||||
-DMYSQL_TCP_PORT=3306 \ 默认端口号3306
|
||||
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \ 用来做网络通信,启动的时候才会产生
|
||||
-DDEFAULT_CHARSET=utf8 \默认字符集
|
||||
-DEXTRA_CHARSETS=all \
|
||||
-DDEFAULT_COLLATION=utf8_general_ci \
|
||||
-DWITH_READLINE=1 \可以上下翻历史命令
|
||||
-DWITH_SSL=system \
|
||||
-DWITH_EMBEDDED_SERVER=1 \ 嵌入式服务器
|
||||
-DENABLED_LOCAL_INFILE=1 \ 支持从本机导入
|
||||
-DWITH_INNOBASE_STORAGE_ENGINE=1 //默认存储引擎
|
||||
|
||||
提示:boost也可以使用如下指令自动下载
|
||||
-DDOWNLOAD_BOOST=1
|
||||
|
||||
模板:
|
||||
cmake . \
|
||||
-DWITH_BOOST=boost/boost_1_59_0/ \
|
||||
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
|
||||
-DSYSCONFDIR=/etc \
|
||||
-DMYSQL_DATADIR=/usr/local/mysql/data \
|
||||
-DINSTALL_MANDIR=/usr/share/man \
|
||||
-DMYSQL_TCP_PORT=3306 \
|
||||
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
|
||||
-DDEFAULT_CHARSET=utf8 \
|
||||
-DEXTRA_CHARSETS=all \
|
||||
-DDEFAULT_COLLATION=utf8_general_ci \
|
||||
-DWITH_READLINE=1 \
|
||||
-DWITH_SSL=system \
|
||||
-DWITH_EMBEDDED_SERVER=1 \
|
||||
-DENABLED_LOCAL_INFILE=1 \
|
||||
-DWITH_INNOBASE_STORAGE_ENGINE=1
|
||||
```
|
||||
|
||||
编译和安装
|
||||
|
||||
```shell
|
||||
[root@mysql-5.7.26 ~]# make && make install
|
||||
```
|
||||
|
||||
初始化
|
||||
|
||||
```shell
|
||||
[root@xingdian ~]# cd /usr/local/mysql 把这个删了就相当于卸载
|
||||
[root@xingdian ~]# mkdir mysql-files
|
||||
[root@xingdian ~]# chown -R mysql.mysql /usr/local/mysql
|
||||
[root@xingdian ~]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
|
||||
初始化,只需要初始化一次
|
||||
```
|
||||
|
||||
启动mysql
|
||||
|
||||
```shell
|
||||
[root@xingdian ~]# ./bin/mysqld_safe --user=mysql & (后台运行)
|
||||
[root@xingdian ~]# ./bin/mysqladmin -u root -p'原密码' password 123
|
||||
```
|
||||
|
||||
客户端测试
|
||||
|
||||
```shell
|
||||
[root@xingdian ~]# ./bin/mysql -u root -p '密码'
|
||||
```
|
||||
|
||||
扩展
|
||||
|
||||
```shell
|
||||
设置环境变量;可以直接使用mysql相关命令:
|
||||
[root@xingdian ~]# echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
|
||||
[root@xingdian ~]# source /etc/profile
|
||||
|
||||
设置开机启动,并用systemctl管理mysql服务:
|
||||
[root@xingdian ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
|
||||
[root@xingdian ~]# chkconfig mysqld on //做开机启动
|
||||
[root@xingdian ~]# systemctl start mysqld
|
||||
[root@xingdian ~]# systemctl stop mysqld
|
||||
```
|
||||
|
||||
#### 3.mysql基础
|
||||
|
||||
##### 编译安装
|
||||
|
||||
```shell
|
||||
[root@47ed2bec974d mysql]# ls
|
||||
COPYING README bin include mysql-test support-files
|
||||
COPYING-test README-test docs lib share
|
||||
```
|
||||
|
||||
bin目录:用于放置一些可执行文件,如mysql、mysqld、mysqlbinlog等
|
||||
|
||||
include目录:用于放置一些头文件,如:mysql.h、mysql_ername.h等
|
||||
|
||||
lib目录:用于放置一系列库文件
|
||||
|
||||
share目录:用于存放字符集、语言等信息。
|
||||
|
||||
##### yum安装
|
||||
|
||||
/var/lib/mysql 存放数据文件
|
||||
|
||||
/usr/share/mysql 用于存放字符集、语言等信息
|
||||
|
||||
##### 配置文件基本参数
|
||||
|
||||
```shell
|
||||
[root@mysql1 mysql]# vim /etc/my.cnf
|
||||
[mysqld]
|
||||
basedir=/usr/local/mysql
|
||||
datadir=/usr/local/mysql/data
|
||||
log-error=/var/log/mysqld.log
|
||||
pid-file=/var/run/mysqld/mysqld.pid
|
||||
validate_password=off 添加后可设置弱密码强度
|
||||
lower_case_table_names=1 不区分大小写
|
||||
|
||||
扩展:
|
||||
key_buffer = 384M //key_buffer是用于索引块的缓冲区大小,增加它可得到更好处理的索引(对所有读和多重写)。索引被所有的线程共享,key_buffer的大小视内存大小而定
|
||||
table_open_cache = 512 //MySQL每打开一个表,都会读入一些数据到table_open_cache缓存中,当MySQL在这个缓存中找不到相应信息时,才会去磁盘上读取。默认值64, 假定系统有200个并发连接,则需将此参数设置为200*N(N为每个连接所需的文件描述符数目);当把table_open_cache设置为很大时,如果系统处理不了那么多文件描述符,那么就会出现客户端失效,连接不上
|
||||
max_allowed_packet = 4M //接受的数据包大小;增加该变量的值十分安全,这是因为仅当需要时才会分配额外内存。例如,仅当你发出长查询或MySQLd必须返回大的结果行时MySQLd才会分配更多内存。 该变量之所以取较小默认值是一种预防措施,以捕获客户端和服务器之间的错误信息包,并确保不会因偶然使用大的信息包而导致内存溢出
|
||||
sort_buffer_size = 2M //MySQL执行排序使用的缓冲大小。如果想要增加ORDER BY的速度,首先看是否可以让MySQL使用索引而不是额外的排序阶段。如果不能,可以尝试增加sort_buffer_size变量的大小
|
||||
read_buffer_size = 2M //读查询操作所能使用的缓冲区大小。和sort_buffer_size一样,该参数对应的分配内存也是每连接独享。对表进行顺序扫描的请求将分配一个读入缓冲区,MySQL会为它分配一段内存缓冲区。 如果对表的顺序扫描请求非常频繁,并且你认为频繁扫描进行得太慢,可以通过增加该变量值以及内存缓冲区大小提高其性能
|
||||
thread_concurrency = 8 //最大并发线程数,取值为服务器逻辑CPU数量×2
|
||||
max_connections = 1000 //MySQL的最大连接数,如果服务器的并发连接请求量比较大,建议调高此值,以增加并行连接数量,当然这建立在机器能支撑的情况下,因为如果连接数越多,介于MySQL会为每个连接提供连接缓冲区,就会开销越多的内存,所以要适当调整该值,不能盲目提高设值
|
||||
```
|
||||
|
||||
#### 4.数据库引擎
|
||||
|
||||
数据库存储引擎是数据库底层软件组织,数据库管理系统(DBMS)使用数据引擎进行创建、查询、更新和删除数据。不同的存储引擎提供不同的存储机制、索引技巧、锁定水平等功能,使用不同的存储引擎,还可以 获得特定的功能。现在许多不同的数据库管理系统都支持多种不同的数据引擎。MySQL的核心就是存储引擎
|
||||
|
||||
##### InnoDB存储引擎
|
||||
|
||||
InnoDB是事务型数据库的首选引擎,支持事务安全表(ACID),支持行锁定和外键;InnoDB是默认的MySQL引擎
|
||||
|
||||
特点:
|
||||
|
||||
支持事务处理,支持外键,支持崩溃修复能力和并发控制。如果需要对事务的完整性要求比较高(比如银行),要求实现并发控制(比如售票),那选择InnoDB有很大的优势。如果需要频繁的更新、删除操作的数据库,也可以选择InnoDB,因为支持事务的提交和回滚
|
||||
|
||||
##### MyISAM存储引擎
|
||||
|
||||
MyISAM基于ISAM存储引擎,并对其进行扩展。它是在Web、数据仓储和其他应用环境下最常使用的存储引擎之一。MyISAM拥有较高的插入、查询速度,但不支持事务
|
||||
|
||||
特点:
|
||||
|
||||
插入数据快,空间和内存使用比较低。如果表主要是用于插入新记录和读出记录,那么选择MyISAM能实现处理高效率。如果应用的完整性、并发性要求比较低,也可以使用
|
||||
|
||||
##### MEMORY存储引擎
|
||||
|
||||
MEMORY存储引擎将表中的数据存储到内存中,为查询和引用其他表数据提供快速访问
|
||||
|
||||
特点:
|
||||
|
||||
所有的数据都在内存中,数据的处理速度快,但是安全性不高。如果需要很快的读写速度,对数据的安全性要求较低,可以选择MEMOEY。它对表的大小有要求,不能建立太大的表。所以,这类数据库只使用在相对较小的数据库表
|
||||
|
||||
##### 如何选择引擎
|
||||
|
||||
如果要提供提交、回滚、崩溃恢复能力的事物安全(ACID兼容)能力,并要求实现并发控制,InnoDB是一个好的选择;如果数据表主要用来插入和查询记录,则MyISAM引擎能提供较高的处理效率;如果只是临时存放数据,数据量不大,并且不需要较高的数据安全性,可以选择将数据保存在内存中的Memory引擎;MySQL中使用该引擎作为临时表,存放查询的中间结果;如果只有INSERT和SELECT操作,可以选择Archive,Archive支持高并发的插入操作,但是本身不是事务安全的。Archive非常适合存储归档数据,如记录日志信息可以使用Archive。
|
||||
使用哪一种引擎需要灵活选择,一个数据库中多个表可以使用不同引擎以满足各种性能和实际需求,使用合适的存储引擎,将会提高整个数据库的性能
|
||||
|
||||
##### 存储引擎查看
|
||||
|
||||
```shell
|
||||
MySQL [(none)]> show engines;
|
||||
+--------------------+---------+----------------------------------------------------------------+-----
|
||||
| Engine | Support | Comment | Transactions | XA | Savepoints |
|
||||
+--------------------+---------+----------------------------------------------------------------+-----
|
||||
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
|
||||
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
|
||||
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
|
||||
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
|
||||
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
|
||||
| CSV | YES | CSV storage engine | NO | NO | NO |
|
||||
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
|
||||
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
|
||||
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
|
||||
```
|
||||
|
||||
Support列的值表示某种引擎是否能使用:YES表示可以使用、NO表示不能使用、DEFAULT表示该引擎为当前默认的存储引擎
|
||||
|
||||
#### 5.mysql事务
|
||||
|
||||
MySQL 事务主要用于处理操作量大,复杂度高的数据。比如说,在人员管理系统中,你删除一个人员,你即需要删除人员的基本资料,也要删除和该人员相关的信息,如信箱,文章等等,这样,这些数据库操作语句就构成一个事务!
|
||||
|
||||
在 MySQL 中只有使用了 Innodb 数据库引擎的数据库或表才支持事务
|
||||
|
||||
事务处理可以用来维护数据库的完整性,保证成批的 SQL 语句要么全部执行,要么全部不执行
|
||||
|
||||
事务用来管理 insert,update,delete 语句
|
||||
|
||||
##### 满足事务条件
|
||||
|
||||
原子性:一个事务(transaction)中的所有操作,要么全部完成,要么全部不完成,不会结束在中间某个环节。事务在执行过程中发生错误,会被回滚(Rollback)到事务开始前的状态,就像这个事务从来没有执行过一样
|
||||
|
||||
一致性:在事务开始之前和事务结束以后,数据库的完整性没有被破坏。这表示写入的资料必须完全符合所有的预设规则,这包含资料的精确度、串联性以及后续数据库可以自发性地完成预定的工作
|
||||
|
||||
隔离性:数据库允许多个并发事务同时对其数据进行读写和修改的能力,隔离性可以防止多个事务并发执行时由于交叉执行而导致数据的不一致。事务隔离分为不同级别,包括读未提交(Read uncommitted)、读提交(read committed)、可重复读(repeatable read)和串行化(Serializable)
|
||||
|
||||
持久性:事务处理结束后,对数据的修改就是永久的,即便系统故障也不会丢失
|
||||
|
||||
##### 了解
|
||||
|
||||
```sshell
|
||||
MYSQL 事务处理主要有两种方法:
|
||||
1、用 BEGIN, ROLLBACK, COMMIT来实现
|
||||
• BEGIN 开始一个事务
|
||||
• ROLLBACK 事务回滚
|
||||
• COMMIT 事务确认
|
||||
2、直接用 SET 来改变 MySQL 的自动提交模式:
|
||||
• SET AUTOCOMMIT=0 禁止自动提交
|
||||
• SET AUTOCOMMIT=1 开启自动提交
|
||||
|
||||
>show variables like 'autocommit'; //查看是否修改成功
|
||||
|
||||
注意:在编写应用程序时,最好事务的控制权限交给开发人员
|
||||
```
|
||||
|
||||
#### 6.数据库锁
|
||||
|
||||
【阅读了解】
|
||||
|
||||
锁是计算机协调多个进程或纯线程并发访问某一资源的机制。在数据库中,除传统的计算资源(CPU、RAM、I/O)的争用以外,数据也是一种供许多用户共享的资源。如何保证数据并发访问的一致性、有效性是所在有数据库必须解决的一个问题,锁冲突也是影响数据库并发访问性能的一个重要因素。从这个角度来说,锁对数据库而言显得尤其重要,也更加复杂;相对其他数据库而言,MySQL的锁机制比较简单,其最显著的特点是不同的存储引擎支持不同的锁机制
|
||||
|
||||
MyISAM在执行查询语句(SELECT)前,会自动给涉及的所有表加读锁,在执行更新操作(UPDATE、DELETE、INSERT等)前,会自动给涉及的表加写锁,这个过程并不需要用户干预,因此用户一般不需要直接用LOCK TABLE命令给MyISAM表显式加锁。给MyISAM表显示加锁,一般是为了一定程度模拟事务操作,实现对某一时间点多个表的一致性读取
|
||||
|
||||
##### 锁分类
|
||||
|
||||
表级锁:开销小,加锁快;不会出现死锁;锁定粒度大,发生锁冲突的概率最高,并发度最低
|
||||
|
||||
行级锁:开销大,加锁慢;会出现死锁;锁定粒度最小,发生锁冲突的概率最低,并发度也最高
|
||||
|
||||
页面锁:开销和加锁时间界于表锁和行锁之间;会出现死锁;锁定粒度界于表锁和行锁之间,并发度一般
|
||||
|
||||
##### 案例
|
||||
|
||||
有一个订单表orders,其中记录有订单的总金额total,同时还有一个订单明细表order_detail,其中记录有订单每一产品的金额小计subtotal,假设我们需要检查这两个表的金额合计是否相等,可能就需要执行如下两条SQL:
|
||||
|
||||
```shell
|
||||
MySQL [(none)]> SELECT SUM(total) FROM orders;
|
||||
MySQL [(none)]> SELECT SUM(subtotal) FROM order_detail;
|
||||
```
|
||||
|
||||
这时,如果不先给这两个表加锁,就可能产生错误的结果,因为第一条语句执行过程中order_detail表可能已经发生了改变。因此,正确的方法应该是:
|
||||
|
||||
```shell
|
||||
MySQL [(none)]> LOCK tables orders read local,order_detail read local;
|
||||
MySQL [(none)]> SELECT SUM(total) FROM orders;
|
||||
MySQL [(none)]> SELECT SUM(subtotal) FROM order_detail;
|
||||
MySQL [(none)]> Unlock tables;
|
||||
```
|
||||
|
||||
上面的例子在LOCK TABLES时加了‘local’选项,其作用就是在满足MyISAM表并发插入条件的情况下,允许其他用户在表尾插入记录
|
||||
|
||||
在用LOCKTABLES给表显式加表锁是时,必须同时取得所有涉及表的锁,并且MySQL支持锁升级;也就是说,在执行LOCK TABLES后,只能访问显式加锁的这些表,不能访问未加锁的表;同时,如果加的是读锁,那么只能执行查询操作,而不能执行更新操作。其实,在自动加锁的情况下也基本如此,MySQL问题一次获得SQL语句所需要的全部锁
|
||||
|
||||
#### 7.外键
|
||||
|
||||
【阅读了解】
|
||||
|
||||
在实际开发的项目中,一个健壮数据库中的数据一定有很好的参照完整性。例如学生档案和成绩单两张表,如果成绩单中有张三的成绩,学生档案中张三的档案却被删除了,这样就会产生垃圾数据或者错误数据。为了保证数据的完整性,将两张表之间的数据建立关系,因此就需要在成绩表中添加外键约束
|
||||
|
||||
外键是指引用另外一个表中的一列或多列数据,被引用的列应该具有主键约束或者唯一性约束。外键用来建立和加强两个表数据之间的连接
|
||||
|
||||
##### 外键的定义
|
||||
|
||||
外键是某个表中的一列,它包含在另一个表的主键中
|
||||
|
||||
外键也是索引的一种,是通过一张表中的一列指向另一张表中的主键,来对两张表进行关联
|
||||
|
||||
一张表可以有一个外键,也可以存在多个外键,与多张表进行关联
|
||||
|
||||
##### 外键的作用
|
||||
|
||||
外键的主要作用是保证数据的一致性和完整性,并且减少数据冗余
|
|
@ -0,0 +1,975 @@
|
|||
<h1><center>数据库查询</center></h1>
|
||||
|
||||
**作者:行癫(盗版必究)**
|
||||
|
||||
------
|
||||
|
||||
## 一:基本查询
|
||||
|
||||
#### 1.简介
|
||||
|
||||
单表查询
|
||||
|
||||
简单查询
|
||||
|
||||
通过条件查询
|
||||
|
||||
查询排序
|
||||
|
||||
限制查询记录数
|
||||
|
||||
使用集合函数查询
|
||||
|
||||
分组查询
|
||||
|
||||
使用正则表达式查询
|
||||
|
||||
#### 2.案例
|
||||
|
||||
创建案例所需表:company.employee5
|
||||
|
||||
```shell
|
||||
雇员编号 id int
|
||||
雇员姓名 name varchar(30)
|
||||
雇员性别 sex enum
|
||||
雇用时期 hire_date date
|
||||
职位 post varchar(50)
|
||||
职位描述 job_description varchar(100)
|
||||
薪水 salary double(15,2)
|
||||
办公室 office int
|
||||
部门编号 dep_id int
|
||||
```
|
||||
|
||||
```shell
|
||||
MySQL [(none)]> CREATE TABLE company.employee5(
|
||||
id int primary key AUTO_INCREMENT not null,
|
||||
name varchar(30) not null,
|
||||
sex enum('male','female') default 'male' not null,
|
||||
hire_date date not null,
|
||||
post varchar(50) not null,
|
||||
job_description varchar(100),
|
||||
salary double(15,2) not null,
|
||||
office int,
|
||||
dep_id int
|
||||
);
|
||||
```
|
||||
|
||||
插入模拟数据:
|
||||
|
||||
```shell
|
||||
MySQL [(none)]> insert into company.employee5(name,sex,hire_date,post,job_description,salary,office,dep_id) values
|
||||
('jack','male','20180202','instructor','teach',5000,501,100),
|
||||
('tom','male','20180203','instructor','teach',5500,501,100),
|
||||
('robin','male','20180202','instructor','teach',8000,501,100),
|
||||
('alice','female','20180202','instructor','teach',7200,501,100),
|
||||
('','male','20180202','hr','hrcc',600,502,101),
|
||||
('harry','male','20180202','hr',NULL,6000,502,101),
|
||||
('emma','female','20180206','sale','salecc',20000,503,102),
|
||||
('christine','female','20180205','sale','salecc',2200,503,102),
|
||||
('zhuzhu','male','20180205','sale',NULL,2200,503,102),
|
||||
('gougou','male','20180205','sale','',2200,503,102);
|
||||
```
|
||||
|
||||
语法格式:
|
||||
|
||||
select 字段名称,字段名称2...... from 表名 [条件]
|
||||
|
||||
##### a.简单查询
|
||||
|
||||
```shell
|
||||
MySQL [company]> select * from employee5;
|
||||
+----+-----------+--------+------------+------------+-----------------+----------+--------+--------+
|
||||
| id | name | sex | hire_date | post | job_description | salary | office | dep_id |
|
||||
+----+-----------+--------+------------+------------+-----------------+----------+--------+--------+
|
||||
| 1 | jack | male | 2018-02-02 | instructor | teach | 5000.00 | 501 | 100 |
|
||||
| 2 | tom | male | 2018-02-03 | instructor | teach | 5500.00 | 501 | 100 |
|
||||
| 3 | robin | male | 2018-02-02 | instructor | teach | 8000.00 | 501 | 100 |
|
||||
| 4 | alice | female | 2018-02-02 | instructor | teach | 7200.00 | 501 | 100 |
|
||||
| 5 | | male | 2018-02-02 | hr | hrcc | 600.00 | 502 | 101 |
|
||||
| 6 | harry | male | 2018-02-02 | hr | NULL | 6000.00 | 502 | 101 |
|
||||
| 7 | emma | female | 2018-02-06 | sale | salecc | 20000.00 | 503 | 102 |
|
||||
| 8 | christine | female | 2018-02-05 | sale | salecc | 2200.00 | 503 | 102 |
|
||||
| 9 | zhuzhu | male | 2018-02-05 | sale | NULL | 2200.00 | 503 | 102 |
|
||||
| 10 | gougou | male | 2018-02-05 | sale | | 2200.00 | 503 | 102 |
|
||||
+----+-----------+--------+------------+------------+-----------------+----------+--------+--------+
|
||||
10 rows in set (0.00 sec)
|
||||
|
||||
MySQL [company]> select name, salary, dep_id from employee5 where id <=5;
|
||||
+-------+---------+--------+
|
||||
| name | salary | dep_id |
|
||||
+-------+---------+--------+
|
||||
| jack | 5000.00 | 100 |
|
||||
| tom | 5500.00 | 100 |
|
||||
| robin | 8000.00 | 100 |
|
||||
| alice | 7200.00 | 100 |
|
||||
| | 600.00 | 101 |
|
||||
+-------+---------+--------+
|
||||
5 rows in set (0.00 sec)
|
||||
```
|
||||
|
||||
##### b.避免重复
|
||||
|
||||
不能部分使用DISTINCT,通常仅用于某一字段
|
||||
|
||||
```shell
|
||||
MySQL [company]> SELECT post FROM employee5;
|
||||
+------------+
|
||||
| post |
|
||||
+------------+
|
||||
| instructor |
|
||||
| instructor |
|
||||
| instructor |
|
||||
| instructor |
|
||||
| hr |
|
||||
| hr |
|
||||
| sale |
|
||||
| sale |
|
||||
| sale |
|
||||
| sale |
|
||||
+------------+
|
||||
10 rows in set (0.00 sec)
|
||||
|
||||
MySQL [company]> SELECT distinct post FROM employee5;
|
||||
+------------+
|
||||
| post |
|
||||
+------------+
|
||||
| instructor |
|
||||
| hr |
|
||||
| sale |
|
||||
+------------+
|
||||
3 rows in set (0.00 sec)
|
||||
```
|
||||
|
||||
##### c.四则运算查询
|
||||
|
||||
```shell
|
||||
MySQL [company]> SELECT name, salary, salary*14 FROM employee5;
|
||||
+-----------+----------+-----------+
|
||||
| name | salary | salary*14 |
|
||||
+-----------+----------+-----------+
|
||||
| jack | 5000.00 | 70000.00 |
|
||||
| tom | 5500.00 | 77000.00 |
|
||||
| robin | 8000.00 | 112000.00 |
|
||||
| alice | 7200.00 | 100800.00 |
|
||||
| | 600.00 | 8400.00 |
|
||||
| harry | 6000.00 | 84000.00 |
|
||||
| emma | 20000.00 | 280000.00 |
|
||||
| christine | 2200.00 | 30800.00 |
|
||||
| zhuzhu | 2200.00 | 30800.00 |
|
||||
| gougou | 2200.00 | 30800.00 |
|
||||
+-----------+----------+-----------+
|
||||
10 rows in set (0.01 sec)
|
||||
|
||||
MySQL [company]> SELECT name, salary, salary*14 AS Annual_salary FROM employee5;
|
||||
+-----------+----------+---------------+
|
||||
| name | salary | Annual_salary |
|
||||
+-----------+----------+---------------+
|
||||
| jack | 5000.00 | 70000.00 |
|
||||
| tom | 5500.00 | 77000.00 |
|
||||
| robin | 8000.00 | 112000.00 |
|
||||
| alice | 7200.00 | 100800.00 |
|
||||
| | 600.00 | 8400.00 |
|
||||
| harry | 6000.00 | 84000.00 |
|
||||
| emma | 20000.00 | 280000.00 |
|
||||
| christine | 2200.00 | 30800.00 |
|
||||
| zhuzhu | 2200.00 | 30800.00 |
|
||||
| gougou | 2200.00 | 30800.00 |
|
||||
+-----------+----------+---------------+
|
||||
10 rows in set (0.00 sec)
|
||||
|
||||
MySQL [company]> SELECT name, salary, salary*14 Annual_salary FROM employee5;
|
||||
+-----------+----------+---------------+
|
||||
| name | salary | Annual_salary |
|
||||
+-----------+----------+---------------+
|
||||
| jack | 5000.00 | 70000.00 |
|
||||
| tom | 5500.00 | 77000.00 |
|
||||
| robin | 8000.00 | 112000.00 |
|
||||
| alice | 7200.00 | 100800.00 |
|
||||
| | 600.00 | 8400.00 |
|
||||
| harry | 6000.00 | 84000.00 |
|
||||
| emma | 20000.00 | 280000.00 |
|
||||
| christine | 2200.00 | 30800.00 |
|
||||
| zhuzhu | 2200.00 | 30800.00 |
|
||||
| gougou | 2200.00 | 30800.00 |
|
||||
+-----------+----------+---------------+
|
||||
10 rows in set (0.00 sec)
|
||||
```
|
||||
|
||||
##### d.定义显示格式
|
||||
|
||||
CONCAT() 函数用于连接字符串
|
||||
|
||||
```shell
|
||||
MySQL [company]> SELECT concat(name, 's annual salary: ', salary*14) AS Annual_salary FROM employee5;
|
||||
+------------------------------------+
|
||||
| Annual_salary |
|
||||
+------------------------------------+
|
||||
| jacks annual salary: 70000.00 |
|
||||
| toms annual salary: 77000.00 |
|
||||
| robins annual salary: 112000.00 |
|
||||
| alices annual salary: 100800.00 |
|
||||
| s annual salary: 8400.00 |
|
||||
| harrys annual salary: 84000.00 |
|
||||
| emmas annual salary: 280000.00 |
|
||||
| christines annual salary: 30800.00 |
|
||||
| zhuzhus annual salary: 30800.00 |
|
||||
| gougous annual salary: 30800.00 |
|
||||
+------------------------------------+
|
||||
10 rows in set (0.00 sec)
|
||||
```
|
||||
|
||||
##### e.单条件查询
|
||||
|
||||
```shell
|
||||
MySQL [company]> SELECT name,post FROM employee5 WHERE post='hr';
|
||||
+-------+------+
|
||||
| name | post |
|
||||
+-------+------+
|
||||
| | hr |
|
||||
| harry | hr |
|
||||
+-------+------+
|
||||
2 rows in set (0.00 sec)
|
||||
```
|
||||
|
||||
##### f.多条件查询
|
||||
|
||||
```shell
|
||||
MySQL [company]> SELECT name,salary FROM employee5 WHERE post='hr' AND salary>10000;
|
||||
Empty set (0.00 sec)
|
||||
|
||||
MySQL [company]> select * from employee5 where salary>5000 and salary<10000 or dep_id=102;
|
||||
+----+-----------+--------+------------+------------+-----------------+----------+--------+--------+
|
||||
| id | name | sex | hire_date | post | job_description | salary | office | dep_id |
|
||||
+----+-----------+--------+------------+------------+-----------------+----------+--------+--------+
|
||||
| 2 | tom | male | 2018-02-03 | instructor | teach | 5500.00 | 501 | 100 |
|
||||
| 3 | robin | male | 2018-02-02 | instructor | teach | 8000.00 | 501 | 100 |
|
||||
| 4 | alice | female | 2018-02-02 | instructor | teach | 7200.00 | 501 | 100 |
|
||||
| 6 | harry | male | 2018-02-02 | hr | NULL | 6000.00 | 502 | 101 |
|
||||
| 7 | emma | female | 2018-02-06 | sale | salecc | 20000.00 | 503 | 102 |
|
||||
| 8 | christine | female | 2018-02-05 | sale | salecc | 2200.00 | 503 | 102 |
|
||||
| 9 | zhuzhu | male | 2018-02-05 | sale | NULL | 2200.00 | 503 | 102 |
|
||||
| 10 | gougou | male | 2018-02-05 | sale | | 2200.00 | 503 | 102 |
|
||||
+----+-----------+--------+------------+------------+-----------------+----------+--------+--------+
|
||||
8 rows in set (0.00 sec)
|
||||
```
|
||||
|
||||
##### g.关键字
|
||||
|
||||
BETWEEN AND
|
||||
|
||||
```shell
|
||||
MySQL [company]> SELECT name,salary FROM employee5 WHERE salary BETWEEN 5000 AND 15000;
|
||||
+-------+---------+
|
||||
| name | salary |
|
||||
+-------+---------+
|
||||
| jack | 5000.00 |
|
||||
| tom | 5500.00 |
|
||||
| robin | 8000.00 |
|
||||
| alice | 7200.00 |
|
||||
| harry | 6000.00 |
|
||||
+-------+---------+
|
||||
5 rows in set (0.00 sec)
|
||||
|
||||
MySQL [company]> SELECT name,salary FROM employee5 WHERE salary NOT BETWEEN 5000 AND 15000;
|
||||
+-----------+----------+
|
||||
| name | salary |
|
||||
+-----------+----------+
|
||||
| | 600.00 |
|
||||
| emma | 20000.00 |
|
||||
| christine | 2200.00 |
|
||||
| zhuzhu | 2200.00 |
|
||||
| gougou | 2200.00 |
|
||||
+-----------+----------+
|
||||
5 rows in set (0.00 sec)
|
||||
```
|
||||
|
||||
IS NULL
|
||||
|
||||
```shell
|
||||
MySQL [company]> SELECT name,job_description FROM employee5 WHERE job_description IS NULL;
|
||||
+--------+-----------------+
|
||||
| name | job_description |
|
||||
+--------+-----------------+
|
||||
| harry | NULL |
|
||||
| zhuzhu | NULL |
|
||||
+--------+-----------------+
|
||||
2 rows in set (0.00 sec)
|
||||
|
||||
MySQL [company]> SELECT name,job_description FROM employee5 WHERE job_description IS NOT NULL;
|
||||
+-----------+-----------------+
|
||||
| name | job_description |
|
||||
+-----------+-----------------+
|
||||
| jack | teach |
|
||||
| tom | teach |
|
||||
| robin | teach |
|
||||
| alice | teach |
|
||||
| | hrcc |
|
||||
| emma | salecc |
|
||||
| christine | salecc |
|
||||
| gougou | |
|
||||
+-----------+-----------------+
|
||||
8 rows in set (0.00 sec)
|
||||
|
||||
MySQL [company]> SELECT name,job_description FROM employee5 WHERE job_description='';
|
||||
+--------+-----------------+
|
||||
| name | job_description |
|
||||
+--------+-----------------+
|
||||
| gougou | |
|
||||
+--------+-----------------+
|
||||
1 row in set (0.00 sec)
|
||||
```
|
||||
|
||||
注意:(NULL说明)
|
||||
|
||||
等价于没有任何值、是未知数
|
||||
|
||||
NULL与0、空字符串、空格都不同,NULL没有分配存储空间
|
||||
|
||||
对空值做加、减、乘、除等运算操作,结果仍为空
|
||||
|
||||
比较时使用关键字用“is null”和“is not null”
|
||||
|
||||
排序时比其他数据都小(索引默认是降序排列,小→大),所以NULL值总是排在最前
|
||||
|
||||
IN集合查询
|
||||
|
||||
```shell
|
||||
MySQL [company]> SELECT name, salary FROM employee5 WHERE salary=4000 OR salary=5000 OR salary=6000 OR salary=9000 ;
|
||||
+-------+---------+
|
||||
| name | salary |
|
||||
+-------+---------+
|
||||
| jack | 5000.00 |
|
||||
| harry | 6000.00 |
|
||||
+-------+---------+
|
||||
2 rows in set (0.00 sec)
|
||||
|
||||
MySQL [company]> SELECT name, salary FROM employee5 WHERE salary IN (4000,5000,6000,9000) ;
|
||||
+-------+---------+
|
||||
| name | salary |
|
||||
+-------+---------+
|
||||
| jack | 5000.00 |
|
||||
| harry | 6000.00 |
|
||||
+-------+---------+
|
||||
2 rows in set (0.00 sec)
|
||||
|
||||
MySQL [company]> SELECT name, salary FROM employee5 WHERE salary NOT IN (4000,5000,6000,9000) ;
|
||||
+-----------+----------+
|
||||
| name | salary |
|
||||
+-----------+----------+
|
||||
| tom | 5500.00 |
|
||||
| robin | 8000.00 |
|
||||
| alice | 7200.00 |
|
||||
| | 600.00 |
|
||||
| emma | 20000.00 |
|
||||
| christine | 2200.00 |
|
||||
| zhuzhu | 2200.00 |
|
||||
| gougou | 2200.00 |
|
||||
+-----------+----------+
|
||||
8 rows in set (0.01 sec)
|
||||
```
|
||||
|
||||
##### h.模糊查询
|
||||
|
||||
关键字LIKE
|
||||
|
||||
通配符%:所有字符
|
||||
|
||||
通配符_: 一个字符
|
||||
|
||||
```shell
|
||||
MySQL [company]> SELECT * FROM employee5 WHERE name LIKE 'al%';
|
||||
+----+-------+--------+------------+------------+-----------------+---------+--------+--------+
|
||||
| id | name | sex | hire_date | post | job_description | salary | office | dep_id |
|
||||
+----+-------+--------+------------+------------+-----------------+---------+--------+--------+
|
||||
| 4 | alice | female | 2018-02-02 | instructor | teach | 7200.00 | 501 | 100 |
|
||||
+----+-------+--------+------------+------------+-----------------+---------+--------+--------+
|
||||
1 row in set (0.00 sec)
|
||||
|
||||
MySQL [company]> SELECT * FROM employee5 WHERE name LIKE 'al___';
|
||||
+----+-------+--------+------------+------------+-----------------+---------+--------+--------+
|
||||
| id | name | sex | hire_date | post | job_description | salary | office | dep_id |
|
||||
+----+-------+--------+------------+------------+-----------------+---------+--------+--------+
|
||||
| 4 | alice | female | 2018-02-02 | instructor | teach | 7200.00 | 501 | 100 |
|
||||
+----+-------+--------+------------+------------+-----------------+---------+--------+--------+
|
||||
1 row in set (0.00 sec)
|
||||
```
|
||||
|
||||
##### i.排序查询
|
||||
|
||||
```shell
|
||||
MySQL [company]> select name,salary from employee5 order by salary;
|
||||
+-----------+----------+
|
||||
| name | salary |
|
||||
+-----------+----------+
|
||||
| | 600.00 |
|
||||
| christine | 2200.00 |
|
||||
| zhuzhu | 2200.00 |
|
||||
| gougou | 2200.00 |
|
||||
| jack | 5000.00 |
|
||||
| tom | 5500.00 |
|
||||
| harry | 6000.00 |
|
||||
| alice | 7200.00 |
|
||||
| robin | 8000.00 |
|
||||
| emma | 20000.00 |
|
||||
+-----------+----------+
|
||||
10 rows in set (0.01 sec)
|
||||
|
||||
MySQL [company]> select name,salary from employee5 order by salary desc;
|
||||
+-----------+----------+
|
||||
| name | salary |
|
||||
+-----------+----------+
|
||||
| emma | 20000.00 |
|
||||
| robin | 8000.00 |
|
||||
| alice | 7200.00 |
|
||||
| harry | 6000.00 |
|
||||
| tom | 5500.00 |
|
||||
| jack | 5000.00 |
|
||||
| christine | 2200.00 |
|
||||
| zhuzhu | 2200.00 |
|
||||
| gougou | 2200.00 |
|
||||
| | 600.00 |
|
||||
+-----------+----------+
|
||||
|
||||
MySQL [company]> select name,salary from employee5 order by salary desc limit 3; //控制显示前3行
|
||||
+-------+----------+
|
||||
| name | salary |
|
||||
+-------+----------+
|
||||
| emma | 20000.00 |
|
||||
| robin | 8000.00 |
|
||||
| alice | 7200.00 |
|
||||
+-------+----------+
|
||||
3 rows in set (0.00 sec)
|
||||
|
||||
MySQL [company]> select name,salary from employee5 order by salary desc limit 1,3; //从序号1开始显示三行的内容
|
||||
+-------+---------+
|
||||
| name | salary |
|
||||
+-------+---------+
|
||||
| robin | 8000.00 |
|
||||
| alice | 7200.00 |
|
||||
| harry | 6000.00 |
|
||||
+-------+---------+
|
||||
3 rows in set (0.00 sec)
|
||||
```
|
||||
|
||||
注意:
|
||||
|
||||
ascending 美音 /ə'sɛndɪŋ/ 升序
|
||||
|
||||
descending 美音 /dɪ'sɛndɪŋ/ 降序
|
||||
|
||||
##### j.集合函数查询
|
||||
|
||||
count:可以查看共有多少条记录
|
||||
|
||||
```shell
|
||||
MySQL [company]> select count(*) from employee5;
|
||||
+----------+
|
||||
| count(*) |
|
||||
+----------+
|
||||
| 10 |
|
||||
+----------+
|
||||
1 row in set (0.00 sec)
|
||||
|
||||
MySQL [company]> select count(name) from employee5;
|
||||
+-------------+
|
||||
| count(name) |
|
||||
+-------------+
|
||||
| 10 |
|
||||
+-------------+
|
||||
1 row in set (0.00 sec)
|
||||
|
||||
```
|
||||
|
||||
max:查看最大值
|
||||
|
||||
```shell
|
||||
MySQL [company]> select max(salary) from employee5;
|
||||
+-------------+
|
||||
| max(salary) |
|
||||
+-------------+
|
||||
| 20000.00 |
|
||||
+-------------+
|
||||
1 row in set (0.00 sec)
|
||||
```
|
||||
|
||||
min:查看最小值
|
||||
|
||||
```shell
|
||||
MySQL [company]> select min(salary) from employee5;
|
||||
+-------------+
|
||||
| min(salary) |
|
||||
+-------------+
|
||||
| 600.00 |
|
||||
+-------------+
|
||||
1 row in set (0.00 sec)
|
||||
```
|
||||
|
||||
avg:查看平均值
|
||||
|
||||
```shell
|
||||
MySQL [company]> select avg(salary) from employee5;
|
||||
+-------------+
|
||||
| avg(salary) |
|
||||
+-------------+
|
||||
| 5890.000000 |
|
||||
+-------------+
|
||||
1 row in set (0.00 sec)
|
||||
```
|
||||
|
||||
sum:求和
|
||||
|
||||
sale这个部门的总工资
|
||||
|
||||
```shell
|
||||
MySQL [company]> select concat("Total Department Wages:",sum(salary)) from employee5 where post='sale';
|
||||
+-------------------------------------------------+
|
||||
| concat("Total Department Wages:",sum(salary)) |
|
||||
+-------------------------------------------------+
|
||||
| Total Department Wages:26600.00 |
|
||||
+-------------------------------------------------+
|
||||
1 row in set (0.00 sec)
|
||||
```
|
||||
|
||||
获取薪水最高的这个人的详细信息
|
||||
|
||||
```shell
|
||||
MySQL [company]> select * from employee5 where salary = (select max(salary) from employee5);
|
||||
+----+------+--------+------------+------+-----------------+----------+--------+--------+
|
||||
| id | name | sex | hire_date | post | job_description | salary | office | dep_id |
|
||||
+----+------+--------+------------+------+-----------------+----------+--------+--------+
|
||||
| 7 | emma | female | 2018-02-06 | sale | salecc | 20000.00 | 503 | 102 |
|
||||
+----+------+--------+------------+------+-----------------+----------+--------+--------+
|
||||
1 row in set (0.00 sec)
|
||||
```
|
||||
|
||||
##### k.分组查询
|
||||
|
||||
GROUP BY和GROUP_CONCAT()函数一起使用
|
||||
|
||||
获取部门ID相同的员工并把名字拼接到一起
|
||||
|
||||
```shell
|
||||
MySQL [company]> SELECT dep_id,GROUP_CONCAT(name) FROM employee5 GROUP BY dep_id;
|
||||
+--------+------------------------------+
|
||||
| dep_id | GROUP_CONCAT(name) |
|
||||
+--------+------------------------------+
|
||||
| 100 | jack,tom,robin,alice |
|
||||
| 101 | ,harry |
|
||||
| 102 | emma,christine,zhuzhu,gougou |
|
||||
+--------+------------------------------+
|
||||
3 rows in set (0.01 sec)
|
||||
```
|
||||
|
||||
GROUP BY和集合函数一起使用
|
||||
|
||||
获取部门最高薪资
|
||||
|
||||
```shell
|
||||
MySQL [company]> SELECT post,max(salary) FROM employee5 GROUP BY post;
|
||||
+------------+-------------+
|
||||
| post | max(salary) |
|
||||
+------------+-------------+
|
||||
| hr | 6000.00 |
|
||||
| instructor | 8000.00 |
|
||||
| sale | 20000.00 |
|
||||
+------------+-------------+
|
||||
3 rows in set (0.00 sec)
|
||||
```
|
||||
|
||||
##### l.正则查询
|
||||
|
||||
```shell
|
||||
以什么开头
|
||||
MySQL [company]> SELECT * FROM employee5 WHERE name REGEXP '^ali';
|
||||
+----+-------+--------+------------+------------+-----------------+---------+--------+--------+
|
||||
| id | name | sex | hire_date | post | job_description | salary | office | dep_id |
|
||||
+----+-------+--------+------------+------------+-----------------+---------+--------+--------+
|
||||
| 4 | alice | female | 2018-02-02 | instructor | teach | 7200.00 | 501 | 100 |
|
||||
+----+-------+--------+------------+------------+-----------------+---------+--------+--------+
|
||||
1 row in set (0.00 sec)
|
||||
|
||||
以什么结尾
|
||||
MySQL [company]> SELECT * FROM employee5 WHERE name REGEXP 'ce$';
|
||||
+----+-------+--------+------------+------------+-----------------+---------+--------+--------+
|
||||
| id | name | sex | hire_date | post | job_description | salary | office | dep_id |
|
||||
+----+-------+--------+------------+------------+-----------------+---------+--------+--------+
|
||||
| 4 | alice | female | 2018-02-02 | instructor | teach | 7200.00 | 501 | 100 |
|
||||
+----+-------+--------+------------+------------+-----------------+---------+--------+--------+
|
||||
1 row in set (0.01 sec)
|
||||
|
||||
连续出现n次
|
||||
MySQL [company]> SELECT * FROM employee5 WHERE name REGEXP 'm{2}';
|
||||
+----+------+--------+------------+------+-----------------+----------+--------+--------+
|
||||
| id | name | sex | hire_date | post | job_description | salary | office | dep_id |
|
||||
+----+------+--------+------------+------+-----------------+----------+--------+--------+
|
||||
| 7 | emma | female | 2018-02-06 | sale | salecc | 20000.00 | 503 | 102 |
|
||||
+----+------+--------+------------+------+-----------------+----------+--------+--------+
|
||||
1 row in set (0.00 sec)
|
||||
```
|
||||
|
||||
## 二:多表联合查询
|
||||
|
||||
【扩展了解】
|
||||
|
||||
#### 1.数据准备
|
||||
|
||||
##### 表:company.employee6
|
||||
|
||||
创建表:
|
||||
|
||||
```shell
|
||||
MySQL [company]> create table employee6(
|
||||
emp_id int auto_increment primary key not null,
|
||||
emp_name varchar(50),
|
||||
age int,
|
||||
dept_id int);
|
||||
Query OK, 0 rows affected (0.65 sec)
|
||||
```
|
||||
|
||||
查看表结构:
|
||||
|
||||
```shell
|
||||
MySQL [company]> desc employee6;
|
||||
+----------+-------------+------+-----+---------+----------------+
|
||||
| Field | Type | Null | Key | Default | Extra |
|
||||
+----------+-------------+------+-----+---------+----------------+
|
||||
| emp_id | int(11) | NO | PRI | NULL | auto_increment |
|
||||
| emp_name | varchar(50) | YES | | NULL | |
|
||||
| age | int(11) | YES | | NULL | |
|
||||
| dept_id | int(11) | YES | | NULL | |
|
||||
+----------+-------------+------+-----+---------+----------------+
|
||||
4 rows in set (0.00 sec)
|
||||
```
|
||||
|
||||
插入模拟数据:
|
||||
|
||||
```shell
|
||||
MySQL [company]> insert into employee6(emp_name,age,dept_id) values
|
||||
('',19,200),
|
||||
('tom',26,201),
|
||||
('jack',30,201),
|
||||
('alice',24,202),
|
||||
('robin',40,200),
|
||||
('xingdian',16,200),
|
||||
('natasha',28,204);
|
||||
```
|
||||
|
||||
查看数据:
|
||||
|
||||
```shell
|
||||
MySQL [company]> select * from employee6;
|
||||
+--------+----------+------+---------+
|
||||
| emp_id | emp_name | age | dept_id |
|
||||
+--------+----------+------+---------+
|
||||
| 1 | | 19 | 200 |
|
||||
| 2 | tom | 26 | 201 |
|
||||
| 3 | jack | 30 | 201 |
|
||||
| 4 | alice | 24 | 202 |
|
||||
| 5 | robin | 40 | 200 |
|
||||
| 6 | xingdian | 16 | 200 |
|
||||
| 7 | natasha | 28 | 204 |
|
||||
+--------+----------+------+---------+
|
||||
7 rows in set (0.00 sec)
|
||||
```
|
||||
|
||||
##### 表:company.department6
|
||||
|
||||
创建表:
|
||||
|
||||
```shell
|
||||
MySQL [company]> create table department6(
|
||||
dept_id int,
|
||||
dept_name varchar(100)
|
||||
);
|
||||
Query OK, 0 rows affected (0.33 sec)
|
||||
```
|
||||
|
||||
查看表结构:
|
||||
|
||||
```shell
|
||||
MySQL [company]> desc department6;
|
||||
+-----------+--------------+------+-----+---------+-------+
|
||||
| Field | Type | Null | Key | Default | Extra |
|
||||
+-----------+--------------+------+-----+---------+-------+
|
||||
| dept_id | int(11) | YES | | NULL | |
|
||||
| dept_name | varchar(100) | YES | | NULL | |
|
||||
+-----------+--------------+------+-----+---------+-------+
|
||||
2 rows in set (0.00 sec)
|
||||
```
|
||||
|
||||
模拟插入数据:
|
||||
|
||||
```shell
|
||||
MySQL [company]> insert into department6 values
|
||||
(200,'hr'),
|
||||
(201,'it'),
|
||||
(202,'sale'),
|
||||
(203,'fd');
|
||||
```
|
||||
|
||||
查看数据:
|
||||
|
||||
```shell
|
||||
MySQL [company]> select * from department6;
|
||||
+---------+-----------+
|
||||
| dept_id | dept_name |
|
||||
+---------+-----------+
|
||||
| 200 | hr |
|
||||
| 201 | it |
|
||||
| 202 | sale |
|
||||
| 203 | fd |
|
||||
+---------+-----------+
|
||||
4 rows in set (0.01 sec)
|
||||
```
|
||||
|
||||
#### 2.多表的连接查询
|
||||
|
||||
交叉连接:生成笛卡尔积,它不使用任何匹配条件;交叉联接返回左表中的所有行,左表中的每一行与右表中的所有行组合
|
||||
|
||||
内连接:只连接匹配的行
|
||||
|
||||
外连接
|
||||
|
||||
左连接:会显示左边表内所有的值,不论在右边表内匹不匹配
|
||||
|
||||
右连接:会显示右边表内所有的值,不论在左边表内匹不匹配
|
||||
|
||||
全外连接:包含左、右两个表的全部行
|
||||
|
||||
##### 交叉连接
|
||||
|
||||
```shell
|
||||
MySQL [company]> select employee6.emp_name,employee6.age,employee6.dept_id,department6.dept_name from employee6,department6;
|
||||
+----------+------+---------+-----------+
|
||||
| emp_name | age | dept_id | dept_name |
|
||||
+----------+------+---------+-----------+
|
||||
| | 19 | 200 | hr |
|
||||
| | 19 | 200 | it |
|
||||
| | 19 | 200 | sale |
|
||||
| | 19 | 200 | fd |
|
||||
| tom | 26 | 201 | hr |
|
||||
| tom | 26 | 201 | it |
|
||||
| tom | 26 | 201 | sale |
|
||||
| tom | 26 | 201 | fd |
|
||||
| jack | 30 | 201 | hr |
|
||||
| jack | 30 | 201 | it |
|
||||
| jack | 30 | 201 | sale |
|
||||
| jack | 30 | 201 | fd |
|
||||
| alice | 24 | 202 | hr |
|
||||
| alice | 24 | 202 | it |
|
||||
| alice | 24 | 202 | sale |
|
||||
| alice | 24 | 202 | fd |
|
||||
| robin | 40 | 200 | hr |
|
||||
| robin | 40 | 200 | it |
|
||||
| robin | 40 | 200 | sale |
|
||||
| robin | 40 | 200 | fd |
|
||||
| xingdian | 16 | 200 | hr |
|
||||
| xingdian | 16 | 200 | it |
|
||||
| xingdian | 16 | 200 | sale |
|
||||
| xingdian | 16 | 200 | fd |
|
||||
| natasha | 28 | 204 | hr |
|
||||
| natasha | 28 | 204 | it |
|
||||
| natasha | 28 | 204 | sale |
|
||||
| natasha | 28 | 204 | fd |
|
||||
+----------+------+---------+-----------+
|
||||
28 rows in set (0.00 sec)
|
||||
```
|
||||
|
||||
##### 内连接
|
||||
|
||||
获取有部门的员工 (部门表中没有natasha所在的部门)
|
||||
|
||||
```shell
|
||||
MySQL [company]> select employee6.emp_name,employee6.age,employee6.dept_id,department6.dept_name from employee6,department6 where employee6.dept_id=department6.dept_id;
|
||||
+----------+------+---------+-----------+
|
||||
| emp_name | age | dept_id | dept_name |
|
||||
+----------+------+---------+-----------+
|
||||
| | 19 | 200 | hr |
|
||||
| tom | 26 | 201 | it |
|
||||
| jack | 30 | 201 | it |
|
||||
| alice | 24 | 202 | sale |
|
||||
| robin | 40 | 200 | hr |
|
||||
| xingdian | 16 | 200 | hr |
|
||||
+----------+------+---------+-----------+
|
||||
6 rows in set (0.00 sec)
|
||||
|
||||
MySQL [company]> select employee6.emp_name,department6.dept_name from employee6 inner join department6 on employee6.dept_id=department6.dept_id;
|
||||
+----------+-----------+
|
||||
| emp_name | dept_name |
|
||||
+----------+-----------+
|
||||
| | hr |
|
||||
| tom | it |
|
||||
| jack | it |
|
||||
| alice | sale |
|
||||
| robin | hr |
|
||||
| xingdian | hr |
|
||||
+----------+-----------+
|
||||
6 rows in set (0.01 sec)
|
||||
```
|
||||
|
||||
##### 外连接
|
||||
|
||||
语法:
|
||||
|
||||
SELECT 字段列表 FROM 表1 LEFT|RIGHT JOIN 表2 ON 表1.字段 = 表2.字段;
|
||||
|
||||
注意:
|
||||
|
||||
先用谁谁就是左
|
||||
|
||||
###### 左连接 left join
|
||||
|
||||
```shell
|
||||
找出所有员工及所属的部门,包括没有部门的员工
|
||||
MySQL [company]> select emp_id,emp_name,dept_name from employee6 left join department6 on employee6.dept_id = department6.dept_id;
|
||||
+--------+----------+-----------+
|
||||
| emp_id | emp_name | dept_name |
|
||||
+--------+----------+-----------+
|
||||
| 1 | | hr |
|
||||
| 5 | robin | hr |
|
||||
| 6 | xingdian | hr |
|
||||
| 2 | tom | it |
|
||||
| 3 | jack | it |
|
||||
| 4 | alice | sale |
|
||||
| 7 | natasha | NULL |
|
||||
+--------+----------+-----------+
|
||||
7 rows in set (0.00 sec)
|
||||
```
|
||||
|
||||
###### 右连接right join
|
||||
|
||||
```
|
||||
找出所有部门包含的员工,包括空部门
|
||||
MySQL [company]> select emp_id,emp_name,dept_name from employee6 right join department6 on employee6.dept_id = department6.dept_id;
|
||||
+--------+----------+-----------+
|
||||
| emp_id | emp_name | dept_name |
|
||||
+--------+----------+-----------+
|
||||
| 1 | | hr |
|
||||
| 2 | tom | it |
|
||||
| 3 | jack | it |
|
||||
| 4 | alice | sale |
|
||||
| 5 | robin | hr |
|
||||
| 6 | xingdian | hr |
|
||||
| NULL | NULL | fd |
|
||||
+--------+----------+-----------+
|
||||
7 rows in set (0.00 sec)
|
||||
```
|
||||
|
||||
##### 全外连接
|
||||
|
||||
```shell
|
||||
MySQL [company]> select * from employee6 full join department6;
|
||||
+--------+----------+------+---------+---------+-----------+
|
||||
| emp_id | emp_name | age | dept_id | dept_id | dept_name |
|
||||
+--------+----------+------+---------+---------+-----------+
|
||||
| 1 | | 19 | 200 | 200 | hr |
|
||||
| 1 | | 19 | 200 | 201 | it |
|
||||
| 1 | | 19 | 200 | 202 | sale |
|
||||
| 1 | | 19 | 200 | 203 | fd |
|
||||
| 2 | tom | 26 | 201 | 200 | hr |
|
||||
| 2 | tom | 26 | 201 | 201 | it |
|
||||
| 2 | tom | 26 | 201 | 202 | sale |
|
||||
| 2 | tom | 26 | 201 | 203 | fd |
|
||||
| 3 | jack | 30 | 201 | 200 | hr |
|
||||
| 3 | jack | 30 | 201 | 201 | it |
|
||||
| 3 | jack | 30 | 201 | 202 | sale |
|
||||
| 3 | jack | 30 | 201 | 203 | fd |
|
||||
| 4 | alice | 24 | 202 | 200 | hr |
|
||||
| 4 | alice | 24 | 202 | 201 | it |
|
||||
| 4 | alice | 24 | 202 | 202 | sale |
|
||||
| 4 | alice | 24 | 202 | 203 | fd |
|
||||
| 5 | robin | 40 | 200 | 200 | hr |
|
||||
| 5 | robin | 40 | 200 | 201 | it |
|
||||
| 5 | robin | 40 | 200 | 202 | sale |
|
||||
| 5 | robin | 40 | 200 | 203 | fd |
|
||||
| 6 | xingdian | 16 | 200 | 200 | hr |
|
||||
| 6 | xingdian | 16 | 200 | 201 | it |
|
||||
| 6 | xingdian | 16 | 200 | 202 | sale |
|
||||
| 6 | xingdian | 16 | 200 | 203 | fd |
|
||||
| 7 | natasha | 28 | 204 | 200 | hr |
|
||||
| 7 | natasha | 28 | 204 | 201 | it |
|
||||
| 7 | natasha | 28 | 204 | 202 | sale |
|
||||
| 7 | natasha | 28 | 204 | 203 | fd |
|
||||
+--------+----------+------+---------+---------+-----------+
|
||||
28 rows in set (0.00 sec)
|
||||
```
|
||||
|
||||
#### 3.复合条件连接查询
|
||||
|
||||
##### 案例一
|
||||
|
||||
找出公司所有部门中年龄大于25岁的员工
|
||||
|
||||
以内连接的方式查询employee6和department6表,并且employee6表中的age字段值必须大于25
|
||||
|
||||
```shell
|
||||
MySQL [company]> select emp_id,emp_name,dept_name FROM employee6,department6 WHERE employee6.dept_id = department6.dept_id AND age > 25;
|
||||
+--------+----------+-----------+
|
||||
| emp_id | emp_name | dept_name |
|
||||
+--------+----------+-----------+
|
||||
| 5 | robin | hr |
|
||||
| 2 | tom | it |
|
||||
| 3 | jack | it |
|
||||
+--------+----------+-----------+
|
||||
3 rows in set (0.01 sec)
|
||||
```
|
||||
|
||||
##### 案例二
|
||||
|
||||
以内连接的方式查询employee6和department6表,并且以age字段的升序方式显示
|
||||
|
||||
```shell
|
||||
MySQL [company]> select emp_id,emp_name,dept_name FROM employee6,department6 WHERE employee6.dept_id = department6.dept_id ORDER BY age asc;
|
||||
+--------+----------+-----------+
|
||||
| emp_id | emp_name | dept_name |
|
||||
+--------+----------+-----------+
|
||||
| 6 | xingdian | hr |
|
||||
| 1 | | hr |
|
||||
| 4 | alice | sale |
|
||||
| 2 | tom | it |
|
||||
| 3 | jack | it |
|
||||
| 5 | robin | hr |
|
||||
+--------+----------+-----------+
|
||||
```
|
||||
|
||||
#### 4.子查询
|
||||
|
||||
子查询是将一个查询语句嵌套在另一个查询语句中
|
||||
|
||||
内层查询语句的查询结果,可以为外层查询语句提供查询条件
|
||||
|
||||
子查询中可以包含:IN、NOT IN等关键字;还可以包含比较运算符:= 、 !=、> 、<等
|
||||
|
||||
##### 案例一
|
||||
|
||||
带IN关键字的子查询;查询employee表,但dept_id必须在department表中出现过
|
||||
|
||||
```shell
|
||||
MySQL [company]> select * from employee6 WHERE dept_id IN (select dept_id FROM department6);
|
||||
+--------+----------+------+---------+
|
||||
| emp_id | emp_name | age | dept_id |
|
||||
+--------+----------+------+---------+
|
||||
| 1 | | 19 | 200 |
|
||||
| 2 | tom | 26 | 201 |
|
||||
| 3 | jack | 30 | 201 |
|
||||
| 4 | alice | 24 | 202 |
|
||||
| 5 | robin | 40 | 200 |
|
||||
| 6 | xingdian | 16 | 200 |
|
||||
+--------+----------+------+---------+
|
||||
6 rows in set (0.00 sec)
|
||||
```
|
||||
|
||||
##### 案例二
|
||||
|
||||
带比较运算符的子查询;查询年龄大于等于25岁员工所在部门(查询老龄化的部门)
|
||||
|
||||
```shell
|
||||
MySQL [company]> select dept_id,dept_name FROM department6 WHERE dept_id IN (SELECT DISTINCT dept_id FROM employee6 WHERE age >=25);
|
||||
+---------+-----------+
|
||||
| dept_id | dept_name |
|
||||
+---------+-----------+
|
||||
| 201 | it |
|
||||
| 200 | hr |
|
||||
+---------+-----------+
|
||||
2 rows in set (0.00 sec)
|
||||
```
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,135 @@
|
|||
<h1><center>数据库日志管理</center></h1>
|
||||
|
||||
**作者:行癫(盗版必究)**
|
||||
|
||||
------
|
||||
|
||||
## 一:日志管理
|
||||
|
||||
<img src="https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20220925214046253.png" alt="image-20220925214046253" style="zoom:50%;" />
|
||||
|
||||
#### 1.日志分类
|
||||
|
||||
错误日志 :启动,停止,关闭失败报错。rpm安装日志位置 /var/log/mysqld.log
|
||||
|
||||
通用查询日志:所有的查询都记下来
|
||||
|
||||
二进制日志:实现备份,增量备份。只记录改变数据,除了select都记
|
||||
|
||||
中继日志:读取主服务器的binlog,在本地回放。保持一致
|
||||
|
||||
slow log:慢查询日志,指导调优,定义某一个查询语句,定义超时时间,通过日志提供调优建议给开发人员
|
||||
|
||||
DDL log: 定义语句的日志
|
||||
|
||||
#### 2.Error Log
|
||||
|
||||
```shell
|
||||
log-error=/var/log/mysqld.log
|
||||
```
|
||||
|
||||
#### 3.Binary Log
|
||||
|
||||
```
|
||||
log-bin=/var/log/mysql-bin/slave2
|
||||
server-id=2
|
||||
|
||||
[root@slave2 ~]# mkdir /var/log/mysql-bin
|
||||
[root@slave2 ~]# chown mysql.mysql /var/log/mysql-bin/
|
||||
[root@slave2 ~]# systemctl restart mysqld
|
||||
```
|
||||
|
||||
注意:
|
||||
|
||||
需要提前开启
|
||||
|
||||
查看binlog日志:
|
||||
|
||||
```shel
|
||||
[root@slave2 ~]# mysqlbinlog slave2-bin.000001 -v --base64-output=decode-rows
|
||||
时间点 : 141126 14:04:49
|
||||
位置点 : at 106
|
||||
注:
|
||||
1. 重启mysqld 会截断
|
||||
2. flush logs 会截断
|
||||
3. reset master 删除所有binlog rm -rf /var/lib/mysql/*.000001
|
||||
4. 删除部分
|
||||
PURGE BINARY LOGS TO 'mysql-bin.010';
|
||||
PURGE BINARY LOGS BEFORE '2019-04-02 22:46:26';
|
||||
|
||||
截取binlog
|
||||
all:
|
||||
# mysqlbinlog mysql.000002
|
||||
|
||||
datetime:
|
||||
# mysqlbinlog mysql.000002 --start-datetime="2018-12-05 10:02:56"
|
||||
# mysqlbinlog mysql.000002 --stop-datetime="2018-12-05 11:02:54"
|
||||
# mysqlbinlog mysql.000002 --start-datetime="2018-12-05 10:02:56" --stop-datetime="2018-12-05 11:02:54"
|
||||
|
||||
position:
|
||||
# mysqlbinlog mysql.000002 --start-position=260
|
||||
# mysqlbinlog mysql.000002 --stop-position=260
|
||||
# mysqlbinlog mysql.000002 --start-position=260 --stop-position=930
|
||||
```
|
||||
|
||||
#### 4.Slow Query Log
|
||||
|
||||
开启慢查询日志:
|
||||
|
||||
```shell
|
||||
[root@xingdian ~]# vim /etc/my.cnf
|
||||
slow_query_log=1
|
||||
slow_query_log_file=/var/log/mysql-slow/slow.log
|
||||
long_query_time=3 设置慢查询超时时间 单位是:秒
|
||||
```
|
||||
|
||||
创建对应目录:
|
||||
|
||||
```shell
|
||||
[root@xingdian ~]# mkdir /var/log/mysql-slow
|
||||
[root@xingdian ~]# chown mysql.mysql mysql-slow
|
||||
```
|
||||
|
||||
重启服务:
|
||||
|
||||
```shell
|
||||
[root@xingdian ~]# systemctl restart mysqld
|
||||
```
|
||||
|
||||
验证:
|
||||
|
||||
```shell
|
||||
[root@xingdian ~]# mysql -uroot -pQianFeng@123
|
||||
mysql: [Warning] Using a password on the command line interface can be insecure.
|
||||
Welcome to the MySQL monitor. Commands end with ; or \g.
|
||||
Your MySQL connection id is 2
|
||||
Server version: 5.7.39-log MySQL Community Server (GPL)
|
||||
|
||||
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
|
||||
|
||||
Oracle is a registered trademark of Oracle Corporation and/or its
|
||||
affiliates. Other names may be trademarks of their respective
|
||||
owners.
|
||||
|
||||
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
|
||||
|
||||
mysql> select sleep(6);
|
||||
+----------+
|
||||
| sleep(6) |
|
||||
+----------+
|
||||
| 0 |
|
||||
+----------+
|
||||
1 row in set (6.00 sec)
|
||||
|
||||
mysql> exit
|
||||
Bye
|
||||
[root@xingdian ~]# cat /var/log/mysql-slow/slow.log
|
||||
/usr/sbin/mysqld, Version: 5.7.39-log (MySQL Community Server (GPL)). started with:
|
||||
Tcp port: 0 Unix socket: /var/lib/mysql/mysql.sock
|
||||
Time Id Command Argument
|
||||
# Time: 2022-09-25T06:58:05.496205Z
|
||||
# User@Host: root[root] @ localhost [] Id: 2
|
||||
# Query_time: 6.007094 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0
|
||||
SET timestamp=1664089085;
|
||||
select sleep(6);
|
||||
```
|
Loading…
Reference in New Issue