Prevent duplicate Insert records SQL implementation method

Source: Internet
Author: User
Tags mysql tutorial

Replace syntax

The syntax format for replace is:

1. Replace into table_name (col_name, ...) VALUES (...)

2. Replace into table_name (col_name, ...) Select ...

3. Replace into table_name set Col_name=value, ...

Algorithm Description:

The run of replace is similar to insert, but if the old record has the same value as the new record, the old record is deleted before the new record is inserted, namely:

1. Try inserting the new row into the table

2. When an insert fails because of a duplicate keyword error for a primary key or unique keyword:

Delete conflicting rows from a table that contain duplicate key values

Try inserting the new row into the table again

The old record has the same value as the new record: The table has a primary key or a unique index, otherwise, it doesn't make sense to use a replace statement.

The statement is the same as the insert, because no indexes are used to determine whether the new row replicates other rows.

return value:

NOT EXISTS

Example one: inserting more than one record
Suppose you have a clients table with a primary key of client_id, you can use the following statement:
Copy code code as follows:

INSERT INTO clients
(client_id, Client_name, Client_type)
Select supplier_id, Supplier_name, ' advertising '
From suppliers
Where NOT EXISTS (SELECT * from clients
where clients.client_id = suppliers.supplier_id);

Example one: inserting a single record
Copy code code as follows:

INSERT INTO clients
(client_id, Client_name, Client_type)
Select 10345, ' IBM ', ' advertising '
From dual
Where NOT EXISTS (SELECT * from clients
where clients.client_id = 10345);


On duplicate key update

As written above, you can also insert into ... followed by the on duplicate key update method.

If you specify on duplicate key update, and inserting a row causes duplicate values to occur in a unique index or primary key.

Executes the old row update. For example, if column A is defined as unique and contains a value of 1, the following two statements have the same effect:

MySQL tutorial >insert into table (A,B,C) VALUES (1,2,3)

->on duplicate key update c=c+1;

Mysql>update table set c=c+1 where a=1;

If the row is inserted as a new record, the value of the affected row is 1, and if the existing record is updated, the value of the affected row is 2.

NOTE: If column B is also a unique column, insert is equivalent to this UPDATE statement:

mysql> Update table set c=c+1 where a=1 or b=2 limit 1;


If the a=1 or b=2 matches more than one row, only one row is updated. In general, you should try to avoid using the on duplicate key clause on tables with multiple unique keywords.

You can use the values (col_name) function in the update clause to refer to the column value from the insert portion of the Insert...update statement.

In other words, if no duplicate keyword conflict occurs, values (col_name) in the update clause can reference the value of the inserted col_name. This function is especially useful for multiple rows of inserts. The values () function only makes sense in the Insert...update statement and returns null at other times.


mysql> insert into table (A,B,C) VALUES (1,2,3), (4,5,6)

-> on duplicate key update c=values (a) +values (b);

This statement acts the same as the following two statements:

mysql> insert into table (A,B,C) VALUES (1,2,3)

-> on duplicate key update c=3;

mysql> insert into table (A,B,C) VALUES (4,5,6)

-> on duplicate key update c=9;

When you use on duplicate key update, the delayed option is ignored.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.