Linux筆記 -- Week11 Q&A

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;
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請(qǐng)通過簡(jiǎn)信或評(píng)論聯(lián)系作者。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容