• MariaDB使用数据库查询《三》


     

                                                                 MariaDB使用数据库查询

     

    案例5:使用数据库查询

    5.1 问题

    本例要求配置MariaDB数据库,完成以下任务:

    1. 禁止空密码root用户访问mariadb数据库
    2. 在系统server0上使用数据库Contacts,通过SQL查询回答下列问题:密码是solicitous的人的名字?有多少人的姓名是Barbara同时居住在 Sunnyvale?

    5.2 方案

    表记录增删改查:

    1. insert  into  [库名.]表名  values(1,2,3);
    2. delete  from  [库名.]表名  where ...;
    3. update  [库名.]表名  set  字段名=字段值  where ....;
    4. select  字段列表  from  [库名.]表名  where  字段名1=  and|or  字段名2=; 

    统计查询结果的数量:

    1. select  count(*)  from  [库名.]表名  where  .. ..;

    5.3 步骤

    实现此案例需要按照如下步骤进行。

    步骤一:清理空密码root用户

    1)确认空密码root用户记录

    MariaDB服务端默认的mysql库user表保存了用户授权记录。

    使用DESC指令查看表结构,以便了解相关字段名:

    1. MariaDB [(none)]> DESC  mysql.user;
    2. +------------------------+-----------------------------------+------+-----+---------+-------+
    3. | Field                  | Type                              | Null | Key | Default | Extra |
    4. +------------------------+-----------------------------------+------+-----+---------+-------+
    5. | Host                   | char(60)                          | NO   | PRI |         |       |
    6. | User                   | char(16)                          | NO   | PRI |         |       |
    7. | Password               | char(41)                          | NO   |     |         |       |

    列出user表中的Host、User、Password字段,限定密码为空的root用户:

    1. MariaDB [(none)]> SELECT  Host,User,Password  FROM  mysql.user  WHERE  User='root'  AND  Password='';
    2. +---------------------+------+----------+
    3. | Host                | User | Password |
    4. +---------------------+------+----------+
    5. | server0.example.com | root |          |
    6. | 127.0.0.1           | root |          |
    7. | ::1                 | root |          |
    8. +---------------------+------+----------+
    9. 3 rows in set (0.00 sec)
    10. MariaDB [(none)]>

    2)删除空密码root用户记录

    使用DELETE指令删除掉需要清除的授权记录:

    1. MariaDB [(none)]> DELETE  FROM  mysql.user  WHERE  User='root'  AND  Password='';
    2. Query OK, 3 rows affected (0.00 sec)

    再次查询,确认删除结果:

    1. MariaDB [(none)]> SELECT  Host,User,Password  FROM  mysql.user  WHERE  User='root'  AND  Password='';
    2. Empty set (0.00 sec)

    步骤二:按条件查询表记录

    1)按单个条件查询

    找出密码是solicitous的人的名字?

    1. MariaDB [(none)]> SELECT  name  FROM  Contacts.base  WHERE  Password='solicitous';
    2. +-------+
    3. | name  |
    4. +-------+
    5. | James | 
    6. +-------+
    7. 1 row in set (0.00 sec)

    2)按多个条件在关联的两张表中查询

    有多少人的姓名是Barbara同时居住在 Sunnyvale?

    1. MariaDB [(none)]> USE  Contacts;
    2. .. ..
    3. Database changed
    4. MariaDB [Contacts]> SELECT  COUNT(*)  FROM  base,location  WHERE  base.name='Barbara'  AND  location.city='Sunnyvale'  AND  base.id=location.id;
    5. +----------+
    6. | COUNT(*) |
    7. +----------+
    8. |        1 |
    9. +----------+
    10. 1 row in set (0.00 sec)
  • 相关阅读:
    深入理解net core中的依赖注入、Singleton、Scoped、Transient(一)
    ActionDescriptorProviderContext
    IActionDescriptorProvider
    JWT签名算法中HS256和RS256有什么区别 转载
    JWT 使用的另一种声音
    JWT(Json Web Token):一种在Web应用中安全传递信息的规范 转载
    http 的session 工作原理。
    一般安全机制
    android 上传图片 .html 和android 客户端
    interpolator, typeEvaluator 以及属性动画的参数
  • 原文地址:https://www.cnblogs.com/qingbai/p/11937060.html
Copyright © 2020-2023  润新知