1. 程式人生 > 實用技巧 >MySQL儲存引擎

MySQL儲存引擎

目錄

MySQL儲存引擎

完整的建表語句

CREATE TABLE `t_abc' (
		`id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

注意:在MySQL當中,凡是識別符號是可以使用“ ``  ”(飄號)括起來的。最好別用,不通用。

建表的時候可以指定儲存引擎,也可以指定字符集。

MySQL預設使用的儲存引擎是InnoDB方式。
預設採用的字符集是UTF8

什麼 是儲存引擎呢?

​ 儲存引擎這個名字只有在MySQL中存在。(Oracle中有對應的機制,但是不叫儲存引擎。Oracle中沒有特殊的名字,就是“表的儲存方式”)

​ MySQL支援很多儲存引擎,每一個儲存引擎都對應了一種不同的儲存方式。

​ 每一個儲存引擎都有自己的優缺點,需要在合適的時機選擇合適的儲存引擎。

檢視當前MySQL支援的儲存引擎?

show engines \G

不同版本的資料庫中,對應的儲存引擎不同,下面是MySQL5.5版本中的9個儲存引擎方式
*************************** 1. row ***************************
      Engine: FEDERATED
     Support: NO
     Comment: Federated MySQL storage engine
Transactions: NULL
          XA: NULL
  Savepoints: NULL
*************************** 2. row ***************************
      Engine: MRG_MYISAM
     Support: YES
     Comment: Collection of identical MyISAM tables
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 3. row ***************************
      Engine: MyISAM
     Support: YES
     Comment: MyISAM storage engine
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 4. row ***************************
      Engine: BLACKHOLE
     Support: YES
     Comment: /dev/null storage engine (anything you write to it disappears)
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 5. row ***************************
      Engine: CSV
     Support: YES
     Comment: CSV storage engine
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 6. row ***************************
      Engine: MEMORY
     Support: YES
     Comment: Hash based, stored in memory, useful for temporary tables
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 7. row ***************************
      Engine: ARCHIVE
     Support: YES
     Comment: Archive storage engine
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 8. row ***************************
      Engine: InnoDB
     Support: DEFAULT
     Comment: Supports transactions, row-level locking, and foreign keys
Transactions: YES
          XA: YES
  Savepoints: YES
*************************** 9. row ***************************
      Engine: PERFORMANCE_SCHEMA
     Support: YES
     Comment: Performance Schema
Transactions: NO
          XA: NO
  Savepoints: NO
9 rows in set (0.01 sec)

常見的儲存引擎

MyISAM

      Engine: MyISAM
     Support: YES
     Comment: MyISAM storage engine
Transactions: NO
          XA: NO
  Savepoints: NO
  
MyISM這種儲存引擎不支援事務。
MyISM是MySQL最常用的儲存引擎,但是這種引擎不是預設的。
  
它管理的表具有以下特徵:
	-使用三個檔案表示每個表:
	-格式檔案―儲存表結構的定義( mytable.frm)
	-資料檔案―儲存錶行的內容(mytable.MYD)
	-檔案―儲存表上索引(mytable.MYI)
	-靈活的AUTO_INCREMENT欄位處理
	-可被轉換為壓縮、只讀表來節省空間

InnoDB

      Engine: InnoDB
     Support: DEFAULT
     Comment: Supports transactions, row-level locking, and foreign keys
Transactions: YES
          XA: YES
  Savepoints: YES
  
  優點:支援事務、行級鎖、外來鍵。這種儲存引擎資料的安全得到保障。
  
  表的結構儲存在xxx.frm檔案中
  資料儲存在tablespace這樣的表空間中(邏輯概念),無法被壓縮,無法轉換成只讀。
  這種InnoDB儲存牽引器在MySQL資料庫崩潰之後提供自動恢復
  InnoDB支援級聯刪除和級聯更新

MEMORY

      Engine: MEMORY
     Support: YES
     Comment: Hash based, stored in memory, useful for temporary tables
Transactions: NO
          XA: NO
  Savepoints: NO
  
  缺點:不支援事務,資料容易丟失。因為所有資料和索引都是儲存在內容當中的。
  優點:查詢速度最快。
  以前叫做HEPA引擎。