溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

MySQL索引與事務(wù)詳解

發(fā)布時(shí)間:2020-07-31 22:43:56 來(lái)源:網(wǎng)絡(luò) 閱讀:266 作者:wx5d8a17c45cb5b 欄目:MySQL數(shù)據(jù)庫(kù)

MySQL索引與事務(wù)詳解

一、前言

? 上一章我們講解了MySQL的手工編譯安裝流程以及相關(guān)的數(shù)據(jù)庫(kù)操作命令(sql語(yǔ)句),本文將要詳細(xì)介紹MySQL索引與事務(wù)的概念及原理,并初步了解MySQL數(shù)據(jù)庫(kù)視圖概念,簡(jiǎn)述數(shù)據(jù)庫(kù)的存儲(chǔ)過(guò)程。

二、索引

2.1索引的概念——什么是索引?

? 一般來(lái)說(shuō),一篇論文,或者說(shuō)一本書(shū),都有其目錄,而目錄一般是所有章節(jié)的概述,或者說(shuō)是要點(diǎn)核心,而索引的概念其實(shí)也與之類似。

? 索引,顧名思義,就是一個(gè)方便用戶搜索所需資源的引導(dǎo),只不過(guò)在數(shù)據(jù)庫(kù)中,索引一般被認(rèn)為是一種特殊的文件,尤其在Linux系統(tǒng)中("一切皆文件")。從專業(yè)術(shù)語(yǔ)上解釋其含義就是“代表記錄的引用指針”。

2.2索引的作用

  • 加快查詢速度,提高工作效率;
  • 降低i/o成本,提供基礎(chǔ)排序;
  • 加快多表之間的連接;
  • 支持唯一性索引(下面會(huì)介紹什么是唯一性索引),保證數(shù)據(jù)表中數(shù)據(jù)的唯一性性;

2.3索引的分類

2.3.1普通索引

? 就是一般的索引,只是為了區(qū)別于其他特殊索引的一個(gè)統(tǒng)稱

2.3.2唯一性索引

? 與普通索引基本類同,區(qū)別在于,唯一性索引的列中的所有值都不相同,即“唯一”。

? 簡(jiǎn)單舉例來(lái)說(shuō),學(xué)生數(shù)據(jù)表,年齡可以是普通索引,但不可以是唯一性索引,但是詳細(xì)住址可以是。

2.3.3主鍵索引

? 本質(zhì)上也是一種唯一性索引,但必須指定為“primary key”,該索引要求主鍵中的每個(gè)值都唯一。上篇文章中,我們提及到了主鍵的概念,其特點(diǎn)也是“非空唯一”。

2.3.4全文索引

? 索引類型為FULLTEXT,全文索引可以在char、vachar或者text類型的列上創(chuàng)建。

2.3.5單列索引與多列索引

? 可以在單列或多列上創(chuàng)建索引。多列索引一般用于區(qū)分其中一列可能有相同值的行。

2.4創(chuàng)建索引的原則:建立在表上的(索引依賴于表)

? 索引可以提升數(shù)據(jù)庫(kù)的查詢速度,但并非所有的數(shù)據(jù)表都需要?jiǎng)?chuàng)建索引。因?yàn)樗饕旧硪彩切枰加孟到y(tǒng)資源的,或許一般情況下這個(gè)問(wèn)題不會(huì)很突出,因?yàn)?a title="服務(wù)器" target="_blank" href="http://www.kemok4.com/">服務(wù)器的資源在一定程度上還是能夠正常支持的,但是如果索引文件過(guò)大,其大小可能達(dá)到操作系統(tǒng)允許的最大文件限制。

? 并且,如果說(shuō)索引使用不當(dāng)也會(huì)造成數(shù)據(jù)庫(kù)的負(fù)擔(dān)。因此,數(shù)據(jù)庫(kù)創(chuàng)建索引也是有其原則的。

2.4.1創(chuàng)建索引的原則依據(jù)
  1. 表的主鍵、外鍵必須有索引;
  2. 達(dá)到一定量的表(300行記錄)應(yīng)該有索引;
  3. 表之間的連接字段上應(yīng)該建立索引;
  4. 不能使用唯一性太差的字段作為索引;
  5. 更新頻繁的字段也不適合作為索引;
  6. 小字段適合建立索引,長(zhǎng)的字段則不適合建立索引;

