REPLACE INTO

Saves data to a table, overwriting the rows based on the primary key. If the given primary key is missing, a new row is added to the table. If the given PRIMARY_KEY exists, the row is overwritten. The values of columns not involved in the operation are replaced by their default values.

Note

Unlike INSERT INTO and UPDATE, the queries UPSERT INTO and REPLACE INTO don’t need to pre-fetch the data, hence they run faster.

  • Setting values for REPLACE INTO using VALUES.

    Example

    1. REPLACE INTO my_table (Key1, Key2, Value2) VALUES
    2. (1u, "One", 101),
    3. (2u, "Two", 102);
    4. COMMIT;

    REPLACE - 图1

  • Fetching values for REPLACE INTO using a SELECT.

    Example

    1. REPLACE INTO my_table
    2. SELECT Key AS Key1, "Empty" AS Key2, Value AS Value1
    3. FROM my_table1;
    4. COMMIT;

    REPLACE - 图2