【发布时间】:2020-06-03 04:21:29
【问题描述】:
这个问题和Check Constraint to Confirm Exactly One is not NULL类似,但是针对MySQL和多列,并且MySQL从v8.0.16开始支持check。
例如,我有一个“发布”表,它有三列,即 author_id、manager_id、editor_id。这三列中只有一列不能为空。
如何在 MySQL Check 约束中实现这一点?
可能是这样的
not_null_sum = (author_id is not null ? 1 : 0) + (manager_id is not null ? 1 : 0) + (editor_id is not null ? 1 : 0)
not_null_sum should == 1
【问题讨论】:
标签: mysql database constraints