覆盖唯一索引值 [英] Overriding unique indexed values

查看:109
本文介绍了覆盖唯一索引值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我现在正在做的(nameUNIQUE):

This is what I'm doing right now (name is UNIQUE):

SELECT * FROM fruits WHERE name='apple';

检查查询是否返回了任何内容 结果.如果是,则什么也不要做.如果 不,必须插入一个新值:

Check if the query returned any result. If yes, don't do anything. If no, a new value has to be inserted:

INSERT INTO fruits (name) VALUES ('apple');

可以将上面的值插入表中而不检查它是否已经存在,而不是上面的方法吗?如果表中已经存在该名称,则将引发错误,如果不存在,则将插入新记录.

Instead of the above is it ok to insert the value into the table without checking if it already exists? If the name already exists in the table, an error will be thrown and if it doesn't, a new record will be inserted.

现在,我必须在for循环中插入500条记录,这将导致1000条查询.跳过已经存在"检查是否可以?

Right now I am having to insert 500 records in a for loop, which results in 1000 queries. Will it be ok to skip the "already-exists" check?

推荐答案

您可以使用IGNORE功能:

You can use the IGNORE feature:

INSERT IGNORE INTO fruits VALUES ('apple')

如果有按键冲突,它将跳过该值

If there is a key violation, it just skips this value

这篇关于覆盖唯一索引值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