Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase

"Indirect" Foreign Key Relationship Expand / Collapse
Posted Monday, March 11, 2013 12:11 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, November 3, 2014 12:13 PM
Points: 356, Visits: 85

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.

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

Post #1429428
Posted Monday, March 11, 2013 12:46 PM



Group: General Forum Members
Last Login: Yesterday @ 9:09 AM
Points: 12,965, Visits: 32,541
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.


--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1429437
Posted Monday, March 11, 2013 12:54 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, November 3, 2014 12:13 PM
Points: 356, Visits: 85
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.
Post #1429441
Posted Monday, March 11, 2013 1:04 PM



Group: General Forum Members
Last Login: Today @ 2:17 PM
Points: 7,935, Visits: 9,662
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.

Post #1429446
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse