理论+实操: MySQL索引与事务、视图、存储过程(软件开发用的多)、存储引擎MyISAM和InnoDB

文章目录


前言:

  • 索引介绍
  • 事务介绍
  • 存储引擎介绍

一:索引的概念

  • 数据库中的索引与书籍中的目录类似
    • 在一本书中,无需阅读整本书,利用目录就可以快速查找所需信息
    • 书中的目录是一个词语列表,其中表明了包含各个词的页码
  • 数据库索引
    • 在数据库中,索引使数据库程序无须对整个表进行扫描,就可以在其中找到所需数据
    • 数据库中的索引是某个表中一列或者若干列值得集合,以及物理标识这些值得数据页的逻辑指针清单

index,索引

索引:可以快速的定位到数据

索引优点:可以快速找到数据

缺点:占用硬盘资源

使用索引看数据是否过多,300条以上才有必要使用索引

二:索引的作用

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

三:索引的分类

3.1 普通索引

  • 这是最基本的索引类型,而且它没有唯一性之类的限制

3.2 唯一性索引

  • 这种索引和前面的“普通索引”基本相同,但有一个区别:索引列的所有值都只能出现一次,即必须唯一
  • 唯一性索引允许为空,但是只能有一次为空

3.3 主键

  • 主键是一种唯一性索引,但它必须指定为“PRIMARY KEY”

备注:字段设置为PRIMARY KEY

3.4 全文索引

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

3.5 单列索引与多列索引

  • 索引可以是单列上创建的索引,也可以是在多列上创建的索引

四:创建索引的原则依据

  • 表的主键、外键必须有索引
  • 数据量超过300行的表应该有索引
  • 经常与其他表进行连接的表,在连接字段(外键)上应该建立索引

外键:一张表,主表,另外一个表是从表

一张表格的外键一定是另外一张表格的主键

候选键:

扫描二维码关注公众号,回复: 8536027 查看本文章

除了主键和外键之外的键就是外键

在同一张表内,主键不可以有多个,外键可以有多个

  • 经常出现在Where子句中的字段,特别是大表的字段,应该建立索引
  • 索引应该建在选择性高的字段上
  • 索引应该建在小字段上,对于大的文本字段甚至超长字段,不要建索引
  • 针对大的文本字段甚至超长字段,建立全文索引,fulltext
  • 唯一性太差的字段不适合建立索引
  • 更新太频繁的字段不适合创建索引

五:创建索引的方法

  • 根据企业需求选择了合适的索引之后,可使用CREATE INDEX创建索引
  • CREATE INDEX加上各个索引关键字便可创建各个类型的索引

实操:

decimal(相当于浮点型double,可以指定有效数字,5指小数点前可以有五个,小数点后面可以有2个)

在这里插入图片描述

Myisam(适合被访问):创建一张表,会产生文件:

表的结构文件

表的数据文件

表的索引文件

innodb:支持事务,适合做读写

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| bbs                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> create database school;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| bbs                |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

mysql> 
mysql> use school;
Database changed
mysql> create table info ( id int(4) not null primary key(主键) auto_increment(自动排列分发),name varchar(10) not null, address varchar(50) default 'nanjing', age int(3) not null);
Query OK, 0 rows affected (0.01 sec)
//auto_increment 代表增量
mysql> describe info;
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int(4)      | NO   | PRI | NULL    | auto_increment |
| name    | varchar(10) | NO   |     | NULL    |                |
| address | varchar(50) | YES  |     | nanjing |                |
| age     | int(3)      | NO   |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> 
mysql> insert into info (name,address,age) values ('zhangsan','beijing',20),('lisi','shanghai',22);
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> 
mysql> select * from info;
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
+----+----------+----------+-----+
2 rows in set (0.00 sec)

mysql> 

5.1 创建普通索引:CREATE INDEX <索引名字> ON tablename (列的列表);

举例:

CREATE INDEX salary_index ON IT_salary(薪资);

索引是表的一部分

