SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


"Indirect" Foreign Key Relationship


"Indirect" Foreign Key Relationship

Author
Message
KJKKPSI
KJKKPSI
SSC-Addicted
SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)

Group: General Forum Members
Points: 487 Visits: 112
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
Lowell
Lowell
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: General Forum Members
Points: 35762 Visits: 40233
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

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

KJKKPSI
KJKKPSI
SSC-Addicted
SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)

Group: General Forum Members
Points: 487 Visits: 112
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.
Tom Thomson
Tom Thomson
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17412 Visits: 12331
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

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search