数据库基础

1、 导入hellodb.sql生成数据库

MariaDB [(none)]> source /scripts/hellodb_innodb.sql
(1) 在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄
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 Classid,avg(age) from students where classid is not null group by classid;
+---------+----------+
| Classid | avg(age) |
+---------+----------+
|       1 |  20.5000 |
|       2 |  36.0000 |
|       3 |  20.2500 |
|       4 |  24.7500 |
|       5 |  46.0000 |
|       6 |  20.7500 |
|       7 |  19.6667 |
+---------+----------+
7 rows in set (0.001 sec)
(3) 显示第2题中平均年龄大于30的分组及平均年龄
MariaDB [hellodb]> select Classid,avg(age) from students where classid is not null group by classid having avg(age) > 30;
+---------+----------+
| Classid | avg(age) |
+---------+----------+
|       2 |  36.0000 |
|       5 |  46.0000 |
+---------+----------+
2 rows in set (0.001 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)

2、数据库授权magedu用户,允许192.168.1.0/24网段可以连接mysql

MariaDB [mysql]> create user 'magedu'@'192.168.1.%' identified by 'magedu';
Query OK, 0 rows affected (0.001 sec)

MariaDB [mysql]> show grants for 'magedu'@'192.168.1.%';
+-----------------------------------------------------------------------------------------------------------------+
| Grants for magedu@192.168.1.%                                                                                   |
+-----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `magedu`@`192.168.1.%` IDENTIFIED BY PASSWORD '*6B8CCC83799A26CD19D7AD9AEEADBCD30D8A8664' |
+-----------------------------------------------------------------------------------------------------------------+
1 row in set (0.000 sec)
posted @ 2021-10-14 22:49  温白开  阅读(29)  评论(0编辑  收藏  举报