You must Sign In to post a response.
  • Category: .NET

    How to set Relationship In Sql DataBase


    How to Add Foreign Key relationship between two Databases.

    I have two tables in two different databases. In table1(It is in database1) there is a column called ID and it is a primary key. Now in table2(It is in databse2) there is a column called Table1ID and I want to add it as a foreign key.

    I tried to add it and it gave me the following error:

    Msg 1763, Level 16, State 0, Line 1
    Cross-database foreign key references are not supported. Foreign key Database2.table2.

    Msg 1750, Level 16, State 0, Line 1
    Could not create constraint. See previous errors.
  • #767787
    need to manage the referential constraint across databases using a Trigger. Just to clarify. This is not the best approach with enforcing referential integrity. Ideally you would want both tables in the same db but if that is not possible, please use trigger.

    Trigger Detail:
    Create it on Insert and Update.
    Inside trigger check existence of foreign key in other database table and handle the insertion, updation accordingly.

  • Sign In to post your comments