Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

สร้างตาราง MySQL ด้วย MyISAM Engine

ใช้ ENGINE = MyISAM ตอน CREATE TABLE เพื่อระบุ Storage Engine

Syntax

CREATE TABLE table_name (
    col1 datatype,
    col2 datatype,
    ...
) ENGINE = MyISAM;

ตัวอย่าง

CREATE TABLE StudentRecordWithMyISAM (
    Id INT,
    StudentName VARCHAR(100),
    StudentAge INT
) ENGINE = MyISAM;

ตรวจสอบ Engine

1. SHOW TABLE STATUS

SHOW TABLE STATUS FROM business LIKE 'StudentRecordWithMyISAM';
+-------------------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+----------------+---------+
| 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 |
+-------------------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+----------------+---------+
| studentrecordwithmyisam | MyISAM | 10      | Dynamic    | 0    | 0              | 0           | 281474976710655 | 1024         | 0         | 1              | 2018-10-22 15:47:01 | 2018-10-22 15:47:02 | NULL       | utf8mb4_unicode_ci | NULL     |                |         |
+-------------------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+----------------+---------+

2. INFORMATION_SCHEMA

SELECT TABLE_NAME, ENGINE 
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_SCHEMA = 'business' AND ENGINE = 'MyISAM';

3. DESCRIBE (ไม่แสดง Engine)

DESC StudentRecordWithMyISAM;
+-------------+--------------+------+-----+---------+-------+
| Field       | Type         | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| Id          | int(11)      | YES  |     | NULL    |       |
| StudentName | varchar(100) | YES  |     | NULL    |       |
| StudentAge  | int(11)      | YES  |     | NULL    |       |
+-------------+--------------+------+-----+---------+-------+

⚠️ คำเตือน: MyISAM เป็น Legacy Engine

  • ❌ ไม่รองรับ Transactions (ACID)
  • ❌ Table-level Locking (Concurrency ต่ำ)
  • ❌ ไม่มี Foreign Key Enforcement
  • ❌ ไม่มี Crash Recovery (ต้อง REPAIR TABLE)
  • ❌ ไม่ได้รับ Feature ใหม่ (Deprecated)

เมื่อไหร่ควรใช้ MyISAM (กรณีพิเศษ)

  • Read-only / Archive Tables (Log, Audit, Historical Data ที่ไม่มี Write)
  • ต้องการ COUNT(*) ทันที (Metadata Counter)
  • Full-text Search บน Version เก่า (Pre-5.6)
  • Compressed Read-only Tables (myisampack)

แปลงเป็น InnoDB (แนะนำ)

ALTER TABLE StudentRecordWithMyISAM ENGINE = InnoDB;

Best Practice

Default ให้ใช้ InnoDB เสมอ เว้นแต่มีเหตุผลทางเทคนิคเฉพาะเจาะจงที่ต้องใช้ MyISAM และเข้าใจ Trade-offs ชัดเจน