表名_字段

字段_索引

mysql> create index index_age on info (age);
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0
//普通索引创建方式
mysql> show index from info;
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name  | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info  |          0 | PRIMARY   |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| info  |          1 | index_age |            1 | age         | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

mysql> drop index index_age on info;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from info;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info  |          0 | PRIMARY  |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)

mysql> 

5.2 创建唯一性索引:CREATE UNIQUE INDEX <索引的名字> ON tablename(列的列表);

举例:

CREATE UNIQUE INDEX salary_unique_index ON IT_salary(姓名);

mysql> create unique index unique_name on info (name);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from info;
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name    | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info  |          0 | PRIMARY     |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| info  |          0 | unique_name |            1 | name        | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

mysql> drop index unique_name on info;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from info;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info  |          0 | PRIMARY  |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)

mysql> 

5.3 创建主键索引:

CREATE TABLE tablename ([…],PRIMARY KEY (列的列表));

ALTER TABLE tablename ADD PRIMARY KEY(列的列表);

举例:

ALTER TABLE IT_salary ADD PRIMARY KEY(员工ID);

alter table 更改表结构

创建主键索引,可以针对一个列建立多种索引

create 是针对新表时创建

alter是针对原有存在的表

mysql> alter table info add unique index index_name (name);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from info;
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info  |          0 | PRIMARY    |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| info  |          0 | index_name |            1 | name        | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

mysql> 

三种方式定义索引

1、创建表时直接定义索引

2、create index 索引名称 on 表名 (列名1,列名2);

3、alter table 表名 add index 索引名称 (列名);

mysql> create table user (id int(4) not null primary key auto_increment,name varchar(10) not null,score decimal not null,hobby int(2) not null default '1',index index_score (score));
Query OK, 0 rows affected (0.02 sec)

mysql> show index from user;
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name    | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| user  |          0 | PRIMARY     |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| user  |          1 | index_score |            1 | score       | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

mysql> 
mysql> insert into user (name,score,hobby) values ('test01',88,1),('stu01',99,2),('wangwu',77,3);
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from user;
+----+--------+-------+-------+
| id | name   | score | hobby |
+----+--------+-------+-------+
|  1 | test01 |    88 |     1 |
|  2 | stu01  |    99 |     2 |
|  3 | wangwu |    77 |     3 |
+----+--------+-------+-------+
3 rows in set (0.00 sec)

mysql> 

5.4 全文索引,通常是对字符串进行索引,不是对数字进行索引

mysql> select * from info;
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
+----+----------+----------+-----+
2 rows in set (0.00 sec)


mysql> show index from info;
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info  |          0 | PRIMARY    |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| info  |          0 | index_name |            1 | name        | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

mysql> create fulltext index full_addr on info (address);
Query OK, 0 rows affected, 1 warning (0.28 sec)
Records: 0  Duplicates: 0  Warnings: 1

mysql> show index from info;
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info  |          0 | PRIMARY    |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| info  |          0 | index_name |            1 | name        | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| info  |          1 | full_addr  |            1 | address     | NULL      |           2 |     NULL | NULL   | YES  | FULLTEXT   |         |               |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)

mysql> 

5.5 组合索引

mysql> select * from user;
+----+---------+-------+-------+
| id | name    | score | hobby |
+----+---------+-------+-------+
|  1 | test01  |    88 |     3 |
|  2 | stu01   |    99 |     2 |
|  3 | wangwu  |    77 |     3 |
|  4 | zhaoliu |    66 |     2 |
+----+---------+-------+-------+
4 rows in set (0.01 sec)

mysql> create index index_name_score on user (name,score);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from user;
+-------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name         | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| user  |          0 | PRIMARY          |            1 | id          | A         |           3 |     NULL | NULL   |      | BTREE      |         |               |
| user  |          1 | index_score      |            1 | score       | A         |           3 |     NULL | NULL   |      | BTREE      |         |               |
| user  |          1 | index_name_score |            1 | name        | A         |           4 |     NULL | NULL   |      | BTREE      |         |               |
| user  |          1 | index_name_score |            2 | score       | A         |           4 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
4 rows in set (0.00 sec)

mysql> 

六:查看索引:

SHOW INDEX FROM tablename;

SHOW INDEX FROM tablename;

举例:

SHOW INDEX FROM IT_salary;

SHOW INDEX FROM IT_salary;

两种都可以

七:事务的概念

  • 事务是一种机制、一个操作序列,包含了一组数据库操作命令,并且把所有的命令作为一个整体一起向系统提交或撤销操作请求,即这一组数据库命令要么都执行,要么都不执行;当执行不成功时,会回滚,回到事务的起点

  • 事务是一个不可分割的工作巡逻单元,在数据库系统上执行并发操作时,事务时最小的控制单元

  • 适用于多用户同时操作的数据库系统的场景,如银行、保险公司及证券公司交易系统等等

  • 通过事务的整体性以保证数据的一致性

八:事务的ACID特点

8.1原子性(Atomicity)

  • 事务是一个完整的操作,事务的各元素是不可分的(原子的)
  • 事务中的所有元素必须作为一个整体提交或回滚
  • 如果事务中的任何元素失败,则整个事务将失败
  • 原子性就是回滚机制

8.2 一致性(Consistency)

  • 当事务完成时,数据必须处于一致状态:在事务开始之前,数据库中存储的数据处于一致状态;在正在进行的事务时,数据可能处于不一致的状态;当事务成功完成时,数据必须再次回到已知的一致状态

8.3 隔离性(Isolation)

  • 对数据进行修改的所有并发事务是彼此隔离的,这表明事务必须是独立的,它不应以任何方式依赖于或影响其他事务
  • 修改数据的事务可以在另一个使用相同数据的事务开始之前访问这些数据,或者在另一个使用相同数据的事务结束之后访问这些数据

8.4 持久性(Durability)

  • 事务持久性不管系统是否发生故障,事务处理的结果都是永久的
  • 一旦事务被提交,事务的效果被永久地保留在数据库中
  • 不可逆性,除非使用回滚操作

九:事务的操作

  • 默认情况下MySQL的事务是自动提交的,当sql语句提交时事务便自动提交
  • 手动对事务进行控制的方法
    • 事务处理命令控制
    • 使用set设置事务处理方式

手动修改是在内存当中,然后提交会把操作覆盖到硬盘

9.1 事务处理命令控制事务

  • begin:开始一个事务
  • commit:提交一个事务
  • rollback:回滚一个事务

9.2 使用set命令进行控制

  • set autocommit=0:禁止自动提交————相当于begin
  • set autocommit=1:开启自动提交

9.3 举例:

9.3.1

在这里插入图片描述

9.3.2

在这里插入图片描述

9.3.3

在这里插入图片描述

9.3.4

在这里插入图片描述

9.4 实操:

mysql> select * from info;
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
+----+----------+----------+-----+
2 rows in set (0.01 sec)

mysql> insert into info (name,address,age) values ('wangwu','hangzhou',30);
Query OK, 1 row affected (0.00 sec)

mysql> select * from info;
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
|  3 | wangwu   | hangzhou |  30 |
+----+----------+----------+-----+
3 rows in set (0.00 sec)

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

mysql> insert into info (name,address,age) values ('zhaoliu','hangzhou',31);
Query OK, 1 row affected (0.00 sec)

//存档 savepoint 存档名;
mysql> savepoint a;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from info;
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
|  3 | wangwu   | hangzhou |  30 |
|  4 | zhaoliu  | hangzhou |  31 |
+----+----------+----------+-----+
4 rows in set (0.00 sec)

mysql> insert into info (name,address,age) values ('tianqi','hangzhou',33);
Query OK, 1 row affected (0.01 sec)

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

mysql> insert into info (name,address,age) values ('heiba','hangzhou',35);
Query OK, 1 row affected (0.00 sec)

