一、导入hellodb.sql生成数据库
[root@centOS8 ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 24
Server version: 10.3.28-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create database test;
Query OK, 1 row affected (0.001 sec)
MariaDB [(none)]> use test;
Database changed
MariaDB [test]>
[root@centOS8 ~]# mysql -uroot -p931215 < /home/hellodb.sql
1、在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄
MariaDB [hellodb]> select * from students;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
| 1 | Shi Zhongyu | 22 | M | 2 | 3 |
| 2 | Shi Potian | 22 | M | 1 | 7 |
| 3 | Xie Yanke | 53 | M | 2 | 16 |
| 4 | Ding Dian | 32 | M | 4 | 4 |
| 5 | Yu Yutong | 26 | M | 3 | 1 |
| 6 | Shi Qing | 46 | M | 5 | NULL |
| 7 | Xi Ren | 19 | F | 3 | NULL |
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 9 | Ren Yingying | 20 | F | 6 | NULL |
| 10 | Yue Lingshan | 19 | F | 3 | NULL |
| 11 | Yuan Chengzhi | 23 | M | 6 | NULL |
| 12 | Wen Qingqing | 19 | F | 1 | NULL |
| 13 | Tian Boguang | 33 | M | 2 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 15 | Duan Yu | 19 | M | 4 | NULL |
| 16 | Xu Zhu | 21 | M | 1 | NULL |
| 17 | Lin Chong | 25 | M | 4 | NULL |
| 18 | Hua Rong | 23 | M | 7 | NULL |
| 19 | Xue Baochai | 18 | F | 6 | NULL |
| 20 | Diao Chan | 19 | F | 7 | NULL |
| 21 | Huang Yueying | 22 | F | 6 | NULL |
| 22 | Xiao Qiao | 20 | F | 1 | NULL |
| 23 | Ma Chao | 23 | M | 4 | NULL |
| 24 | Xu Xian | 27 | M | NULL | NULL |
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
+-------+---------------+-----+--------+---------+-----------+
25 rows in set (0.000 sec)
MariaDB [hellodb]> select Name,Age from students where Age>25 and Gender='M';
+--------------+-----+
| Name | Age |
+--------------+-----+
| Xie Yanke | 53 |
| Ding Dian | 32 |
| Yu Yutong | 26 |
| Shi Qing | 46 |
| Tian Boguang | 33 |
| Xu Xian | 27 |
| Sun Dasheng | 100 |
+--------------+-----+
7 rows in set (0.001 sec)
2、以ClassID为分组依据,显示每组的平均年龄
MariaDB [hellodb]> select AVG(Age),ClassID from students group by ClassID;
+----------+---------+
| AVG(Age) | ClassID |
+----------+---------+
| 63.5000 | NULL |
| 20.5000 | 1 |
| 36.0000 | 2 |
| 20.2500 | 3 |
| 24.7500 | 4 |
| 46.0000 | 5 |
| 20.7500 | 6 |
| 19.6667 | 7 |
+----------+---------+
8 rows in set (0.001 sec)
3、 显示第2题中平均年龄大于30的分组及平均年龄
MariaDB [hellodb]> select AVG(Age),ClassID from students group by ClassID having AVG(Age) > 30;
+----------+---------+
| AVG(Age) | ClassID |
+----------+---------+
| 63.5000 | NULL |
| 36.0000 | 2 |
| 46.0000 | 5 |
+----------+---------+
3 rows in set (0.000 sec)
4、显示以L开头的名字的同学的信息
MariaDB [hellodb]> select * from students where Name like 'L%';
+-------+-------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+-------------+-----+--------+---------+-----------+
| 8 | Lin Daiyu | 17 | F | 7 | NULL |
| 14 | Lu Wushuang | 17 | F | 3 | NULL |
| 17 | Lin Chong | 25 | M | 4 | NULL |
+-------+-------------+-----+--------+---------+-----------+
3 rows in set (0.001 sec)
二、数据库授权magedu用户,允许192.168.1.10/24网段可以连接mysql
MariaDB [hellodb]> use 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
MariaDB [mysql]> create user 'magedu'@'192.168.0.%';
Query OK, 0 rows affected (0.000 sec)
MariaDB [mysql]> select authentication_string,password,user,host from user;
+-----------------------+-------------------------------------------+--------+-------------+
| authentication_string | password | user | host |
+-----------------------+-------------------------------------------+--------+-------------+
| | *8EEFE024CEAF03EEDABABF22B6AC07E9A5D57600 | root | localhost |
| | *8EEFE024CEAF03EEDABABF22B6AC07E9A5D57600 | root | 127.0.0.1 |
| | *8EEFE024CEAF03EEDABABF22B6AC07E9A5D57600 | root | ::1 |
| | | magedu | 192.168.0.% |
+-----------------------+-------------------------------------------+--------+-------------+
4 rows in set (0.000 sec)
MariaDB [mysql]> update user set password=password('qazwsx') where user='magedu';
Query OK, 1 row affected (0.001 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [mysql]> select authentication_string,password,user,host from user;
+-----------------------+-------------------------------------------+--------+-------------+
| authentication_string | password | user | host |
+-----------------------+-------------------------------------------+--------+-------------+
| | *8EEFE024CEAF03EEDABABF22B6AC07E9A5D57600 | root | localhost |
| | *8EEFE024CEAF03EEDABABF22B6AC07E9A5D57600 | root | 127.0.0.1 |
| | *8EEFE024CEAF03EEDABABF22B6AC07E9A5D57600 | root | ::1 |
| | *E800315DF924876A1EB11B8DD8FBF928B65E65C7 | magedu | 192.168.0.% |
+-----------------------+-------------------------------------------+--------+-------------+
4 rows in set (0.000 sec)