Insert when the MySQL record does not exist
In MySQL, inserting a record is very simple, but in some special applications, before inserting a record, you need to check whether the record already exists. The insert operation is performed only when the record does not exist, this article describes the solution to this problem.
Q: I have created a table to store customer information. I know that the insert statement can be used to insert information into the table. But how can I ensure that duplicate records are not inserted?
Answer: You can use the exists condition to prevent duplicate records from being inserted.
Example 1: insert multiple records
Suppose there is a clients table with the primary key of client_id, you can use the following statement:
CopyCode The Code is 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 codeThe Code is 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 );
Using Dual as the table name allows you to directly keep up with the values of the fields to be inserted after the SELECT statement, even if these values do not exist in the current table.
Implementation of updating when the insert record exists when the MySQL record does not exist
copy Code the code is as follows: mysql> truncate '123456 ';
query OK, 0 rows affected (0.01 Sec)
mysql> select * From '000000';
empty set (200702 Sec)
mysql> insert into '000000' ('domain ', '2nd _ Domain', 'tld ', 'query _ NS1 ', 'query _ ns', 'Report _ date') values ('dnspod. com ', 'dnspod', 'com ', 1000,200 0, '2017-02-04') on duplicate key update 'query _ NS1 '= 'query _ NS1' + 2007, 'query _ rns' = 'query _ rns' + 2000;
query OK, 1 row affected (0.00 Sec)
Mysql> select * From '123 ';
+ ---- + ------------ + ------ + ----------- + ------------- +
| ID | domain | 2nd_domain | TLD | query_ns1 | query_ns| query_ns3 | query_ns4 | report_date |
+ ---- + ------------ + ------ + ----------- + ------------- +
| 1 | dnspod.com | dnspod | com | 1000 | 2000 | 0 | 0 |
+ ---- + ------------ + ------ + ----------- + ------------- +
1 row in SET (0.00 Sec)
Mysql> insert into '000000' ('domain ', '2nd _ Domain', 'tld ', 'query _ NS1', 'query _ ns', 'Report _ date ') values ('dnspod. com ', 'dnspod', 'com ', 1000,200 0, '2017-02-04') on duplicate key update 'query _ NS1 '= 'query _ NS1' + 2007, 'query _ rns' = 'query _ rns' + 2000;
Query OK, 2 rows affected (0.01 Sec)
Mysql> select * From '123 ';
+ ---- + ------------ + ------ + ----------- + ------------- +
| ID | domain | 2nd_domain | TLD | query_ns1 | query_ns| query_ns3 | query_ns4 | report_date |
+ ---- + ------------ + ------ + ----------- + ------------- +
| 1 | dnspod.com | dnspod | com | 2000 | 4000 | 0 | 0 |
+ ---- + ------------ + ------ + ----------- + ------------- +
1 row in SET (0.01 Sec)
Mysql>
Of course, when creating a table, never forget to create a unique for domain.
Unique key 'domain' ('domain ', 'Report _ date ')