Unique constraint that allows empty values in MySQL(在 MySQL 中允许空值的唯一约束)
问题描述
我有一个存储产品代码的字段.代码是唯一的,但有些产品根本没有代码.我无法发明代码,因为那些是提供者代码.
这种约束在 MySQL 中可行吗?
我是存储过程和触发器的菜鸟,所以如果解决方案涉及其中之一,请耐心等待.
更新:该列不是 Null.这就是我无法做到这一点的原因.
是的,您可以这样做.请参阅MySQL 参考(5.5 版).><块引用>
UNIQUE 索引创建了一个约束,使得索引中的所有值都必须是不同的.如果您尝试添加具有与现有行匹配的键值的新行,则会发生错误.对于所有引擎,UNIQUE 索引允许包含 NULL 的列有多个 NULL 值.
I have a field that stores product codes. The codes are unique, but some products simply doesn't have a code. I can't invent codes because those are providers codes.
Is this kind of constraint possible in MySQL?
I'm a noob with stored procedures and triggers, so if the solution involves one of these, please be patient.
Update: The column is NOT Null. That's why I was unable to do this.
Yes, you can do this. See the 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 中允许空值的唯一约束


基础教程推荐
- 从字符串 TSQL 中获取数字 2021-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 带更新的 sqlite CTE 2022-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- 带有WHERE子句的LAG()函数 2022-01-01