1、 導(dǎo)入hellodb.sql生成數(shù)據(jù)庫
[root@centos7 ~]# mysql -p < hellodb.sql
(1) 在students表中,查詢年齡大于25歲,且為男性的同學(xué)的名字和年齡
MariaDB [hellodb]> select Name,Age,Gender from students where age > 25 and Gender = 'm';
+--------------+-----+--------+
| Name | Age | Gender |
+--------------+-----+--------+
| Xie Yanke | 53 | M |
| Ding Dian | 32 | M |
| Yu Yutong | 26 | M |
| Shi Qing | 46 | M |
| Tian Boguang | 33 | M |
| Xu Xian | 27 | M |
| Sun Dasheng | 100 | M |
+--------------+-----+--------+
7 rows in set (0.00 sec)
(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 |
+---------+----------+
8 rows in set (0.00 sec)
(3) 顯示第2題中平均年齡大于30的分組及平均年齡
MariaDB [hellodb]> select classid,avg(age) as age from students group by classid having age > 30;
+---------+---------+
| classid | age |
+---------+---------+
| NULL | 63.5000 |
| 2 | 36.0000 |
| 5 | 46.0000 |
+---------+---------+
3 rows in set (0.00 sec)
(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 |
+-------+-------------+-----+--------+---------+-----------+
3 rows in set (0.00 sec)
2、數(shù)據(jù)庫授權(quán)magedu用戶,允許192.168.1.0/24網(wǎng)段可以連接mysql
MariaDB [hellodb]> create user magedu@'192.168.0.%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)
MariaDB [hellodb]> select user,host,password from mysql.user where user='magedu';
+--------+-------------+-------------------------------------------+
| user | host | password |
+--------+-------------+-------------------------------------------+
| magedu | 192.168.0.% | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+--------+-------------+-------------------------------------------+
1 row in set (0.00 sec)
MariaDB [mysql]> grant all on *.* to magedu@'192.168.0.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
MariaDB [mysql]> show grants for magedu@'192.168.0.%';
+-----------------------------------------------------------------------------------------------------------------+
| Grants for magedu@192.168.0.% |
+-----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'magedu'@'192.168.0.%' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
+-----------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
#已有賬戶修改權(quán)限
MariaDB [mysql]> update user set host='192.168.0.%' where user='magedu';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [mysql]> select user,host,password from user where user='magedu';
+--------+-------------+-------------------------------------------+
| user | host | password |
+--------+-------------+-------------------------------------------+
| magedu | 192.168.0.% | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+--------+-------------+-------------------------------------------+
1 row in set (0.00 sec)
3、總結(jié)mysql常見的存儲引擎以及特點。
特點
(1)不支持事務(wù)
(2)表級鎖定
(3)讀寫相互阻塞,寫入不能讀,讀時不能寫
(4)只緩存索引
(5)不支持外鍵約束
(6)不支持聚簇索引
(7)讀取數(shù)據(jù)較快,占用資源較少
(8)不支持MVCC(多版本并發(fā)控制機(jī)制)高并發(fā)
(9)崩潰恢復(fù)性較差
(10)MySQL5.5.5前默認(rèn)的數(shù)據(jù)庫引擎
適用場景
只讀(或者寫較少)、表較?。梢越邮荛L時間進(jìn)行修復(fù)操作)
引擎文件
tbl_name.frm 表格式定義
tbl_name.MYD 數(shù)據(jù)文件
tbl_name.MYI 索引文件
InnoDB引擎
特點
行級鎖
支持事務(wù),適合處理大量短期事務(wù)
讀寫阻塞與事務(wù)隔離級別相關(guān)
可緩存數(shù)據(jù)和索引
支持聚簇索引
崩潰恢復(fù)性更好
支持MVCC高并發(fā)
從MySQL5.5后支持全文索引
從MySQL5.5.5開始為默認(rèn)的數(shù)據(jù)庫引擎
數(shù)據(jù)庫文件
所有InnoDB表的數(shù)據(jù)和索引放置于同一個表空間中
表空間文件:datadir定義的目錄下
數(shù)據(jù)文件:ibddata1, ibddata2, …
每個表單獨使用一個表空間存儲表的數(shù)據(jù)和索引
啟用:innodb_file_per_table=ON
參看:https://mariadb.com/kb/en/library/xtradbinnodb-server-
system-variables/#innodb_file_per_table
ON (>= MariaDB 5.5)
兩類文件放在數(shù)據(jù)庫獨立目錄中
數(shù)據(jù)文件(存儲數(shù)據(jù)和索引):tb_name.ibd
表格式定義:tb_name.frm
其它存儲引擎
(1)Performance_Schema:Performance_Schema數(shù)據(jù)庫使用
(2)Memory :將所有數(shù)據(jù)存儲在RAM中,以便在需要快速查找參考和其他類似數(shù)據(jù)的環(huán)境中進(jìn)行快速訪問。適用存放臨時數(shù)據(jù)。引擎以前被稱為HEAP引擎
(3)MRG_MyISAM:使MySQL DBA或開發(fā)人員能夠?qū)σ幌盗邢嗤腗yISAM表進(jìn)行邏輯分組,并將它們作為一個對象引用。適用于VLDB(Very Large DataBase)環(huán)境,如數(shù)據(jù)倉庫
(4)Archive :為存儲和檢索大量很少參考的存檔或安全審核信息,只支持SELECT和INSERT操作;支持行級鎖和專用緩存區(qū)
(5)Federated聯(lián)合:用于訪問其它遠(yuǎn)程MySQL服務(wù)器一個代理,它通過創(chuàng)建一個到遠(yuǎn)程MySQL服務(wù)器的客戶端連接,并將查詢傳輸?shù)竭h(yuǎn)程服務(wù)器執(zhí)行,而后完成數(shù)據(jù)存取,提供鏈接單獨MySQL服務(wù)器的能力,以便從多個物理服務(wù)rchive :為存儲和檢索大量很少參考的存檔或安全審核信息,只支持SELECT和INSERT操作;支持行級鎖和專用緩存區(qū)
(6)Federated聯(lián)合:用于訪問其它遠(yuǎn)程MySQL服務(wù)器一個代理,它通過創(chuàng)建一個到遠(yuǎn)程MySQL服務(wù)器的客戶端連接,并將查詢傳輸?shù)竭h(yuǎn)程服務(wù)器執(zhí)行,而后完成數(shù)據(jù)存取,提供鏈接單獨MySQL服務(wù)器的能力,以便從多個物理服務(wù)器創(chuàng)建一個邏輯數(shù)據(jù)庫。非常適合分布式或數(shù)據(jù)集市環(huán)境。