MySQL avoids repeated insert recording methods

Source: Internet
Author: User

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

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.