Insert when MySQL record does not exist
In MySQL, inserting (insert) A record is very simple, but some special applications, before inserting the record, need to check whether this record already exists, only if the record does not exist, the insert operation, this article describes the solution to this problem.
Question: I created a table to hold the customer information, and I know that you can insert the information into the table with the INSERT statement, but how do you guarantee that you don't insert duplicate records?
Answer: You can prevent the insertion of duplicate records by using EXISTS conditional sentences.
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);
Using dual as a table name allows you to follow directly to the value of the field you want to insert after the SELECT statement, even if the values do not yet exist in the current table.
when the MySQL record is not present, the insert record exists the implementation method of the update
Copy Code code as follows:
mysql> truncate ' 200702 ';
Query OK, 0 rows affected (0.01 sec)
Mysql> select * from ' 200702 ';
Empty Set (0.01 sec) <span id= "More-22" ></span>
mysql> insert INTO ' 200702 ' (' Domain ', ' 2nd_domain ', ' TLD ', ' query_ns1 ', ' query_ns2 ', ' report_date ') VALUES (' DNSPOD.C Om ', ' dnspod ', ' com ', 1000, Watts, ' 2007-02-04 ') on DUPLICATE KEY UPDATE ' query_ns1 ' = ' query_ns1 ' + 1000, ' query_ns2 ' = ' q Uery_ns2 ' + 2000;
Query OK, 1 row Affected (0.00 sec)
Mysql> select * from ' 200702 ';
+----+------------+------------+------+-----------+-----------+-----------+-----------+-------------+
| ID | Domain | 2nd_domain | TLD | query_ns1 | Query_ns2 | QUERY_NS3 | QUERY_NS4 | Report_date |
+----+------------+------------+------+-----------+-----------+-----------+-----------+-------------+
| 1 | dnspod.com | Dnspod | com | 1000 | 2000 | 0 | 0 | 2007-02-04 |
+----+------------+------------+------+-----------+-----------+-----------+-----------+-------------+
1 row in Set (0.00 sec)
mysql> insert INTO ' 200702 ' (' Domain ', ' 2nd_domain ', ' TLD ', ' query_ns1 ', ' query_ns2 ', ' report_date ') VALUES (' DNSPOD.C Om ', ' dnspod ', ' com ', 1000, Watts, ' 2007-02-04 ') on DUPLICATE KEY UPDATE ' query_ns1 ' = ' query_ns1 ' + 1000, ' query_ns2 ' = ' q Uery_ns2 ' + 2000;
Query OK, 2 rows affected (0.01 sec)
Mysql> select * from ' 200702 ';
+----+------------+------------+------+-----------+-----------+-----------+-----------+-------------+
| ID | Domain | 2nd_domain | TLD | query_ns1 | Query_ns2 | QUERY_NS3 | QUERY_NS4 | Report_date |
+----+------------+------------+------+-----------+-----------+-----------+-----------+-------------+
| 1 | dnspod.com | Dnspod | com | 2000 | 4000 | 0 | 0 | 2007-02-04 |
+----+------------+------------+------+-----------+-----------+-----------+-----------+-------------+
1 row in Set (0.01 sec)
Mysql>
Of course, when building a table, don't forget to give Domain a unique
UNIQUE KEY ' domain ' (' domain ', ' report_date ')