[NOIP2020]字串匹配
阿新 • • 發佈:2020-12-22
文章目錄
一、索引的概念
1.1 資料庫索引
- 是一個排序的列表,儲存著索引值和這個值所對應的實體地址
- 無需對整個表進行掃描,通過實體地址就可以找到所需資料
- 是表中一列或者若干列值排序的方法
- 需要額外的磁碟空間
1.2 索引的作用
- 資料庫利用各種快速定位技術,能夠大大加快查詢速率
- 當表很大或查詢涉及到多個表時,可以成千上萬倍的提高查詢速度
- 可以降低資料庫的IO成本,並且還可以資料庫的排序成本
- 通過建立唯一性索引保證資料表資料的唯一性
- 可以加快表與表之間的連線
- 在使用分組和排序時,可大大減少分組和排序時間
1.3 索引的分類
普通索引、唯一索引、主鍵索引、組合索引、全文索引
索引建立的方法:
1)、直接建立索引
2)、修改表結構方式新增索引
3)、建立表結構時建立索引
1.3.1 普通索引
1)直接建立
mysql> create index id on student(id);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index in school.student;
+---------+------------+ ----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| student | 0 | PRIMARY | 1 | studynum | A | 0 | NULL | NULL | | BTREE | | |
| student | 1 | id | 1 | id | A | 0 | NULL | NULL | YES | BTREE | | |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
2)建立表時建立
mysql> create table teacher(id int(2) not null primary key, name char(16),adress int(10),index id_index(id));
Query OK, 0 rows affected (0.01 sec)
mysql> show index in school.teacher;
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| teacher | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
| teacher | 1 | id_index | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
3)修改表結構方式建立
mysql> alter table student add index name_index(name);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index in school.student;
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| student | 0 | PRIMARY | 1 | studynum | A | 0 | NULL | NULL | | BTREE | | |
| student | 1 | id | 1 | id | A | 0 | NULL | NULL | YES | BTREE | | |
| student | 1 | name_index | 1 | name | A | 0 | NULL | NULL | YES | BTREE | | |
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)
1.3.2 唯一索引
- 與“普通索引”基本相同
- 與普通索引的區別是索引列的所有值只能出現一次,即必須唯一
- 建立唯一索引的方式
1)直接建立
mysql> create unique index adress on teacher(adress);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index in school.teacher;
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| teacher | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
| teacher | 0 | adress | 1 | adress | A | 0 | NULL | NULL | YES | BTREE | | |
| teacher | 1 | id_index | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2)建立表時建立
mysql> create table lisi(studynum char(10),name char(16) not null primary key,adress ,adress int(10),unique idexName(studynum));
Query OK, 0 rows affected (0.01 sec)
mysql> show index in school.lisi;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| lisi | 0 | PRIMARY | 1 | name | A | 0 | NULL | NULL | | BTREE | | |
| lisi | 0 | idexName | 1 | studynum | A | 0 | NULL | NULL | YES | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3)修改表結構建立
mysql> alter table lisi add unique index adress_index(adress);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index in school.lisi;
+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| lisi | 0 | PRIMARY | 1 | name | A | 0 | NULL | NULL | | BTREE | | |
| lisi | 0 | idexName | 1 | studynum | A | 0 | NULL | NULL | YES | BTREE | | |
| lisi | 0 | adress_index | 1 | adress | A | 0 | NULL | NULL | YES | BTREE | | |
+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1.3.3 主鍵索引
- 主鍵索引是一種特殊的唯一索引,指定為“PRIMARY KEY”
- 一個表只能有一個主鍵,不允許有空值
1)建立表時建立
mysql> create table zhangsan(studynum int(2) not null primary key,name char(48),adress 8),adress int(10));
Query OK, 0 rows affected (0.01 sec)
mysql> create table wangwu(studynum int(2) not null, name char(48),adress int(10),primary key(studynum));
Query OK, 0 rows affected (0.01 sec)
mysql> show index in school.wangwu;
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| wangwu | 0 | PRIMARY | 1 | studynum | A | 0 | NULL | NULL | | BTREE | | |
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2)修改表時建立
mysql> create table xiaoming(studynum int(2) not null,name char(48),adress int(10));
Query OK, 0 rows affected (0.01 sec) //建立一個新的表
mysql> alter table xiaoming add primary key(studynum); //修改表結構建立主鍵索引
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index in school.xiaoming;
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| xiaoming | 0 | PRIMARY | 1 | studynum | A | 0 | NULL | NULL | | BTREE | | |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1.3.4 組合索引
- 可以是單列上建立的索引,也可以是在多列上建立的索引
- 最左原則,從左往右依次執行
mysql> create table xiaohong(id int(2) not null, name char(48),adress int(10),index xiaohong(id,name,adress));
Query OK, 0 rows affected (0.01 sec)
mysql> show index in school.xiaohong;
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| xiaohong | 1 | xiaohong | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
| xiaohong | 1 | xiaohong | 2 | name | A | 0 | NULL | NULL | YES | BTREE | | |
| xiaohong | 1 | xiaohong | 3 | adress | A | 0 | NULL | NULL | YES | BTREE | | |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)
1.3.5 全文索引
- MySQL從3.23.23版開始支援全文索引和全文檢索
- 索引型別為FULLTEXT
- 可以在CHAR、VARCHAR或者TEXT型別的列上建立
1)建立表時建立全文索引
mysql> create table xiaobai(id int(2) not null,name char(48) primary key,adress int(10),fulltext (name));
Query OK, 0 rows affected (0.03 sec)
mysql> show index in school.xiaobai;
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| xiaobai | 0 | PRIMARY | 1 | name | A | 0 | NULL | NULL | | BTREE | | |
| xiaobai | 1 | name | 1 | name | NULL | 0 | NULL | NULL | | FULLTEXT | | |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2)在已存在的表上建立全文索引
mysql> create fulltext index name_index on xiaobai(name);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index in school.xiaobai;
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| xiaobai | 0 | PRIMARY | 1 | name | A | 0 | NULL | NULL | | BTREE | | |
| xiaobai | 1 | name | 1 | name | NULL | 0 | NULL | NULL | | FULLTEXT | | |
| xiaobai | 1 | name_index | 1 | name | NULL | 0 | NULL | NULL | | FULLTEXT | | |
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3)通過SQL語句alter table建立全文索引
mysql> alter table student add fulltext index studynum_index(studynum);
Query OK, 0 rows affected, 1 warning (0.36 sec)
Records: 0 Duplicates: 0 Warnings: 1
mysql> show index in school.student;
+---------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| student | 0 | PRIMARY | 1 | studynum | A | 0 | NULL | NULL | | BTREE | | |
| student | 1 | id | 1 | id | A | 0 | NULL | NULL | YES | BTREE | | |
| student | 1 | name_index | 1 | name | A | 0 | NULL | NULL | YES | BTREE | | |
| student | 1 | studynum_index | 1 | studynum | NULL | 0 | NULL | NULL | | FULLTEXT | | |
+---------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1.4 建立索引的原則依據
- 表的主鍵,外來鍵必須有索引
- 記錄數超過300行的表應該有索引
- 經常與其他表進行連線的表,在連線欄位上應該建立索引
- 唯一性太差的欄位不合適建立索引
- 更新太頻繁的欄位不適合建立索引
- 經常出現在where子句中的欄位,特別是大表的欄位,應該建立索引
- 索引應該建在選擇性高的欄位上
- 索引應該建在小欄位上,對於大的文字欄位甚至超長欄位,不要建索引
1.5 檢視索引語法
SHOW INDEX FROM table_name;
SHOW KEYS FROM table_name;
檢視索引的例項
show index in school.student;
+---------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| student | 0 | PRIMARY | 1 | studynum | A | 0 | NULL | NULL | | BTREE | | |
| student | 1 | id | 1 | id | A | 0 | NULL | NULL | YES | BTREE | | |
| student | 1 | name_index | 1 | name | A | 0 | NULL | NULL | YES | BTREE | | |
| student | 1 | studynum_index | 1 | studynum | NULL | 0 | NULL | NULL | | FULLTEXT | | |
+---------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1.6 刪除索引語法
DROP INDEX index_name ON table_name;
ALTER TABLE table_name DROP INDEX index_name;
刪除索引的示例:
mysql> drop index studynum_index on student;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index in school.student;
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| student | 0 | PRIMARY | 1 | studynum | A | 0 | NULL | NULL | | BTREE | | |
| student | 1 | id | 1 | id | A | 0 | NULL | NULL | YES | BTREE | | |
| student | 1 | name_index | 1 | name | A | 0 | NULL | NULL | YES | BTREE | | |
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
mysql> alter table student drop index id;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
show index in school.student;
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| student | 0 | PRIMARY | 1 | studynum | A | 0 | NULL | NULL | | BTREE | | |
| student | 1 | name_index | 1 | name | A | 0 | NULL | NULL | YES | BTREE | | |
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
二、事務
2.1 事務的概念
- 是一種機制、一個操作序列,包含了一組資料庫操作命令,並且把所有的命令作為一個整體一起向系統提交或撤銷操作請求,即這一組資料庫命令要麼都執行,要麼都不執行
- 是一個不可分割的工作邏輯單元,在資料庫系統上執行併發操作時,事務是最小的控制單元
- 適用於多使用者同時操作的資料庫系統的場景,如銀行、保險公司以及證券交易系統等等
- 通過事務的整體性保證資料的一致性
2.2 事務的ACID特點
- 原子性(Atomicity)
- 事務是一個完整的操作,事務的各元素是不可分的
- 事務中的所有元素必須作為一個整體提交或回滾
- 如果事務中的任何元素失敗,則整個事務將失敗
- 一致性(Consistency)
- 當事務完成時,資料必須處於一致狀態
- 在事務開始前,資料庫中儲存的資料處於一致狀態
- 在正在進行的事務中,資料可能處於不一致的狀態
- 當事務成功完成時,資料必須再次回到已知的一致狀態
- 隔離性(LsoLation)
- 對資料進行修改的所有併發事務是彼此隔離的,表明事務必須是獨立的,它不應以任何方式依賴於或影響其他事務
- 修改資料的事務可在另一個相同資料的事務開始之前訪問這些資料,或者在另一個使用相同資料的事務結束之後訪問這些資料
- 永續性(Durablilty)
- 指不管系統是否發生故障,事務處理的結果都是永久的
- 一旦事務被提交,事務的效果會被永久的保留在資料庫中
2.3 事務控制語句
- MySQL事務預設是自動提交的,當SQL語句提交時事務便自動提交
- 事務控制語句
- BEGIN或START TRANSACTION //開始一個事務
- COMMIT // 提交
- ROLLBACK // 回滾
- SAVEPOINT identifier // 建立儲存點
- RELEASE SAVEPOINT identifier //刪除儲存點
- ROLLBACK TO identifier // 回滾到指定儲存點
- SET TRANSACTION //設定事務的隔離級別
2.4 事務的控制方法
- 手動對事務進行控制的方法
- 事務處理命令控制事務
- bebin:開始一個事務
- commit:提交一個事務
- rollback:回滾一個事務
- 使用set命令進行控制
- set autocommit=0:禁止自動提交
- set autocommit=1:開啟自動提交
- 事務處理命令控制事務
mysql> begin; //開始一個事務
Query OK, 0 rows affected (0.00 sec)
mysql> insert into student value(1,'lisi',10);
Query OK, 1 row affected (0.00 sec)
mysql> insert into student value(2,'xiaobai',20);
Query OK, 1 row affected (0.00 sec)
mysql> savepoint a;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into student value(3,'xiaoming',30);
Query OK, 1 row affected (0.00 sec)
mysql> savepoint b;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from student;
+------+----------+----------+
| id | name | studynum |
+------+----------+----------+
| 1 | lisi | 10 |
| 2 | xiaobai | 20 |
| 3 | xiaoming | 30 |
+------+----------+----------+
3 rows in set (0.00 sec)
mysql> rollback to a; //回滾一個事務
Query OK, 0 rows affected (0.00 sec)
mysql> select * from student;
+------+---------+----------+
| id | name | studynum |
+------+---------+----------+
| 1 | lisi | 10 |
| 2 | xiaobai | 20 |
+------+---------+----------+
2 rows in set (0.00 sec)
mysql> rollback to b; //回滾一個事務b,但是隻能向前回滾,無法向後回滾,所以會出現報錯
ERROR 1305 (42000): SAVEPOINT b does not exist
mysql> commit; //結束一個事務
Query OK, 0 rows affected (0.01 sec)
三、儲存引擎
3.1 儲存引擎的概念介紹
- MySQL中的資料用各種不同的技術儲存在檔案中,每一種技術都使用不同的儲存機制、索引技巧、鎖定水平並最終提供不同的功能和能力,這些不同的技術以及配套的功能在MySQL中稱為儲存引擎
- 儲存引擎是MySQL將資料儲存在檔案系統中的儲存方式或者儲存格式
- MySQL常用的儲存引擎
- MyISAM
- InnoDB
- MySQL資料庫中的元件,負責執行實際的資料I/O操作
- MySQL系統中,儲存引擎處於檔案系統之上,在資料儲存到資料檔案之前會傳輸到儲存引擎,之後按照各個儲存引擎的儲存格式進行儲存
3.2 MyISAM的介紹
- MyISAM不支援事務,也不支援外來鍵
- 訪問速度快
- 對書屋完整性沒有要求
- MyISAM在磁碟上儲存成三個檔案
- .frm檔案儲存表定義
- 資料檔案的副檔名為.MYD(MYData)
- 索引檔案的副檔名為.MYI(MYIndex)
- 表級鎖定形式,資料在更新時鎖定整個表
- 資料庫在讀寫過程中相互阻塞
- 會在資料寫入的過程阻塞使用者資料的讀取
- 也會在資料讀取的過程中阻塞使用者資料的寫入
- 資料單獨寫入或讀取,速度過程較快且佔用資源相對少
3.3 - MyISAM支援的儲存格式
- 靜態表
- 動態表
- 壓縮表
3.4 MyISAM適用的生產場景舉例
- 公司業務不需要事務的支援
- 單方面讀取或寫入資料比較多的業務
- MyISAM儲存引擎資料讀寫都比較頻繁場景不適合
- 使用讀寫併發訪問相對較低的業務
- 資料修改相對較少的業務
- 對資料業務一致性要求不是非常高的業務
- 伺服器硬體資源相對比較差
3.5 InonDB特點介紹
- 支援4個事務隔離級別
- 行級鎖定,但是全表掃描仍然會是表級鎖定
- 讀寫阻塞與事務隔離級別相關
- 能非常搞笑的快取索引和資料
- 表與主鍵以簇的方式儲存
- 支援分割槽、表空間、類似Oracle資料庫
- 支援外來鍵的約束,5.5前不支援全文索引,5.5後支援全文索引
- 對硬體資源要求還是比較高的場合
3.6 InnoDB使用生產場景分析
- 業務需要事務的支援
- 行級鎖定對高併發有很好的適應能力,但需確保查詢是通過索引來完成
- 業務資料更新較為頻繁的場景
如:論壇、微博等 - 業務資料一致性要求較高
如:銀行業務 - 硬體裝置記憶體較大(因為事務都先放記憶體),利用innodb較好的快取能力來提高記憶體利用率,減少磁碟IO的壓力
3.7 企業選擇儲存引擎依據
- 需要考慮每個儲存引擎提供的核心功能及應用場景
- 支援的欄位和資料型別
所有引擎都支援通用的資料型別
但不是所有的引擎都支援其它的欄位型別,如二進位制物件 - 鎖定型別:不同的儲存引擎支援不同級別的鎖定
表鎖定
行鎖定
3.8 修改儲存引擎
3.8.1 alter table 修改
未修改之前:
mysql> mysql> show table status from school;
+----------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+
| Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment |
+----------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+
| lisi | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 16384 | 0 | NULL | 2020-12-24 14:58:24 | NULL | NULL | utf8_general_ci | NULL | | |
| student | InnoDB | 10 | Dynamic | 3 | 5461 | 16384 | 0 | 32768 | 0 | NULL | 2020-12-24 15:28:53 | 2020-12-24 15:42:43 | NULL | utf8_general_ci | NULL | | |
| teacher | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 16384 | 0 | NULL | 2020-12-24 14:48:41 | NULL | NULL | utf8_general_ci | NULL | | |
| wangwu | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 0 | 0 | NULL | 2020-12-24 15:03:05 | NULL | NULL | utf8_general_ci | NULL | | |
| xiaobai | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 32768 | 0 | NULL | 2020-12-24 15:19:51 | NULL | NULL | utf8_general_ci | NULL | | |
| xiaohong | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 16384 | 0 | NULL | 2020-12-24 15:12:05 | NULL | NULL | utf8_general_ci | NULL | | |
| xiaoming | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 0 | 0 | NULL | 2020-12-24 15:08:15 | NULL | NULL | utf8_general_ci | NULL | | |
| zhangsan | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 0 | 0 | NULL | 2020-12-24 15:01:55 | NULL | NULL | utf8_general_ci | NULL | | |
+----------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+
修改zhangshan儲存引擎為MyISAM
mysql> alter table zhangsan engine=myisam;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
修改之後:
mysql> show table status from school;
+----------+--------+---------+------------+------+----------------+-------------+-------------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+
| Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment |
+----------+--------+---------+------------+------+----------------+-------------+-------------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+
| lisi | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 16384 | 0 | NULL | 2020-12-24 14:58:24 | NULL | NULL | utf8_general_ci | NULL | | |
| student | InnoDB | 10 | Dynamic | 3 | 5461 | 16384 | 0 | 32768 | 0 | NULL | 2020-12-24 15:28:53 | 2020-12-24 15:42:43 | NULL | utf8_general_ci | NULL | | |
| teacher | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 16384 | 0 | NULL | 2020-12-24 14:48:41 | NULL | NULL | utf8_general_ci | NULL | | |
| wangwu | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 0 | 0 | NULL | 2020-12-24 15:03:05 | NULL | NULL | utf8_general_ci | NULL | | |
| xiaobai | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 32768 | 0 | NULL | 2020-12-24 15:19:51 | NULL | NULL | utf8_general_ci | NULL | | |
| xiaohong | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 16384 | 0 | NULL | 2020-12-24 15:12:05 | NULL | NULL | utf8_general_ci | NULL | | |
| xiaoming | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 0 | 0 | NULL | 2020-12-24 15:08:15 | NULL | NULL | utf8_general_ci | NULL | | |
| zhangsan | MyISAM | 10 | Fixed | 0 | 0 | 0 | 43065671436730367 | 1024 | 0 | NULL | 2020-12-24 15:52:32 | 2020-12-24 15:52:32 | NULL | utf8_general_ci | NULL | | |
+----------+--------+---------+------------+------+----------------+-------------+-------------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+
8 rows in set (0.00 sec)
3.8.2 修改my.cnf,指定預設儲存引擎並重啟服務
在/etc/my.cfg配置檔案棕修改[mysqld],下面新增default-storage-engine=InnoDB,然後重啟服務
3.8.3 create table建立表時指定儲存引擎
mysql> create table xusi(id int(2) not null primary key,name char(48),adress int(10)) engine=myisam;
Query OK, 0 rows affected (0.00 sec)
mysql> show table status from school;
+----------+--------+---------+------------+------+----------------+-------------+-------------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+
| Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment |
+----------+--------+---------+------------+------+----------------+-------------+-------------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+
| lisi | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 16384 | 0 | NULL | 2020-12-24 14:58:24 | NULL | NULL | utf8_general_ci | NULL | | |
| student | InnoDB | 10 | Dynamic | 3 | 5461 | 16384 | 0 | 32768 | 0 | NULL | 2020-12-24 15:28:53 | 2020-12-24 15:42:43 | NULL | utf8_general_ci | NULL | | |
| teacher | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 16384 | 0 | NULL | 2020-12-24 14:48:41 | NULL | NULL | utf8_general_ci | NULL | | |
| wangwu | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 0 | 0 | NULL | 2020-12-24 15:03:05 | NULL | NULL | utf8_general_ci | NULL | | |
| xiaobai | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 32768 | 0 | NULL | 2020-12-24 15:19:51 | NULL | NULL | utf8_general_ci | NULL | | |
| xiaohong | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 16384 | 0 | NULL | 2020-12-24 15:12:05 | NULL | NULL | utf8_general_ci | NULL | | |
| xiaoming | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 0 | 0 | NULL | 2020-12-24 15:08:15 | NULL | NULL | utf8_general_ci | NULL | | |
| xusi | MyISAM | 10 | Fixed | 0 | 0 | 0 | 43065671436730367 | 1024 | 0 | NULL | 2020-12-24 15:55:45 | 2020-12-24 15:55:45 | NULL | utf8_general_ci | NULL | | |
| zhangsan | MyISAM | 10 | Fixed | 0 | 0 | 0 | 43065671436730367 | 1024 | 0 | NULL | 2020-12-24 15:52:32 | 2020-12-24 15:52:32 | NULL | utf8_general_ci | NULL | | |
+----------+--------+---------+------------+------+----------------+-------------+-------------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+
9 rows in set (0.00 sec)