Dependend Constraint in MySQL
I have not much experience with MyS开发者_如何学运维QL or SQL in general, and I don't want to dive into it too deeply. But I just wanna ask this:
I've got two columns, like this:
CREATE TABLE foo(parent INT(11) unsigned NOT NULL, ordering INT);
I just want a constraint that makes sure that if the parent
key is the same, the ordering
must be different. Or in maths:
For all f1,f2 in foo: parent(f1) = parent(f2) => ordering(f1) =/= ordering(f2)
How can I express that in MySQL?
I assume that =/=
means "not equal" (which would be !=
or <>
in SQL terminology).
A unique index on (parent, ordering) should do that:
CREATE UNIQUE INDEX idx_order_unique ON foo (parent, ordering);
精彩评论