คำอธิบาย
MySQL UPDATE จะ ไม่ เขียนทับ (Overwrite) ค่าที่เหมือนเดิม ทำให้ Rows affected = 0 แม้จะ Match แถวได้
ขั้นตอนการสร้างตารางและข้อมูลตัวอย่าง
mysql> create table DemoTable (
StudentId int,
StudentMathMarks int,
StudentMySQLMarks int );
Query OK, 0 rows affected (0.46 sec)
การเพิ่มข้อมูล
mysql> insert into DemoTable values(1, 56, 78); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(2, 88, 99); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(3, 34, 98); Query OK, 1 row affected (0.13 sec)
แสดงข้อมูลทั้งหมด
mysql> select * from DemoTable;
ผลลัพธ์:
+-----------+------------------+-------------------+ | StudentId | StudentMathMarks | StudentMySQLMarks | +-----------+------------------+-------------------+ | 1 | 56 | 78 | | 2 | 88 | 99 | | 3 | 34 | 98 | +-----------+------------------+-------------------+ 3 rows in set (0.00 sec)
คิวรี่ UPDATE ด้วยค่าเดิมซ้ำ
mysql> update DemoTable set StudentId = 2, StudentMathMarks = 88, StudentMySQLMarks = 99 where StudentId = 2; Query OK, 0 rows affected (0.00 sec) Rows matched: 1 Changed: 0 Warnings: 0
คำอธิบายผลลัพธ์
- Rows matched: 1: พบ 1 แถวที่ตรงเงื่อนไข WHERE
- Changed: 0: ไม่มีแถวไหนถูกเปลี่ยนแปลง เพราะค่าใหม่เท่ากับค่าเดิมทุกคอลัมน์
- 0 rows affected: MySQL ไม่นับเป็น "Affected" หากค่าไม่เปลี่ยน
ประโยชน์ของพฤติกรรมนี้
- ลด I/O เขียนดิสก์ (ไม่เขียนข้อมูลซ้ำ)
- ไม่ Trigger ON UPDATE CURRENT_TIMESTAMP (ถ้ามี)
- ไม่เขียน Binary Log (สำหรับ Replication)
- ประหยัด Lock time
การทดสอบ: บังคับให้อัปเดตแม้ค่าเดิม
ไม่สามารถบังคับได้โดยตรง แต่สามารถใช้ REPLACE INTO หรือ DELETE + INSERT แทน (ไม่แนะนำ)