我们在谈论什么数据库?在 SQL 2005 中,我无法创建引用没有唯一约束(主键或其他)的列的外键约束。
create table t1
(
id int identity,
fk int
);
create table t2
(
id int identity,
);
CREATE NONCLUSTERED INDEX [IX_t2] ON [t2]
(
[id] ASC
);
ALTER TABLE t1 with NOCHECK
ADD CONSTRAINT FK_t2 FOREIGN KEY (fk)
REFERENCES t2 (id) ;
Msg 1776, Level 16, State 0, Line 1
There are no primary or candidate keys in the referenced table 't2'
that match the referencing column list in the foreign key 'FK_t2'.
Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.
如果您真的可以做到这一点,您将有效地建立多对多关系,如果没有中间表,这是不可能的。我真的很想听到更多关于这个的信息......
另请参阅此相关问题和答案。