MySQL索引、事务与存储引擎

一、索引的概念

1.1 数据库索引

  • 是一个排序的列表,存储着索引值和这个值所对应的物理地址
  • 无需对整个表进行扫描,通过物理地址就可以找到所需数据
  • 是表中一列或者若干列值排序的方法
  • 需要额外的磁盘空间

1.2 索引的作用

  • 数据库利用各种快速定位技术,能够大大加快查询速率
  • 当表很大或查询涉及到多个表时,可以成千上万倍的提高查询速度
  • 可以降低数据库的IO成本,并且还可以数据库的排序成本
  • 通过创建唯一性索引保证数据表数据的唯一性
  • 可以加快表与表之间的连接
  • 在使用分组和排序时,可大大减少分组和排序时间

1.3 索引的分类

普通索引、唯一索引、主键索引、组合索引、全文索引
索引创建的方法:
1)、直接创建索引
2)、修改表结构方式添加索引
3)、创建表结构时创建索引

1.3.1 普通索引

1)直接创建

mysql> create index id on student(id);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> show index in school.student;
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table   | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| student |          0 | PRIMARY  |            1 | studynum    | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| student |          1 | id       |            1 | id          | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

2)创建表时创建

mysql> create table teacher(id int(2) not null primary key, name char(16),adress int(10),index id_index(id));
Query OK, 0 rows affected (0.01 sec)
mysql> show index in school.teacher;
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table   | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| teacher |          0 | PRIMARY  |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| teacher |          1 | id_index |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

3)修改表结构方式创建

mysql> alter table student add index name_index(name);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
 mysql> show index in school.student;
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table   | Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| student |          0 | PRIMARY    |            1 | studynum    | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| student |          1 | id         |            1 | id          | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
| student |          1 | name_index |            1 | name        | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)

1.3.2 唯一索引

  • 与“普通索引”基本相同
  • 与普通索引的区别是索引列的所有值只能出现一次,即必须唯一
  • 创建唯一索引的方式

1)直接创建

mysql> create unique index adress on teacher(adress);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> show index in school.teacher;
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table   | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| teacher |          0 | PRIMARY  |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| teacher |          0 | adress   |            1 | adress      | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
| teacher |          1 | id_index |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

2)创建表时创建

mysql> create table lisi(studynum char(10),name char(16) not null primary key,adress ,adress int(10),unique idexName(studynum));
Query OK, 0 rows affected (0.01 sec)
mysql> show index in school.lisi;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| lisi  |          0 | PRIMARY  |            1 | name        | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| lisi  |          0 | idexName |            1 | studynum    | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

3)修改表结构创建

mysql> alter table lisi add unique index adress_index(adress);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> show index in school.lisi;
+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name     | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| lisi  |          0 | PRIMARY      |            1 | name        | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| lisi  |          0 | idexName     |            1 | studynum    | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
| lisi  |          0 | adress_index |            1 | adress      | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

1.3.3 主键索引

  • 主键索引是一种特殊的唯一索引,指定为“PRIMARY KEY”
  • 一个表只能有一个主键,不允许有空值

1)创建表时创建

 mysql> create table zhangsan(studynum int(2) not null primary key,name char(48),adress 8),adress int(10));
Query OK, 0 rows affected (0.01 sec)

mysql> create table wangwu(studynum int(2) not null, name char(48),adress int(10),primary key(studynum));
Query OK, 0 rows affected (0.01 sec)

 mysql> show index in school.wangwu;
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table  | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| wangwu |          0 | PRIMARY  |            1 | studynum    | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

2)修改表时创建

mysql> create table xiaoming(studynum int(2) not null,name char(48),adress int(10));
Query OK, 0 rows affected (0.01 sec)   //创建一个新的表
mysql> alter table xiaoming add primary key(studynum);  //修改表结构创建主键索引
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> show index in school.xiaoming;
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table    | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| xiaoming |          0 | PRIMARY  |            1 | studynum    | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+


1.3.4 组合索引

  • 可以是单列上创建的索引,也可以是在多列上创建的索引
  • 最左原则,从左往右依次执行
mysql> create table xiaohong(id int(2) not null, name char(48),adress int(10),index xiaohong(id,name,adress));
Query OK, 0 rows affected (0.01 sec)

mysql> show index in school.xiaohong;
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table    | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| xiaohong |          1 | xiaohong |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| xiaohong |          1 | xiaohong |            2 | name        | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
| xiaohong |          1 | xiaohong |            3 | adress      | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)

