SQL Server foreign key to multiple tables

前端 未结 5 1310
情话喂你
情话喂你 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:30

    Operating under the fact that you can't change the table structure:

    Option 1

    How important is referential integrity to you? Are you only doing inner joins between these tables? If you don't have to worry too much about it, then don't worry about it.

    Option 2

    Ok, you probably have to do something about this. Maybe you do have inner joins only, but you have to deal with data in profiles that doesn't relate to anything in the members tables. Could you create a job that runs once per day or week to clean it out?

    Option 3

    Yeah, that one may not work either. You could create a trigger on the profiles table that checks the reference to the members tables. This is far from ideal, but it does guarantee instantaneous checks.

    My Opinion

    I would go with option 2. You're obviously dealing with a less-than-ideal schema. Why make this worse than it has to be. Let the bad data sit for a week; clean the table every weekend.

提交回复
热议问题