
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Use MySQL REPLACE Statement to Prevent Duplicate Data Insertion
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)
Advertisements