1、 导入hellodb.sql生成数据库
登录mysql数据库,导入SQL脚本并执行
[root@localhost][(none)]> \! ls /data
hellodb_innodb.sql
[root@localhost][(none)]> source /data/hellodb_innodb.sql
[root@localhost][hellodb]> SHOW TABLES;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
7 rows in set (0.00 sec)
[root@localhost][hellodb]>
1-1 在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄
[root@localhost][hellodb]> SELECT Name,Age FROM students WHERE Gender='M' and Age>25;
+--------------+-----+
| 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.00 sec)
[root@localhost][hellodb]>
1-2 以ClassID为分组依据,显示每组的平均年龄
[root@localhost][hellodb]> SELECT ROUND(AVG(Age), 0) as Avg_Age,ClassID FROM students GROUP BY ClassID;
+---------+---------+
| Avg_Age | ClassID |
+---------+---------+
| 36 | 2 |
| 21 | 1 |
| 25 | 4 |
| 20 | 3 |
| 46 | 5 |
| 20 | 7 |
| 21 | 6 |
| 64 | NULL |
+---------+---------+
8 rows in set (0.00 sec)
[root@localhost][hellodb]>
1-3 显示第2题中平均年龄大于30的分组及平均年龄
root@localhost][hellodb]> SELECT ROUND(AVG(Age), 0) as Avg_Age,ClassID FROM students GROUP BY ClassID HAVING Avg_age>30;
+---------+---------+
| Avg_Age | ClassID |
+---------+---------+
| 36 | 2 |
| 46 | 5 |
| 64 | NULL |
+---------+---------+
3 rows in set (0.00 sec)
[root@localhost][hellodb]>
1-4 显示以L开头的名字的同学的信息
[root@localhost][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.00 sec)
[root@localhost][hellodb]>
2、数据库授权magedu用户,允许192.168.1.0/24网段可以连接mysql
CREATE USER magedu@'192.168.1.%' IDENTIFIED BY '123456';
FLUSH PRIVILEGES;