Name

REPLACE

Synopsis

REPLACE INTO table [( column, ...)] VALUES ( value , ...)
REPLACE INTO table [(column, ...)] SELECT select_clause

Inserts data to a table, replacing any old data that conflicts. This statement is identical to INSERT except that if a value conflicts with an existing unique key, the new value replaces the old one. The first form of this statement simply inserts the given values into the given columns. Columns in the table that are not given values are set to their default value or NULL. The second form takes the results of a SELECT query and inserts them into the table.

Examples

# Insert a record into the 'people' table.
REPLACE INTO people ( name, rank, serial_number ) VALUES ( 'Bob Smith',
    'Captain', 12345 )
# Copy all records from 'data' that are older than a certain date into
# 'old_data'. This would usually be followed by deleting the old data from
# 'data'.
REPLACE INTO old_data ( id, date, field ) SELECT ( id, date, field) FROM data
    WHERE date < 87459300

Get MySQL and mSQL now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.