在SqlServer中,如何用两个字段联合起来做外键,比如,表a(f1,f2,f3),b(f2,f3,f4),其中a表的f2,f3作为外键,与b表的f2,f3对应,其中f2,f3是b表的联合主键!请问具体如何实现,最好能在设计试图中解决,tslql也行。

解决方案 »

  1.   

    create table b(f2 int,f3 int,f4 int ,constraint bkey primary key (f2,f3))
    create table a(f1 int,f2 int,f3 int ,constraint afkey foreign key(f2,f3) references a(f2,f3))
      

  2.   

    树上的鸟儿的好像行不通哟..
    处理方法:
    create table b(ID int identity(1,1)primary key ,f2 int,f3 int,f4 int constraint U_b unique) create table a(f1 int,b_ID int foreign key references b(ID))---用组合主健用唯一约束,用ID为主键,引用表直接用ID
      

  3.   

    自增列做外键好吗?自增列做主键不用程序去维护。
    惟一的作用就是值是惟一的?本身不具备唯一约束,通过重置种子或dts 时,值可以重复。
    比如一个表的经常会删除很多记录,有可能重排自增列?只会空出来如:删2,值会为1,3..2不会自动出现,除人去改动..