避免在唯一约束条件下出现插入错误。

huangapple go评论55阅读模式
英文:

Avoid error on INSERT with UNIQUE CONSTRAINT

问题

I've altered my table by adding a UNIQUE CONSTRAINT to avoid inserting duplicate records, however now I have an issue with INSERT, when it encounters a duplicate in the table and throws an error, which breaks my code.

我已通过添加唯一约束来更改我的表,以避免插入重复记录,但现在我在INSERT时遇到了问题,当它遇到表中的重复项并抛出错误时,会导致我的代码中断。

Is there a way to modify the query to simply ignore the insert when a duplicate index is found and go to another insert, since I'm doing it via a loop for multiple values? Or do I need to add a SELECT query first to check if the duplicate exists, which seems pretty inefficient?

是否有一种方法可以修改查询,以在找到重复索引时简单地忽略插入并继续另一个插入,因为我正在通过循环插入多个值?还是我需要首先添加一个SELECT查询来检查重复是否存在,这似乎相当低效?

My current query looks like:

我的当前查询如下:

INSERT INTO t1 (user, email)
VALUES ('$row['user']', '$email')

英文:

I've altered my table by adding a UNIQUE CONSTRAINT to avoid inserting duplicate records, however now I have an issue with INSERT, when it encounters a duplicate in the table and throws an error, which breaks my code.

ALTER TABLE `t1` ADD UNIQUE `unique_index`(`user`, `email`);

Is there a way to modify the query to simply ignore the insert when a duplicate index is found and go to another insert, since I'm doing it via a loop for multiple values? Or do I need to add a SELECT query first to check if the duplicate exists, which seems pretty inefficient?

My current query looks like:

INSERT INTO t1 (`user`, `email`) 
VALUES ('".$row['user']."', '".$email."')

答案1

得分: 1

INSERT IGNORE 可以用来替代 INSERT。

INSERT IGNORE INTO t1 (`user`, `email`) 
VALUES ('".$row['user']."', '".$email."')

然而,使用 INSERT IGNORE 有时可能存在风险,因为它会忽略所有错误,而不仅仅是重复键错误。

一个更安全的替代方法是使用 INSERT ... ON DUPLICATE KEY UPDATE。虽然它主要用于在出现键冲突时执行对行的更新,但你可以让它什么都不做。

INSERT INTO t1 (`user`, `email`) 
VALUES ('".$row['user']."', '".$email."')
ON DUPLICATE KEY UPDATE `user`=`user`;

这个语句插入具有指定值的新行,如果某行导致重复键错误,则不执行任何操作,继续下一行。这是一个更安全的选项,因为它只忽略重复键错误。其他类型的错误仍然会中断语句并返回错误消息。

英文:

You can use INSERT IGNORE instead of INSERT.

INSERT IGNORE INTO t1 (`user`, `email`) 
VALUES ('".$row['user']."', '".$email."')

However, using INSERT IGNORE can sometimes be risky, as it ignores all other errors, not just the duplicate key error.

A safer alternative is to use INSERT ... ON DUPLICATE KEY UPDATE. Although it's primarily intended to perform an update to the row where a key conflict occurs, you can make it do nothing.

INSERT INTO t1 (`user`, `email`) 
VALUES ('".$row['user']."', '".$email."')
ON DUPLICATE KEY UPDATE `user`=`user`;

This statement inserts new rows with the specified values, or if a row causes a duplicate key error, it does nothing and moves on to the next row. It's a safer option because it ignores only duplicate key errors. Other types of errors still interrupt the statement and return an error message.

huangapple
  • 本文由 发表于 2023年6月6日 01:09:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定