中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MySQL的索引與事務、存儲引擎MyISA和InnoDB

發布時間:2020-02-27 08:13:02 來源:網絡 閱讀:996 作者:wx5d2c2cbaaf223 欄目:MySQL數據庫

MySQL索引與事務,視圖,存儲引擎MylSAM和lnnoDB

索引的概念

數據庫中的索引與書籍中的目錄類似:

1.一本書中,無須閱讀整本書,利用目錄就可以快速查找所需信息
2.書中的目錄是一個詞語列表,其中注明了包含各個詞的頁碼

數據庫索引:

1.在數據庫中,索引使數據庫程序無須對整個表進行掃描,就可以在其中找到所需數據
2.數據庫中的索弓|是某個表中一列或者若干列值的集合,以及物理標識這些值的數據頁的邏輯指針清單

索引的作用:

1.設置了合適的索引之后,數據庫利用各種快速的定位技術,能夠大大加快查詢速率
2.特別是當表很大時,或者查詢涉及到多個表時,使用索引可使查詢加快成干倍
3.可以降低數據庫的IO成本,并且索引還可以降低數據庫的排序成本
4.通過創建唯一性索引保證數據表數據的唯一性
5.可以加快表與表之間的連接
6.在使用分組和排序時,可大大減少分組和排序時間

索引的分類

普通索引:

是最基本的索引類型,而且它沒有唯一性之類的限制

唯一性索引:

這種索引和前面的“普通索引”基本相同,但有一個區別:索引列的所有值都只能出現一次,即必須唯一

主鍵:

主鍵是一種唯一性索引,但它必須指定為”PRIMARY KEY"

全文索引:

MySQL從3.23.23版開始支持全文索引和全文檢索。在MySQL中全文索引的索引類型為FULLTEXT,全文索引可以在VARCHAR或者TEXT類型的列上創建

單列索引與多列索引:

索引可以是單列上創建的索引,也可以是在多列上創建的索引

創建索引的原則依據

1.表的主鍵、外鍵必須有索引
2.數據量超過300行的表應該有索引
3.經常與其他表進行連接的表,在連接字段.上應該建立索引
4.唯一性太差的字段不適合建立索引
5.更新太頻繁地字段不適合創建索引6.經常出現在Where子句中的字段,特別是大表的字段,應該建立索引
7.索引應該建在選擇性高的字段上
8.索引應該建在小字段上,對于大的文本字段甚至超長字段,不要建索引

事務的概念

1.事務是一種機制、一個操作序列,包含了一組數據庫操作命令,并且把所有的命令作為-一個整體一起向系統提交或撤銷操作請求,即這一組數據庫命令要么都執行,要么都不執行
2.事務是一個不可分割的工作邏輯單元,在數據庫系統上執行并發操作時,事務是最小的控制單元
3.適用于多用戶同時操作的數據庫系統的場景,如銀行、保險公司及證券交易系統等等
4.通過事務的整體性以保證數據的一致性

事務的ACID特點

原子性(Atomicity):

1.事務是一個完整的操作,事務的各元素是不可分的(原子的)
2.事務中的所有元素必須作為一個整體提交或回滾
3.如果事務中的任何元素失敗,則整個事務將失敗

一致性(Consistency):

當事務完成時,數據必須處于一 致狀態:在事務開始之前,數據庫中存儲的數據處于一致狀態;在正在進行的事務中,數據可能處于不一致的狀態;當事務成功完成時,數據必須再次回到已知的一致狀態

隔離性(Isolation):

1.對數據進行修改的所有并發事務是彼此隔離的,這表明事務必須是獨立的,它不應以任何方式依賴于或影響其他事務
2.修改數據的事務可以在另一個使用相同數據的事務開始之前訪問這些數據,或者在另一個使用相同數據的事務結束之后訪問這些數據

持久性(Durability):

1.事務持久性指不管系統是否發生故障,事務處理的結果都是永久的
2.一旦事務被提交,事務的效果會被永久地保留在數據庫中

事務的操作

1.默認情況下MySQL的事務是自動提交的,當sql語句提交時事務便自動提交
2.手動對事務進行控制的方法:

①事務處理命令控制
②使用set設置事務處理方式

3.事務處理命令控制事務:

