我有一个存储产品代码的字段。代码是唯一的,但有些产品根本没有代码。我不能发明代码,因为那些是提供者代码。
这种约束在 MySQL 中是否可行?
我是存储过程和触发器的菜鸟,所以如果解决方案涉及其中之一,请耐心等待。
更新: 该列不为空。这就是为什么我无法做到这一点。
最佳答案
是的,您可以这样做。见 MySQL reference (version 5.5) .
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. For all engines, a UNIQUE index allows multiple NULL values for columns that can contain NULL.
关于mysql - MySQL 中允许空值的唯一约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1346765/