mysql> select * from info;
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
|  3 | wangwu   | hangzhou |  30 |
|  4 | zhaoliu  | hangzhou |  31 |
|  5 | tianqi   | hangzhou |  33 |
|  6 | heiba    | hangzhou |  35 |
+----+----------+----------+-----+
6 rows in set (0.00 sec)

mysql> 

此时使用另外一个终端去登陆

[root@localhost ~]# mysql -uroot -p
Enter password: 
mysql> use school;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| a                |
| hob              |
| info             |
| user             |
| view_user        |
+------------------+
5 rows in set (0.01 sec)

mysql> select * from info;
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
|  3 | wangwu   | hangzhou |  30 |
+----+----------+----------+-----+
3 rows in set (0.00 sec)

mysql> 

可以发现在begin之后的操作并没有写入到表内,写入的数据在内存里临时存放,只要没有commit提交,就不会写入到硬盘内

回到原本的终端,继续测试

mysql> rollback to b;
//回滚到指定存档点:rollback to 存档点
Query OK, 0 rows affected (0.01 sec)

mysql> select * from info;
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
|  3 | wangwu   | hangzhou |  30 |
|  4 | zhaoliu  | hangzhou |  31 |
|  5 | tianqi   | hangzhou |  33 |
+----+----------+----------+-----+
5 rows in set (0.00 sec)

mysql> rollback to a;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from info;
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
|  3 | wangwu   | hangzhou |  30 |
|  4 | zhaoliu  | hangzhou |  31 |
+----+----------+----------+-----+
4 rows in set (0.00 sec)

mysql> rollback;
//直接回滚到begin时
Query OK, 0 rows affected (0.00 sec)

mysql> select * from info;
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
|  3 | wangwu   | hangzhou |  30 |
+----+----------+----------+-----+
3 rows in set (0.00 sec)

mysql> 

尝试再次回滚到节点b,发现此时的b不存在

mysql> rollback to b;
ERROR 1305 (42000): SAVEPOINT b does not exist
mysql> 

此时验证一个事务是否完成

mysql> insert into info (name,address,age) values ('zhaoliu','hangzhou',31);
Query OK, 1 row affected (0.01 sec)
mysql> select * from info;
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
|  3 | wangwu   | hangzhou |  30 |
|  7 | zhaoliu  | hangzhou |  31 |
+----+----------+----------+-----+
4 rows in set (0.00 sec)
mysql> rollback;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from info;
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
|  3 | wangwu   | hangzhou |  30 |
|  7 | zhaoliu  | hangzhou |  31 |
+----+----------+----------+-----+
4 rows in set (0.00 sec)
//发现并没有回滚成功,到另外的一个终端查看

发现zhaoliu数据已经被写入

mysql> select * from info;
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
|  3 | wangwu   | hangzhou |  30 |
|  7 | zhaoliu  | hangzhou |  31 |
+----+----------+----------+-----+
4 rows in set (0.00 sec)

结论:rollback回滚到begin时,此时一个事务已经结束,要想执行下一个事务,还需要重新敲begin;即rollback或commit执行任意一个,都代表当前的事务的结束,使用rollback to 节点,还没有结束

结论:存档点的数据写在数据操作之后,如果回滚,就会回到数据操作之后

如果回滚rollback后面不加断点,会直接回到begin

如果回到begin上之后再回到当时设置的断点时,因为内存被释放掉,事务完成,所以无法回到
一旦commit提交,再用rollback也回不去了,
set autocommit =0 相当于begin,
set autocommit=1 相当于开启自动提交,可以把这个事务之前的未提交的事务一并提交掉,而且接下来对于输入的所有命令都会直接修改硬盘的数据

十:视图概述

视图:是一张虚拟的表,数据不存在视图中,它只不过是一个真实表的映射数据,便于用户友好的形式查看数据

利用,条件筛选,分组,排序等产生出一个结果集。(结果集是保存在内存上)并且做成持久化保存。

视图类似一个软连接,真实数据改变,视图的数据也会改变

视图不占过大的空间

————水中捞月

create view 视图名称 as 查询语句(select id,name,age) from 表名 where(条件) id=1 or id=5;

实操:

mysql> create table hob (id int(2) not null primary key,hob_name varchar(10) not null);
Query OK, 0 rows affected (0.02 sec)

mysql> desc hob
    -> ;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(2)      | NO   | PRI | NULL    |       |
| hob_name | varchar(10) | NO   |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

mysql> insert into hob (id,hob_name) values (1,'看书'),(2,'运动'),(3,'游戏');
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from hob;
+----+----------+
| id | hob_name |
+----+----------+
|  1 | 看书     |
|  2 | 运动     |
|  3 | 游戏     |
+----+----------+
3 rows in set (0.00 sec)

mysql> 

将两张表关联起来,join,内联,条件用on

  • select user.name,hob.hob_name from user inner join hob on user.hobby=hob.id;
mysql> insert into user (name,score,hobby) values ('zhaoliu',66,2)
    -> ;
Query OK, 1 row affected (0.01 sec)

mysql> select * from user;
+----+---------+-------+-------+
| id | name    | score | hobby |
+----+---------+-------+-------+
|  1 | test01  |    88 |     1 |
|  2 | stu01   |    99 |     2 |
|  3 | wangwu  |    77 |     3 |
|  4 | zhaoliu |    66 |     2 |
+----+---------+-------+-------+
4 rows in set (0.01 sec)

mysql> select * from user inner join hob on user.hobby=hob.id;
+----+---------+-------+-------+----+----------+
| id | name    | score | hobby | id | hob_name |
+----+---------+-------+-------+----+----------+
|  1 | test01  |    88 |     1 |  1 | 看书     |
|  2 | stu01   |    99 |     2 |  2 | 运动     |
|  3 | wangwu  |    77 |     3 |  3 | 游戏     |
|  4 | zhaoliu |    66 |     2 |  2 | 运动     |
+----+---------+-------+-------+----+----------+
4 rows in set (0.00 sec)

mysql> select user.name,hob.hob_name from user inner join hob on user.hobby=hob.id;
+---------+----------+
| name    | hob_name |
+---------+----------+
| test01  | 看书     |
| stu01   | 运动     |
| wangwu  | 游戏     |
| zhaoliu | 运动     |
+---------+----------+
4 rows in set (0.00 sec)

mysql> select u.name,h.hob_name from user u inner join hob h on u.hobby=h.id;
+---------+----------+
| name    | hob_name |
+---------+----------+
| test01  | 看书     |
| stu01   | 运动     |
| wangwu  | 游戏     |
| zhaoliu | 运动     |
+---------+----------+
4 rows in set (0.00 sec)

mysql> 

创建视图,

视图作用:

1.可以节省命令步骤,方便语句操作

2.根据角色的权限查看不同的数据

3.确保安全性

mysql> create view view_user as select u.name,h.hob_name from user u inner join hob h on u.hobby
Query OK, 0 rows affected (0.02 sec)

mysql> select * from view_user;
+---------+----------+
| name    | hob_name |
+---------+----------+
| test01  | 看书     |
| stu01   | 运动     |
| wangwu  | 游戏     |
| zhaoliu | 运动     |
+---------+----------+
4 rows in set (0.01 sec)

mysql> update user set hobby=3 where name='test01';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from view_user;
+---------+----------+
| name    | hob_name |
+---------+----------+
| test01  | 游戏     |
| stu01   | 运动     |
| wangwu  | 游戏     |
| zhaoliu | 运动     |
+---------+----------+
4 rows in set (0.00 sec)

mysql> 

十一:存储过程概述

存储过程:存储过程多用于软件开发方向

防止代码在网络传输过程中被截获,做了安全性保障

原始状态:代码中需要嵌入sql语句:通过连接驱动(java就是jdbc)————把sql语句作为参数,传递给mysql数据库进行执行。

