主键(x,y)和主键(x)之间的SQL差异,唯一(y)

时间:2017-03-25 18:52:01

标签: mysql sql

在SQL中,这个

之间的区别是什么
create table marriage (
    person1 int not null references human on delete cascade,
    person2 int not null references human on delete cascade,
    primary key (person1, person2)
);

和这个

create table marriage (
    person1 int not null references human on delete cascade,
    person2 int not null references human on delete cascade,
    primary key (person1),
    unique (person2)
);

带有两个主键的表是否会阻止这种情况?

marriage:    

person1 | person2
   1         2
   2         1

1 个答案:

答案 0 :(得分:1)

第一个索引是两列的唯一索引:这意味着两者的组合应该是唯一的。第1,2和2,1行不违反索引,因为索引将它们视为一个集合。 1,2和1,3也没有违反索引。

第二个样本包含两个索引,两者都必须是唯一的。这意味着1,2和1,3的行违反了索引约束。