1.3.5 全文索引

  • MySQL从3.23.23版开始支持全文索引和全文检索
  • 索引类型为FULLTEXT
  • 可以在CHAR、VARCHAR或者TEXT类型的列上创建

1)创建表时创建全文索引

mysql> create table xiaobai(id int(2) not null,name char(48) primary key,adress int(10),fulltext (name));
Query OK, 0 rows affected (0.03 sec)
mysql> show index in school.xiaobai;
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table   | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| xiaobai |          0 | PRIMARY  |            1 | name        | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| xiaobai |          1 | name     |            1 | name        | NULL      |           0 |     NULL | NULL   |      | FULLTEXT   |         |               |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

2)在已存在的表上创建全文索引

mysql> create fulltext index name_index on xiaobai(name);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> show index in school.xiaobai;
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table   | Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| xiaobai |          0 | PRIMARY    |            1 | name        | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| xiaobai |          1 | name       |            1 | name        | NULL      |           0 |     NULL | NULL   |      | FULLTEXT   |         |               |
| xiaobai |          1 | name_index |            1 | name        | NULL      |           0 |     NULL | NULL   |      | FULLTEXT   |         |               |
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

3)通过SQL语句alter table创建全文索引

mysql> alter table student add fulltext index studynum_index(studynum);
Query OK, 0 rows affected, 1 warning (0.36 sec)
Records: 0  Duplicates: 0  Warnings: 1

mysql> show index in school.student;
+---------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table   | Non_unique | Key_name       | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| student |          0 | PRIMARY        |            1 | studynum    | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| student |          1 | id             |            1 | id          | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
| student |          1 | name_index     |            1 | name        | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
| student |          1 | studynum_index |            1 | studynum    | NULL      |           0 |     NULL | NULL   |      | FULLTEXT   |         |               |
+---------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

1.4 创建索引的原则依据

  • 表的主键,外键必须有索引
  • 记录数超过300行的表应该有索引
  • 经常与其他表进行连接的表,在连接字段上应该建立索引
  • 唯一性太差的字段不合适建立索引
  • 更新太频繁的字段不适合创建索引
  • 经常出现在where子句中的字段,特别是大表的字段,应该建立索引
  • 索引应该建在选择性高的字段上
  • 索引应该建在小字段上,对于大的文本字段甚至超长字段,不要建索引

1.5 查看索引语法

SHOW INDEX FROM table_name;
SHOW KEYS FROM table_name;

查看索引的实例

show index in school.student;
+---------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table   | Non_unique | Key_name       | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| student |          0 | PRIMARY        |            1 | studynum    | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| student |          1 | id             |            1 | id          | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
| student |          1 | name_index     |            1 | name        | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
| student |          1 | studynum_index |            1 | studynum    | NULL      |           0 |     NULL | NULL   |      | FULLTEXT   |         |               |
+---------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

1.6 删除索引语法

DROP INDEX index_name ON table_name;
ALTER TABLE table_name DROP INDEX index_name;

删除索引的示例:

mysql> drop index studynum_index on student;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> show index in school.student;
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table   | Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| student |          0 | PRIMARY    |            1 | studynum    | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| student |          1 | id         |            1 | id          | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
| student |          1 | name_index |            1 | name        | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

mysql> alter table student drop index id;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0
show index in school.student;
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table   | Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| student |          0 | PRIMARY    |            1 | studynum    | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| student |          1 | name_index |            1 | name        | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

二、事务

2.1 事务的概念

  • 是一种机制、一个操作序列,包含了一组数据库操作命令,并且把所有的命令作为一个整体一起向系统提交或撤销操作请求,即这一组数据库命令要么都执行,要么都不执行
  • 是一个不可分割的工作逻辑单元,在数据库系统上执行并发操作时,事务是最小的控制单元
  • 适用于多用户同时操作的数据库系统的场景,如银行、保险公司以及证券交易系统等等
  • 通过事务的整体性保证数据的一致性

2.2 事务的ACID特点

  • 原子性(Atomicity)
    • 事务是一个完整的操作,事务的各元素是不可分的
    • 事务中的所有元素必须作为一个整体提交或回滚
    • 如果事务中的任何元素失败,则整个事务将失败
  • 一致性(Consistency)
    • 当事务完成时,数据必须处于一致状态
    • 在事务开始前,数据库中存储的数据处于一致状态
    • 在正在进行的事务中,数据可能处于不一致的状态
    • 当事务成功完成时,数据必须再次回到已知的一致状态
  • 隔离性(LsoLation)
    • 对数据进行修改的所有并发事务是彼此隔离的,表明事务必须是独立的,它不应以任何方式依赖于或影响其他事务
    • 修改数据的事务可在另一个相同数据的事务开始之前访问这些数据,或者在另一个使用相同数据的事务结束之后访问这些数据
  • 持久性(Durablilty)
    • 指不管系统是否发生故障,事务处理的结果都是永久的
    • 一旦事务被提交,事务的效果会被永久的保留在数据库中

