March 11, 2013 at 12:11 pm
Greetings,
I am wondering if anyone could help me either define a design situation I have in the data warehouse environment I work in, or perhaps a possible way to correct it.
There are several tables within our warehouse model that have what I like to call, "Indirect" Foreign Keys. These are columns that have an unofficial key relationship with another table in a separate database.
Example:
DataBaseA.GIS.StateRef.StateId <--> DatabaseB.dbo.Organization.StateId
We are currently relying on our ETL processes to enforce constraints at load time, but I am curious if these relationships can be established, and if so how? If not, is there a term that is used to describe this occurrence?
Thanks very much
KJ
March 11, 2013 at 12:46 pm
actual foreign key constraints cannot expand to objects outside of the existing database.
you could potentially create a user defined function that is used as a CHECK CONSTRAINT, which checks if the data matches the cross database criteria, but if the other database was no longer accessible(network errors,security issues, linked server credentials being invalid, double hop issues), or the data on the linked server changed due to a restore, the check constraint would only be preventing new rows from being added, and maybe raise an error during an update, both of which could prevent data from being saved.
probably not what you want.
Lowell
March 11, 2013 at 12:54 pm
Thanks Lowell, that's what I thought was the case. The setup as-is is not causing an issue, I just became curious while working on the warehouse documentation as to whether or not there was another or better solution to this design.
March 11, 2013 at 1:04 pm
These constraints are often called cross-database foreign key constraints. SQL Server offers no support for them, in fact I don't think any widely used commercial SQL database does. If you want to enforce them it's possible to do it using triggers: triggers on update and delete in the referenced table and on insert and update in the referencing table. But you have to decide what to do with an operation in database A when database B is inaccessible so that the trigger can't do its job. This may be no worse than the problem with FK constraints withing a database when the tables involved are on different discs and one may be unavailable when the other isn't, but it may happen much more often when there are two databases than when there is only one - particular if the two databases are on different servers. And even if you do it with triggers, you don't have a nice object (foreign key constraint) in the metadata that everyone who is interested can see; you can of course invent an analog of the sys.foreignkeys table in which you record all references to other databases and all references from other databases, but it's not as good as having the DBMS do it for you.
Incidentally, I agree with Lowell that doing something with check constraints is not where you want to go on this; but triggers can work, although there are potential problems.
And of course you shouldn't do it unless it provides real benefits that make it worth the effort. I'm inclined to believe that in a datawarehouse environment where your RTL processes are already enforcing these constraints that is rather unlikely to be the case.
Tom
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply