When doing web site development, sometimes you need to restrict certain fields, and then return to the following requirements
When a field in the database is unique, when the same data is inserted again, the update operation
Syntax: REPLACE into tablename (column) values (values)
Let's take a look at some examples
1. Create a table
Mysql>create table T1 (id int primary KEY auto_increment,
Name varchar (32)
);
2. Create a unique index
Mysql>create UNIQUE INDEX nameindex on T1 (name);
3. Inserting test data
Mysql>insert into T1 (name) VALUES (' Test ');
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>
Mysql> select * from T1;
+----+------+
| ID | name |
+----+------+
| 1 | Test |
+----+------+
1 row in Set (0.00 sec)
4. Testing
mysql> replace into T1 (name) VALUES (' Test ');
Query OK, 2 rows Affected (0.00 sec)
Mysql> select * from T1;
+----+------+
| ID | name |
+----+------+
| 2 | Test |
+----+------+
1 row in Set (0.01 sec)
See the effect, when inserting new data, update the old data, if you want to ignore the new data, use ignore
Syntax INSERT IGNORE tablename (column) values (values)
This article is from the "Jeff" blog, so be sure to keep this source http://zhangxz.blog.51cto.com/5490116/1602720
MySQL avoids repeated insert recording methods