我试图使 mysql 表中的两列(组合)唯一。我已经搜索了堆栈,但解决方案不起作用。所以我会用一个例子来解释
cartID productID
10 7 -> allowed
10 8 -> allowed
11 7 -> allowed
11 7 -> not allowed, the combination of 11 7 already exists in table
Run Code Online (Sandbox Code Playgroud)
我找到的“解决方案”如下: ALTER TABLE cart ADD CONSTRAINT uq_cart UNIQUE(cartID, ProductID);
但这似乎只是对每个列单独添加了一个约束(因此,如果 10 7 已经存在,则 10 8 也会引发异常)。
mysql ×1