MYSQL在一行上忽略插入 [英] MYSQL Insert Ignore on one column
问题描述
我正在尝试使用insert ignore语句在数据库中放置一个名称.我不想创建重复项,但是,我使用的insert ignore语句会不断插入!
I am trying to use the insert ignore statement to put a name in a database. I do not want to create duplicates, however, the insert ignore statement I use keeps inserting!
声明是:
INSERT IGNORE INTO firstname(name) VALUES ('Tommy')
我的数据库显示以下内容:
My database shows this:
id | name
-------------
1 Tommy
2 Tommy
3 Tommy
一些帮助我的人的信息.我的ID是我的主键,并且会自动递增.
A couple bits of information for people helping me. My id is my primary key and it is auto incremented.
我的猜测是,由于新ID是自动创建的,因此会认为它是新结果!我不确定如何使用更大的SQL查询来解决这个问题,请检查名称Tommy是否首先存在
My guess is that because the new id is created automatically it thinks its a new result! I am not sure how to solve this expcept with a bigger SQL query checking if the name Tommy exists first
谢谢
推荐答案
这是因为firstname
表中的name
列是not unique
.尝试使用此DDL语句添加unique
约束,
It's because the column name
from firstname
table is not unique
. Try to add unique
constraint using this DDL statement,
ALTER TABLE firstname ADD UNIQUE (name);
然后尝试是否仍然添加重复的名称.希望它现在可以工作:)
then try if it still add duplicate name. Hope it works now :)
这篇关于MYSQL在一行上忽略插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!