目录
一,mysql索引介绍
1,索引的概念
2,索引创建的基本原则
3,索引的创建和分类
1,普通索引
2,唯一索引
3,主键索引
4,组合索引(单列索引与多列索引)
5、全文索引(FULLTEXT)
4,查看索引
5,删除索引
二,mysql事务介绍
1,事务的概念
2,事务的ACID特点
3,mysql事务的隔离级别
4,事务控制语句
5,使用set设置控制事务
一,mysql索引介绍
1,索引的概念
数据库的索引
索引是一个排序的列表,在这个列表中存储着索引的值和包含这个值的数据所在行的物理地址
使用索引后可以不用扫描全表来定位某行的数据,而是先通过索引表找到该行数据对应的物理地址然后访问相应的数据,因此能加快数据库的查询度。
索引是表中一列或者若干列值排序的方法。
建立索引的目的是加快对表中记录的查找或排序
索引需要额外的磁盘空间
索引的作用
数据库利用各种快速定位技术,能够大大加快查询速率。
当表很大或查询涉及到多个表时,可以成千上万倍的提高查询速度。
可以降低数据库的IO成本,并且还可以降低数据库的排序成本。
通过创建唯一性索引保证数据表数据的唯一性。
可以加快表与表之间的连接。
在使用分组和排序时,可以大大减少分组和排序的时间。
索引的副作用
索引需要占用额外的磁盘空间。
对于 MyISAM 引擎而言,索引文件和数据文件是分离的,索引文件用于保存数据记录的地址。
而 InnoDB 引擎的表数据文件本身就是索引文件。
在插入和修改数据时要花费更多的时间,因为索引也要随之变动。
2,索引创建的基本原则
索引虽可以提升数据库查询的速度,但并不是任何情况下都适合创建索引。因为索引本身会消耗系统资源,在有索引的情况下,数据库会先进行索引查询,然后定位到具体的数据行,如果索引使用不当,反而会增加数据库的负担。
●表的主键、外键必须有索引。因为主键具有唯一性,外键关联的是子表的主键,查询时可以快速定位
●记录数超过300行的表应该有索引。如果没有索引,需要把表遍历一遍,会严重影响数据库的性能。
●经常与其他表进行连接的表,在连接字段上应该建立索引。
●唯一性太差的字段不适合建立索引。
●更新太频繁地字段不适合创建索引。
●经常出现在 where 子句中的字段,特别是大表的字段,应该建立索引。
●索引应该建在选择性高的字段上。
●索引应该建在小字段上,对于大的文本字段甚至超长字段,不要建索引。
3,索引的创建和分类
环境创建一个数据表
[root@localhost ~]# mysql -uroot -p123123 #登录数据库
(root@localhost) [(none)]> show databases; #查看数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| class |
| hellodb |
| mysql |
| performance_schema |
| school |
| sys |
+--------------------+
7 rows in set (0.00 sec)
(root@localhost) [(none)]> creat database test; #创建库
(root@localhost) [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| class |
| hellodb |
| mysql |
| performance_schema |
| school |
| sys |
| test |
+--------------------+
8 rows in set (0.00 sec)
(root@localhost) [(none)]> use test; #进入库
Database changed
(root@localhost) [test]> create table info (id int,name char(40),cardid varchar(50),address varchar(50),remark text); #创建表
(root@localhost) [test]> desc info; #查看表
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(40) | YES | | NULL | |
| cardid | varchar(50) | YES | | NULL | |
| address | varchar(50) | YES | | NULL | |
| remark | text | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
添加数据
(root@localhost) [test]> insert into info values(1,'zhangsi',123123,'shangghai','vip');
Query OK, 1 row affected (0.00 sec) #继续添加数据
(root@localhost) [test]> select *from info; #查看表
+------+----------+--------+----------+--------+
| id | name | cardid | address | remark |
+------+----------+--------+----------+--------+
| 1 | zhangsan | 123456 | beijing | vip |
| 2 | zhangsi | 123123 | shanghai | vip |
| 3 | liwu | 123321 | shanghai | vvip |
| 4 | liliu | 654321 | hangzhou | vip |
| 5 | liqi | 123654 | shenzhen | vvip |
+------+----------+--------+----------+--------+
5 rows in set (0.00 sec)
1,普通索引
最基本的索引类型,没有唯一性之类的限制。
●直接创建索引
CREATE INDEX 索引名 ON 表名 (列名[(length)]);
#(列名(length)):length是可选项。如果忽略 length 的值,则使用整个列的值作为索引。
如果指定使用列前的 length 个字符来创建索引,这样有利于减小索引文件的大小。
#索引名建议以“_index”结尾。
例:直接创建索引
(root@localhost) [test]> create index name_index on info(name); #添加普通索引
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
(root@localhost) [test]> show create table info; 查看表
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| info | CREATE TABLE `info` (
`id` int(11) DEFAULT NULL,
`name` char(40) DEFAULT NULL,
`cardid` varchar(50) DEFAULT NULL,
`address` varchar(50) DEFAULT NULL,
`remark` text,
KEY `name_index` (`name`) #添加成功
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
(root@localhost) [test]> explain select name from info;
+----+-------------+-------+------------+-------+---------------+------------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+------------+---------+------+------+----------+-------------+
| 1 | SIMPLE | info | NULL | index | NULL | name_index | 41 | NULL | 5 | 100.00 | Using index |
+----+-------------+-------+------------+-------+---------------+------------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec) #查看name时type是索引查找key是name_index
修改表方式创建索引
ALTER TABLE 表名 ADD INDEX 索引名 (列名);
(root@localhost) [test]> alter table info add index id_index(id); #修改表创建索引
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
(root@localhost) [test]> explain select id from info;
+----+-------------+-------+------------+-------+---------------+----------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+----------+---------+------+------+----------+-------------+
| 1 | SIMPLE | info | NULL | index | NULL | id_index | 5 | NULL | 6 | 100.00 | Using index |
+----+-------------+-------+------------+-------+---------------+----------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec) #查找ID时是索引查找索引是id_index
创建表的时候指定索引
CREATE TABLE 表名 ( 字段1 数据类型,字段2 数据类型[,...],INDEX 索引名 (列名));
(root@localhost) [test]> create table info1 (id int,name char(20)not null,
cardid varrchar(40)not null,index id_index(id)); #创建表时指定索引id_index
Query OK, 0 rows affected (0.01 sec)
(root@localhost) [test]> show create table info1;
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| info1 | CREATE TABLE `info1` (
`id` int(11) DEFAULT NULL,
`name` char(20) NOT NULL,
`cardid` varchar(40) NOT NULL,
KEY `id_index` (`id`) #创建成功
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
(root@localhost) [test]> explain select id from info1;
+----+-------------+-------+------------+-------+---------------+----------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+----------+---------+------+------+----------+-------------+
| 1 | SIMPLE | info1 | NULL | index | NULL | id_index | 5 | NULL | 1 | 100.00 | Using index |
+----+-------------+-------+------------+-------+---------------+----------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec) 查看时是索引查找id_index
2,唯一索引
与普通索引类似,但区别是唯一索引列的每个值都唯一。
唯一索引允许有空值(注意和主键不同)。如果是用组合索引创建,则列值的组合必须唯一。添加唯一键将自动创建唯一索引。
例:
直接创建唯一索引
CREATE UNIQUE INDEX 索引名 ON 表名(列名);
(root@localhost) [test]> select *from info;
+------+----------+--------+----------+--------+
| id | name | cardid | address | remark |
+------+----------+--------+----------+--------+
| 1 | zhangsan | 123456 | beijing | vip |
| 2 | zhangsi | 123123 | shanghai | vip |
| 3 | liwu | 123321 | shanghai | vvip |
| 4 | liliu | 654321 | hangzhou | vip |
| 5 | liqi | 123654 | shenzhen | vvip |
| 3 | liwu | 123321 | shanghai | vvip |
+------+----------+--------+----------+--------+
(root@localhost) [test]> create unique index name_index on info(name);
ERROR 1062 (23000): Duplicate entry 'liwu' for key 'name_index' #唯一键每个数据需不同
(root@localhost) [test]> create unique index name_index on info(name); #创建唯一键索引
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
(root@localhost) [test]> show create table info;
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| info | CREATE TABLE `info` (
`id` int(11) DEFAULT NULL,
`name` char(40) DEFAULT NULL,
`cardid` varchar(50) DEFAULT NULL,
`address` varchar(50) DEFAULT NULL,
`remark` text,
UNIQUE KEY `name_index` (`name`), #创建成功
KEY `id_index` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
修改表的方式创建
ALTER TABLE 表名 ADD UNIQUE 索引名 (列名);
(root@localhost) [test]> alter table info add unique index cardid_index(cardid);
Query OK, 0 rows affected (0.01 sec) #修改表方式创建唯一索引
Records: 0 Duplicates: 0 Warnings: 0
(root@localhost) [test]> show create table info;
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| info | CREATE TABLE `info` (
`id` int(11) DEFAULT NULL,
`name` char(40) DEFAULT NULL,
`cardid` varchar(50) DEFAULT NULL,
`address` varchar(50) DEFAULT NULL,
`remark` text,
UNIQUE KEY `name_index` (`name`),
UNIQUE KEY `cardid_index` (`cardid`), #创建成功
KEY `id_index` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
删除索引
(root@localhost) [test]> drop index name_index on info; #删除索引name_index
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
(root@localhost) [test]> drop index cardid_index on info; #删除索引cardid_index
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
(root@localhost) [test]> drop index id_index on info; 删除索引id_index
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
(root@localhost) [test]> show create table info; #查看
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| info | CREATE TABLE `info` (
`id` int(11) DEFAULT NULL,
`name` char(40) DEFAULT NULL,
`cardid` varchar(50) DEFAULT NULL,
`address` varchar(50) DEFAULT NULL,
`remark` text #删除后没有了索引
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
创建表的时候指定
CREATE TABLE 表名 (字段1 数据类型,字段2 数据类型[,...],UNIQUE 索引名 (列名));
(root@localhost) [test]> create table info2 (id int,name char(20)not null,
cardid varrchar(40)not null,unique id_index(id)); #指定唯一索引创建表
Query OK, 0 rows affected (0.00 sec)
(root@localhost) [test]> show create table info2; #查看表info2
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| info2 | CREATE TABLE `info2` (
`id` int(11) DEFAULT NULL,
`name` char(20) NOT NULL,
`cardid` varchar(40) NOT NULL,
UNIQUE KEY `id_index` (`id`) #创建成功唯一键
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
3,主键索引
是一种特殊的唯一索引,必须指定为“PRIMARY KEY”。
一个表只能有一个主键,不允许有空值。 添加主键将自动创建主键索引。
例:
创建表的时候创建
CREATE TABLE 表名 ([...],PRIMARY KEY (列名));
(root@localhost) [test]> create table info3 (id int primary key,name char(20)not
null,cardid varchar(40)not null); #创建表的时候指定主键
Query OK, 0 rows affected (0.00 sec)
(root@localhost) [test]> show create table info3;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| info3 | CREATE TABLE `info3` (
`id` int(11) NOT NULL,
`name` char(20) NOT NULL,
`cardid` varchar(40) NOT NULL,
PRIMARY KEY (`id`) #主键创建成功
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
修改表的方式创建
ALTER TABLE 表名 ADD PRIMARY KEY (列名);
(root@localhost) [test]> alter table info add primary key (id); #修改表方式
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
(root@localhost) [test]> show create table info;
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| info | CREATE TABLE `info` (
`id` int(11) NOT NULL,
`name` char(40) DEFAULT NULL,
`cardid` varchar(50) DEFAULT NULL,
`address` varchar(50) DEFAULT NULL,
`remark` text,
PRIMARY KEY (`id`) #主键创建成功
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
4,组合索引(单列索引与多列索引)
可以是单列上创建的索引,也可以是在多列上创建的索引。需要满足最左原则,因为select语句的 where条件是依次从左往右执行的,所以在使用select 语句查询时where条件使用的字段顺序必须和组合索引中的排序一致,否则索引将不会生效。
CREATE TABLE 表名 (列名1 数据类型,列名2 数据类型,列名3 数据类型,INDEX 索引名 (列名1,列名2,列名3));
组合索引创建的字段顺序是其触发索引的查询顺序
select * from 表名 where 列名1='...' AND 列名2='...' AND 列名3='...';
(root@localhost) [test]> create table info4 (id int primary key,name char(20)not
nulll,cardid varchar(40)not null,index inc_index(id,name,cardid));
Query OK, 0 rows affected (0.01 sec) #创建组合索引
(root@localhost) [test]> show create table info4;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| info4 | CREATE TABLE `info4` (
`id` int(11) NOT NULL,
`name` char(20) NOT NULL,
`cardid` varchar(40) NOT NULL,
PRIMARY KEY (`id`),
KEY `inc_index` (`id`,`name`,`cardid`) #组合索引
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
select id,name,cardid from info4; #会触发组合索引
select cardid,name,id from info4; #不会触发组合索引
5、全文索引(FULLTEXT)
适合在进行模糊查询的时候使用,可用于在一篇文章中检索文本信息。
在 MySQL5.6 版本以前FULLTEXT 索引仅可用于 MyISAM 引擎,在 5.6 版本之后 innodb 引擎也支持 FULLTEXT 索引。全文索引可以在 CHAR、VARCHAR 或者 TEXT 类型的列上创建。每个表只允许有一个全文索引。
例:
直接创建索引
CREATE FULLTEXT INDEX 索引名 ON 表名 (列名);
(root@localhost) [test]> create fulltext index name_index on info(name);
Query OK, 0 rows affected, 1 warning (0.07 sec) #直接创建全文索引
Records: 0 Duplicates: 0 Warnings: 1
(root@localhost) [test]> show create table info; #查看索引
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| info | CREATE TABLE `info` (
`id` int(11) NOT NULL,
`name` char(40) DEFAULT NULL,
`cardid` varchar(50) DEFAULT NULL,
`address` varchar(50) DEFAULT NULL,
`remark` text,
PRIMARY KEY (`id`),
FULLTEXT KEY `name_index` (`name`) #全文索引创建成功
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
修改表方式创建
ALTER TABLE 表名 ADD FULLTEXT 索引名 (列名);
(root@localhost) [test]> alter table info add fulltext cardid_index(cardid);
Query OK, 0 rows affected (0.22 sec) #修改表创建全文索引
Records: 0 Duplicates: 0 Warnings: 0
(root@localhost) [test]> show create table info; #查看索引
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| info | CREATE TABLE `info` (
`id` int(11) NOT NULL,
`name` char(40) DEFAULT NULL,
`cardid` varchar(50) DEFAULT NULL,
`address` varchar(50) DEFAULT NULL,
`remark` text,
PRIMARY KEY (`id`),
FULLTEXT KEY `name_index` (`name`),
FULLTEXT KEY `cardid_index` (`cardid`) #全文索引创建成功
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
创建表的时候指定索引
CREATE TABLE 表名 (字段1 数据类型[,...],FULLTEXT 索引名 (列名));
数据类型可以为 CHAR、VARCHAR 或者 TEXT
(root@localhost) [test]> create table info5 (id int ,name char(20)not null,
cardid varchar(40)not null,fulltext cardid_index(cardid)); #创建表的时候创建全文索引
Query OK, 0 rows affected (0.01 sec)
(root@localhost) [test]> show create table info5; #查看索引
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| info5 | CREATE TABLE `info5` (
`id` int(11) DEFAULT NULL,
`name` char(20) NOT NULL,
`cardid` varchar(40) NOT NULL,
FULLTEXT KEY `cardid_index` (`cardid`) #创建全文索引成功
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
使用全文索引查询
SELECT * FROM 表名 WHERE MATCH(列名) AGAINST('查询内容');
(root@localhost) [test]> select *from info5;
+------+----------+--------+
| id | name | cardid |
+------+----------+--------+
| 1 | zhangsan | 123 |
| 2 | zhangsi | 1234 |
| 3 | zhangwu | 12345 |
| 4 | zhangliu | 123456 |
| 5 | zhangqi | 123456 |
+------+----------+--------+
5 rows in set (0.00 sec)
(root@localhost) [test]> select *from info5 where match(cardid) against(123456);
+------+----------+--------+
| id | name | cardid |
+------+----------+--------+
| 4 | zhangliu | 123456 |
| 5 | zhangqi | 123456 |
+------+----------+--------+
2 rows in set (0.00 sec)
(root@localhost) [test]> select *from info5 where cardid=123456;
+------+----------+--------+
| id | name | cardid |
+------+----------+--------+
| 4 | zhangliu | 123456 |
| 5 | zhangqi | 123456 |
+------+----------+--------+
2 rows in set (0.00 sec)
4,查看索引
show index from 表名;
show index from 表名\G; 竖向显示表索引信息
(root@localhost) [test]> show index from info5;
+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info5 | 1 | cardid_index | 1 | cardid | NULL | 5 | NULL | NULL | | FULLTEXT | | |
+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)
show keys from 表名;
show keys from 表名\G;
(root@localhost) [test]> show keys from info5;
+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info5 | 1 | cardid_index | 1 | cardid | NULL | 5 | NULL | NULL | | FULLTEXT | | |
+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)
各字段的含义如下:
Table 表的名称
Non_unique 如果索引内容唯一,则为 0;如果可以不唯一,则为 1。
Key_name 索引的名称。
Seq_in_index 索引中的列序号,从 1 开始。 limit 2,3
Column_name 列名称。
Collation 列以什么方式存储在索引中。在 MySQL 中,有值‘A’(升序)或 NULL(无分类)。
Cardinality 索引中唯一值数目的估计值。
Sub_part 如果列只是被部分地编入索引,则为被编入索引的字符的数目(zhangsan)。如果整列被编入索引,则为 NULL。
Packed 指示关键字如何被压缩。如果没有被压缩,则为 NULL。
Null 如果列含有 NULL,则含有 YES。如果没有,则该列含有 NO。
Index_type 用过的索引方法(BTREE, FULLTEXT, HASH, RTREE)。
Comment 备注。
5,删除索引
DROP INDEX 索引名 ON 表名;
(root@localhost) [test]> show create table info;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| info | CREATE TABLE `info` (
`id` int(11) NOT NULL,
`name` char(40) DEFAULT NULL,
`cardid` varchar(50) DEFAULT NULL,
`address` varchar(50) DEFAULT NULL,
`remark` text,
PRIMARY KEY (`id`),
FULLTEXT KEY `name_index` (`name`),
FULLTEXT KEY `cardid_index` (`cardid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
(root@localhost) [test]> drop index name_index on info; #删除索引
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
(root@localhost) [test]> show create table info;
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| info | CREATE TABLE `info` (
`id` int(11) NOT NULL,
`name` char(40) DEFAULT NULL,
`cardid` varchar(50) DEFAULT NULL,
`address` varchar(50) DEFAULT NULL,
`remark` text,
PRIMARY KEY (`id`),
FULLTEXT KEY `cardid_index` (`cardid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
修改表方式删除索引
ALTER TABLE 表名 DROP INDEX 索引名;
(root@localhost) [test]> alter table info drop index cardid_index;
Query OK, 0 rows affected (0.00 sec) #修改表方式删除索引
Records: 0 Duplicates: 0 Warnings: 0
(root@localhost) [test]> show create table info;
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| info | CREATE TABLE `info` (
`id` int(11) NOT NULL,
`name` char(40) DEFAULT NULL,
`cardid` varchar(50) DEFAULT NULL,
`address` varchar(50) DEFAULT NULL,
`remark` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
删除主键索引
ALTER TABLE 表名 DROP PRIMARY KEY;
(root@localhost) [test]> alter table info drop primary key; #删除主键
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0
(root@localhost) [test]> show create table info;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| info | CREATE TABLE `info` (
`id` int(11) NOT NULL,
`name` char(40) DEFAULT NULL,
`cardid` varchar(50) DEFAULT NULL,
`address` varchar(50) DEFAULT NULL,
`remark` text
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
二,mysql事务介绍
1,事务的概念
MySQL 事务主要用于处理操作量大,复杂度高的数据。比如说,在人员管理系统中, 要删除一个人员,即需要删除人员的基本资料,
又需要删除和该人员相关的信息,如信箱, 文章等等。这样,这些数据库操作语句就构成一个事务!
●事务是一种机制、一个操作序列,包含了一组数据库操作命令,
并且把所有的命令作为一个整体一起向系统提交或撤销操作请求,即这一组数据库命令要么都执行,要么都不执行。●事务是一个不可分割的工作逻辑单元,在数据库系统上执行并发操作时,事务是最小的控制单元。
●事务适用于多用户同时操作的数据库系统的场景,如银行、保险公司及证券交易系统等等。
●事务是通过事务的整体性以保证数据的一致性。
所谓事务,它是一个操作序列,这些操作要么都执行,要么都不执行,它是一个不可分割的工作单位。
2,事务的ACID特点
ACID,是指在可靠数据库管理系统(DBMS)中,事务(transaction)应该具有的四个特性:
原子性(Atomicity)、
一致性(Consistency)、
隔离性(Isolation)、
持久性(Durability)。
原子性:指事务是一个不可再分割的工作单位,事务中的操作要么都发生,要么都不发生。
事务是一个完整的操作,事务的各元素是不可分的。
事务中的所有元素必须作为一个整体提交或回滚。
如果事务中的任何元素失败,则整个事务将失败。
案例:
A给B转帐100元钱的时候只执行了扣款语句,就提交了,此时如果突然断电,A账号已经发生了扣款,B账号却没收到加款,
在生活中就会引起纠纷。这种情况就需要事务的原子性来保证事务要么都执行,要么就都不执行。
一致性:指在事务开始之前和事务结束以后,数据库的完整性约束没有被破坏。
当事务完成时,数据必须处于一致状态。
在事务开始前,数据库中存储的数据处于一致状态。
在正在进行的事务中,数据可能处于不一致的状态。
当事务成功完成时,数据必须再次回到已知的一致状态。
案例:
对银行转帐事务,不管事务成功还是失败,应该保证事务结束后表中A和B的存款总额跟事务执行前一致。
隔离性:指在并发环境中,当不同的事务同时操纵相同的数据时,每个事务都有各自的完整数据空间。
对数据进行修改的所有并发事务是彼此隔离的,表明事务必须是独立的,它不应以任何方式依赖于或影响其他事务。修改数据的事务可在另一个使用相同数据的事务开始之前访问这些数据,或者在另一个使用相同数据的事务结束之后访问这些数据。一个事务的执行不能被其他事务干扰
事务之间的相互影响分为几种,分别为
1、脏读(读取未提交数据):脏读指的是读到了其他事务未提交的数据,未提交意味着这些数据可能会回滚,
也就是可能最终不会存到数据库中,也就是不存在的数据。读到了并一定最终存在的数据,这就是脏读
2、不可重复读(前后多次读取,数据内容不一致):一个事务内两个相同的查询却返回了不同数据。这是由于查询时系统中其他事务修改的提交而引起的。
3、幻读(前后多次读取,数据总量不一致):一个事务对一个表中的数据进行了修改,这种修改涉及到表中的全部数据行。同时,另一个事务也修改这个表中的数据,这种修改是向表中插入一行新数据。那么,操作前一个事务的用户会发现表中还有没有修改的数据行,就好象发生了幻觉一样。
4、丢失更新:两个事务同时读取同一条记录,A先修改记录,B也修改记录(B不知道A修改过),B提交数据后B的修改结果覆盖了A的修改结果。
持久性:在事务完成以后,该事务所对数据库所作的更改便持久的保存在数据库之中,并不会被回滚。
指不管系统是否发生故障,事务处理的结果都是永久的。
一旦事务被提交,事务的效果会被永久地保留在数据库中。
3,mysql事务的隔离级别
(1)read uncommitted(未提交读) : 读取尚未提交的数据 :不解决脏读
允许脏读,其他事务只要修改了数据,即使未提交,本事务也能看到修改后的数据值。也就是可能读取到其他会话中未提交事务修改的数居。
(2)read committed(提交读):读取已经提交的数据 :可以解决脏读
只能读取到已经提交的数据。Oracle等多数数据库默认都是该级别〈不重复读)
(3)repeatable read(可重复度):重读读取:可以解决脏读 和 不可重复读 —mysql默认的
可重复读。无论其他事务是否修改并提交了数据,在这个事务中看到的数据值始终不受其他事务影响
(4)serializable:串行化:可以解决 脏读 不可重复读 和 虚读—相当于锁表
完全串行化的读,每次读都需要获得表级共享锁,读写相互都会阻塞。
mysql默认的事务处理级别是 repeatable read ,而Oracle和SQL Server是 read committed 。
事务隔离级别的作用范围分为两种:
全局级:对所有的会话有效
会话级:|只对当前的会话有效
1,查询全局事务管理
show global variables like '%isolation%';
SELECT @@global.tx_isolation;
(root@localhost) [test]> show global variables like '%isolation%';
+-----------------------+-----------------+
| Variable_name | Value |
+-----------------------+-----------------+
| transaction_isolation | REPEATABLE-READ |
| tx_isolation | REPEATABLE-READ |
+-----------------------+-----------------+
2 rows in set (0.00 sec)
(root@localhost) [test]> SELECT @@global.tx_isolation;
+-----------------------+
| @@global.tx_isolation |
+-----------------------+
| REPEATABLE-READ |
+-----------------------+
1 row in set, 1 warning (0.00 sec)
2、查询会话事务隔离级别
show session variables like '%isolation%';
SELECT @@session.tx_isolation;
SELECT @@tx_isolation;
(root@localhost) [test]> show session variables like '%isolation%';
+-----------------------+-----------------+
| Variable_name | Value |
+-----------------------+-----------------+
| transaction_isolation | REPEATABLE-READ |
| tx_isolation | REPEATABLE-READ |
+-----------------------+-----------------+
2 rows in set (0.00 sec)
(root@localhost) [test]> SELECT @@session.tx_isolation;
+------------------------+
| @@session.tx_isolation |
+------------------------+
| REPEATABLE-READ |
+------------------------+
1 row in set, 1 warning (0.00 sec)
(root@localhost) [test]> SELECT @@tx_isolation;
+-----------------+
| @@tx_isolation |
+-----------------+
| REPEATABLE-READ |
+-----------------+
1 row in set, 1 warning (0.00 sec)
3、设置全局事务隔离级别
set global transaction isolation level read committed;
(root@localhost) [test]> set global transaction isolation level read committed;
Query OK, 0 rows affected (0.00 sec)
(root@localhost) [test]> SELECT @@global.tx_isolation;
+-----------------------+
| @@global.tx_isolation |
+-----------------------+
| READ-COMMITTED |
+-----------------------+
1 row in set, 1 warning (0.00 sec)
(root@localhost) [test]> set global transaction isolation level repeatable read;
Query OK, 0 rows affected (0.00 sec)
(root@localhost) [test]> SELECT @@global.tx_isolation;
+-----------------------+
| @@global.tx_isolation |
+-----------------------+
| REPEATABLE-READ |
+-----------------------+
1 row in set, 1 warning (0.00 sec) #再改回默认设置
4、设置会话事务隔离级别
set session transaction isolation level read committed;
(root@localhost) [test]> set session transaction isolation level read committed;
Query OK, 0 rows affected (0.00 sec)
(root@localhost) [test]> SELECT @@tx_isolation;
+----------------+
| @@tx_isolation |
+----------------+
| READ-COMMITTED |
+----------------+
1 row in set, 1 warning (0.00 sec)
再改回默认设置
(root@localhost) [test]> set session transaction isolation level repeatable read;
Query OK, 0 rows affected (0.00 sec)
(root@localhost) [test]> SELECT @@tx_isolation;
+-----------------+
| @@tx_isolation |
+-----------------+
| REPEATABLE-READ |
+-----------------+
1 row in set, 1 warning (0.00 sec)
4,事务控制语句
BEGIN 或 START TRANSACTION:显式地开启一个事务。
COMMIT 或 COMMIT WORK:提交事务,并使已对数据库进行的所有修改变为永久性的。
ROLLBACK 或 ROLLBACK WORK:回滚会结束用户的事务,并撤销正在进行的所有未提交的修改。
SAVEPOINT S1:使用 SAVEPOINT 允许在事务中创建一个回滚点,一个事务中可以有多个 SAVEPOINT;“S1”代表回滚点名称。
ROLLBACK TO [SAVEPOINT] S1:把事务回滚到标记点。
测试:
创建一个数据库
(root@localhost) [test]> create database bank;
Query OK, 1 row affected (0.00 sec)
(root@localhost) [test]> use bank;
Database changed
(root@localhost) [bank]> create table test(id int primary key,name char(20),money vaarchar(20));
Query OK, 0 rows affected (0.01 sec)
(root@localhost) [bank]> insert into test values(1,'A',100);
Query OK, 1 row affected (0.00 sec)
(root@localhost) [bank]> insert into test values(2,'B',100);
Query OK, 1 row affected (0.00 sec)
(root@localhost) [bank]> select *from test;
+----+------+-------+
| id | name | money |
+----+------+-------+
| 1 | A | 100 |
| 2 | B | 100 |
+----+------+-------+
2 rows in set (0.00 sec)
1、测试提交事务
(root@localhost) [bank]> begin; #开启事务
Query OK, 0 rows affected (0.00 sec)
(root@localhost) [bank]> update test set money=money-10 where name='A'; #操作
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
(root@localhost) [bank]> select *from test; #查看数据
+----+------+-------+
| id | name | money |
+----+------+-------+
| 1 | A | 90 |
| 2 | B | 100 |
+----+------+-------+
2 rows in set (0.00 sec)
(root@localhost) [bank]> commit; #提交事务
Query OK, 0 rows affected (0.00 sec)
(root@localhost) [bank]> quit #退出mysql
Bye
[root@localhost ~]# mysql -uroot -p123123 #登录mysql
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
(root@localhost) [(none)]> use bank
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
(root@localhost) [bank]> select *from test; #查看数据
+----+------+-------+
| id | name | money |
+----+------+-------+
| 1 | A | 90 |
| 2 | B | 100 |
+----+------+-------+
2 rows in set (0.00 sec)
2、测试回滚事务
(root@localhost) [bank]> begin; #开启事务
Query OK, 0 rows affected (0.00 sec)
(root@localhost) [bank]> update test set money=money+10 where name='A'; #操作
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
(root@localhost) [bank]> select *from test; #查看
+----+------+-------+
| id | name | money |
+----+------+-------+
| 1 | A | 100 |
| 2 | B | 100 |
+----+------+-------+
2 rows in set (0.00 sec)
(root@localhost) [bank]> rollback; #回滚未提交事务
Query OK, 0 rows affected (0.00 sec)
(root@localhost) [bank]> quit #退出mysql
Bye
[root@localhost ~]# mysql -uroot -p123123 #登录mysql
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
(root@localhost) [(none)]> use bank; #进入库
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
(root@localhost) [bank]> select *from test; #查看数据表(A的money回滚到操作前)
+----+------+-------+
| id | name | money |
+----+------+-------+
| 1 | A | 90 |
| 2 | B | 100 |
+----+------+-------+
2 rows in set (0.00 sec)
3、测试多点回滚
(root@localhost) [bank]> select *from test; #查看原数据
+----+------+-------+
| id | name | money |
+----+------+-------+
| 1 | A | 90 |
| 2 | B | 100 |
+----+------+-------+
2 rows in set (0.00 sec)
(root@localhost) [bank]> begin;
Query OK, 0 rows affected (0.00 sec)
(root@localhost) [bank]> update test set money=money-20 where name='A'; #操作
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
(root@localhost) [bank]> savepoint s1; #设置回滚点s1
Query OK, 0 rows affected (0.00 sec)
(root@localhost) [bank]> select *from test; #查看
+----+------+-------+
| id | name | money |
+----+------+-------+
| 1 | A | 70 |
| 2 | B | 100 |
+----+------+-------+
2 rows in set (0.00 sec)
(root@localhost) [bank]> update test set money=money+20 where name='B'; #操作
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
(root@localhost) [bank]> savepoint s2; #设置回滚点s2
Query OK, 0 rows affected (0.00 sec)
(root@localhost) [bank]> select *from test; #查看数据
+----+------+-------+
| id | name | money |
+----+------+-------+
| 1 | A | 70 |
| 2 | B | 120 |
+----+------+-------+
2 rows in set (0.00 sec)
(root@localhost) [bank]> insert into test values(3,'C',200); #添加数据
Query OK, 1 row affected (0.00 sec)
(root@localhost) [bank]> select *from test; #查看数据
+----+------+-------+
| id | name | money |
+----+------+-------+
| 1 | A | 70 |
| 2 | B | 120 |
| 3 | C | 200 |
+----+------+-------+
3 rows in set (0.00 sec)
(root@localhost) [bank]> rollback to s1; #回滚到节点s1
Query OK, 0 rows affected (0.00 sec)
(root@localhost) [bank]> select *from test; #查看
+----+------+-------+
| id | name | money |
+----+------+-------+
| 1 | A | 70 |
| 2 | B | 100 |
+----+------+-------+
2 rows in set (0.00 sec)
5,使用set设置控制事务
SET AUTOCOMMIT=0; #禁止自动提交
SET AUTOCOMMIT=1; #开启自动提交,Mysql默认为1
SHOW VARIABLES LIKE 'AUTOCOMMIT'; #查看Mysql中的AUTOCOMMIT值
如果没有开启自动提交,当前会话连接的mysql的所有操作都会当成一个事务直到你输入rollback|commit;当前事务才算结束。
当前事务结束前新的mysql连接时无法读取到任何当前会话的操作结果。
如果开起了自动提交,mysql会把每个sql语句当成一个事务,然后自动的commit。
当然无论开启与否,begin; commit|rollback; 都是独立的事务。
例:
(root@localhost) [bank]> select *from test; #原数据
+----+------+-------+
| id | name | money |
+----+------+-------+
| 1 | A | 70 |
| 2 | B | 100 |
+----+------+-------+
2 rows in set (0.00 sec)
(root@localhost) [bank]> set autocommit=0; #关闭自动提交事务
Query OK, 0 rows affected (0.00 sec)
(root@localhost) [bank]> show variables like 'autocommit'; #查看事务提交状态
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | OFF |
+---------------+-------+
1 row in set (0.00 sec)
(root@localhost) [bank]> update test set money=money-50 where name='A'; #操作
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
(root@localhost) [bank]> select *from test; #查看数据
+----+------+-------+
| id | name | money |
+----+------+-------+
| 1 | A | 20 |
| 2 | B | 100 |
+----+------+-------+
2 rows in set (0.00 sec)
(root@localhost) [bank]> quit #退出
Bye
[root@localhost ~]# mysql -uroot -p123123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.7.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
(root@localhost) [(none)]> use bank;
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
(root@localhost) [bank]> select *from test; #查看数据
+----+------+-------+
| id | name | money |
+----+------+-------+
| 1 | A | 70 |
| 2 | B | 100 |
+----+------+-------+
2 rows in set (0.00 sec)
#因为关闭了自动提交事务,所做的操作需手动提交,而刚才的操作没有提交事务。
所以所做的操作退出在登录上数据库是没有保存的,只有原始的数据。