我有一个 MySQL 数据库:
CREATE TABLE `users votes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userid` int(11) NOT NULL,
`forumtopicid` int(11) DEFAULT NULL,
`replyid` int(11) DEFAULT NULL
)
我正在尝试创建两个独特的约束:
CREATE UNIQUE INDEX index_first
ON `users votes`(userid, forumtopicid, replyid)
WHERE forumtopicid IS NOT NULL;
CREATE UNIQUE INDEX index_second
ON `users votes`(userid, replyid)
WHERE forumtopicid IS NULL;
这引发了一个我不确定如何修复的错误。 MySQL 约束不能添加 WHERE 子句吗?
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE forumtopicid IS NOT NULL' at line 3
最佳答案
这是正确的 - 您不能在 CREATE INDEX
语句(在 mysql 中)中使用 WHERE
。
关于MySQL:使用 WHERE 子句创建约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20962885/