第十一周作业

1、 导入hellodb.sql生成数据库
(1) 在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄
(2) 以ClassID为分组依据,显示每组的平均年龄
(3) 显示第2题中平均年龄大于30的分组及平均年龄
(4) 显示以L开头的名字的同学的信息
2、数据库授权magedu用户,允许192.168.1.0/24网段可以连接mysql
3、总结mysql常见的存储引擎以及特点。

1.导入hellodb.sql生成数据库
首先倒入hellodb.sql 两种方式 脚本式和交互式

  • 脚本式
root@17  ~]# mysql < hellodb.sql
  • 交互式
 (root@localhost) [(none)]> source /root/hellodb.sql
  • 查询倒入数据库结果
(root@localhost) [hellodb]> show create database hellodb\G;
*************************** 1. row ***************************
       Database: hellodb
Create Database: CREATE DATABASE `hellodb` /*!40100 DEFAULT CHARACTER SET utf8 */ /*!80016 DEFAULT ENCRYPTION='N' */
1 row in set (0.00 sec)

ERROR:
No query specified

在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄

(root@localhost) [hellodb]> desc students;
+-----------+---------------------+------+-----+---------+----------------+
| Field     | Type                | Null | Key | Default | Extra          |
+-----------+---------------------+------+-----+---------+----------------+
| StuID     | int(10) unsigned    | NO   | PRI | NULL    | auto_increment |
| Name      | varchar(50)         | NO   |     | NULL    |                |
| Age       | tinyint(3) unsigned | NO   |     | NULL    |                |
| Gender    | enum('F','M')       | NO   |     | NULL    |                |
| ClassID   | tinyint(3) unsigned | YES  |     | NULL    |                |
| TeacherID | int(10) unsigned    | YES  |     | NULL    |                |
+-----------+---------------------+------+-----+---------+----------------+
6 rows in set (0.01 sec)

(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)

以ClassID为分组依据,显示每组的平均年龄

思路:
1.classid作为group by后面的字段
2.age作为聚合函数avg()的参数
(root@localhost) [hellodb]> select classid,avg(age) from students group by classid;
+---------+----------+
| classid | avg(age) |
+---------+----------+
|       2 |  36.0000 |
|       1 |  20.5000 |
|       4 |  24.7500 |
|       3 |  20.2500 |
|       5 |  46.0000 |
|       7 |  19.6667 |
|       6 |  20.7500 |
|    NULL |  63.5000 |
+---------+----------+
8 rows in set (0.00 sec)

显示第2题中平均年龄大于30的分组及平均年龄

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

显示以L开头的名字的同学的信息

#考察where like子句用法
(root@localhost) [hellodb]> select name from students where name like 'L%';
+-------------+
| name        |
+-------------+
| Lin Daiyu   |
| Lu Wushuang |
| Lin Chong   |
+-------------+
3 rows in set (0.00 sec)

# where rlike也可以 但是不走索引,不推荐
(root@localhost) [hellodb]> select name from students where name rlike '^L';
+-------------+
| name        |
+-------------+
| Lin Daiyu   |
| Lu Wushuang |
| Lin Chong   |
+-------------+
3 rows in set (0.01 sec)
  1. 数据库授权magedu用户,允许192.168.1.0/24网段可以连接mysql
  • mysql8.0以前 授权连同创建用户一起做了
grant Usage on *.* to magedu@'192.168.0.%' identified by '123';
  • mysql8.0 需要先创建用户 后授权
create user magedu@'192.168.1.%' identified by '123';
grant Usage on *.* to magedu@'192.168.1.%';
  1. 总结mysql常见的存储引擎以及特点

InnoDB在存储层面
数据存储于表空间
表空间分为共享表空间于独立表空间


第十一周作业_第1张图片
InnoDB存储层面

第十一周作业_第2张图片
InnoDB其它层面

InnoDB的宏观文件与磁盘宏观结构


第十一周作业_第3张图片
InnoDB表的宏观结构.jpg

MyISAM


第十一周作业_第4张图片
MyISAM与其它存储引擎特点

存储引擎远远不止这些,还有很多要写。例如事务特性ACID 事务隔离级别等,作业不做太多叙述

你可能感兴趣的:(第十一周作业)