SQL implementation to prevent repeated insert records

Source: Internet
Author: User

Replace syntax

The syntax format of 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 replace operation is similar to the insert operation. However, if the old record has the same value as the new record, the old record is deleted before the new record is inserted, that is:

1. Try to Insert a new row into the table

2. When insertion fails due to a duplicate keyword error for the primary key or unique Keyword:

Delete conflicting rows with duplicate keyword values from the table

Try to Insert a new row into the table again

The criteria for determining the values of the old record and the new record are as follows: The table has a primary key or unique index. Otherwise, using a replace statement is meaningless.

This statement is the same as insert, because no index is used to determine whether other rows have been copied in the new row.

Return Value:

Not exists

Example 1: insert multiple records
Suppose there is a clients table with the primary key of client_id, you can use the following statement:
Copy the 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 1: Insert a single record
Copy the 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

For example‍You can also add the on duplicate key update method to insert.

 

If you specify on duplicate key update and insert a row, duplicate values will appear in a unique index or primary key,

The old row is updated. 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 a row is inserted as a new record, the value of the affected row is 1. If the original 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 a = 1 or B = 2 matches multiple rows, only one row is updated. Generally, you should avoid using the on duplicate key clause for tables with multiple unique keywords.

You can use the values (col_name) function in the update clause to reference the column values from the insert section of the insert... update statement.

In other words, if there is no duplicate keyword conflict, the values (col_name) in the update clause can reference the value of the inserted col_name. This function is particularly applicable to multiline inserts. The values () function only makes sense in the insert... update statement. Otherwise, null is returned.


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 serves the same purpose 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.