Query ตาราง information_schema.tables ดูค่า AUTO_INCREMENT
ไวยากรณ์
SELECT AUTO_INCREMENT FROM information_schema.tables WHERE table_name = 'your_table' AND table_schema = 'your_db';
ตัวอย่างการใช้งาน
1. สร้างตารางและแทรก 3 แถว
mysql> CREATE TABLE DemoTable (Id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(Id)); mysql> INSERT INTO DemoTable VALUES (), (), ();
2. ดูค่า Auto Increment ถัดไป
mysql> SELECT AUTO_INCREMENT FROM information_schema.tables WHERE table_name = 'DemoTable';
ผลลัพธ์
+----------------+ | AUTO_INCREMENT | +----------------+ | 4 | +----------------+
คำสั่งอื่นที่เกี่ยวข้อง
-- ดูค่าปัจจุบัน (Last Insert ID) SELECT LAST_INSERT_ID(); -- เปลี่ยนค่า Auto Increment ถัดไป ALTER TABLE DemoTable AUTO_INCREMENT = 1000; -- ดูตัวแปรระบบ SHOW VARIABLES LIKE 'auto_inc%';
ข้อควรระวัง
AUTO_INCREMENTใน information_schema เป็นค่าถัดไปที่จะใช้ (Next Value)- InnoDB: ค่าไม่ Persistent หลัง Restart (MySQL < 8.0)
- MySQL 8.0+: Persistent Auto Increment (เก็บใน Redo Log)
- ไม่สามารถตั้งค่าต่ำกว่า MAX(id) ที่มีอยู่ได้