Insert On Duplicate Key Update
INSERT INTO students (roll, name, marks) VALUES (12, 'Asha', 90) ON DUPLICATE KEY UPDATE marks = VALUES(marks); If roll 12 is new, insert. If PK/UNIQUE clashes, update marks. This is the usual upsert.
MySQL 8.0.20+ prefers aliases — AS new THEN marks = new.marks. VALUES(col) in the UPDATE clause is older style.
Trap: ON DUPLICATE KEY without understanding which UNIQUE key clashed — you might update the wrong kind of conflict (email vs roll).
On the example next to this theory: Insert On Duplicate Key Update — iNSERT adds rows. List columns explicitly for clarity.
students.id ← PK (unique)
▲
│ FK
marks.student_idExam tip
INSERT … ON DUPLICATE KEY UPDATE marks = 90;