Home  >  Article  >  Database  >  How can we use MySQL REPLACE statement to prevent duplicate data from being inserted?

How can we use MySQL REPLACE statement to prevent duplicate data from being inserted?

王林
王林forward
2023-08-26 11:33:07515browse

我们如何使用MySQL REPLACE语句来防止插入重复数据?

#We can use the REPLACE statement when inserting data to prevent inserting duplicate data. If we use REPLACE command instead of INSERT command then if the record is new it will be inserted like INSERT else if it is duplicate the new record will replace the old record. p>

Syntax

REPLACE INTO table_name(…)

Here, table_name is the name of the table into which we want to insert the value.

Example

In this example, we will insert 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)

The above is the detailed content of How can we use MySQL REPLACE statement to prevent duplicate data from being inserted?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete