【发布时间】:2013-03-26 13:16:04
【问题描述】:
创建表时,是否可以根据两列的计算值声明约束?
这是解释我要做什么的伪代码:
CREATE TABLE employee_comments(
id int(11),
user_id int(11),
franchise_branch_id int(11) default 0,
corporate_branch_id int(11) default 0,
primary key id,
key corp_xor_franch(corporate_branch_id + franchise_branch_id > 0)
)
基本上,用户在公司级别或特许经营级别插入评论。对于每条评论,任何一个都可以留空,但必须至少声明一个。
【问题讨论】:
-
我很确定 MySQL 不支持这个。但请参阅:stackoverflow.com/questions/5422729/mysql-computed-column
-
你为什么不只有一个
branch_id(它可能对branch有一个FOREIGN KEY约束)? -
如何检查值是否正确的插入和更新触发器?在此处查看触发器文档dev.mysql.com/doc/refman/5.0/en/create-trigger.html