Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relationships between tables from different databases

Is it possible to define relationships between tables in different databases in SQL server 2008? And can you recommend an online tutorial for studying it? (I prefer ASP.NET, C#)

like image 601
string QNA Avatar asked Nov 29 '22 03:11

string QNA


2 Answers

No, you can't have foreign keys between databases.

Data integrity is within a single database only. If you need transactional consistency across databases then you should use a single database. The main issue is backups/restores: you will end up with broken data after a restore because your backups are not consistent.

A recent blog article "One Database or Ten?" explains in more details

Saying that, you can use triggers if you need this and are prepared to have broken data

like image 103
gbn Avatar answered Dec 10 '22 04:12

gbn


Yes you can but NOT using FOREIGN KEYS:

  1. You can use specific stored procs, which checks the consistency - in this case you have to make the user to use only these procedures for all the CRUD operations in both DBS

  2. Triggers, which will check the same

  3. All of the above have to run within properly isolated transaction to be sure, that your "just checked" values will not be deleted in a moment

like image 25
Oleg Dok Avatar answered Dec 10 '22 05:12

Oleg Dok