2.3 事务控制语句

  • MySQL事务默认是自动提交的,当SQL语句提交时事务便自动提交
  • 事务控制语句
    • BEGIN或START TRANSACTION //开始一个事务
    • COMMIT // 提交
    • ROLLBACK // 回滚
    • SAVEPOINT identifier // 创建保存点
    • RELEASE SAVEPOINT identifier //删除保存点
    • ROLLBACK TO identifier // 回滚到指定保存点
    • SET TRANSACTION //设置事务的隔离级别

2.4 事务的控制方法

  • 手动对事务进行控制的方法
    • 事务处理命令控制事务
      • bebin:开始一个事务
      • commit:提交一个事务
      • rollback:回滚一个事务
    • 使用set命令进行控制
      • set autocommit=0:禁止自动提交
      • set autocommit=1:开启自动提交
mysql> begin;   //开始一个事务
Query OK, 0 rows affected (0.00 sec)
mysql> insert into student value(1,'lisi',10);
Query OK, 1 row affected (0.00 sec)
mysql> insert into student value(2,'xiaobai',20);
Query OK, 1 row affected (0.00 sec)
mysql> savepoint a;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into student value(3,'xiaoming',30);
Query OK, 1 row affected (0.00 sec)

mysql> savepoint b;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from student;
+------+----------+----------+
| id   | name     | studynum |
+------+----------+----------+
|    1 | lisi     | 10       |
|    2 | xiaobai  | 20       |
|    3 | xiaoming | 30       |
+------+----------+----------+
3 rows in set (0.00 sec)

mysql> rollback to a;  //回滚一个事务
Query OK, 0 rows affected (0.00 sec)

mysql> select * from student;
+------+---------+----------+
| id   | name    | studynum |
+------+---------+----------+
|    1 | lisi    | 10       |
|    2 | xiaobai | 20       |
+------+---------+----------+
2 rows in set (0.00 sec)

mysql> rollback to b;  //回滚一个事务b,但是只能向前回滚,无法向后回滚,所以会出现报错
ERROR 1305 (42000): SAVEPOINT b does not exist
mysql> commit;				//结束一个事务
Query OK, 0 rows affected (0.01 sec)

三、存储引擎

3.1 存储引擎的概念介绍

  • MySQL中的数据用各种不同的技术存储在文件中,每一种技术都使用不同的存储机制、索引技巧、锁定水平并最终提供不同的功能和能力,这些不同的技术以及配套的功能在MySQL中称为存储引擎
  • 存储引擎是MySQL将数据存储在文件系统中的存储方式或者存储格式
  • MySQL常用的存储引擎
    • MyISAM
    • InnoDB
  • MySQL数据库中的组件,负责执行实际的数据I/O操作
  • MySQL系统中,存储引擎处于文件系统之上,在数据保存到数据文件之前会传输到存储引擎,之后按照各个存储引擎的存储格式进行存储

3.2 MyISAM的介绍

  • MyISAM不支持事务,也不支持外键
  • 访问速度快
  • 对书屋完整性没有要求
  • MyISAM在磁盘上存储成三个文件
    • .frm文件存储表定义
    • 数据文件的扩展名为.MYD(MYData)
    • 索引文件的扩展名为.MYI(MYIndex)
  • 表级锁定形式,数据在更新时锁定整个表
  • 数据库在读写过程中相互阻塞
    • 会在数据写入的过程阻塞用户数据的读取
    • 也会在数据读取的过程中阻塞用户数据的写入
  • 数据单独写入或读取,速度过程较快且占用资源相对少

3.3 - MyISAM支持的存储格式

  • 静态表
  • 动态表
  • 压缩表

3.4 MyISAM适用的生产场景举例

  • 公司业务不需要事务的支持
  • 单方面读取或写入数据比较多的业务
  • MyISAM存储引擎数据读写都比较频繁场景不适合
  • 使用读写并发访问相对较低的业务
  • 数据修改相对较少的业务
  • 对数据业务一致性要求不是非常高的业务
  • 服务器硬件资源相对比较差

3.5 InonDB特点介绍

  • 支持4个事务隔离级别
  • 行级锁定,但是全表扫描仍然会是表级锁定
  • 读写阻塞与事务隔离级别相关
  • 能非常搞笑的缓存索引和数据
  • 表与主键以簇的方式存储
  • 支持分区、表空间、类似Oracle数据库
  • 支持外键的约束,5.5前不支持全文索引,5.5后支持全文索引
  • 对硬件资源要求还是比较高的场合

3.6 InnoDB使用生产场景分析

  • 业务需要事务的支持
  • 行级锁定对高并发有很好的适应能力,但需确保查询是通过索引来完成
  • 业务数据更新较为频繁的场景
    如:论坛、微博等
  • 业务数据一致性要求较高
    如:银行业务
  • 硬件设备内存较大(因为事务都先放内存),利用innodb较好的缓存能力来提高内存利用率,减少磁盘IO的压力

3.7 企业选择存储引擎依据

  • 需要考虑每个存储引擎提供的核心功能及应用场景
  • 支持的字段和数据类型
    所有引擎都支持通用的数据类型
    但不是所有的引擎都支持其它的字段类型,如二进制对象
  • 锁定类型:不同的存储引擎支持不同级别的锁定
    表锁定
    行锁定

3.8 修改存储引擎

3.8.1 alter table 修改

未修改之前:

mysql> mysql> show table status from school;
+----------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+
| Name     | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time         | Update_time         | Check_time | Collation       | Checksum | Create_options | Comment |
+----------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+
| lisi     | InnoDB |      10 | Dynamic    |    0 |              0 |       16384 |               0 |        16384 |         0 |           NULL | 2020-12-24 14:58:24 | NULL                | NULL       | utf8_general_ci |     NULL |                |         |
| student  | InnoDB |      10 | Dynamic    |    3 |           5461 |       16384 |               0 |        32768 |         0 |           NULL | 2020-12-24 15:28:53 | 2020-12-24 15:42:43 | NULL       | utf8_general_ci |     NULL |                |         |
| teacher  | InnoDB |      10 | Dynamic    |    0 |              0 |       16384 |               0 |        16384 |         0 |           NULL | 2020-12-24 14:48:41 | NULL                | NULL       | utf8_general_ci |     NULL |                |         |
| wangwu   | InnoDB |      10 | Dynamic    |    0 |              0 |       16384 |               0 |            0 |         0 |           NULL | 2020-12-24 15:03:05 | NULL                | NULL       | utf8_general_ci |     NULL |                |         |
| xiaobai  | InnoDB |      10 | Dynamic    |    0 |              0 |       16384 |               0 |        32768 |         0 |           NULL | 2020-12-24 15:19:51 | NULL                | NULL       | utf8_general_ci |     NULL |                |         |
| xiaohong | InnoDB |      10 | Dynamic    |    0 |              0 |       16384 |               0 |        16384 |         0 |           NULL | 2020-12-24 15:12:05 | NULL                | NULL       | utf8_general_ci |     NULL |                |         |
| xiaoming | InnoDB |      10 | Dynamic    |    0 |              0 |       16384 |               0 |            0 |         0 |           NULL | 2020-12-24 15:08:15 | NULL                | NULL       | utf8_general_ci |     NULL |                |         |
| zhangsan | InnoDB |      10 | Dynamic    |    0 |              0 |       16384 |               0 |            0 |         0 |           NULL | 2020-12-24 15:01:55 | NULL                | NULL       | utf8_general_ci |     NULL |                |         |
+----------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+

修改zhangshan存储引擎为MyISAM

mysql> alter table zhangsan engine=myisam;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

修改之后:

