MySQL复合在FK'上是唯一的;s

MySQL复合在FK'上是唯一的;s,mysql,foreign-keys,constraints,mysql-error-1062,Mysql,Foreign Keys,Constraints,Mysql Error 1062,我想在mysql中实现以下约束: create table TypeMapping( ... constraint unique(server_id,type_id), constraint foreign key(server_id) references Server(id), constraint foreign key(type_id) references Type(id) ); 当我发出将打破约束的插入/更新时,这会引发密钥“server_id”

我想在mysql中实现以下约束:

create table TypeMapping(
    ...
    constraint unique(server_id,type_id),

    constraint foreign key(server_id) references Server(id),

    constraint foreign key(type_id) references Type(id)
);

当我发出将打破约束的插入/更新时,这会引发密钥“server_id”的“ERROR 1062(23000):重复条目“3-4”。这种类型的约束可能吗?如果是,怎么做?谢谢。

是的,这是完全正确的。请确保您了解,只有当您尝试在
TypeMapping
中插入新行时,复合唯一约束才会中断,其中具有相同
server\u id
type\u id
的另一行已经存在。

是的,我没有想到这实际上是预期的行为。:s