"Go" MySQL data table record does not exist then insert, presence is updated

Source: Internet
Author: User
Tags tld

The MySQL record does not exist when inserting
In MySQL, inserting (insert) A record is simple, but some special applications, before inserting a record, need to check whether the record already exists, only if the record does not exist to perform the insert operation, this article describes the solution of this problem.

Question: I created a table to hold the customer information, I know I can insert the information into the table with the INSERT statement, but how can I guarantee that no duplicate records will be inserted?

Answer: You can prevent the insertion of duplicate records by using EXISTS conditional clauses.

Example one: inserting multiple records

Suppose you have a clients table with a primary key of client_id, you can use the following statement:

Copy CodeThe 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 one: Inserting 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 to make a table name allows you to follow the SELECT statement directly following the value of the field you want to insert, even if the value does not already exist in the current table.

The MySQL record does not exist when the insert record exists then the update implementation method

Copy CodeThe code is 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 ', ' 2007-02-04 ') on DUPLICATE KEY UPDATE ' query_ns1 ' = ' query_ns1 ' + +, ' 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 ', ' 2007-02-04 ') on DUPLICATE KEY UPDATE ' query_ns1 ' = ' query_ns1 ' + +, ' 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 you build a table, don't forget to give Domain a unique
UNIQUE KEY ' domain ' (' domain ', ' report_date ')

"Go" MySQL data table record does not exist then insert, presence is updated

Related Article

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.