今天我发现你可以使用两列(tsql)的主键。 PK必须是唯一的,但两列都不是(组合必须是唯一的)。
我以为那很酷至少有两个SO问题,我问谁在喊我,我正在做我的(mysql)数据库错误,只有一个人说我做得很好。所以…这让我有些怀疑
这是做我认为的吗?
create table User(
id INT primary key AUTO_INCREMENT ,
ipaddr TEXT NOT NULL ,
email TEXT NOT NULL
);
create table test(
a INT NOT NULL ,
b INT NOT NULL ,
dummy INT NOT NULL ,
FOREIGN KEY (a) REFERENCES User(id),
FOREIGN KEY (b) REFERENCES User(id),
PRIMARY KEY(a,b)
);
我运行下面,所以看起来我正在做我想的(组合必须是唯一的,但列中的相同值不需要是唯一的)。我应该知道什么吗?有没有人对mysql提出这个问题呢?
mysql> insert into test(a,b,dummy) select 1,1,1;
Query OK, 1 row affected (0.03 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into test(a,b,dummy) select 1,2,2;
Query OK, 1 row affected (0.03 sec)
Records: 1 Duplicates: 0