错误:键“电子邮件"的重复条目“" [英] Error: Duplicate entry '' for key 'email'

查看:100
本文介绍了错误:键“电子邮件"的重复条目“"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前在运行php脚本的人中看到过此错误,但这是我在phpmyadmin中发生的事情?

I have seen this error with people running php scripts before but this is happending to me in phpmyadmin ??

Error
SQL query:

UPDATE  `cl56-goldeng`.`users` SET `email` =  '' WHERE  `users`.`id` =118

MySQL said: Documentation

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

如果我给该字段另一个值,它可以正常工作,但是如果我清除该字段并按Enter,则会出现上述错误.

It works fine if I give the field another value, but if I clear the field and press enter I get the above error.

表本身看起来像这样:

The table itself looks like this :

推荐答案

在表cl56-goldeng.users上,创建时指定了字段email,不允许在其中输入相同值以上的1个值.这是通过在MySQL中创建表时使用UNIQUE标识符完成的. 您可以在此链接上找到有关UNIQUE标识符的更多信息.

On your table cl56-goldeng.users, the field email was specified on creation to not allow more than 1 of the same value to be allowed into it. This is done using the UNIQUE identifier on table creation in MySQL. You can see more on the UNIQUE identifier at this link.

您可以选择2种选择.

You have 2 options that you could go about doing.

  • 首先是删除email字段上的唯一约束.这完全取决于代码中的逻辑,但是由于电子邮件应该几乎始终是唯一的,因此不建议这样做.
  • First would be to remove the unique constraint on the email field. This entirely depends on your logic in your code, but seeing as emails should almost always be unique, this is not suggested.

您可以通过运行以下命令来删除唯一键: alter table [table-name] drop index [unique-key-index-name];

You can drop a unique key by running the command: alter table [table-name] drop index [unique-key-index-name];

  • 第二,将使用NULL而不是一个空字符串.我的假设是,当用户电子邮件不存在时,您将设置一个空字符串.在这种情况下,最好使用NULL,然后在从数据库中检索数据时进行检查.
  • Second, would be to use NULL instead of an empty string. My assumption is that you are setting an empty string when the users email does not exist. In this scenario, it would be better to use NULL, and then check for that when retrieving data from the database.

您可以在MySQL语句中使用NULL标识符插入NULL值,例如:

You can insert a NULL value by using the NULL identifier in your MySQL statement, like such:

INSERT INTO users (firstName,lastName,email)
  VALUES ('Bob','Ross',NULL);

然后使用您要从中访问此数据的任何语言检查NULL值.

And then check for a NULL value in whatever language you are accessing this data from.

这篇关于错误:键“电子邮件"的重复条目“"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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