mysql> show table status from school;
+----------+--------+---------+------------+------+----------------+-------------+-------------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+
| Name     | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length   | Index_length | Data_free | Auto_increment | Create_time         | Update_time         | Check_time | Collation       | Checksum | Create_options | Comment |
+----------+--------+---------+------------+------+----------------+-------------+-------------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+
| lisi     | InnoDB |      10 | Dynamic    |    0 |              0 |       16384 |                 0 |        16384 |         0 |           NULL | 2020-12-24 14:58:24 | NULL                | NULL       | utf8_general_ci |     NULL |                |         |
| student  | InnoDB |      10 | Dynamic    |    3 |           5461 |       16384 |                 0 |        32768 |         0 |           NULL | 2020-12-24 15:28:53 | 2020-12-24 15:42:43 | NULL       | utf8_general_ci |     NULL |                |         |
| teacher  | InnoDB |      10 | Dynamic    |    0 |              0 |       16384 |                 0 |        16384 |         0 |           NULL | 2020-12-24 14:48:41 | NULL                | NULL       | utf8_general_ci |     NULL |                |         |
| wangwu   | InnoDB |      10 | Dynamic    |    0 |              0 |       16384 |                 0 |            0 |         0 |           NULL | 2020-12-24 15:03:05 | NULL                | NULL       | utf8_general_ci |     NULL |                |         |
| xiaobai  | InnoDB |      10 | Dynamic    |    0 |              0 |       16384 |                 0 |        32768 |         0 |           NULL | 2020-12-24 15:19:51 | NULL                | NULL       | utf8_general_ci |     NULL |                |         |
| xiaohong | InnoDB |      10 | Dynamic    |    0 |              0 |       16384 |                 0 |        16384 |         0 |           NULL | 2020-12-24 15:12:05 | NULL                | NULL       | utf8_general_ci |     NULL |                |         |
| xiaoming | InnoDB |      10 | Dynamic    |    0 |              0 |       16384 |                 0 |            0 |         0 |           NULL | 2020-12-24 15:08:15 | NULL                | NULL       | utf8_general_ci |     NULL |                |         |
| zhangsan | MyISAM |      10 | Fixed      |    0 |              0 |           0 | 43065671436730367 |         1024 |         0 |           NULL | 2020-12-24 15:52:32 | 2020-12-24 15:52:32 | NULL       | utf8_general_ci |     NULL |                |         |
+----------+--------+---------+------------+------+----------------+-------------+-------------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+
8 rows in set (0.00 sec)

3.8.2 修改my.cnf,指定默认存储引擎并重启服务

在/etc/my.cfg配置文件棕修改[mysqld],下面添加default-storage-engine=InnoDB,然后重启服务

3.8.3 create table创建表时指定存储引擎

mysql> create table xusi(id int(2) not null primary key,name char(48),adress int(10)) engine=myisam;
Query OK, 0 rows affected (0.00 sec)
mysql> show table status from school;
+----------+--------+---------+------------+------+----------------+-------------+-------------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+
| Name     | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length   | Index_length | Data_free | Auto_increment | Create_time         | Update_time         | Check_time | Collation       | Checksum | Create_options | Comment |
+----------+--------+---------+------------+------+----------------+-------------+-------------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+
| lisi     | InnoDB |      10 | Dynamic    |    0 |              0 |       16384 |                 0 |        16384 |         0 |           NULL | 2020-12-24 14:58:24 | NULL                | NULL       | utf8_general_ci |     NULL |                |         |
| student  | InnoDB |      10 | Dynamic    |    3 |           5461 |       16384 |                 0 |        32768 |         0 |           NULL | 2020-12-24 15:28:53 | 2020-12-24 15:42:43 | NULL       | utf8_general_ci |     NULL |                |         |
| teacher  | InnoDB |      10 | Dynamic    |    0 |              0 |       16384 |                 0 |        16384 |         0 |           NULL | 2020-12-24 14:48:41 | NULL                | NULL       | utf8_general_ci |     NULL |                |         |
| wangwu   | InnoDB |      10 | Dynamic    |    0 |              0 |       16384 |                 0 |            0 |         0 |           NULL | 2020-12-24 15:03:05 | NULL                | NULL       | utf8_general_ci |     NULL |                |         |
| xiaobai  | InnoDB |      10 | Dynamic    |    0 |              0 |       16384 |                 0 |        32768 |         0 |           NULL | 2020-12-24 15:19:51 | NULL                | NULL       | utf8_general_ci |     NULL |                |         |
| xiaohong | InnoDB |      10 | Dynamic    |    0 |              0 |       16384 |                 0 |        16384 |         0 |           NULL | 2020-12-24 15:12:05 | NULL                | NULL       | utf8_general_ci |     NULL |                |         |
| xiaoming | InnoDB |      10 | Dynamic    |    0 |              0 |       16384 |                 0 |            0 |         0 |           NULL | 2020-12-24 15:08:15 | NULL                | NULL       | utf8_general_ci |     NULL |                |         |
| xusi     | MyISAM |      10 | Fixed      |    0 |              0 |           0 | 43065671436730367 |         1024 |         0 |           NULL | 2020-12-24 15:55:45 | 2020-12-24 15:55:45 | NULL       | utf8_general_ci |     NULL |                |         |
| zhangsan | MyISAM |      10 | Fixed      |    0 |              0 |           0 | 43065671436730367 |         1024 |         0 |           NULL | 2020-12-24 15:52:32 | 2020-12-24 15:52:32 | NULL       | utf8_general_ci |     NULL |                |         |
+----------+--------+---------+------------+------+----------------+-------------+-------------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+
9 rows in set (0.00 sec)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值