Home > Database > Mysql Tutorial > 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?

王林
Release: 2023-08-26 11:33:07
forward
603 people have browsed it

我们如何使用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(…)
Copy after login

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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template