Skip to content
Steve Buberl edited this page Nov 24, 2018 · 2 revisions

Insert or replace one or more rows into a table

REPLACE INTO tableName
  [ ( columnName[ ,... ] ) ]
     [ DEFAULT VALUES
      | VALUES ( columnValue [ ,... ] )[ ,... ]
      | SELECT ...
     ]
  ]
  | [SET columnName=columnValue[ ,... ]

REPLACE is just like INSERT except if a primary key violation occurs, REPLACE will replace the matching row(s) in the table rather returning an error like INSERT does.

Example:

This example shows primary key violation for an empty table students with the first columns its primary key.

REPLACE INTO students VALUES (1, 'John', 'Smith', 90210, 3.6, 'XL')
REPLACE INTO students VALUES (1, 'Jane', 'Doe', 12345, 4.0, 'S'))

The first REPLACE inserts the row normally like INSERT does. The second REPLACE will see the primary key violation and replace the row with John Smith with the new Jane Doe data

Clone this wiki locally