---
title: "Foreign key works if we communicate with two Databases in MSSql server?"  
description: "Foreign key works if we communicate with two Databases in MSSql server?"  
author: "Sandra Emily"  
published: 2023-07-30  
updated: 2023-07-31  
canonical: https://www.mindstick.com/forum/159376/foreign-key-works-if-we-communicate-with-two-databases-in-mssql-server  
category: "mssql server"  
tags: ["sql server", "sql"]  
reading_time: 2 minutes  

---

# Foreign key works if we communicate with two Databases in MSSql server?

[Foreign key](https://www.mindstick.com/blog/174/foreign-key-self-reference-constraint) works if we [communicate with two](https://www.mindstick.com/forum/159375/how-to-communicate-with-two-separate-database-in-mssql-server) [Databases](https://www.mindstick.com/startup/34/airbyte-the-open-source-platform-simplifying-data-integration-for-warehouses-lakes-and-databases) in [MSSql](https://www.mindstick.com/forum/168/join-in-mssql-server-2005) [server](https://www.mindstick.com/articles/43769/what-is-serverless-architecture-is-it-worth-switching-over)?

## Replies

### Reply by Aryan Kumar

No, [foreign](https://yourviews.mindstick.com/story/2164/here-are-the-best-foreign-universities-for-higher-education) keys cannot be used to enforce referential integrity between two databases in SQL Server. This is because foreign keys are a feature of a single database, and they cannot be used to enforce constraints across databases.

If you need to enforce referential integrity between two databases, you can use triggers. Triggers are stored procedures that are executed when a specific event occurs in a database. You can use triggers to check for referential integrity violations and to take corrective action if a violation occurs.

For example, you could create a trigger that would be executed when a row is deleted from a table. The trigger could check to see if the row that is being deleted is referenced by any rows in another table. If it is, the trigger could prevent the row from being deleted.

Another option is to use linked servers. Linked servers allow you to connect to a remote database as if it were a local database. You can then use foreign keys to enforce referential integrity between tables in the local database and tables in the remote database.

However, it is important to note that linked servers can be a performance bottleneck. If you are concerned about performance, you may want to consider using triggers instead of linked servers.


---

Original Source: https://www.mindstick.com/forum/159376/foreign-key-works-if-we-communicate-with-two-databases-in-mssql-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
