Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use MySQL INSERT IGNORE statement

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

How to use MySQL INSERT IGNORE statement, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, there are people who need this can learn, I hope you can gain something.

When the IGNORE keyword is used in an INSERT statement, errors that occur during the execution of the INSERT statement are ignored. For example, inserting duplicate fields into fields with UNIQUE indexes or primary keys results in duplicate-key errors and the executed statement fails. When the IGNORE keyword is present, this error is ignored and only a warning is generated.

The IGNORE keyword has a similar effect when inserting data into a partitioned table. MySQL ignores errors when there are no partitions to insert.

When data conversion causes an error, the IGNORE keyword can be used to ignore the error, MySQL will generate a warning without exiting the execution of the statement, invalid values will be adjusted to the nearest value and inserted.

mysql> desc t10;

+-------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+-------------+------+-----+---------+-------+

| id | int(10) | NO | PRI | 0 | |

| name | varchar(15) | YES | | NULL | |

+-------+-------------+------+-----+---------+-------+

2 rows in set (0.00 sec)

mysql> select * from t10;

+----+--------+

| id | name |

+----+--------+

| 10 | Neo |

| 20 | Lucy |

| 30 | Lily |

| 50 | Dingyi |

| 60 | Jones |

+----+--------+

5 rows in set (0.00 sec)

mysql> desc t7;

+-------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+-------------+------+-----+---------+-------+

| id | int(10) | NO | PRI | 0 | |

| name | varchar(15) | YES | | NULL | |

+-------+-------------+------+-----+---------+-------+

2 rows in set (0.00 sec)

mysql> select * from t7;

+----+--------+

| id | name |

+----+--------+

| 50 | Dingyi |

| 60 | Jones |

+----+--------+

2 rows in set (0.00 sec)

mysql> insert into t7 select * from t10;

ERROR 1062 (23000): Duplicate entry '50' for key 'PRIMARY'

mysql> select * from t7;

+----+--------+

| id | name |

+----+--------+

| 50 | Dingyi |

| 60 | Jones |

+----+--------+

2 rows in set (0.00 sec)

mysql> select * from t10;

+----+--------+

| id | name |

+----+--------+

| 10 | Neo |

| 20 | Lucy |

| 30 | Lily |

| 50 | Dingyi |

| 60 | Jones |

+----+--------+

5 rows in set (0.00 sec)

mysql> insert ignore into t7 select * from t10;

Query OK, 3 rows affected, 1 warning (0.00 sec)

Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report