MySQL FAQs
FAQs Categories
Client Server Commands
Database Structure
Table Types or Storage Engines
Indexes
SQL Statements
Table Joins
Funtions and Operators
Tricky Select Queries
Speed Up Queries
Data Back Up
General Questions
Errors
1PLs Company - #1Payday.Loans Agency - Loans online and near me $100-$2500 (Same Day)
Powered by MySQL
 
Home / SQL Statements / Replace Statement

The REPLACE statement, like INSERT, add new records to a table. The two statements have very similar syntax. The primary difference between them lies in how they handle duplicate records. Also, REPLACE does not support the ON DUPLICATE KEY UPDATE clause.

If a table contains a unique-valued index and you attempt to insert a record containing a key value that already exists in the index, a duplicate-key violation occurs and the row is not inserted. What if you want the new record to take priority over the existing one? You could remove the existing record with DELETE and then use INSERT to add the new record. However, MySQL provides REPLACE as an alternative that is easier to use and is more efficient because it performs both actions with a single statement. REPLACE is like INSERT except that it deletes old records as necessary when a duplicate unique key value is present in a new record. Suppose that you're inserting a record into the people table, which has id as a PRIMARY KEY:

  • If the new record doesn't duplicate an existing id value, MySQL just inserts it.
  • If the new record does duplicate an existing id value, MySQL first deletes any old records containing that value before inserting the new record.

An advantage of using REPLACE instead of an equivalent DELETE (if needed) and INSERT is that REPLACE is performed as a single atomic operation. There's no need to do any explicit table locking as there might be were you to issue separate DELETE and INSERT statements.

The action of REPLACE in replacing rows with duplicate keys depends on the table having a unique-valued index:

  • In the absence of any such indexes, REPLACE is equivalent to INSERT because no duplicates will ever be detected.
  • Even in the presence of a unique-valued index, if an indexed column allows NULL values, it allows multiple NULL values. A new record with a NULL value in that column does not cause a duplicate-key violation and no replacement occurs.

REPLACE returns an information string that indicates how many rows it affected. If the count is one, the row was inserted without replacing an existing row. If the count is two, a row was deleted before the new row was inserted. If the count is greater than two, it means the table has multiple unique-valued indexes and the new record matched key values in multiple rows, resulting in multiple duplicate-key violations. This causes multiple rows to be deleted, a situation that's described in more detail later in this section.

REPLACE statement syntax is similar to that for INSERT. The following are each valid forms of REPLACE. They're analogous to examples shown earlier in the chapter for INSERT:

  • A single-record REPLACE with separate column and value lists:
    REPLACE INTO people (id,name,age) VALUES(12,'William',25);
  • A multiple-record REPLACE that inserts several rows:
    REPLACE INTO people (id,name,age) VALUES(12,'William',25),(13,'Bart',15),(14,'Mary',12);

    The rows-affected count for a multiple-row REPLACE often is greater than two because the statement may insert (and delete) several records in a single operation.

  • A single-record REPLACE with a SET clause that lists column assignments:
    REPLACE INTO people SET id = 12, name = 'William', age = 25;

If you want to replace the old record with the new one when a duplicate key occurs, use the REPLACE statement instead of INSERT.

CREATE TABLE multikey( a INT NOT NULL UNIQUE, b INT NOT NULL UNIQUE, c INT NOT NULL UNIQUE);

Suppose that the table has these contents:

mysql> SELECT * FROM multikey;
+---+---+---+
| a | b | c |
+---+---+---+
| 1 | 1 | 1 |
| 2 | 2 | 2 |
| 3 | 3 | 3 |
| 4 | 4 | 4 |
+---+---+---+

Using REPLACE to add a record that duplicates a row in each column causes several records to be replaced with the new row:

mysql> REPLACE INTO multikey (i,j,k) VALUES(1,2,3);

mysql> SELECT * FROM multikey;
+---+---+---+
| a | b | c |
+---+---+---+
| 1 | 2 | 3 |
| 4 | 4 | 4 |
+---+---+---+

The REPLACE statement reports a row count of four because it deletes three records and inserts one.

Replace Statement Related FAQs

About FAQs: Recently Added FAQs

About MySQL FAQs: Site Map | Bookmark Us | Recommend this Site to Your Friend | Contact Us

Useful Links: Wikipedia.org | Oracle.com | w3schools.com | www.php.net | Github.com

© 2023  www.mysqlfaqs.net
All rights reserved.