Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Old Forums
 CLOSED - General SQL Server
 Cross-database foreign key references

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2003-12-08 : 08:29:41
Tim writes "I am in the process of developing multiple databases for multiple websites that will be sharing information and I have ru
n into an issue. I created a database to hold all the user information that will be used by all the websites. In addition, each website is driven by its own database. The problem that I have run into is that in the user records need to be linked to other records in the websites' databases. Since SQL Server 2000 does not support cross-database foreign key relationships how can I preserve the referential integrity? Any help will greatly be appreciated. Thanks."

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2003-12-08 : 09:45:15
ONe option might be to create a UDF as a CHECK constraint on the website DB tables
which calls the user table from the users db:


USE <UserDB>
GO

create table dbo.users ( user_id varchar(20) )
insert into dbo.users select 'test'
GO

USE <WebSiteDB>
GO

CREATE FUNCTION dbo.IsValidUser ( @user_id varchar(20) )
RETURNS BIT
AS
BEGIN
DECLARE @Bit BIT
SET @Bit = 0
IF EXISTS(SELECT 1 FROM <UserDB>.dbo.users WHERE user_id = @user_id )
SET @Bit =1
RETURN @Bit
END
GO

create table websiteDB
(
user_id varchar(20), CHECK (dbo.IsValidUser(user_id) = 1)
)
GO


--Constraint Catches
Insert websiteDB values ('invalid user')
--OK
Insert websiteDB values ('test')



USE <UserDB>
GO
DROP TABLE users
GO

USE <WebSiteDB>
GO
DROP FUNCTION dbo.IsValidUser
DROP TABLE websiteDB


Though this will not account for updates/deletes to users...
Go to Top of Page

tacket
Starting Member

47 Posts

Posted - 2003-12-08 : 16:42:38
We have a similar environment in that many databases lack the cross-database referential integrity due to the fact that you cannot have a foreign key accross DB's. The way I counter that is to encourage alike tables to remain on the same DB if possible. That doesn't sound possible in your environment, so you are stuck with writing code that assures the integrity for you. Before you do an insert/update/delete ALWAYS check if the parent key exists something like this:

if not exists
(
select *
from parent_table
where key = @Key
)
Begin
goto error_routine
End


Otherwise, do your insert/update/delete next. I think if you follow this rule and create SP's for doing all your insert/update/deletes and make sure everybody uses those SP's you should be ok.


That is the way we do it. Another option might be to create a trigger that checks the data in the other database and if the parent record doesn't exist will rollback the transaction. I think you can do this in theory, but as I stated before just write good code and use SP's. Hopefully in a future version of SQL they will allow for cross database referential integrity.


Phil
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2003-12-08 : 20:17:30
quote:
Hopefully in a future version of SQL they will allow for cross database referential integrity.
No, they won't. The whole idea of a relational database is that it contains ALL of the data that is RELATED to each other, and ONLY that data. Allowing the entire domain of users for a database that actually uses only a small fraction defeats the purpose, however convenient it might be.

Suppose there are other integrity rules that determine whether a user should be in a certain database (for instance, medical insurance, car insurance, and home insurance databases) Certain people may be declined for medical insurance yet still be able to receive home or car insurance, or vice versa. It is not the responsibility of the user database to determine membership in the various other databases, so it makes no sense to tie them together. Indeed, it would be a referential integrity violation to allow this kind of feature.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-12-09 : 09:22:46
Funny, I was just pouring over the CREATE TRIGGER BOL yesterday for another reason, and though it didn't apply at the time, it applies now...

BOL:

quote:

Remarks
Triggers are often used for enforcing business rules and data integrity. SQL Server provides declarative referential integrity (DRI) through the table creation statements (ALTER TABLE and CREATE TABLE); however, DRI does not provide cross-database referential integrity. To enforce referential integrity (rules about the relationships between the primary and foreign keys of tables), use primary and foreign key constraints (the PRIMARY KEY and FOREIGN KEY keywords of ALTER TABLE and CREATE TABLE). If constraints exist on the trigger table, they are checked after the INSTEAD OF trigger execution and prior to the AFTER trigger execution. If the constraints are violated, the INSTEAD OF trigger actions are rolled back and the AFTER trigger is not executed (fired).







Brett

8-)
Go to Top of Page
   

- Advertisement -