马哥Linux第十四周
2020-05-22 本文已影响0人
Liang_JC
1、 导入hellodb.sql生成数据库
(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为分组依据,显示每组的平均年龄
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、数据库授权magedu用户,允许192.168.1.0/24网段可以连接mysql
#授权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、总结mysql常见的存储引擎以及特点。
-
**MyISAM引擎特点 **
不支持事务
表级锁定
读写相互阻塞,写入不能读,读时不能写
只缓存索引
不支持外键约束
不支持聚簇索引
读取数据较快,占用资源较少
不支持MVCC(多版本并发控制机制)高并发
崩溃恢复性较差
MySQL5.5.5前默认的数据库引擎 -
InnoDB引擎特点
行级锁
支持事务,适合处理大量短期事务
读写阻塞与事务隔离级别相关
可缓存数据和索引
支持聚簇索引
崩溃恢复性更好
支持MVCC高并发
从MySQL5.5后支持全文索引
从MySQL5.5.5开始为默认的数据库引擎