①begin:開始一個事務
②commit:提交一個事務
②rollback:回滾一個事務

4.使用set命令進行控制:

①set autocommit=0:禁止自動提交
②set autocommit=1:開啟自動提交

存儲引擎概念介紹

1.MySQL中的數據用各種不同的技術存儲在文件中,每一種技術都使用不同的存儲機制、索引技巧、鎖定水平并最終提供不同的功能和能力,這些不同的技術以及配套的功能在MySQL中稱為存儲引擎

2.存儲引擎就是MySQL將數據存儲在文件系統中的存儲方式或者存儲格式
3.目前MySQL常用的兩種存儲引擎:

①MyISAM(適合讀)
②InnoDB(適合寫)

4.MySQL存儲引擎是MySQL數據庫服務器中的組件,負責為數據庫執行實際的數據I/O操作

5.使用特殊存儲引擎的主要優點之一在于, 僅需提供特殊應用所需的特性,數據庫中的系統開銷較小,具有更有效和更高的數據庫性能

6.MySQL系統中,存儲引擎處于文件系統之.上,在數據保存到數據文件之前會傳輸到存儲引擎,之后按照各個存儲引擎的存儲格式進行存儲

MyISAM的介紹

1.MyISAM存儲引擎是MySQ關系數據庫系統5.5版本之前默認的存儲引擎,前身是ISAM

2.ISAM是一個定義明確且歷經時間考驗的數據表格管理方法,在設計之時就考慮到數據庫被查詢的次數要遠大于更新的次數
3.SAM的特點:

①ISAM執行讀取操作的速度很快,
②它不支持事務處理
③而且不占用大量的內存和存儲資源
④不能夠容錯

MyISAM適用的生產場景舉例:

1.公司業務不需要事務的支持
2.一般單方面讀取數據比較多的業務,或單方面寫入數據比較多的業務
3.MyISAM存儲引擎數據讀寫都比較頻繁場景不適合
4.使用讀寫并發訪問相對較低的業務
5.數據修改相對較少的業務
6.對數據業務一致性要求不是非常高的業務
7.服務器硬件資源相對比較差

InnoDB特點介紹

1.支持事務:支持4個事務隔離級別
2.行級鎖定,但是全表掃描仍然會是表級鎖定
3.讀寫阻塞與事務隔離級別相關
4.具有非常高效的緩存特性:能緩存索引,也能緩存數據
5.表與主鍵以簇的方式存儲
6.支持分區、表空間,類似oracle數據庫
7.支持外鍵約束,5.5以前不支持全文索引,5.5版本以后支持全文索引
8.對硬件資源要求還是比較高的場合

InnoDB適用生產場景分析

1.業務需要事務的支持
2.行級鎖定對高并發有很好的適應能力,但需確保查詢是通過索引來完成
3.業務數據更新較為頻繁的場景, 如:論壇,微博等
4.業務數據一致性要求較高,例如:銀行業務
5.硬件設備內存較大, 利用Innodb較好的緩存能力來提高內存利用率,減少磁盤IO的壓力

配置存儲引擎

1.在企業中選擇好合適的存儲引擎之后,就可以進行修改了
2.修改步驟:

①查看數據庫可配置的存儲引擎
②查看表正在使用的存儲引擎
③配置存儲引擎為所選擇的類型

3.使用show engines查看系統支持的存儲引擎查看表使用的存儲引擎

方法1: show table status from庫名where name='表名;
方法2: show create table表名

索引實操

1,進入數據庫,創建數據庫,創建表

[root@master2 ~]# mysql -uroot -p     ##進入數據庫
Enter password:     ##輸入密碼

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database school;    ##創建數據庫school
Query OK, 1 row affected (0.00 sec)

mysql> use school;       ##使用數據庫
Database changed
mysql> create table info(        ##創建表
        -> id int(4) not null primary key auto_increment,   ##設置主鍵,自動增加
        -> name varchar(10) not null,       ##名字類型varchar不為空
        -> address varchar(50) default 'nanjing',    ##默認南京
        -> age int(3) not null);
Query OK, 0 rows affected (0.02 sec)

2,向表中插入數據

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> select * from info;   ##查看表內容
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
+----+----------+----------+-----+
2 rows in set (0.00 sec)

mysql> desc 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)

3,使用create,alter,創建表直接定義三種方式創建索引(普通索引,唯一性索引)

mysql> create index index_age on info (age);   ##創建普通索引
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  |          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.01 sec)

mysql> create unique index unique_name on info (name); ##創建唯一性索引
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      |         |               |
| 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.00 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> alter table info add unique index index_name (name);   ##使用alter插入表索引
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      |         |               |
| info  |          0 | index_name |            1 | name        | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

4,創建兩個表,進行關聯,多表查詢

mysql> create table user(     ##創建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',  ##默認1
        -> index index_score (score));    ##設置索引score
Query OK, 0 rows affected (0.01 sec)

mysql> desc user;  ##查看表結構
+-------+---------------+------+-----+---------+----------------+
| Field | Type          | Null | Key | Default | Extra          |
+-------+---------------+------+-----+---------+----------------+
| id    | int(4)        | NO   | PRI | NULL    | auto_increment |
| name  | varchar(10)   | NO   |     | NULL    |                |
| score | decimal(10,0) | NO   | MUL | NULL    |                |
| hobby | int(2)        | NO   |     | 1       |                |
+-------+---------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

mysql> insert into user (name,score,hobby) values ('test01',88,1),('stu01',99,2),('wangwu',77,3); 
##向表中插入數據
Query OK, 3 rows affected (0.00 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> create table hob(     ##創建hob表
        -> id int(2) not null primary key,
        -> hob_name varchar(10) not null);
Query OK, 0 rows affected (0.00 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.00 sec)

mysql> insert into hob (id,hob_name) values (1,'看書'),(2,'運動'),(3,'聽歌');   ##插入表數據
Query OK, 3 rows affected (0.00 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> select * from user inner join hob on user.hobby=hob.id;   ##關聯user和hob兩張表
+----+--------+-------+-------+----+----------+
| id | name   | score | hobby | id | hob_name |
+----+--------+-------+-------+----+----------+
|  1 | test01 |    88 |     1 |  1 | 看書     |
|  2 | stu01  |    99 |     2 |  2 | 運動     |
|  3 | wangwu |    77 |     3 |  3 | 聽歌     |
+----+--------+-------+-------+----+----------+
3 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內容
+--------+----------+
| name   | hob_name |
+--------+----------+
| test01 | 看書     |
| stu01  | 運動     |
| wangwu | 聽歌     |
+--------+----------+
3 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 | 聽歌     |
+--------+----------+
3 rows in set (0.00 sec)

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.00 sec)

mysql> select * from view_user;  ##查看視圖
+--------+----------+
| name   | hob_name |
+--------+----------+
| test01 | 看書     |
| stu01  | 運動     |
| wangwu | 聽歌     |
+--------+----------+
3 rows in set (0.00 sec)

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

mysql> select * from view_user;   ##查看視圖,即視圖就是表的一個鏈接
+--------+----------+
| name   | hob_name |
+--------+----------+
| stu01  | 運動     |
| test01 | 聽歌     |
| wangwu | 聽歌     |
+--------+----------+
3 rows in set (0.00 sec)

5,全文索引,組合索引

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);      ##以address創建全文索引
Query OK, 0 rows affected, 1 warning (0.07 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> select * from user;     ##查看user表內容
+----+--------+-------+-------+
| id | name   | score | hobby |
+----+--------+-------+-------+
|  1 | test01 |    88 |     3 |
|  2 | stu01  |    99 |     2 |
|  3 | wangwu |    77 |     3 |
+----+--------+-------+-------+
3 rows in set (0.00 sec)

mysql> create index index_name_score on user (name,score);    ##創建name和score的組合索引
Query OK, 0 rows affected (0.01 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         |           3 |     NULL | NULL   |      | BTREE      |         |               |
| user  |          1 | index_name_score |            2 | score       | A         |           3 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
4 rows in set (0.00 sec)

事務實操

1,開啟事務,往表中插入數據

mysql> select * from info;           ##查看表內容數據
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
+----+----------+----------+-----+
2 rows in set (0.00 sec)

mysql> insert into info (name,address,age) values ('wangwu','hangzhou',30);        ##插入數據

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)

mysql> savepoint a;                 ##設置保存節點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',32);  ##繼續插入數據
Query OK, 1 row affected (0.00 sec)

mysql> savepoint b;                  ##設置保存節點b
Query OK, 0 rows affected (0.00 sec)

mysql> insert into info (name,address,age) values ('heiba','hangzhou',32);      ##繼續插入數據
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 |  32 |
|  6 | heiba    | hangzhou |  32 |
+----+----------+----------+-----+
6 rows in set (0.00 sec)

2,利用另一個終端查看是否成功插入

[root@master2 ~]# mysql -uroot -p         ##進入數據庫
Enter password:                                      ##輸入密碼

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

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> 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)

3,使用回滾,返回保存的節點

mysql> rollback to b;    ##利用回滾到保存節點b
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 |
|  5 | tianqi   | hangzhou |  32 |
+----+----------+----------+-----+
5 rows in set (0.00 sec)

mysql> rollback to a;  ##回滾到保存節點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;     ##回滾到初始,退出事務狀態
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)

4,使用commit提交事務

mysql> begin;    ##開啟事務
Query OK, 0 rows affected (0.00 sec)

mysql> insert into info (name,address,age) values ('heiba','hangzhou',32);  ##插入數據
Query OK, 1 row affected (0.00 sec)

mysql> commit;   ##提交事務
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 | heiba    | hangzhou |  32 |
+----+----------+----------+-----+
4 rows in set (0.00 sec)

5,使用另一個終端查看

mysql> select * from info;   ##查看表數據
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
|  3 | wangwu   | hangzhou |  30 |
|  7 | heiba    | hangzhou |  32 |
+----+----------+----------+-----+
4 rows in set (0.00 sec)

6,另一種事務的操作方式

mysql> set autocommit=0;   ##設置不自動提交事務
Query OK, 0 rows affected (0.00 sec)

mysql> update info set address='beijing' where name='heiba';  ##修改表數據
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from info;   ##查看表信息
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
|  3 | wangwu   | hangzhou |  30 |
|  7 | heiba    | beijing  |  32 |
+----+----------+----------+-----+
4 rows in set (0.00 sec)

##另一個終端查看
mysql> select * from info;  ##查看表信息,并沒有修改
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
|  3 | wangwu   | hangzhou |  30 |
|  7 | heiba    | hangzhou |  32 |
+----+----------+----------+-----+
4 rows in set (0.00 sec)

mysql> set autocommit=1;   ##開啟自動提交事務
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 | heiba    | beijing  |  32 |
+----+----------+----------+-----+
4 rows in set (0.00 sec)

存儲引擎MyLSAM和InnoDB

1,查看系統默認存儲引擎

mysql> show engines;   ##查看默認存儲引擎innodb
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |

mysql> show create table info;   ##查看創建的表的存儲引擎innodb

| 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=8 DEFAULT CHARSET=utf8

2,修改MySQL配置文件,設置默認的存儲引擎

[root@localhost ~]# vim /etc/my.cnf ##修改配置文件

[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysql.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
default-storage-engine=Myisam   ##添加默認存儲引擎為Myisam

[root@master2 ~]# systemctl restart mysqld.service ##重啟MySQL服務

3,進入數據庫

[root@master2 ~]# 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> create table a ( id int );    ##創建一個a表
Query OK, 0 rows affected (0.00 sec)

mysql> show create table a;   ##查看表默認的存儲引擎Myisam
+-------+-------------------------------------------------------------------------------------+
| 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> alter table a engine=innodb;  ##修改表的存儲引擎為innodb
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table a;  ##查看表的存儲引擎innodb
+-------+-------------------------------------------------------------------------------------+
| Table | Create Table                                                                        |
+-------+-------------------------------------------------------------------------------------+
| a     | CREATE TABLE "a" (
    "id" int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

新乡市| 兰西县| 普定县| 神农架林区| 辽宁省| 健康| 石城县| 宁河县| 当雄县| 苍溪县| 曲周县| 大安市| 遵义市| 屏边| 阜平县| 阿勒泰市| 伊金霍洛旗| 旺苍县| 德格县| 塔城市| 手游| 谢通门县| 延边| 昭平县| 古田县| 泰州市| 连山| 隆昌县| 迁安市| 林周县| 定陶县| 洮南市| 抚宁县| 邵阳县| 石柱| 东源县| 吴江市| 永善县| 顺平县| 保山市| 霍城县|