馬哥Linux第十四周

1、 導入hellodb.sql生成數(shù)據(jù)庫

(1) 在students表中,查詢年齡大于25歲,且為男性的同學的名字和年齡

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 stuid,name,avg(age),gender,classid,teacherid from students group by classid;
+-------+--------------+----------+--------+---------+-----------+
| stuid | name         | avg(age) | gender | classid | teacherid |
+-------+--------------+----------+--------+---------+-----------+
|    24 | Xu Xian      |  63.5000 | M      |    NULL |      NULL |
|     2 | Shi Potian   |  20.5000 | M      |       1 |         7 |
|     1 | Shi Zhongyu  |  36.0000 | M      |       2 |         3 |
|     5 | Yu Yutong    |  20.2500 | M      |       3 |         1 |
|     4 | Ding Dian    |  24.7500 | M      |       4 |         4 |
|     6 | Shi Qing     |  46.0000 | M      |       5 |      NULL |
|     9 | Ren Yingying |  20.7500 | F      |       6 |      NULL |
|     8 | Lin Daiyu    |  19.6667 | F      |       7 |      NULL |
+-------+--------------+----------+--------+---------+-----------+
8 rows in set (0.00 sec)

(3) 顯示第2題中平均年齡大于30的分組及平均年齡

MariaDB [hellodb]> select avg(age),classid from students group by classid having avg(age) > 30;
+----------+---------+
| avg(age) | classid |
+----------+---------+
|  63.5000 |    NULL |
|  36.0000 |       2 |
|  46.0000 |       5 |
+----------+---------+
3 rows in set (0.00 sec)

(4) 顯示以L開頭的名字的同學的信息

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

#授權(quán)magedu
MariaDB [(none)]> grant all on *.* to magedu@'192.168.1.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

#驗證
[root@ka2 ~]# mysql -umagedu -p123456 -h192.168.1.177
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.02 sec)

MariaDB [(none)]> select host,user,password from mysql.user;
+--------------+--------+-------------------------------------------+
| host         | user   | password                                  |
+--------------+--------+-------------------------------------------+
| localhost    | root   |                                           |
| centos7.6    | root   |                                           |
| 127.0.0.1    | root   |                                           |
| ::1          | root   |                                           |
| 192.168.1.%  | magedu | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+--------------+--------+-------------------------------------------+

3、總結(jié)mysql常見的存儲引擎以及特點。

  • **MyISAM引擎特點 **

    不支持事務(wù)
    表級鎖定
    讀寫相互阻塞,寫入不能讀,讀時不能寫
    只緩存索引
    不支持外鍵約束
    不支持聚簇索引
    讀取數(shù)據(jù)較快,占用資源較少
    不支持MVCC(多版本并發(fā)控制機制)高并發(fā)
    崩潰恢復性較差
    MySQL5.5.5前默認的數(shù)據(jù)庫引擎

  • InnoDB引擎特點
    行級鎖
    支持事務(wù),適合處理大量短期事務(wù)
    讀寫阻塞與事務(wù)隔離級別相關(guān)
    可緩存數(shù)據(jù)和索引
    支持聚簇索引
    崩潰恢復性更好
    支持MVCC高并發(fā)
    從MySQL5.5后支持全文索引
    從MySQL5.5.5開始為默認的數(shù)據(jù)庫引擎

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

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

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