▲就業(yè)班和全程班的小伙伴看這里:(學(xué)習(xí)老王視頻的作業(yè)第27-28節(jié))
1、?導(dǎo)入hellodb.sql生成數(shù)據(jù)庫(kù)
[root@centos7 ~]#mysql < hellodb_innodb.sql
[root@centos7 ~]#mysql
MariaDB [(none)]> show databases;
+--------------------+
| Database? ? ? ? ? |
+--------------------+
| information_schema |
| hellodb? ? ? ? ? ? |
| mysql? ? ? ? ? ? ? |
| performance_schema |
| test? ? ? ? ? ? ? |
+--------------------+
5 rows in set (0.00 sec)
(1)?在students表中,查詢年齡大于25歲,且為男性的同學(xué)的名字和年齡
MariaDB [(none)]> use hellodb
MariaDB [hellodb]> select * from students;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name? ? ? ? ? | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
|? ? 1 | Shi Zhongyu? ? ? |? 22 | M? ? ? |? ? ? 2 |? ? ? ? 3 |
|? ? 2 | Shi Potian? ? ? ? ? |? 22 | M? ? ? |? ? ? 1 |? ? ? ? 7 |
|? ? 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 |
|? ? 7 | Xi Ren? ? ? ? ? ? ? ?|? 19 | F? ? ? |? ? ? 3 |? ? ? NULL |
|? ? 8 | Lin Daiyu? ? ? ? ? ?|? 17 | F? ? ? |? ? ? 7 |? ? ? NULL |
|? ? 9 | Ren Yingying? ? ?|? 20 | F? ? ? |? ? ? 6 |? ? ? NULL |
|? ? 10 | Yue Lingshan? ?|? 19 | F? ? ? |? ? ? 3 |? ? ? NULL |
|? ? 11 | Yuan Chengzhi |? 23 | M? ? ? |? ? ? 6 |? ? ? NULL |
|? ? 12 | Wen Qingqing? |? 19 | F? ? ? |? ? ? 1 |? ? ? NULL |
|? ? 13 | Tian Boguang? |? 33 | M? ? ? |? ? ? 2 |? ? ? NULL |
|? ? 14 | Lu Wushuang? |? 17 | F? ? ? |? ? ? 3 |? ? ? NULL |
|? ? 15 | Duan Yu? ? ? ? ? |? 19 | M? ? ? |? ? ? 4 |? ? ? NULL |
|? ? 16 | Xu Zhu? ? ? ? ? ? |? 21 | M? ? ? |? ? ? 1 |? ? ? NULL |
|? ? 17 | Lin Chong? ? ? ?|? 25 | M? ? ? |? ? ? 4 |? ? ? NULL |
|? ? 18 | Hua Rong? ? ? ?|? 23 | M? ? ? |? ? ? 7 |? ? ? NULL |
|? ? 19 | Xue Baochai? ?|? 18 | F? ? ? |? ? ? 6 |? ? ? NULL |
|? ? 20 | Diao Chan? ? ? |? 19 | F? ? ? |? ? ? 7 |? ? ? NULL |
|? ? 21 | HuanYueying |? 22 | F? ? ? |? ? ? 6 |? ? ? NULL |
|? ? 22 | Xiao Qiao? ? ? ?|? 20 | F? ? ? |? ? ? 1 |? ? ? NULL |
|? ? 23 | Ma Chao? ? ? ? |? 23 | M? ? ? |? ? ? 4 |? ? ? NULL |
|? ? 24 | Xu Xian? ? ? ? ? |? 27 | M? ? ? |? ? NULL |? ? ? NULL |
|? ? 25 | Sun Dasheng? | 100 | M? ? ? |? ? NULL |? ? ? NULL |
+-------+---------------+-----+--------+---------+-----------+
25 rows in set (0.00 sec)
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.00 sec)
(2)?以ClassID為分組依據(jù),顯示每組的平均年齡
MariaDB [hellodb]> select classid,avg(age) as 'average age' from students group by classid;
+---------+-------------+
| classid | average 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 |
+---------+-------------+
8 rows in set (0.00 sec)
MariaDB [hellodb]> select classid,avg(age) as 'average age' from students group by classid having classid != "NULL";
+---------+-------------+
| classid | average 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, 1 warning (0.00 sec)
(3)?顯示第2題中平均年齡大于30的分組及平均年齡
MariaDB [hellodb]> select classid,avg(age) as 'average age' from students group by classid having avg(age)>30;
+---------+-------------+
| classid | average age |
+---------+-------------+
|? ? NULL |? ? 63.5000 |
|? ? ? 2 |? ? 36.0000 |
|? ? ? 5 |? ? 46.0000 |
+---------+-------------+
3 rows in set (0.00 sec)
MariaDB [hellodb]> select classid,avg(age) as 'average age' from students group by classid having classid != "NULL" and avg(age) >30;
+---------+-------------+
| classid | average age |
+---------+-------------+
|? ? ? 2 |? ? 36.0000 |
|? ? ? 5 |? ? 46.0000 |
+---------+-------------+
(4)?顯示以L開(kāi)頭的名字的同學(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 |
+-------+-------------+-----+--------+---------+-----------+
3 rows in set (0.00 sec)
2、數(shù)據(jù)庫(kù)授權(quán)magedu用戶,允許192.168.1.0/24網(wǎng)段可以連接mysql
MariaDB [(none)]> grant all on mysql.* to magedu@'192.168.1.%' identified by 'centos';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> select host,User,Password from mysql.user where user='magedu';
+-------------+--------+-------------------------------------------+
| host? ? ? ? | User? | Password? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |
+-------------+--------+-------------------------------------------+
| 192.168.1.% | magedu | *128977E278358FF80A246B5046F51043A2B1FCED |
+-------------+--------+-------------------------------------------+
1 row in set (0.00 sec)
3、總結(jié)mysql常見(jiàn)的存儲(chǔ)引擎以及特點(diǎn)。
1、InnoDB引擎:
行級(jí)鎖
支持事務(wù),適合處理大量短期事務(wù)
讀寫阻塞與事務(wù)隔離級(jí)別相關(guān)
可緩存數(shù)據(jù)和索引
支持聚簇索引
崩潰恢復(fù)性更好
支持MVCC高并發(fā)
從MySQL5.5后支持全文索引
從MySQL5.5.5開(kāi)始為默認(rèn)的數(shù)據(jù)庫(kù)引擎
2、 MyISAM :
不支持事務(wù)
表級(jí)鎖定
讀寫相互阻塞,寫入不能讀,讀時(shí)不能寫
只緩存索引
不支持外鍵約束
不支持聚簇索引
讀取數(shù)據(jù)較快,占用資源較少
不支持MVCC(多版本并發(fā)控制機(jī)制)高并發(fā)
崩潰恢復(fù)性較差
MySQL5.5.5前默認(rèn)的數(shù)據(jù)庫(kù)引擎
3、BLACKHOLE?
黑洞存儲(chǔ)引擎接受但不存儲(chǔ)數(shù)據(jù),檢索總是返回一個(gè)空集。該功 能可用于分布式數(shù)據(jù)庫(kù)設(shè)計(jì),數(shù)據(jù)自動(dòng)復(fù)制,但不是本地存儲(chǔ);
4、Performance_Schema
Performance_Schema數(shù)據(jù)庫(kù)使用;
5、CSV
CSV存儲(chǔ)引擎使用逗號(hào)分隔值格式將數(shù)據(jù)存儲(chǔ)在文本文件中??梢允褂?CSV引擎以CSV格式導(dǎo)入和導(dǎo)出其他軟件和應(yīng)用程序之間的數(shù)據(jù)交換 ;
6、ARCHIVEArchive?
為存儲(chǔ)和檢索大量很少參考的存檔或安全審核信息,只支持 SELECT和INSERT操作;支持行級(jí)鎖和專用緩存區(qū);
7、MEMORY?
將所有數(shù)據(jù)存儲(chǔ)在RAM中,以便在需要快速查找參考和其他類似 數(shù)據(jù)的環(huán)境中進(jìn)行快速訪問(wèn)。適用存放臨時(shí)數(shù)據(jù)。引擎以前被稱為HEAP引擎;
8、Federated聯(lián)合
用于訪問(wèn)其它遠(yuǎn)程MySQL服務(wù)器一個(gè)代理,它通過(guò)創(chuàng)建一 個(gè)到遠(yuǎn)程MySQL服務(wù)器的客戶端連接,并將查詢傳輸?shù)竭h(yuǎn)程服務(wù)器執(zhí)行,而 后完成數(shù)據(jù)存取,提供鏈接單獨(dú)MySQL服務(wù)器的能力,以便從多個(gè)物理服務(wù) 器創(chuàng)建一個(gè)邏輯數(shù)據(jù)庫(kù)。非常適合分布式或數(shù)據(jù)集市環(huán)境 ;