SQL Server foreign key to multiple tables

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

    Come on you can create a table but you cannot modify members_company1 nor members_company2?

    Your idea of a create a members table will require more actions when new records are inserted into members_company tables.
    So you can create triggers on members_company1 and members_company2 - that is not modify?

    What are the constraints to what you can do?

    If you just need compatibility on selects to members_company1 and members_company2 then create a real members table and create views for members_company1 and members_company2.
    A basic select does not know it is a view or a table on the other end.

    CREATE VIEW dbo.members_company1
    AS
    SELECT id, name 
    FROM members
    where companyID = 1
    

    You could possible even handle insert, updates, and deletes with instead-of

    INSTEAD OF INSERT Triggers

提交回复
热议问题