Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL:使用计算约束创建表_Mysql_Sql_Key - Fatal编程技术网

MySQL:使用计算约束创建表

MySQL:使用计算约束创建表,mysql,sql,key,Mysql,Sql,Key,创建表时,是否可以基于两列的计算值声明约束 以下是psuedo代码,解释了我要做的事情: 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_i

创建表时,是否可以基于两列的计算值声明约束

以下是psuedo代码,解释了我要做的事情:

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文档

使用类似于此示例的信息应该会有所帮助

CREATE TRIGGER checkFieldsValid 
BEFORE INSERT ON employee_comments
FOR EACH ROW BEGIN

    DECLARE errmsg VARCHAR(255) DEFAULT "REQUIRED DATA NOT FOUND";

    IF NOT NEW.franchise_branch_id = 0 XOR NEW.corporate_branch_id = 0 THEN

        SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = errmsg;

    END IF;
END;

我很确定MySQL不支持这个。但是请参见:为什么您不只拥有一个
分支id
(它可能会对
分支
具有
外键
约束)?插入和更新触发器如何检查值是否正确?请参阅触发器文档,这里有一些有趣的东西。非常感谢。我会调查你提供的两份参考资料。