1、 導(dǎo)入hellodb.sql生成數(shù)據(jù)庫(kù)
登錄mysql數(shù)據(jù)庫(kù),導(dǎo)入SQL腳本并執(zhí)行
[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歲,且為男性的同學(xué)的名字和年齡
[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為分組依據(jù),顯示每組的平均年齡
[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開頭的名字的同學(xué)的信息
[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、數(shù)據(jù)庫(kù)授權(quán)magedu用戶,允許192.168.1.0/24網(wǎng)段可以連接mysql
CREATE USER magedu@'192.168.1.%' IDENTIFIED BY '123456';
FLUSH PRIVILEGES;