简体   繁体   English

如何在具有多列的 sql 服务器中创建外键?

[英]How do i create a foreign key in sql server with multiple columns?

i'm trying to create foreign key:我正在尝试创建外键:

create table visit
      (Num_vst int primary key,
      date_vst date,
      num_agnt int,
      code_type int,
      Constraint fk_vst foreign key (num_agnt,code_type) 
        references agente,type_de_verification  (num_agnt,code_type);

where is the problem?哪里有问题?

You are describing two separate relationships to two tables:您正在描述与两个表的两个独立关系:

num_agnt is related to agente and code_type is related to type_de_verification num_agnt与agente相关,code_type与type_de_verification相关

In this case you can define both foreign keys as shown below:在这种情况下,您可以定义两个外键,如下所示:

create table visit (
  Num_vst int primary key,
  date_vst date,
  num_agnt int,
  code_type int,
  constraint fk1 foreign key (num_agnt) references agente (num_agnt),
  constraint fk2 foreign key (code_type)
    references type_de_verification (code_type)
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM