คำสั่ง SHOW TABLES
แสดงรายการตารางทั้งหมดใน Database ที่กำหนดหรือ Database ปัจจุบัน
รูปแบบคำสั่ง
SHOW TABLES [FROM db_name] [LIKE 'pattern' | WHERE expr];
ตัวอย่างการใช้งาน
แสดงตารางใน Database 'hb_student_tracker'
mysql> SHOW TABLES FROM hb_student_tracker;
ผลลัพธ์ (44 ตาราง):
+------------------------------+ | Tables_in_hb_student_tracker | +------------------------------+ | demotable192 | | demotable193 | | demotable194 | | demotable195 | | demotable196 | | demotable197 | | demotable198 | | demotable199 | | demotable200 | | demotable201 | | demotable202 | | demotable203 | | demotable204 | | demotable205 | | demotable206 | | demotable207 | | demotable208 | | demotable209 | | demotable210 | | demotable211 | | demotable212 | | demotable213 | | demotable214 | | demotable215 | | demotable216 | | demotable217 | | demotable218 | | demotable219 | | demotable220 | | demotable221 | | demotable223 | | demotable224 | | demotable225 | | demotable226 | | demotable227 | | demotable228 | | demotable229 | | demotable230 | | demotable231 | | demotable232 | | demotable233 | | insertcurrentdate | | reorderintegerexcept0 | | student | +------------------------------+ 44 rows in set (0.00 sec)
ตัวเลือกเพิ่มเติม
1. LIKE Pattern (Wildcard % _)
SHOW TABLES LIKE 'demo%'; -- เริ่มต้นด้วย demo SHOW TABLES LIKE '%_log'; -- ลงท้ายด้วย _log SHOW TABLES LIKE 'temp_%_%'; -- Pattern ซับซ้อน
2. WHERE Clause (MySQL 8.0+)
SHOW TABLES WHERE TABLE_TYPE = 'BASE TABLE'; SHOW TABLES WHERE Tables_in_mydb LIKE 'user_%';
3. SHOW FULL TABLES (แสดง Table Type)
mysql> SHOW FULL TABLES FROM hb_student_tracker;
ผลลัพธ์:
+------------------------------+------------+ | Tables_in_hb_student_tracker | Table_Type | +------------------------------+------------+ | demotable192 | BASE TABLE | | student | BASE TABLE | | some_view | VIEW | +------------------------------+------------+
คำสั่งเกี่ยวข้อง
| คำสั่ง | วัตถุประสงค์ |
|---|---|
| SHOW DATABASES | รายชื่อ Database ทั้งหมด |
| SHOW COLUMNS FROM table | โครงสร้างคอลัมน์ (เหมือน DESC) |
| SHOW CREATE TABLE table | DDL ที่สร้างตาราง |
| SHOW INDEX FROM table | รายการ Index |
| SHOW TABLE STATUS | สถิติตาราง (Rows, Size, Engine, AutoInc, etc.) |
Query ผ่าน INFORMATION_SCHEMA (Programmatic)
SELECT TABLE_NAME, TABLE_TYPE, ENGINE, TABLE_ROWS, CREATE_TIME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'hb_student_tracker' ORDER BY TABLE_NAME;
กรณีการใช้งานจริง
- Database Exploration: สำรวจ Schema ใหม่
- Migration Script: ตรวจสอบ Table มีอยู่ก่อน Create/Drop
- Dynamic SQL: สร้าง Query Loop ทุกตาราง
- Documentation: Generate Schema Doc อัตโนมัติ