1201、数据库基础、搭建mysql服务器、密码管理、安装图形软件、基础命令、查询命令

目录

一、数据库概述

1  数据库介绍

2 常见的数据库服务软件

3 专业术语 

4 mysql介绍

二、搭建MySQL服务器

1、搭建第1台数据库服务器

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

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

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

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

5 使用初始密码连接服务

6 修改登录密码

7 断开连接

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

以上操作失败的解决办法:

2、搭建第2台数据库服务器

三、密码管理

密码策略

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

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

 2.1 破解线下数据库服务器管理员root 密码

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

3、修改root密码 

四、安装图形软件

五、数据库必备命令的使用

1、基本操作

2、 相关参数

3、连接数据库服务的连接方式:

 五、查询 select    

1、查询命令格式:

2、select 命令用法:

        1、查看常量

        2、查看mysql环境变量

        3、查看计算结果

        4、查询时使用函数

        练习环境准备

        5、只查看表里的某个表头

3、常用的筛选条件有: 

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

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

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

         4.数值比较

        5、字符比较        符号    =    !=

        6、空和非空

        7、范围匹配条件

        8、模糊匹配条件

        9、正则匹配

        10、逻辑匹配

         11、()  提高优先级

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

         () 提高执行的优先级


准备实验使用的虚拟机

准备做数据库服务器的虚拟机(RHEL7操作系统)
使用准备的模板机克隆(链接克隆就可以)2台虚拟机:配置要求如下
     ip   192.168.4.50     192.168.4.51

[root@host50 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth1

TYPE=Ethernet
BOOTPROTO=none
IPV6INIT=no
NAME=eth1
DEVICE=eth1
ONBOOT=yes
PROXY_METHOD=none
BROWSER_ONLY=no
IPADDR=192.168.4.50
PREFIX=24
DEFROUTE=yes
IPV4_FAILURE_FATAL=no

[root@host50 ~]# ifdown eth1

[root@host50 ~]# ifup eth1


     配置YUM源
     关闭firewalld  和  selinux
     拷贝软件  mysql-5.7.17.tar 到创建的虚拟机里

一、数据库概述

数据库介绍

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

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

2 常见的数据库服务软件

      

3 专业术语 

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

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

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

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

4 mysql介绍

主要特点:
                  适用于中小规模、关系型数据库系统;
                  支持Linux、Unix、Windows等操作系统;
                  支持Python、java、Perl、PHP等编程语言。

典型应用环境:
                   LAMP平台,与Apache   HTTP  Server组合
                   LNMP平台,与Nginx组合

二、搭建MySQL服务器

1、搭建第1台数据库服务器

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 社区开源版软件

从官网下载rpm包

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

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 使用修改的密码登录并查看数据

默认的4个库 不允许删除  库存放的是不同类型的数据 后边的课程会陆续讲解

[root@host50 ~]# mysql    -hlocalhost    -uroot     -p123qqq...A
mysql> show databases;      # 查看已有的库 
mysql> exit;

以上操作失败的解决办法:

[root@host50 ~]#systemctl    stop    mysqld        # 停掉服务
[root@host50 ~]#
rm   -rf   /var/lib/mysqld/*         #  删除刚才生成的全部文件
[root@host50 ~]#systemctl    start    mysqld       
[root@host50 ~]#grep  password  /var/log/mysqld.log  |    tail  -1
[root@host50 ~]#mysql   -hlocalhost    -uroot   -p'p.7jr.uy.aiZ'    # 用新生成的密码再练一遍
[root@host50 ~]#mysql>  alter user   root@"localhost" identified by "123qqq...A"; #修改密码

2、搭建第2台数据库服务器

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

和192.168.4.50操作一样

三、密码管理

密码策略

 

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

操作步骤:
        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 密码

具体操作如下:

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

        2 重启数据库服务

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

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

        5 重启数据库服务

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

---------------------------------------------------------------------------------------------------------------------------

1 修改主配置文件 (使其可以无密码登录)
[root@host50 ~]# vim /etc/my.cnf

[mysqld]
#validate_password_policy=0
#validate_password_length=6
skip-grant-tables                    
#跳过授权库MySQL库启动服务,作用连接服务不需要输入密码

2 重启数据库服务
[root@host50 ~]# systemctl  restart mysqld

3 无密码登录,并修改登录密码,断开连接
[root@host50 ~]# mysql                                      # 不输入密码就可以登陆

Mysql> desc  mysql.user;
mysql> select user , host , authentication_string  from mysql.user;

+------------+-------------+-----------------------------------------------------------------------------+
| user         | host         | authentication_string                                                               |
+------------+-------------+-----------------------------------------------------------------------------+
| root          | localhost | *F19C699342FA5C91EBCF8E0182FB71470EB2AF30         |
| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+-----------+--------------+-----------------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql>
mysql> select password("123456");
+-------------------------------------------+
| password("123456")                        |
+-------------------------------------------+
| *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-------------------------------------------+
1 row in set, 1 warning (0.00 sec)

mysql>

#修改管理员root 本机登陆密码为  123qqq...A

mysql> update  mysql.user set authentication_string=password("123qqq...A")

where user="root" and host="localhost"; 

mysql> flush privileges;                         # 确保修改生效

mysql> exit;                          # 断开连接

4 还原对主配置文件 的修改
[root@host50 ~]# vim /etc/my.cnf                 #  还原修改

[mysqld]
validate_password_policy=0
validate_password_length=6
#skip-grant-tables

5 重启数据库服务
[root@host50 ~]# systemctl  restart mysqld                # 重启服务

6 使用破解后的密码登录(能登录为成功)
[root@host50 ~]# mysql -hlocalhost -uroot -p123qqq...A               # 使用恢复的密码登陆
                       

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

具体步骤如下:

       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

                  |-mysqld(1806)-+-{mysqld}(1807)

[root@host50 ~]# kill   -SIGHUP   1807

[root@host50 ~]# mysql     -hlocalhost    -uroot     -p51主机的密码

3、修改root密码 

说明工作:为数据库管理员root用户密码的安全,可以定期修改密码(比如每隔10天修改一次密码)
注意:修改密码,必须要知道旧密码,才能设置新密码

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

(2) mysqladmin修改 (操作系统管理员 修改本机数据库服务的登陆密码)
        ]# mysqladmin  -uroot    -p旧密码   password  新密码

(3) (交互式)隐藏旧密码和新密码,根据提示输入密码
        ]# mysqladmin  -uroot    -p   password
第一次提示 输入旧密码
第二次提示 输入新密码  (新密码 要服务密码策略要求)

四、安装图形软件

在数据库服务器安装图形软件 ,通过连接图形软件,对数据库做管理

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

   在host50主机做如下配置:

步骤一:安装phpmyadmin软件。

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

2) 安装phpmyadmin软件

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

[root@host50 ~]# yum -y install httpd php  php-mysql 

[root@host50 ~]# systemctl  start httpd  ; systemctl  enable httpd

[root@host50 mysql]tar -xf phpMyAdmin-2.11.11-all-languages.tar.gz 

[root@host50 mysql]# mv phpMyAdmin-2.11.11-all-languages /var/www/html/phpmyadmin

[root@host50 mysql]# cd /var/www/html/phpmyadmin

[root@host50 phpmyadmin]cp config.sample.inc.php  config.inc.php  创建主配置文件 

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

        $cfg['blowfish_secret'] = 'plj123';

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

  打开真机的浏览器输入

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

  用户名   root

  密码     root用户密码

五、数据库必备命令的使用

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

1、基本操作

mysql> show  databases;         # 显示服务器上已有的库(文件夹)

mysql> select user();                # 显示当前登录的用户名和客户端地址
+----------------+
| user()         |
+----------------+
| root@localhost |                        # 数据库管理员root 用户本机登录
+----------------+

mysql> select    version();          # 显示数据库服务软件的版本号

mysql> select database();           # 显示当前所在的库
+------------+
| database() |
+------------+
| NULL         |                             # 表示没有在任何库里  在数据库目录里/var/lib/mysql
+------------+

mysql> use mysql;                   # 进入到mysql库里

mysql> show tables;                  # 显示所在库下已有的表 (表就是用来存数据的文件 )

mysql> exit;                                   # 断开连接(退回到系统命令行)

2、 相关参数

3、连接数据库服务的连接方式:

  •         1.命令行连接( mysql命令)
    •         2.访问安装的图形软件连接
      •         3.编写脚本连接(如 : python连接脚本  PHP连接脚本 java连接脚本)

 五、查询 select    

练习所用文件https://download.csdn.net/download/weixin_56619848/85566102

1、查询命令格式:

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

2、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)

        练习环境准备

[root@host50 ~]# mysql  -hlocalhost  -u用户名  -p密码   /root/tarena.sql        #导入外部数据库文件

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| studb              |
| sys                |
| tarena             |
+--------------------+
6 rows in set (0.00 sec)

mysql> use tarena;
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> show tables;
+------------------+
| Tables_in_tarena |
+------------------+
| departments      |
| employees        |
| salary           |
| user             |
+------------------+
4 rows in set (0.00 sec)

mysql> desc tarena.user;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int(11)     | NO   | PRI | NULL    | auto_increment |
| name     | char(20)    | YES  |     | NULL    |                |
| password | char(1)     | YES  |     | NULL    |                |
| uid      | int(11)     | YES  |     | NULL    |                |
| gid      | int(11)     | YES  |     | NULL    |                |
| comment  | varchar(50) | YES  |     | NULL    |                |
| homedir  | varchar(80) | YES  |     | NULL    |                |
| shell    | char(30)    | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)

mysql> select * from tarena.user;


mysql>
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;    # 加条件查

3、常用的筛选条件有: 

        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           |

+----------------+

         4.数值比较

符号=!=>>=<<=
相等不相等大于大于等于小于小于等于
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)

        5、字符比较        符号    =    !=

mysql>  select  name  from  tarena.user where name="apache" ;


mysql>  select  name , shell  from  tarena.user where shell != "/bin/bash";

        6、空和非空

空           is  null         表头下没有数据
非空       is  not 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);   # 表示空

        7、范围匹配条件

                        in   、                                                         # 在

                        not  in  、                                                  # 不在

                        between  num1   and    num2                  # 在...和...之间

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)

        8、模糊匹配条件

where  字段名   like  ‘表达式’

 统配符号: 

  _          # 表示 1个字符

  %         # 表示零个或多个字符

mysql> 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 "%o%";(没有空格挨着敲)
+------+
| name |
+------+
| root |
+------+
mysql> select name from  tarena.user where name  like "r%t";(没有空格挨着敲)
+------+
| name |
+------+
| root |
+------+

        9、正则匹配

使用正则表达式做判断条件        

  格式:   字段名 regexp  '正则表达式'

        ^ 匹配行首

        $ 匹配行尾

        [] 匹配范围内任意一个

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

        |  或者

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> 

        10、逻辑匹配

就是有多个判断条件

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

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

逻辑非   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";      # 取反
mysql> select name,shell from tarena.user where  not  shell = "/bin/bash";  #  not 也是取反,要放在表达式的前边

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 ;  取反 

         11、()  提高优先级

                作用:改变执行顺序

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)

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

逻辑与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)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值