ALTER TABLE admins ADD CONSTRAINT uc_EmailandDeleted_at UNIQUE (email,deleted_at);
我已经将我的表更改为这样工作:
ID EMAIL DELETED_AT
1. user@example.com NULL <- accept
2. user@example.com NULL <- reject because of uniqueness.
但它并没有像我希望的那样工作。如果我给了两个相同的日期
deleted_at正如我所料,它正在产生错误。但不适用于 NULL 值。有什么想法吗?
注意:我无法更改 deleted_at
列的行为,因为我正在使用 acts_as_paranoid。所以我必须将 deleted_at
字段保持为 NULL 或任何日期。
最佳答案
来自 the documentation :
A UNIQUE index creates a constraint such that all values in the index must be distinct. An error occurs if you try to add a new row with a key value that matches an existing row. This constraint does not apply to NULL values except for the BDB storage engine. For other engines, a UNIQUE index permits multiple NULL values for columns that can contain NULL.
以上摘录自 MySQL 5.0。 MySQL 在 5.1.12 中放弃了 BDB 存储引擎。 所以现在从最新documentation :
For all engines, a UNIQUE index permits multiple NULL values for columns that can contain NULL. If you specify a prefix value for a column in a UNIQUE index, the column values must be unique within the prefix.
关于mysql - mysql中的多个唯一不支持NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6851584/