REPLACE
statement updates the listed columns if primary key (one of the listed columns) already exists. Otherwise inserts as a new record
REPLACE INTO students (id, first_name, points)
VALUES (41, 'ილიკო', 147)
Similar to the
REPLACE INTO
statement is
ON DUPLICATE KEY
.
The only difference is that on duplicate key it updates only the listed columns but omitted values stays the same
INSERT INTO students (id, first_name, points)
VALUES (41, 'გიორგი', 149)
ON DUPLICATE KEY
UPDATE first_name = 'გიორგი', points = 123
Note 1
: If primary key is not listed, it will insert the record, but if the primary key is one of the listed columns, it will update the specified row that matches the primary key.
Note 2
: If the primary key exists, all the other omitted columns will get the default values after updating the record.