Error: Duplicate entry #39;#39; for key #39;email#39;(错误:关键“电子邮件的重复条目“)
问题描述
我以前在运行 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
emailfield. 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
NULLinstead 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 useNULL, 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.
这篇关于错误:关键“电子邮件"的重复条目“"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:错误:关键“电子邮件"的重复条目“"
基础教程推荐
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 无法解决整理冲突 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
