1、 导入hellodb.sql生成数据库
导入数据库:
[root@centos7 ~]# mysql -uroot < hellodb.sql
[root@centos7 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 10.2.25-MariaDB-log Source distribution
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 |
+--------------------+
| hellodb |
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> use hellodb;
Database changed
MariaDB [hellodb]> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
7 rows in set (0.01 sec)
(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 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) from students group by ClassID having avg(age)>30;
+---------+----------+
| ClassID | avg(age) |
+---------+----------+
| NULL | 63.5000 |
| 2 | 36.0000 |
| 5 | 46.0000 |
+---------+----------+
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
MariaDB [hellodb]> grant all on mysql.* to magedu@"192.168.1.%" idendified by "qwe123";
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show grants for magedu@"192.168.1.%";
+-----------------------------------------------------------------------------------------------------------------+
| Grants for magedu@192.168.1.% |
+-----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'magedu'@'192.168.1.%' IDENTIFIED BY PASSWORD '*8DCDD69CE7D121DE8013062AEAEB2A148910D50E' |
| GRANT ALL PRIVILEGES ON `mysql`.* TO 'magedu'@'192.168.1.%' |
+-----------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
3、总结mysql常见的存储引擎以及特点。
MySQL的存储引擎有多种,登录数据库中,通过show engines命令可以查看数据库存储引擎
MariaDB [(none)]> show engines;
+--------------------+---------+----------------------------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------------------------+--------------+------+------------+
| CSV | YES | Stores tables as CSV files | NO | NO | NO |
| MRG_MyISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MyISAM | YES | Non-transactional engine with good performance and small data footprint | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| ARCHIVE | YES | gzip-compresses tables for a low storage footprint | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, foreign keys and encryption for tables | YES | YES | YES |
| Aria | YES | Crash-safe tables with MyISAM heritage | NO | NO | NO |
| SEQUENCE | YES | Generated tables filled with sequential values | YES | NO | YES |
+--------------------+---------+----------------------------------------------------------------------------------+--------------+------+------------+
10 rows in set (0.00 sec)
比较常用的有InnoDB、MyISAM
1、InnoDB引擎
MySQL5.5及后续版本中默认的存储引擎,主要有以下特点:
- 使用行级锁;
- 支持事务,适合处理大量短期事务;
- 读写阻塞与事务隔离级别相关;
- 可缓存数据和索引;
- 支持聚簇索引;
- 崩溃恢复性更好;
- 支持热备份;
- 支持MVCC高并发;
- MySQL5.5之后支持全文索引;
- 所有InnoDB表的数据和索引放置于同一个表空间,每个表单独使用一个表空间存储表的数据和索引
2、MyISAM引擎:
MyISAM是MySQL5.5之前默认的数据库引擎,特点如下:
- 不支持事务;
- 表级锁定;
- 读写相互阻塞,写时不能读,读时不能写;
- 只缓存索引;
- 只支持固定大小的行。Varchar类型的字段会存储为固定长度的char类型,浪费空间;
- 不支持外键约束;
- 不支持聚簇索引;
- 读取数据较快,占用资源较少;
- 不支持MVCC高并发;
- 崩溃恢复性较差;