2.5索引的優(yōu)缺點(diǎn)

優(yōu)點(diǎn):快速查詢所需資源

缺點(diǎn):占用空間以及資源

2.6創(chuàng)建及查看索引

首先我們需要保證數(shù)據(jù)庫(kù)中有表,且表內(nèi)有數(shù)據(jù);

mysql> select * from fruit_info;                               
+----+-------+---------+
| id | price | newtype |
+----+-------+---------+
|  1 |  2.50 | banana  |
|  2 |  5.50 | apple   |
|  3 |  6.00 | peach   |
+----+-------+---------+
3 rows in set (0.00 sec)
2.6.1創(chuàng)建普通索引

命令格式:create index <索引名> on 數(shù)據(jù)表 (列名);

實(shí)例:

mysql> create index id_index on fruit_info(id);
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> show index from fruit_info;  #查看索引語(yǔ)句也可以將index換成“keys”
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table      | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| fruit_info |          1 | id_index |            1 | id          | A         |           3 |     NULL | NULL   |      | BTREE      |         |               |
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)

其中Non_unique為1,表示不是唯一性索引;Key_name 對(duì)應(yīng)的是索引名稱,這里就是id_index;

2.6.2創(chuàng)建唯一性索引

命令格式:create unique index <索引名稱> on 數(shù)據(jù)表 (列名);

實(shí)例:

mysql> create unique index type_index on fruit_info(newtype);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from fruit_info;
+------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table      | Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| fruit_info |          0 | type_index |            1 | newtype     | A         |           3 |     NULL | NULL   | YES  | BTREE      |         |               |
| fruit_info |          1 | id_index   |            1 | id          | A         |           3 |     NULL | NULL   |      | BTREE      |         |               |
+------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

這里的索引名稱為type_index,與之對(duì)應(yīng)的Non_unique的值為0,表示其為唯一性索引。并且唯一性索引的值都不一樣。

那么我們考慮一個(gè)問(wèn)題:數(shù)據(jù)庫(kù)中的表的字段是否既可以是普通索引,又可以是唯一性索引?

我們來(lái)實(shí)操驗(yàn)證一下:

mysql> create unique index id_index_new on fruit_info(id);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from fruit_info;
+------------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table      | Non_unique | Key_name     | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+------------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| fruit_info |          0 | id_index_new |            1 | id          | A         |           3 |     NULL | NULL   |      | BTREE      |         |               |
| fruit_info |          0 | type_index   |            1 | newtype     | A         |           3 |     NULL | NULL   | YES  | BTREE      |         |               |
| fruit_info |          1 | id_index     |            1 | id          | A         |           3 |     NULL | NULL   |      | BTREE      |         |               |
+------------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)

事實(shí)證明是可以的,但是我們需要知道唯一性索引與普通索引的區(qū)別就在于“唯一性”上。如果創(chuàng)建了唯一性索引,那么在插入數(shù)據(jù)記錄的時(shí)候就需要注意字段匹配時(shí)的唯一性。

2.6.3創(chuàng)建主鍵索引

命令格式:(1)創(chuàng)建表的時(shí)候創(chuàng)建主鍵:create table 表名 ([ ... ],primary key(列的列表));

(2)修改表結(jié)構(gòu)加入主鍵:alter table 表名 add primary key;

實(shí)例:

mysql> create table student (id int not null,sex char(2),age int not null,hobby varchar(20),primary key(id,hobby));
Query OK, 0 rows affected (0.01 sec)

mysql> show index from 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 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| student |          0 | PRIMARY  |            2 | hobby       | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.01 sec)

一個(gè)表里面只能有一個(gè)主鍵,但一個(gè)主鍵可以由多個(gè)字段組成。

mysql> alter table fruit_info add primary key(id);
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from fruit_info;
+------------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table      | Non_unique | Key_name     | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+------------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| fruit_info |          0 | PRIMARY      |            1 | id          | A         |           4 |     NULL | NULL   |      | BTREE      |         |               |
| fruit_info |          0 | id_index_new |            1 | id          | A         |           4 |     NULL | NULL   |      | BTREE      |         |               |
| fruit_info |          0 | type_index   |            1 | newtype     | A         |           4 |     NULL | NULL   | YES  | BTREE      |         |               |
| fruit_info |          1 | id_index     |            1 | id          | A         |           4 |     NULL | NULL   |      | BTREE      |         |               |
+------------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
4 rows in set (0.00 sec)

主鍵索引是設(shè)置主鍵后自動(dòng)創(chuàng)建的,無(wú)需指定名稱,系統(tǒng)自動(dòng)生成名字“primary”。主鍵索引與唯一性索引區(qū)別就在于唯一性索引可以為null,而主鍵索引為not null,所以可以簡(jiǎn)單用公式理解:primary index = not null + unique index;

2.6.4全文索引

全文索引可以建立的字段類型在前面已經(jīng)提及了,命令格式如下:

1、create table 表名 (列名 text,F(xiàn)ULLTEXT(列名))engine=MyISAM;

2、alter table 表名 add FULLTEXT(列名);

實(shí)例:

查看數(shù)據(jù)庫(kù)的存儲(chǔ)引擎類型:(存儲(chǔ)引擎我們下一篇文章會(huì)講解)

mysql> show table status from fruit where name='student'\G
*************************** 1. row ***************************
           Name: student
         Engine: InnoDB
        Version: 10
     Row_format: Dynamic
           Rows: 0
 Avg_row_length: 0
    Data_length: 16384
Max_data_length: 0
   Index_length: 16384
      Data_free: 0
 Auto_increment: NULL
    Create_time: 2020-01-06 19:12:24
    Update_time: NULL
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
 Create_options: 
        Comment: 
1 row in set (0.00 sec)
mysql> alter table student add fulltext(hobby);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> mysql> show keys from 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 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| student |          0 | PRIMARY     |            2 | hobby       | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| student |          1 | hobby       |            1 | hobby       | NULL      |           0 |     NULL | NULL   |      | FULLTEXT   |         |               |
+---------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)
2.6.5多列索引

在創(chuàng)建索引時(shí)指定多列即可

命令格式:create index 索引名 on 表名(字段1,字段2);

實(shí)例:

mysql> create index mo_index on student(id,hobby)
    -> ;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from 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 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| student |          0 | PRIMARY     |            2 | hobby       | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| student |          1 | mo_index    |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| student |          1 | mo_index    |            2 | hobby       | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| student |          1 | hobby       |            1 | hobby       | NULL      |           0 |     NULL | NULL   |      | FULLTEXT   |         |               |
+---------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
6 rows in set (0.00 sec)

2.7刪除索引

命令格式:

2.7.1直接刪除索引

格式:drop index 索引名 on 表名;

實(shí)例:

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

mysql> show index from 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 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| student |          0 | PRIMARY     |            2 | hobby       | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| student |          1 | hobby       |            1 | hobby       | NULL      |           0 |     NULL | NULL   |      | FULLTEXT   |         |               |
+---------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
4 rows in set (0.00 sec)
2.7.2修改表是刪除索引

格式:alter table 表名 drop index 索引名;

實(shí)例:

mysql> alter table student drop index hobby;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from 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 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| student |          0 | PRIMARY  |            2 | hobby       | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
2.7.3刪除主鍵索引

格式:alter table 表名 drop primary key;

實(shí)例:

mysql> alter table student drop primary key;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from student;
Empty set (0.00 sec)

當(dāng)然如果在修改表結(jié)構(gòu)的時(shí)候,刪除了包含索引的列,那么對(duì)應(yīng)的索引也會(huì)被刪除。

三、事務(wù)

3.1事務(wù)的概念

將多個(gè)命令作為整體執(zhí)行,要么都成功要么都不執(zhí)行,如銀行轉(zhuǎn)賬;

如果執(zhí)行事務(wù)的時(shí)候,前面的一部分成功了,而最后的一部分失敗了,那么就會(huì)實(shí)行“回滾“機(jī)制,將執(zhí)行操作回到事務(wù)的起點(diǎn)位置,數(shù)據(jù)沒(méi)有發(fā)生結(jié)果性變化。

3.2事務(wù)的ACID特點(diǎn)(4個(gè))

3.2.1原子性

? 事務(wù)是不可分割的工作邏輯單元,事務(wù)可以包含多個(gè)sql語(yǔ)句,但整個(gè)事務(wù)是一個(gè)完整的操作,不可分割;(比如轉(zhuǎn)賬,轉(zhuǎn)賬過(guò)程中一但出現(xiàn)error,那么就會(huì)回滾到起初狀態(tài),二者資產(chǎn)不會(huì)有任何變化)

3.2.2一致性

? 事務(wù)執(zhí)行前和執(zhí)行后數(shù)據(jù)必須處于一致?tīng)顟B(tài),但是執(zhí)行過(guò)程中是動(dòng)態(tài)變化的;(比如轉(zhuǎn)賬,轉(zhuǎn)賬前和轉(zhuǎn)賬后雙方資產(chǎn)的總和是不變的(不考慮手續(xù)費(fèi)等其他費(fèi)用的情況))

3.2.3隔離性

? 并發(fā)事務(wù)是彼此隔離的,事務(wù)之間必須是獨(dú)立(比如打電話,甲和乙打電話不會(huì)影響丙和丁打電話)

3.2.4持久性

? 事務(wù)結(jié)果都是永久的并且是不可逆的(比如轉(zhuǎn)賬的結(jié)果,如果發(fā)生糾紛再次處理就是另一個(gè)事務(wù)了。)

3.3事務(wù)的操作

3.3.1自動(dòng)提交

? 默認(rèn)的情況下是自動(dòng)提交的,就是輸入了sql語(yǔ)句就自動(dòng)提交執(zhí)行該命令,但一般來(lái)說(shuō)這是不安全的;

3.3.2手動(dòng)提交

? 在生產(chǎn)環(huán)境中,該模式使用比較多,這是因?yàn)槭謩?dòng)提交可以通過(guò)緩存,內(nèi)存中的數(shù)據(jù)顯示的結(jié)果查看是否出錯(cuò),錯(cuò)了即實(shí)行回滾操作(rollback)(一般會(huì)設(shè)置回滾點(diǎn))。

3.3.3使用事務(wù)命令控制事務(wù)(4個(gè))

begin:表示開(kāi)始一個(gè)事務(wù),后面接多個(gè)sql語(yǔ)句;0

commit:表示提交一個(gè)事務(wù),對(duì)應(yīng)前面的begin

rollback:表示回滾一個(gè)事務(wù),在begin和rollback之間,錯(cuò)誤的時(shí)候可以回滾。

savepoint:表示設(shè)置回滾點(diǎn)配合rollback命令使用。

實(shí)例:

首先我們有一個(gè)如下的數(shù)據(jù)表:

mysql> desc fruit_info;   #表結(jié)構(gòu)
+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id      | int(4)       | NO   | PRI | NULL    |       |
| price   | decimal(3,2) | NO   |     | NULL    |       |
| newtype | varchar(6)   | YES  | UNI | NULL    |       |
+---------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> select * from fruit_info; #表數(shù)據(jù)
+----+-------+---------+
| id | price | newtype |
+----+-------+---------+
|  1 |  2.50 | banana  |
|  2 |  5.50 | apple   |
|  3 |  6.00 | peach   |
|  4 |  6.00 | orange  |
+----+-------+---------+
4 rows in set (0.00 sec)

mysql> begin; #開(kāi)始一個(gè)事務(wù)的標(biāo)志
Query OK, 0 rows affected (0.00 sec)

mysql> insert into fruit_info values(5,4,'pear');  #插入一個(gè)記錄
Query OK, 1 row affected (0.00 sec)

mysql> select * from fruit_info; #此時(shí)只是放入緩存中使用rollback可以回到最初狀態(tài);
+----+-------+---------+
| id | price | newtype |
+----+-------+---------+
|  1 |  2.50 | banana  |
|  2 |  5.50 | apple   |
|  3 |  6.00 | peach   |
|  4 |  6.00 | orange  |
|  5 |  4.00 | pear    |
+----+-------+---------+
5 rows in set (0.00 sec)

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

mysql> select * from fruit_info;
+----+-------+---------+
| id | price | newtype |
+----+-------+---------+
|  1 |  2.50 | banana  |
|  2 |  5.50 | apple   |
|  3 |  6.00 | peach   |
|  4 |  6.00 | orange  |
+----+-------+---------+
4 rows in set (0.00 sec)

mysql> insert into fruit_info values(5,4,'pear');
Query OK, 1 row affected (0.00 sec)

mysql> commit;  #提交之后無(wú)法使用rollback回到最初狀態(tài);
Query OK, 0 rows affected (0.00 sec)

mysql> select * from fruit_info;
+----+-------+---------+
| id | price | newtype |
+----+-------+---------+
|  1 |  2.50 | banana  |
|  2 |  5.50 | apple   |
|  3 |  6.00 | peach   |
|  4 |  6.00 | orange  |
|  5 |  4.00 | pear    |
+----+-------+---------+
5 rows in set (0.00 sec)

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

mysql> select * from fruit_info;
+----+-------+---------+
| id | price | newtype |
+----+-------+---------+
|  1 |  2.50 | banana  |
|  2 |  5.50 | apple   |
|  3 |  6.00 | peach   |
|  4 |  6.00 | orange  |
|  5 |  4.00 | pear    |
+----+-------+---------+
5 rows in set (0.01 sec)

設(shè)置斷點(diǎn)——“回滾點(diǎn)”

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

mysql> insert into fruit_info values(5,4,'pear');
ERROR 1062 (23000): Duplicate entry '5' for key 'PRIMARY'
mysql> insert into fruit_info values(6,4,'grape');
Query OK, 1 row affected (0.00 sec)

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

mysql> insert into fruit_info values(7,4,'cherry');
Query OK, 1 row affected (0.00 sec)

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

mysql> select * from fruit_info;
+----+-------+---------+
| id | price | newtype |
+----+-------+---------+
|  1 |  2.50 | banana  |
|  2 |  5.50 | apple   |
|  3 |  6.00 | peach   |
|  4 |  6.00 | orange  |
|  5 |  4.00 | pear    |
|  6 |  4.00 | grape   |
|  7 |  4.00 | cherry  |
+----+-------+---------+
7 rows in set (0.00 sec)

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

mysql> select * from fruit_info;
+----+-------+---------+
| id | price | newtype |
+----+-------+---------+
|  1 |  2.50 | banana  |
|  2 |  5.50 | apple   |
|  3 |  6.00 | peach   |
|  4 |  6.00 | orange  |
|  5 |  4.00 | pear    |
|  6 |  4.00 | grape   |
|  7 |  4.00 | cherry  |
+----+-------+---------+
7 rows in set (0.00 sec)

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

mysql> select * from fruit_info;
+----+-------+---------+
| id | price | newtype |
+----+-------+---------+
|  1 |  2.50 | banana  |
|  2 |  5.50 | apple   |
|  3 |  6.00 | peach   |
|  4 |  6.00 | orange  |
|  5 |  4.00 | pear    |
|  6 |  4.00 | grape   |
+----+-------+---------+
6 rows in set (0.00 sec)
mysql> rollback;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from fruit_info;
+----+-------+---------+
| id | price | newtype |
+----+-------+---------+
|  1 |  2.50 | banana  |
|  2 |  5.50 | apple   |
|  3 |  6.00 | peach   |
|  4 |  6.00 | orange  |
|  5 |  4.00 | pear    |
+----+-------+---------+
5 rows in set (0.00 sec)

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

根據(jù)以上的結(jié)果我們可以得到如下結(jié)論:

使用事務(wù)命令控制事務(wù)是可以實(shí)現(xiàn)回滾機(jī)制的;

在使用savepoint結(jié)合rollback命令時(shí),回滾的位置是根據(jù)你執(zhí)行的命令的最終位置;

如果直接使用rollback命令是直接回到最初狀態(tài),且無(wú)法回到其他回滾節(jié)點(diǎn)。

3.3.4set命令

set autocommit=0:禁止自動(dòng)提交 ——就相當(dāng)于begin;

set autocommit-=1:開(kāi)啟自動(dòng)提交

四、總結(jié)

? 本文主要是對(duì)MySQL數(shù)據(jù)庫(kù)中的索引和事務(wù)的概念進(jìn)行詳細(xì)的介紹,這里的概念和原理以及對(duì)應(yīng)的使用場(chǎng)景需要我們結(jié)合實(shí)例進(jìn)行詳細(xì)理解。索引的分類以及主鍵索引與唯一性索引的區(qū)別,事務(wù)的4大特點(diǎn)和事務(wù)的回滾機(jī)制。

? 本文的操作并不難,但是細(xì)節(jié)上的原理上的理解還是比較瑣碎拗口的,需要真正的理解,面試的時(shí)候這塊內(nèi)容十分重要,謝謝您的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI