1片拍、 導(dǎo)入hellodb.sql生成數(shù)據(jù)庫
[root@centos7 ~]# mysql < hellodb_innodb.sql
(1) 在students表中谬哀,查詢年齡大于25歲,且為男性的同學(xué)的名字和年齡
MariaDB [hellodb]> select * from students where Age >25 and Gender='M';
+-------+--------------+-----+--------+---------+-----------+
| StuID | Name | Age | Gender | ClassID | TeacherID |
+-------+--------------+-----+--------+---------+-----------+
| 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 |
| 13 | Tian Boguang | 33 | M | 2 | NULL |
| 24 | Xu Xian | 27 | M | NULL | NULL |
| 25 | Sun Dasheng | 100 | M | NULL | NULL |
+-------+--------------+-----+--------+---------+-----------+
(2) 以ClassID為分組依據(jù)黍析,顯示每組的平均年齡
MariaDB [hellodb]> select ClassID,avg(Age) from students group by ClassID;
+---------+----------+
| ClassID | avg(Age) |
+---------+----------+
| NULL | 63.5000 |
| 1 | 20.5000 |
| 2 | 36.0000 |
| 3 | 20.2500 |
| 4 | 24.7500 |
| 5 | 46.0000 |
| 6 | 20.7500 |
| 7 | 19.6667 |
+---------+----------+
(3) 顯示第2題中平均年齡大于30的分組及平均年齡
MariaDB [hellodb]> select ClassID,avg(Age) from students group by ClassID having avg(Age) >30;
+---------+----------+
| ClassID | avg(Age) |
+---------+----------+
| NULL | 63.5000 |
| 2 | 36.0000 |
| 5 | 46.0000 |
+---------+----------+
(4) 顯示以L開頭的名字的同學(xué)的信息
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 |
+-------+-------------+-----+--------+---------+-----------+
2、數(shù)據(jù)庫授權(quán)magedu用戶屎开,允許192.168.1.0/24網(wǎng)段可以連接mysql
MariaDB [mysql]> create user magedu@'192.168.1.%' IDENTIFIED BY '123456';
MariaDB [(none)]> SHOW GRANTS FOR magedu@'192.168.1.%';
+-----------------------------------------------------------------------------------------------------------------+
| Grants for magedu@192.168.1.% |
+-----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'magedu'@'192.168.1.%' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
+-----------------------------------------------------------------------------------------------------------------+