无法使字段独一无二!

发布于 2024-08-04 01:00:09 字数 547 浏览 11 评论 0原文

表中不能有超过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

终难遇 2024-08-11 01:00:09

它告诉您电子邮件字段中已经有非唯一条目,即值“”(空字符串)

It's telling you you already have non-unique entries in the email field, namely, the value '' (empty string)

一场春暖 2024-08-11 01:00:09

您在多行的 email 列中具有相同的值(即 '')。这意味着您不能对该列施加 UNIQUE 约束。

您可以使该列可为空,然后将当前 '' 的值更新为 NULL,然后对其创建 UNIQUE 约束,因为 UNIQUE > 允许空值。

ALTER TABLE users MODIFY email VARCHAR(100);

UPDATE users SET email = NULL WHERE email = '';

ALTER TABLE users ADD UNIQUE KEY (email);

顺便说一句,为什么电子邮件列中有值 '' ?这不是一个有效的电子邮件地址。

You have the same value (i.e. '') in the email column on more than one row. That means you can't put a UNIQUE constraint on that column.

You could make the column nullable, then update the value to NULL where it's currently '', and then create a UNIQUE constraint on it, because UNIQUE permits nulls.

ALTER TABLE users MODIFY email VARCHAR(100);

UPDATE users SET email = NULL WHERE email = '';

ALTER TABLE users ADD UNIQUE KEY (email);

BTW, why do you have the value '' in the email column? That's not a valid email address.

戈亓 2024-08-11 01:00:09

这意味着(至少)2 条记录有一封空电子邮件。

请记住:NULL != ''

要查找它们:

SELECT id FROM users WHERE email= '';

This means that (at least) 2 records have an empty email.

Remember: NULL != ''

To find them:

SELECT id FROM users WHERE email= '';
π浅易 2024-08-11 01:00:09

您可以拥有多个独特的字段。

我认为错误是抱怨电子邮件字段中的数据。 (我认为您有不止一行的值为 '' )

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 '' )

别想她 2024-08-11 01:00:09

没有值也被认为是唯一的,因此如果要按照唯一的标准来判断,则没有任何内容的两个电子邮件行是重复的。

No value is also considered unique, so two email rows with nothing are duplicate, if they are to be judged by a unique standard.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文