# How can we use MySQL REPLACE statement to prevent of insertion of duplicate data?

MySQLMySQLi Database

We can use the REPLACE statement while inserting the data to prevent the insertion of duplicate data. If we will use the REPLACE command rather than the INSERT command then if the record is new, it is inserted just as with INSERT else if it is a duplicate, the new record replaces the old one.

## Syntax

REPLACE INTO table_name(…)

Here, table_name is the name of the table in which we want to insert the values.

## Example

In this example we will insert the data with the help of REPLACE statement as follows −

mysql> REPLACE INTO person_tbl (last_name, first_name)
-> VALUES( 'Ajay', 'Kumar');
Query OK, 1 row affected (0.00 sec)

mysql> REPLACE INTO person_tbl (last_name, first_name)
-> VALUES( 'Ajay', 'Kumar');
Query OK, 2 rows affected (0.00 sec)
Published on 22-Feb-2018 09:53:43