Insert a non-repeated record to MySQL to search for related articles: not exists MySQL before, my general practice is to first use PHP to query whether there are records. If yes, no operation is performed, if the statement does not exist, insert the statement. This ensures that no duplicate records exist in MySQL. In fact, the MySQL statement not exists can be used to detect the error.
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:
Code:
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
Code:
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.
This article is reproduced from
Http://www.21andy.com/blog/20061227/491.html
Article from: Lao Li's log. Source Address: http://www.dayanmei.com/blog.php/ID_660.htm
How to avoid repeated
Author: anonymous tips Source: Unknown clicks: 10 updated:
/>
How to avoid repeated
[Chinasoft network]
[Chinasoft network]
Programming Forum owner
The table eat contains two items: ID and foods. I set ID as the primary key. How can I set it to ensure that the values in foods are not repeated ??
[Chinasoft network]
[Chinasoft network]
One programmer at the programming Forum replied:
SQL code
Create unique index uk_eat on eat (foods );
==== Thoughts over skills ====
[Chinasoft network]
[Chinasoft network]
Two programmers at the programming Forum replied:
SQL code
Create Table eat
(
Id int auto_increment primary key,
Foods carchar (10) unique key
)
==== Thoughts over skills ====
[Chinasoft network]
[Chinasoft network]
In the programming Forum, three netizens replied:
As you have some unclosed stickers, I would like to introduce the method
If your problem has been solved, please post it in time to thank your friends for helping you.
Post completion method: Click [manage] in the upper-right or lower-right corner of the page. after entering the page, you can enter the password, separate points, and end the post.
Or refer:
Http://www.csdn.NET/help/over.ASP
Http://topic.csdn.NET/u/20080110/19/7cb462f1-cac6-4c28-848e-0a879f4fd642.HTML