▲就业班和全程班的小伙伴看这里:(学习老王视频的作业第27-28节)
1、 导入hellodb.sql生成数据库
[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岁,且为男性的同学的名字和年龄
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为分组依据,显示每组的平均年龄
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开头的名字的同学的信息
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、数据库授权magedu用户,允许192.168.1.0/24网段可以连接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、总结mysql常见的存储引擎以及特点。
1、InnoDB引擎:
行级锁
支持事务,适合处理大量短期事务
读写阻塞与事务隔离级别相关
可缓存数据和索引
支持聚簇索引
崩溃恢复性更好
支持MVCC高并发
从MySQL5.5后支持全文索引
从MySQL5.5.5开始为默认的数据库引擎
2、 MyISAM :
不支持事务
表级锁定
读写相互阻塞,写入不能读,读时不能写
只缓存索引
不支持外键约束
不支持聚簇索引
读取数据较快,占用资源较少
不支持MVCC(多版本并发控制机制)高并发
崩溃恢复性较差
MySQL5.5.5前默认的数据库引擎
3、BLACKHOLE
黑洞存储引擎接受但不存储数据,检索总是返回一个空集。该功 能可用于分布式数据库设计,数据自动复制,但不是本地存储;
4、Performance_Schema
Performance_Schema数据库使用;
5、CSV
CSV存储引擎使用逗号分隔值格式将数据存储在文本文件中。可以使用 CSV引擎以CSV格式导入和导出其他软件和应用程序之间的数据交换 ;
6、ARCHIVEArchive
为存储和检索大量很少参考的存档或安全审核信息,只支持 SELECT和INSERT操作;支持行级锁和专用缓存区;
7、MEMORY
将所有数据存储在RAM中,以便在需要快速查找参考和其他类似 数据的环境中进行快速访问。适用存放临时数据。引擎以前被称为HEAP引擎;
8、Federated联合
用于访问其它远程MySQL服务器一个代理,它通过创建一 个到远程MySQL服务器的客户端连接,并将查询传输到远程服务器执行,而 后完成数据存取,提供链接单独MySQL服务器的能力,以便从多个物理服务 器创建一个逻辑数据库。非常适合分布式或数据集市环境 ;