无法使字段独一无二!
表中不能有超过 2 个唯一字段,还是我在这里做错了什么?
我有 1 个唯一的用户名密钥,我也希望将其用于电子邮件,但我收到
#1062 - Duplicate entry '' for key 'email'
alter table users
add unique (email)
Tbl:
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`password` varchar(40) NOT NULL,
`email` varchar(100) NOT NULL,
`registered` int(11) unsigned NOT NULL DEFAULT '0',
`is_active` tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
Can you not have more than 2 unique fields in a table or am i doing something wrong here?
I have 1 unique key for username and i want it for email too but i get
#1062 - Duplicate entry '' for key 'email'
alter table users
add unique (email)
Tbl:
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`password` varchar(40) NOT NULL,
`email` varchar(100) NOT NULL,
`registered` int(11) unsigned NOT NULL DEFAULT '0',
`is_active` tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它告诉您电子邮件字段中已经有非唯一条目,即值“”(空字符串)
It's telling you you already have non-unique entries in the email field, namely, the value '' (empty string)
您在多行的
email
列中具有相同的值(即''
)。这意味着您不能对该列施加UNIQUE
约束。您可以使该列可为空,然后将当前
''
的值更新为 NULL,然后对其创建UNIQUE
约束,因为UNIQUE
> 允许空值。顺便说一句,为什么电子邮件列中有值
''
?这不是一个有效的电子邮件地址。You have the same value (i.e.
''
) in theemail
column on more than one row. That means you can't put aUNIQUE
constraint on that column.You could make the column nullable, then update the value to NULL where it's currently
''
, and then create aUNIQUE
constraint on it, becauseUNIQUE
permits nulls.BTW, why do you have the value
''
in the email column? That's not a valid email address.这意味着(至少)2 条记录有一封空电子邮件。
请记住:NULL != ''
要查找它们:
This means that (at least) 2 records have an empty email.
Remember: NULL != ''
To find them:
您可以拥有多个独特的字段。
我认为错误是抱怨电子邮件字段中的数据。 (我认为您有不止一行的值为 '' )
You can have more than one unique field.
I think the error is complaining about the data in the email field. (I think you have more than one row with a value of '' )
没有值也被认为是唯一的,因此如果要按照唯一的标准来判断,则没有任何内容的两个电子邮件行是重复的。
No value is also considered unique, so two email rows with nothing are duplicate, if they are to be judged by a unique standard.