This question is actually about two different things.
- Should a foreign key to a remote database also a be a foreign key to a local table.
- Can you maintain referential integrity to a foreign database
These are two different things entirely, although the quick answer to both is actually the same:
No.
But let me get into the details a bit.
1. Use of foreign keys to a remote database
To reduce dependancy on the remote database, you should only store those foreign keys in one location in your database.
Example: Let's just say you had a blog where users could post comments. These users will login through Facebook. You now have a remote database (Facebook) and a local one that stores your users' comments. You could now follow one of two designs:
- a
comments
table that stores thefacebook_id
as foreign key
or
- a separate
users
table storing thefacebook_id
along with a localid
and acomments
table that uses your local id as foreign key.
You should not use the facebook_id in both. While that would actually work, you're introducing a dependancy on a remote database without need. You wouldn't be able to add a comment from a Non-Facebook user since that would break your design.
2.Referential integrity with remote databases
You might not have intended to ask this, but the term referential integrity
implies all foreign keys to the remote database actually refer to an existing remote record (i.e. user). The only way to maintain that integrity would be if the remote database would inform you of changes to a remote record or its deletion, which usually is not the case.
Example: Lets go back to above mentioned hypothetical blog. Some Facebook user posted a comment. Later the same person decides to delete their Facebook account. The Facebook database will not likely inform you of that happening, leaving you with "dead" records in your database which do not link to a valid record in the remote database anymore. This breaks referential integrity. So unless you have a good way of actually maintaining that integrity, such as receiving deletion notifications etc, you should design your application so that it won't break if the Facebook user got deleted.