#1062-尝试添加唯一键(MySQL)时,键'unique_id'的重复条目'' [英] #1062 - Duplicate entry '' for key 'unique_id' When Trying to add UNIQUE KEY (MySQL)

查看:533
本文介绍了#1062-尝试添加唯一键(MySQL)时,键'unique_id'的重复条目''的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试添加UNIQUE KEY时,MySQL出现错误.这就是我想要做的.我有一个名为"unique_id"的列,该列为VARCHAR(100).表上没有定义索引.我收到此错误:

I've got an error on MySQL while trying to add a UNIQUE KEY. Here's what I'm trying to do. I've got a column called 'unique_id' which is VARCHAR(100). There are no indexes defined on the table. I'm getting this error:

#1062 - Duplicate entry '' for key 'unique_id' 

当我尝试添加UNIQUE键时.这是我如何在phpMyAdmin中进行设置的屏幕截图:

When I try to add a UNIQUE key. Here is a screenshot of how I'm setting it up in phpMyAdmin:

这是由phpMyAdmin生成的MySQL查询:

Here is the MySQL query that's generate by phpMyAdmin:

ALTER TABLE  `wind_archive` ADD  `unique_id` VARCHAR( 100 ) NOT NULL FIRST ,
ADD UNIQUE (
`unique_id`
)

我过去曾遇到过此问题,但从未解决过,因此我只是从头开始重建表.不幸的是,在这种情况下我不能这样做,因为表中已经有很多条目.感谢您的帮助!

I've had this problem in the past and never resolved it so I just rebuilt the table from scratch. Unfortunately in this case I cannot do that as there are many entries in the table already. Thanks for your help!

推荐答案

错误说明了一切:

Duplicate entry '' 

因此,请运行以下查询:

So run the following query:

SELECT unique_id,COUNT(unique_id)
FROM yourtblname
GROUP BY unique_id
HAVING COUNT(unique_id) >1

此查询还会向您显示问题

This query will also show you the problem

SELECT *
FROM yourtblname
WHERE unique_id=''

这将向您显示在哪里有重复的值.您正在尝试在具有重复项的字段上创建唯一索引.您将需要先解决重复的数据,然后添加索引.

This will show you where there are values that have duplicates. You are trying to create a unique index on a field with duplicates. You will need to resolve the duplicate data first then add the index.

这篇关于#1062-尝试添加唯一键(MySQL)时,键'unique_id'的重复条目''的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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