SQL Server foreign key to multiple tables

前端 未结 5 1315
情话喂你
情话喂你 2020-12-11 19:04

I have the following database schema:

members_company1(id, name, ...);
members_company2(id, name, ...);
profiles(memberid, membertypeid, ...);
membertypes(id         


        
5条回答
  •  醉酒成梦
    2020-12-11 19:14

    A foreign key cannot reference two tables. Assuming you don't want to correct your design by merging members_company1 and members_company2 tables, the best approach would be to:

    Add two columns called member_company1_id and member_company2_id to your profiles table and create two foreign keys to the two tables and allow nulls. Then you could add a constraint to ensure 1 of the columns is null and the other is not, at all times.

提交回复
热议问题