存储过程是写在数据库中,并不是程序中。

程序是通过调用存储过程名称去触发操作,(类似shell中的函数),

优点:代码量优化,减少代码量,

​ 传输安全,隐藏sql语句

​ 网络优化

十二:存储引擎概念介绍

  • MySQL中的数据用各种不同的技术存储在文件中,每一种技术都使用不同的存储机制、索引技巧、锁定水平并最终提供不同的功能和能力,这些不同的技术以及配套的功能在MySQL中称为存储引擎
  • 存储引擎就是MySQL将数据存储在文件系统中的存储方式或者存储格式————因此适用的场景也不一样
  • 目前MySQL常用的两种存储引擎
    • MyISAM
    • InnoDB

存储引擎:一种基础的驱动力,存储技术:存储机制、索引技巧、锁定水平

  • mysql存储引擎是mysql数据库服务器中的组建,负责为数据库执行实际的数据I/O操作
  • 使用特殊存储的主要优点之一在于:仅需要提供特殊应用所需的特性,数据库中的系统开销较小,具有更有效和更高的数据库性能
  • mysql系统中,存储引擎处于文件系统之上,在数据保存到数据文件之前会传输到存储引擎,之后按照各个存储引擎的存储格式进行储存

在这里插入图片描述

前后端的开发语言去提取拿取数据,这就需要装驱动,,具体的对象叫connector连接者

connection pool 叫连接池,类似所谓的空闲的线程或者进程去进行连接,在里面回房多个被连接对象,以备连接,并且节省响应时间,优化访问效率

使用线程或者进程去连接,链接需要媒介载体,就是sock通信文件,以提供pid文件,所有的线程都需要sock文件去管理,

数据放在磁盘上,在逻辑层面上放在file system 中,管理数据需要management services

sql interface 支持功能,ddl,dml,view

parser 查询功能query,object privilege 权限功能

optimizer :access paths

buffers :缓存区、global 全局

files logs 日志文件:日志文件是数据库的核心命脉,用来做主从复制,备份需要单独备份

12.1 MyISAM的简单介绍

  • myisam存储引擎是Mysql关系数据库系统5.5版本之前默认的存储引擎,前身是ISAM
  • ISAM是一个定义明确且历经时间考验的数据表格管理方法,在设计之时就考虑到数据库被查询的次数要远大于更新的次数

12.2 ISAM的特点

  • ISAM执行读取操作的速度很快

  • 而且不占用大量的内存和存储资源

  • 但是不支持事务处理

  • 而且不能够容错

  • ISAM更看重读,读>写

12.3 MyISAM的详细介绍

  • MyISAM管理非事务表,是ISAM的扩展格式
  • 提供索引和字段管理等大量功能
  • MyISAM使用一种表格锁定的机制,以优化多个并发的读写操作————在写入数据时,无法读,在读的时候无法写
  • MyISAM提供高速存储和检索,以及全文搜索能力,受到web开发的青睐

12.4 MyISAM的特点

  • 不支持事务

  • 表级锁定形式,数据在更新时锁定整个表

  • 数据库在读写过程中相互阻塞

    • 会在数据写入的过程阻塞用户数据的读取
    • 也会在数据读取的过程中阻塞用户的数据写入
  • 可通过key_buffer_size来设置缓存索引,提高访问性能,减少磁盘IO的压力

    • 但是缓存只会缓存索引文件,不会缓存数据
  • 采用MsISAM存储引擎数据单独写入或读取,速度过程较快且占用资源相对少

  • MyISAM存储引擎不支持外键约束,只支持全文索引

  • 每个MyISAM在磁盘上存储成三个文件,每一个文件的名字以表的名字开始,扩展名指出文件类型

  • MyISAM在磁盘上存储的文件:

    • .frm文件,用来存储表的定义结构
    • 数据文件的扩展名为.MYD(MYData)
    • 索引文件的扩展名是.MYI(MYIndex)

12.5 MyISAM使用的生产场景举例

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

十三: InnoDB特点介绍

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

13.1 InnoDB使用生产场景分析

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

十四:企业选择存储引擎依据

  • 需要考虑每个存储引擎提供了哪些不同的核心功能及应用场景

  • 支持的字段和数据类型

    • 所有引擎都支持通用的数据类型
    • 但不是所有的引擎都支持其它的字段类型,如二进制对象
  • 锁定类型:不同的存储引擎支持不同级别的锁定

    • 表锁定:MyISAM、InnoDB
    • 行锁定:InnoDB4
  • 索引的支持

    • 建立索引在搜索和回复数据库中的数据的时候能够显著提高性能
    • 不同的存储引擎提供不同的制作索引的技术
    • 有些存储引擎根本不支持索引
  • 事务处理的支持

    • 事务处理功能通过提供在向表中更新和插入信息期间的可靠性
    • 可根据企业业务是否要支持事务选择存储引擎

十五:配置存储引擎

  • 在企业中选择好合适的存储引擎之后,就可以进行修改

15.1修改步骤

  • 查看数据库可配置的存储引擎
  • 查看表正在使用的存储引擎
  • 配置存储引擎为所选择的类型

15.2使用show engines查看系统支持的存储引擎

mysql> show engines
    -> ;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)


  • 查看表使用的存储引擎
    • 方法1:show table status from 库名 where name=‘表名’;
    • 方法2:show create table 表名;

备注:切换存储引擎需要切换表结构

mysql> show create table info;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                                                                                           |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| info  | CREATE TABLE "info" (
  "id" int(4) NOT NULL AUTO_INCREMENT,
  "name" varchar(10) NOT NULL,
  "address" varchar(50) DEFAULT 'nanjing',
  "age" int(3) NOT NULL,
  PRIMARY KEY ("id"),
  UNIQUE KEY "index_name" ("name"),
  FULLTEXT KEY "full_addr" ("address")
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> show create table info\G
*************************** 1. row ***************************
       Table: info
Create Table: CREATE TABLE "info" (
  "id" int(4) NOT NULL AUTO_INCREMENT,
  "name" varchar(10) NOT NULL,
  "address" varchar(50) DEFAULT 'nanjing',
  "age" int(3) NOT NULL,
  PRIMARY KEY ("id"),
  UNIQUE KEY "index_name" ("name"),
  FULLTEXT KEY "full_addr" ("address")
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)


15.3 示例

在这里插入图片描述

\G 代替分号表示垂直显示结果

15.4 修改存储引擎

在这里插入图片描述修改配置文件,用于以后创建表格是直接设置

convert 转化,密码是管理员密码

举例:

在这里插入图片描述

[root@localhost ~]# vim /etc/my.cnf
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
‘default-storage-engine=Myisam							'增加'

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES

重启mysql服务

[root@localhost ~]# systemctl restart mysqld.service

可以发现,此时myisam成为default了

mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB             | YES     | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| MyISAM             | DEFAULT | MyISAM storage engine                                          | NO           | NO   | NO         |
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)

mysql> 
mysql> use school;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> create table a (id int);
Query OK, 0 rows affected (0.01 sec)

mysql> show create table a;
+-------+-------------------------------------------------------------------------------------+
| Table | Create Table                                                                        |
+-------+-------------------------------------------------------------------------------------+
| a     | CREATE TABLE "a" (
  "id" int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
+-------+-------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> 

修改表的存储引擎

mysql> alter table a engine=innodb;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table a\G
*************************** 1. row ***************************
       Table: a
Create Table: CREATE TABLE "a" (
  "id" int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> 

总结:索引的概念及适用场景

​ 索引的配置方法

​ 事务的特点及原则

​ 配置事务的方法

​ 主流的存储引擎技术

​ 查看和修改存储引擎的方法

发布了87 篇原创文章 · 获赞 26 · 访问量 4522

猜你喜欢

转载自blog.csdn.net/Lfwthotpt/article/details/103858277