
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can we use MySQL REPLACE statement to prevent of insertion of duplicate data?
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)
- Related Articles
- How can we use SIGNAL statement with MySQL triggers?
- How to use REPLACE() function with column’s data of MySQL table?
- How can we use a MySQL subquery with INSERT statement?
- How to prevent duplicate INSERT in MySQL?
- How can we modify a MySQL view with CREATE OR REPLACE VIEW statement?
- How can we use MySQL SELECT statement to count number of rows in a table?
- Can we use WHERE clause inside MySQL CASE statement?
- How to prevent MySQL double insert (duplicate entry)?
- How to prevent duplicate rows in MySQL INSERT?
- How can I use MySQL replace() to replace strings in multiple records?
- Can we use SELECT NULL statement in a MySQL query?
- Can we use ADD and CHANGE with ALTER Statement in MySQL?
- How can we use SET statement to assign a SELECT result to a MySQL user variable?
- How can we MySQL LOAD DATA INFILE statement with ‘ENCLOSED BY’ option to import data from text file into MySQL table?
- How can we use MySQL POWER() function with the column’s data values?

Advertisements