当前位置:首页 > 有云笔记 > RDBMS1 > 正文内容

数据库服务概述 、 构建MySQL服务 、 数据库基本管理 、 MySQL数据类型

小白2年前 (2022-03-09)RDBMS1200330

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

第一科  数据库基础  RDBMS1_day01

完成的学习任务:

一、数据库概述 (相关的理论知识多)

二、搭建mysql服务器 (手动操作多)

三、破解数据库管理密码

四、安装MySQL服务图形管理软件

三、基础查询 (手动操作多)

一、数据库概述 (相关的理论知识)

1) 数据库介绍(数据库是啥东东?)

数据库就是存储数据的仓库

用来存储数据的服务器 就称为数据库服务器

库  (仓库)  人类社会里的仓库 指的就是有很多空间的空房子

计算机世界里  大的存储空间 只就容量大的硬盘  

数据库服务器  一定要有足够大的磁盘容量 (网络共享存储 、 直连式存储 )

 对于我们每个人 来说 :数据包括哪些?

个人信息 : 姓名   性别   年龄  学历  籍贯   家庭地址

生活信息(聊天信息    订餐信息     购物信息)

微信、QQ    美团饿了吗   京东 淘宝

2) 常见的数据库服务软件 ? (要知道)

3)专业术语 (行业黑话) 

DB   数据库 : 在数据库服务器上创建的存储数据库的文件夹     

DBMS 数据管理系统 : 提供数据存储服务软件   如 mysql  、oracle 、SQL SERVER  、 DB2 ......  

DBS  数据库系统 : 安装了数据库服务软件的主机   

DBA  数据库管理员 : 维护数据库服务器的工作人员  负责数据存储架构的部署  数据库服务器的维护和优化  、 监控 、数据的备份与恢复  

4)mysql介绍

4.1  特点 及  发展史

4.2  应用场景

准备做数据库服务器的虚拟机(整个课程实验用的都是RHEL7操作系统)

使用准备的模板机克隆(链接克隆就可以)2台虚拟机:配置要求如下

ip   192.168.4.50     192.168.4.51

配置YUM源 (家里上课的同学 网络源或本地源都可以) 教室上课的同学不需要自己配置 已经配置好了 

关闭firewalld  和  selinux 

拷贝软件  mysql-5.7.17.tar 到创建的虚拟机里 

案例1:构建MySQL服务器 

诉求: 在192.168.4.50主机 部署MySQL服务  设置数据库管理员连接密码为123qqq...A

具体步骤如下:

1 清除冲突软件mariadb (如果安装了的话) 

