Site icon TestingDocs.com

MySQL REPLACE Statement

Overview

In this tutorial, we will learn about MySQL REPLACE statement. The REPLACE statement is a MySQL extension to the SQL standard. The statement either inserts or deletes and inserts rows.

REPLACE works like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or UNIQUE constraint, the old row is deleted before the new row is inserted.

The INSERT statement fails because a duplicate-key error occurs for a primary key or unique constraint if the column has the same value for new row and existing old row.

Syntax

The REPLACE statement uses the following syntax:

mysql> REPLACE INTO table_name (column_list) VALUES(values_list)

The values for all the columns are taken from the values specified in the REPLACE statement. Any missing columns are set to their default values, just like in the INSERT statement. We cannot refer to values from the current row and use them in the new row.

Examples

In this example, we will replace a current row of data in the Country table from the world MySQL database.

mysql> DESC City;
+————-+———-+——+—–+———+—————-+
| Field | Type | Null | Key | Default | Extra |
+————-+———-+——+—–+———+—————-+
| ID | int | NO | PRI | NULL | auto_increment |
| Name | char(35) | NO | | | |
| CountryCode | char(3) | NO | MUL | | |
| District | char(20) | NO | | | |
| Population | int | NO | | 0 | |
+————-+———-+——+—–+———+—————-+

 

Note that unless the table has a PRIMARY KEY or a UNIQUE constraint, using a REPLACE statement makes no sense. It becomes equivalent to INSERT, because there is no index to be used to determine whether a new row is duplicate to another existing row. The primary key for the Country table is ID.

For example, the current row that we replace in the table is shown below. We will change the value of the Population for the New York City to 8009000 using the REPLACE statement.

mysql> SELECT * FROM City WHERE Name=’New York’;
+——+———-+————-+———-+————+
| ID | Name | CountryCode | District | Population |
+——+———-+————-+———-+————+
| 3793 | New York | USA | New York | 8008278 |
+——+———-+————-+———-+————+

 

 

mysql> REPLACE INTO City VALUES(3793,’New York’,’USA’,’New York’,8009000);
Query OK, 2 rows affected (0.01 sec)

The REPLACE statement returns a count to indicate the number of rows affected.

Rows Affected = Sum of the rows deleted and inserted.

If the count is 1 for single-row REPLACE, a row was inserted and no rows were deleted. If the count is greater then 1, one or more old rows were deleted before the new row was inserted.

mysql> SELECT * FROM City WHERE Name=’New York’;
+——+———-+————-+———-+————+
| ID | Name | CountryCode | District | Population |
+——+———-+————-+———-+————+
| 3793 | New York | USA | New York | 8009000 |
+——+———-+————-+———-+————+
1 row in set (0.00 sec)

Note that when a table has more than one UNIQUE or PRIMARY KEY constraint, replacing one row can actually lead to the deletion of several rows, if there are conflicts on more than one constraint.

MySQL Tutorials

MySQL Tutorials on this website:

https://www.testingdocs.com/mysql-tutorials-for-beginners/

For more information on MySQL Database:

https://www.mysql.com/

Exit mobile version