rpm  -q  mariadb-server  mariadb
systemctl  stop  mariadb 
rpm  -e  --nodeps  mariadb  mariadb-server 
rm  -rf  /etc/my.cnf  
rm  -rf  /var/lib/mysql/*

2 安装软件mysql 社区开源版软件

tar  -xf mysql-5.7.17.tar
yum -y install mysql-community-*.rpm

3 启动服务并设置开机运行 查看进程和端口

systemctl  start mysqld
systemctl  enable mysqld
netstat  -utnlp  | grep  3306  等效与   ss  -utnlp  | grep  3306
ps -C mysqld   等效于  ps  aux  |  grep   mysqld

4 查看连接MySQL服务初始密码

[root@host50 ~]# grep password  /var/log/mysqld.log | tail -1
2021-12-06T01:47:49.262056Z 1 [Note] A temporary password is generated for root@localhost: p.7jr.uy.aiZ
[root@host50 ~]#

5 使用初始密码连接服务

[root@host50 ~]# mysql -hlocalhost -uroot -p'p.7jr.uy.aiZ'

6 修改登录密码(服务强制修改且修改的密码要符合服务要求的复杂度)

mysql>  alter user   root@"localhost" identified by "123qqq...A";

7 断开连接

mysql> exit;

8 使用修改的密码登录并查看数据

[root@host50 ~]# mysql -hlocalhost -uroot -p123qqq...A
mysql> show databases;  #查看已有的库  默认的4个库 不允许删除  库存放的是不同类型的数据 后边的课程会陆续讲解
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
mysql> exit;

搭建第2台数据库服务器 

诉求: 在ip地址  192.168.4.51 部署MySQL服务  数据库管理员录密码设置为 NSD2021...a 

具体操作如下:

systemctl  stop  mariadb 
rpm  -e  --nodeps  mariadb  mariadb-server 
rm  -rf  /etc/my.cnf  
rm  -rf  /var/lib/mysql/*
tar -xf mysql-5.7.17.tar 
yum -y install mysql-community-*.rpm
systemctl  start mysqld
grep password /var/log/mysqld.log | tail -1
mysql -hlocalhost -uroot -p'_gJ>u2h?SxNg'
mysql> alter user  root@"localhost" identified by "NSD2021...a";
mysql> exit;
mysql -hlocalhost -uroot -pNSD2021...a
 mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
mysql> exit ;

案例2:密码管理(统一在 50主机做实验)

1) 修改数据库服务器的密码策略(设置密码的复杂度)

说明:要求数据管理员root 能连接数据库服务

1 查看默认使用的密码策略和密码长度

2 命令行修改密码策略和密码长度

3 修改密码验证修改的密码策略和密码长度

4 永久修改密码策略和密码长度

[root@host50 ~]# mysql -hlocalhost -uroot -p123qqq...A
mysql> show variables like "%password%";  #查看与密码相关的配置项
mysql> set global validate_password_policy=0;  #修改密码等级为0
mysql> set global validate_password_length=6;  #修改最小密码长度
mysql> alter user  root@"localhost" identified by "tarena"; #根据新密码策略修改密码
mysql> exit;
[root@host50 ~]# mysql -hlocalhost -uroot -ptarena  #使用修改后的密码登陆
mysql> exit;
[root@host50 ~]# vim /etc/my.cnf  #(永久配置)把修改 添加到配置文件里 数据库服务重启了 依然有效 
[mysqld]
validate_password_policy=0
validate_password_length=6
:wq
[root@host50 ~]# systemctl  restart mysqld  #重启服务
[root@host50 ~]# mysql -hlocalhost -uroot -ptarena #登陆后
mysql> show variables like "%password%"; #查看密码策略

2)破解数据库管理员root 密码(忘了root密码)

2.1 破解线下数据库服务器管理员root 密码 (统一在host50 主机做实验)

 具体操作如下:

1 修改主配置文件 (使其可以无密码登录)

2 重启数据库服务

3 无密码登录 并修改登录密码  断开连接

4 还原对主配置文件 的修改

5 重启数据库服务

6 使用破解后的密码登录(能登录为成功)

[root@host50 ~]# vim /etc/my.cnf
[mysqld]
#validate_password_policy=0
#validate_password_length=6
skip-grant-tables  #跳过授权库MySQL库启动服务 作用连接服务不需要输入密码
:wq
[root@host50 ~]# systemctl  restart mysqld
[root@host50 ~]# mysql  #不输入密码就可以登陆
mysql> update  mysql.user set authentication_string=password("123qqq...A") 
where user="root" and host="localhost";  #修改管理员root 本机登陆密码为  123qqq...A
mysql> flush privileges; #确保修改生效
mysql> exit; #断开连接
[root@host50 ~]# vim /etc/my.cnf  #还原修改
[mysqld]
validate_password_policy=0
validate_password_length=6
#skip-grant-tables
:wq
[root@host50 ~]# systemctl  restart mysqld #重启服务 
[root@host50 ~]# mysql -hlocalhost -uroot -p123qqq...A #使用恢复的密码登陆
mysql>

2.2 破解线上数据库服务器管理员root 密码(上一种破解必须重启服务,但线上服务器不能随便重启)

统一在host50 主机做实验

具体 步骤如下:

1  拷贝其他数据库服务器 管理员root用户能正常连接数据库服务的mysql库 覆盖本机的mysql库

!!!!mysql库存放的是 数据库服务器的用户和密码!!!  

2  查看mysql服务 父进程的pid 

3  给mysql服务的父进程发送 SIGHUP信息 (作用重新加载数据库目录下的文件 ,可以重新识别 mysql库里的用户和密码)

4  使用破解后的密码登录(密码和root用户能正常登录服务主机的的root密码一样)

[root@host50 ~]# scp -r 192.168.4.51:/var/lib/mysql/mysql  /var/lib/mysql/
[root@host50 ~]# which  pstree || yum  -y install psmisc
[root@host50 ~]# pstree -p | grep mysqld | head  -1 
[root@host50 ~]# kill  -SIGHUP  1603 
[root@host50 ~]# mysql -hlocalhost -uroot -pNSD2021...a

3)修改root密码  

说明工作:为数据库管理员root用户密码的安全,可以定期修改密码(比如每隔10天修改一次密码)

注意:修改密码 必须要知道旧密码 才能设置新密码

使用alter user 修改 :数据库管理员连接服务后 修改自己的登陆密码

或mysqladmin修改 (操作系统管理员 修改本机数据库服务的登陆密码)

]# mysqladmin  -uroot    -p旧密码   password  新密码或 隐藏旧密码和新密码,根据提示输入密码

]# mysqladmin  -uroot    -p   password 

第一次提示 输入旧密码

第二次提示 输入新密码  (新密码 要服务密码策略要求)

案例3:安装图形软件(在数据库服务器安装图形软件 ,通过连接图形软件,对数据库做管理)

要求:在IP地址192.168.4.50主机安装phpmyadmin软件

在host50主机做如下配置:

步骤一:安装phpmyadmin软件。

1) 部署phpmyadmin运行环境 LAP  (L 指的是 linux 系统  A 指的是apache   P 指的是PHP)

2) 安装phpmyadmin软件

3)  修改配置文件 :  修改phpmyadmin软件的配置文件

  365  yum -y install httpd php  php-mysql 

  367  systemctl  start httpd  ; systemctl  enable httpd

  371  tar -xf phpMyAdmin-2.11.11-all-languages.tar.gz 

  373  mv phpMyAdmin-2.11.11-all-languages /var/www/html/phpmyadmin

  374  cd /var/www/html/phpmyadmin

  378  cp config.sample.inc.php  config.inc.php  创建主配置文件 

  380  vim +17 config.inc.php  #在''号里添加plj123

 步骤二:客户端通过访问phpmyadmin软件管理数据库。

 打开真机的浏览器 输入

 访问的网址  http://192.168.4.50/phpmyadmin

 用户名   root

 密码     root用户密码

数据库必备命令的使用:  连接数据库服务器后使用的命令  在 mysql>  状态下执行的命令

mysql> show  databases;    #显示服务器上已有的库(文件夹)
+--------------------+
| Database                  |
+--------------------+
| information_schema |
| mysql                       |
| performance_schema |
| sys                            |
+--------------------+
4 rows in set (0.00 sec)
mysql>
mysql> select user();  #显示当前登录的用户名和客户端地址
+----------------+
| user()         |
+----------------+
| root@localhost |     #数据库管理员root 用户本机登录
+----------------+
1 row in set (0.00 sec)
mysql> 
mysql> select    version();  #显示数据库服务软件的版本号
+-----------+
| version() |
+-----------+
| 5.7.17    |
+-----------+
1 row in set (0.00 sec)
mysql> 
mysql> select database();   #显示当前所在的库
+------------+
| database() |
+------------+
| NULL         |  # 表示没有在任何库里  在数据库目录里/var/lib/mysql
+------------+
1 row in set (0.00 sec)
mysql> use mysql;   #进入到mysql库里
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select database();  #在查看所在的库
+------------+
| database() |
+------------+
| mysql        |   #当前在mysql库下
+------------+
1 row in set (0.00 sec)
mysql> 
mysql> show tables;  #显示所在库下已有的表 (表就是用来存数据的文件 )
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| engine_cost               |
mysql> exit;   #断开连接(退回到系统命令行)
Bye
[root@host50 ~]#

相关参数(必须熟知 ) 

主配置文件  /etc/my.cnf    

数据库目录   /var/lib/mysql

服务的进程名/端口   mysqld/3306

进程所有者和所属组    mysql/mysql 

数据传输协议    tcp 

错误日志文件名 /var/log/mysqld.log

基本查询 : 

连接数据库服务的连接方式:命令行连接( mysql命令)

访问安装的图形软件连接

编写脚本连接(如 : python连接脚本  PHP连接脚本 java连接脚本)

mysql的管理环境:(在使用数据库服务时,要遵循的一些规则)

1)语法规范 (要熟知)

2)SQL语句分类  (行业黑话  要知道) 

(SQL 中文意思  结构化查询语言)

在 mysql> 的状态下执行的命令统称为SQL语句

3)基础查询 :对已经存储在表里的数据做查找

查询命令格式

select  字段名列表  from  库名.表名; #查看表里的所有行

select  字段名列表  from  库名.表名  where   查询条件 ; #只查看与条件匹配的行

要先准备数据查询练习的环境:(统一在host50 主机准备练习环境)

练习使用user表 (存储的是操作系统的用户信息,就是/etc/passwd文件的内容)

4)select 命令用法演示 (select 是对数据做查询的命令)

用法1   查看常量

mysql> select 3;
+---+
| 3 |
+---+
| 3 |
+---+
1 row in set (0.00 sec)
mysql> select 5;
+---+
| 5 |
+---+
| 5 |
+---+
1 row in set (0.00 sec)

用法2   查看mysql环境变量

mysql> select @@version;
+-----------+
| @@version |
+-----------+
| 5.7.17    |
+-----------+
1 row in set (0.00 sec)

用法3   查看计算结果

mysql> select 3+5;
+-----+
| 3+5 |
+-----+
|   8 |
+-----+
1 row in set (0.00 sec)

用法4  查询时使用函数

mysql> select count(*) from  tarena.user; #统计表的行数
+----------+
| count(*) |
+----------+
|       23 |
+----------+
1 row in set (0.00 sec)

~~~~~~~~~~~~练习环境准备

host50~]# ls /root/*.sql
/root/tarena.sql
[root@host50 ~]# mysql -hlocalhost -uroot -p654321 < /root/tarena.sql 
[root@host50 ~]# mysql -hlocalhost -uroot -p654321
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| tarena             |
+--------------------+
7 rows in set (0.00 sec)
mysql> use tarena;
mysql> show tables;
+------------------+
| Tables_in_tarena |
+------------------+
| departments      |
| employees        |
| salary           |
| user             |
+------------------+
4 rows in set (0.00 sec)

使用user 表做查询练习 

user表里存储的是 系统用户信息 就是  /etc/passwd  文件的内容 

id  行号 

name 用户名           

password  密码占位符x 

uid  用户的uid号  

gid  用户的组id号

comment   用户的说明信息

homedir    用户的家目录          

shell   用户使用的的shell

用法5  :只查看表里的某个表头

mysql> select name from  tarena.user;   #查一个表头
mysql> select name ,uid from  tarena.user;  #查多个表头
mysql> select *  from  tarena.user;  #查看所有表头
mysql> select *  from  tarena.user where  id = 1; #加条件查
+----+------+----------+------+------+---------+---------+-----------+
| id | name | password | uid  | gid  | comment | homedir | shell     |
+----+------+----------+------+------+---------+---------+-----------+
|  1 | root | x        |    0 |    0 | root    | /root   | /bin/bash |
+----+------+----------+------+------+---------+---------+-----------+
1 row in set (0.00 sec)
mysql>

~~~~~~~~~查询条件的使用 :(不加查询条件时 会显示查询的所有行和列,如果加了查询条件,只显示与条件匹配的行和列)

1、给查找到的数据定义别名 使用 as  或 空格    

mysql> select name , homedir  from tarena.user;

mysql> select name as 用户名 , homedir 家目录 from tarena.user;

2、给查找到的数据拼接   concat()

mysql> select name,uid from tarena.user;
mysql> select concat(name,"-",uid) from tarena.user;
mysql> select concat(name,"-",uid)  as  用户信息  from tarena.user;

3、查询的数据有重复时 ,不显示重复     distinct  字段名列表

mysql> select shell from  tarena.user;
mysql> select distinct shell from  tarena.user;
+----------------+
| shell          |
+----------------+
| /bin/bash      |
| /sbin/nologin  |
| /bin/sync      |
| /sbin/shutdown |
| /sbin/halt     |
| /bin/false     |
| NULL           |
+----------------+
7 rows in set (0.00 sec)

数值比较 符号   =      !=       >       >=       <       <= 

相等    不相等   大于   大于等于    小于    小于等于

说明符号两边要是数字或数字类型的表头  用符号左边的和右边的做比较

mysql> select  id,name,uid,gid  from  tarena.user where  id = 3;
+----+--------+------+------+
| id | name   | uid  | gid  |
+----+--------+------+------+
|  3 | daemon |    2 |    2 |
+----+--------+------+------+
1 row in set (0.00 sec)
mysql> select  id,name,uid,gid  from  tarena.user where  id < 3;
+----+------+------+------+
| id | name | uid  | gid  |
+----+------+------+------+
|  1 | root |    0 |    0 |
|  2 | bin  |    1 |    1 |
+----+------+------+------+
2 rows in set (0.00 sec)
mysql> select  id,name,uid,gid  from  tarena.user where  id <= 3;
+----+--------+------+------+
| id | name   | uid  | gid  |
+----+--------+------+------+
|  1 | root   |    0 |    0 |
|  2 | bin    |    1 |    1 |
|  3 | daemon |    2 |    2 |
+----+--------+------+------+
3 rows in set (0.00 sec)
mysql> select  id,name,uid,gid  from  tarena.user where  uid > 6000;
+----+-----------+-------+-------+
| id | name      | uid   | gid   |
+----+-----------+-------+-------+
| 22 | nfsnobody | 65534 | 65534 |
+----+-----------+-------+-------+
1 row in set (0.00 sec)
mysql> select  id,name,uid,gid  from  tarena.user where  uid >= 1000;
+----+-----------+-------+-------+
| id | name      | uid   | gid   |
+----+-----------+-------+-------+
| 22 | nfsnobody | 65534 | 65534 |
| 24 | plj       |  1000 |  1000 |
+----+-----------+-------+-------+
2 rows in set (0.00 sec)
mysql> select  id,name,uid,gid  from  tarena.user where  uid = gid;
+----+-----------------+-------+-------+
| id | name            | uid   | gid   |
+----+-----------------+-------+-------+
|  1 | root            |     0 |     0 |
|  2 | bin             |     1 |     1 |
|  3 | daemon          |     2 |     2 |
| 13 | nobody          |    99 |    99 |
| 14 | systemd-network |   192 |   192 |
| 15 | dbus            |    81 |    81 |
| 17 | sshd            |    74 |    74 |
| 18 | postfix         |    89 |    89 |
| 20 | rpc             |    32 |    32 |
| 21 | rpcuser         |    29 |    29 |
| 22 | nfsnobody       | 65534 | 65534 |
| 23 | haproxy         |   188 |   188 |
| 24 | plj             |  1000 |  1000 |
| 25 | apache          |    48 |    48 |
| 26 | mysql           |    27 |    27 |
+----+-----------------+-------+-------+
15 rows in set (0.00 sec)
mysql> select  id,name,uid,gid  from  tarena.user where  uid != gid;
+----+----------+------+------+
| id | name     | uid  | gid  |
+----+----------+------+------+
|  4 | adm      |    3 |    4 |
|  5 | lp       |    4 |    7 |
|  6 | sync     |    5 |    0 |
|  7 | shutdown |    6 |    0 |
|  8 | halt     |    7 |    0 |
|  9 | mail     |    8 |   12 |
| 10 | operator |   11 |    0 |
| 11 | games    |   12 |  100 |
| 12 | ftp      |   14 |   50 |
| 16 | polkitd  |  999 |  998 |
| 19 | chrony   |  998 |  996 |
+----+----------+------+------+
11 rows in set (0.00 sec)
mysql>

字符比较 符号   =   !=  

mysql> select  name  from  tarena.user where name="apache" ;
+--------+
| name   |
+--------+
| apache |
+--------+
1 row in set (0.00 sec)
mysql> select  name , shell  from  tarena.user where shell != "/bin/bash";
+-----------------+----------------+
| name            | shell          |
+-----------------+----------------+
| bin             | /sbin/nologin  |
| daemon          | /sbin/nologin  |
| adm             | /sbin/nologin  |
| lp              | /sbin/nologin  |
| sync            | /bin/sync      |
| shutdown        | /sbin/shutdown |
| halt            | /sbin/halt     |
| mail            | /sbin/nologin  |
| operator        | /sbin/nologin  |
| games           | /sbin/nologin  |
| ftp             | /sbin/nologin  |
| nobody          | /sbin/nologin  |
| systemd-network | /sbin/nologin  |
| dbus            | /sbin/nologin  |
| polkitd         | /sbin/nologin  |
| sshd            | /sbin/nologin  |
| postfix         | /sbin/nologin  |
| chrony          | /sbin/nologin  |
| rpc             | /sbin/nologin  |
| rpcuser         | /sbin/nologin  |
| nfsnobody       | /sbin/nologin  |
| haproxy         | /sbin/nologin  |
| apache          | /sbin/nologin  |
| mysql           | /bin/false     |
+-----------------+----------------+
24 rows in set (0.00 sec)

空       is  null      表头下没有数据

非空       is  not null  表头下有数据 

例子 :征婚 要填个人信息

有车 (无)     有房(有)   存款(有)  有孩子(有)

null  

mysql服务  使用关键字  null  或 NULL  表示没有数据

mysql> select id, name from  tarena.user where name is null;  #查看没有名字的用户和行号 都有名字查询结果是empty
mysql> insert into  tarena.user(name) values(null) ; #添加用户没给名字
mysql> insert into  tarena.user(name) values(null) ; #添加用户没给名字
mysql> select id, name from  tarena.user where name is null; #查看没有名字的用户和行号
+----+------+
| id | name |
+----+------+
| 28 | NULL |
| 29 | NULL |
+----+------+

这这几种赋值方式都不是空

mysql> insert into tarena.user(id,name) values(71,"");    #零个字符
mysql> insert into tarena.user(id,name) values(72,"null");  #是普通字母
mysql> insert into tarena.user(id,name) values(73,NULL);  #表示空
mysql> insert into tarena.user(id,name) values(74,null);  #表示空
mysql> select id , name from  tarena.user where  id >= 71;
+----+------+
| id | name |
+----+------+
| 71 |      |
| 72 | null |
| 73 | NULL |
| 74 | NULL |
+----+------+
mysql> select id , name from  tarena.user where name is null;
mysql> select id , name from  tarena.user where name is null;
+----+------+
| id | name |
+----+------+
| 28 | NULL |
| 29 | NULL |
| 73 | NULL |
| 74 | NULL |
+----+------+
mysql> select id , name from  tarena.user where name="";
+----+------+
| id | name |
+----+------+
| 71 |      |
+----+------+
1 row in set (0.00 sec)
mysql> select id , name from  tarena.user where name="null";
+----+------+
| id | name |
+----+------+
| 72 | null |
+----+------+
1 row in set (0.00 sec)
mysql> select id , name from  tarena.user where name is not null;
+----+-----------------+
| id | name            |
+----+-----------------+
|  1 | root            |
|  2 | bin             |
|  3 | daemon          |
|  4 | adm             |
|  5 | lp              |
....
....
| 27 | bob             |
| 71 |                 |
| 72 | null            |
+----+-----------------+

范围匹配条件   in   、  not  in  、  between  数字1 and   数字2  

mysql> select name , uid  from  tarena.user where  uid  in (10 , 20 , 30 , 50);
Empty set (0.00 sec)
mysql> select name , uid  from  tarena.user where  uid  in (1 , 3 , 5 , 7);  
+------+------+
| name | uid  |
+------+------+
| bin  |    1 |
| adm  |    3 |
| sync |    5 |
| halt |    7 |
+------+------+
mysql> select name , shell  from  tarena.user where  shell  not in ("/bin/bash","/sbin/nologin");
+----------+----------------+
| name     | shell          |
+----------+----------------+
| sync     | /bin/sync      |
| shutdown | /sbin/shutdown |
| halt     | /sbin/halt     |
| mysql    | /bin/false     |
+----------+----------------+
mysql> select  id, name,uid  from  tarena.user where  id between 10 and 20 ;
+----+-----------------+------+
| id | name            | uid  |
+----+-----------------+------+
| 10 | operator        |   11 |
| 11 | games           |   12 |
| 12 | ftp             |   14 |
| 13 | nobody          |   99 |
| 14 | systemd-network |  192 |
| 15 | dbus            |   81 |
| 16 | polkitd         |  999 |
| 17 | sshd            |   74 |
| 18 | postfix         |   89 |
| 19 | chrony          |  998 |
| 20 | rpc             |   32 |
+----+-----------------+------+
11 rows in set (0.00 sec)
mysql>

模糊匹配条件   where  字段名   like  ‘表达式’

统配符号  

_ 表示 1个字符

%表示零个或多个字符

ysql> select name from  tarena.user where  name like "_ _ _"; #找名字必须是3个字符的 (没有空格挨着敲)
+------+
| name |
+------+
| bin  |
| adm  |
| ftp  |
| rpc  |
| plj  |
| bob  |
+------+
6 rows in set (0.00 sec)
mysql> select name from  tarena.user where  name like "_ _ _ _"; #找名字必须是4个字符的(没有空格挨着敲)
+------+
| name |
+------+
| root |
| sync |
| halt |
| mail |
| dbus |
| sshd |
| null |
+------+
7 rows in set (0.00 sec)
mysql> select name from  tarena.user where  name like "a%";  #找名字以字母a开头的(没有空格挨着敲)

查找名字等于等于4个字符的

mysql> select name from  tarena.user where  name like "%_ _ _ _%";#(没有空格挨着敲)
mysql> select name from  tarena.user where name  like "_ _%_ _";#(没有空格挨着敲)

正则匹配   使用正则表达式做判断条件        格式:   字段名 regexp  '正则表达式'

回忆shell课程学过的元字符(正则符号)

^ 匹配行首

$ 匹配行尾

 [] 匹配范围内任意一个

*  前边的表达式出现零次或多次

|  或者

mysql> insert into  tarena.user(name)values("yaya9");
mysql> insert into  tarena.user(name)values("6yaya");
mysql> insert into  tarena.user(name)values("ya7ya");
mysql> insert into  tarena.user(name)values("yay8a");
mysql> select name from  tarena.user where name regexp "[0-9]";
+-------+
| name  |
+-------+
| yaya9 |
| 6yaya |
| ya7ya |
| yay8a |
+-------+
4 rows in set (0.00 sec)
mysql> select name from  tarena.user where name regexp "^[0-9]";
+-------+
| name  |
+-------+
| 6yaya |
+-------+
1 row in set (0.00 sec)
mysql> select name from  tarena.user where name regexp "[0-9]$";
+-------+
| name  |
+-------+
| yaya9 |
+-------+
1 row in set (0.00 sec)
mysql> 
mysql> select name from  tarena.user where name regexp "^r";
+---------+
| name    |
+---------+
| root    |
| rpc     |
| rpcuser |
+---------+
3 rows in set (0.00 sec)
mysql> select name from  tarena.user where name regexp "t$";
+------+
| name |
+------+
| root |
| halt |
+------+
2 rows in set (0.00 sec)
mysql> 
mysql> select name from  tarena.user where name regexp "^r|t$";
+---------+
| name    |
+---------+
| root    |
| halt    |
| rpc     |
| rpcuser |
+---------+
4 rows in set (0.00 sec)
mysql> select name from  tarena.user where name regexp "^r.*t$";
+------+
| name |
+------+
| root |
+------+
1 row in set (0.00 sec)
mysql>

逻辑匹配  就是有多个判断条件

逻辑与   and  &&        多个判断条件必须同时成立

逻辑或   or   ||        多个判断条件其中某个条件成立即可

逻辑非   not  !         取反

招聘条件

性别   女   and    学历   本科  and    英语水平   四级  and  颜值  高    (4个条件要同时满足)

性别   女   or    学历   本科   or     英语水平   四级   or  颜值  高    (4个条件满足其中任意一个条件就可以)

not  性别   女     (取反)

mysql> select name,shell from tarena.user where  shell = "/bin/bash";
+------+-----------+
| name | shell     |
+------+-----------+
| root | /bin/bash |
| plj  | /bin/bash |
+------+-----------+
2 rows in set (0.01 sec)
mysql> select name,shell from tarena.user where  shell != "/bin/bash"; #取反
not 也是取反  要放在表达式的前边
mysql> select name,shell from tarena.user where  not  shell = "/bin/bash";
mysql> select id , name from tarena.user where id between 10 and 20 ;
+----+-----------------+
| id | name            |
+----+-----------------+
| 10 | operator        |
| 11 | games           |
| 12 | ftp             |
| 13 | nobody          |
| 14 | systemd-network |
| 15 | dbus            |
| 16 | polkitd         |
| 17 | sshd            |
| 18 | postfix         |
| 19 | chrony          |
| 20 | rpc             |
+----+-----------------+
11 rows in set (0.00 sec)
mysql> select id , name from tarena.user where not  id between 10 and 20 ;  #取反

()  提高优先级  作用:改变执行顺序

mysql> select  2 + 3 ;
+-------+
| 2 + 3 |
+-------+
|     5 |
+-------+
1 row in set (0.00 sec)
mysql> select  2 + 3  *  5;
+-------------+
| 2 + 3  *  5 |
+-------------+
|          17 |
+-------------+
1 row in set (0.00 sec)
mysql> select  (2 + 3)  *  5;
+---------------+
| (2 + 3)  *  5 |
+---------------+
|            25 |
+---------------+
1 row in set (0.00 sec)
mysql>

逻辑匹配什么时候需要加()

逻辑与and  的优先级要高于逻辑或 or 

如果在筛选条件里既有and 又有 or   先判断and 再判断or 

既有and又有or  优先匹配and 

mysql> select name , uid from tarena.user  where name = "root" or name = "bin"  and uid = 1 ;
+------+------+
| name | uid  |
+------+------+
| root |    0 |
| bin  |    1 |
+------+------+
2 rows in set (0.00 sec)

() 提高执行的优先级

mysql> select name , uid from tarena.user  where name = "root" or name = "bin"  and uid = 1 ;  #没加() 的查询结果
+------+------+
| name | uid  |
+------+------+
| root |    0 |
| bin  |    1 |
+------+------+
2 rows in set (0.00 sec)
mysql> select name , uid from tarena.user  where (name = "root" or name = "bin")  and uid = 1 ;  #加了()的查询结果
+------+------+
| name | uid  |
+------+------+
| bin  |    1 |
+------+------+
1 row in set (0.00 sec)
mysql>
数据库服务概述 、 构建MySQL服务 、 数据库基本管理 、 MySQL数据类型  您阅读本篇文章共花了: 

分享到:

    扫描二维码推送至手机访问。

    版权声明:本文由有云转晴发布,如需转载请注明出处。

    本文链接:https://www.yyzq.cf/?id=137

    分享给朋友:
    返回列表

    没有更早的文章了...

    下一篇:表结构 、 MySQL键值

    发表评论

    访客

    ◎欢迎参与讨论,请在这里发表您的看法和观点。