473,406 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Foreign Keys between databases

I realize that FK cannot be created across databases. The question I
have is what are the alternatives?

In my thinking I have two choices.

1) First choice (which I read in a different post) is to use a stored
procedure as a constraint to go check the table in the other database.

2) Second choice would be to set up publish/subcribe and replicate the
table across the databases. THEN set up a Foreign Key.

Is there a pro or con (besides having two copies of the table with the
second choice) to either choice? Is there a better way?
Jul 20 '05 #1
4 23599
Cross-database DRI in SQL Server is impossible. The best workaround is to
use triggers.

Using replication is overkill & not a relevant solution here at all. About
using stored procedures, well, you can do it proactively using triggers,
which is much better, IMO.

--
- Anith
( Please reply to newsgroups only )
Jul 20 '05 #2
[posted and mailed, please reply in news]

Jason (Ja*******@hotmail.com) writes:
I realize that FK cannot be created across databases. The question I
have is what are the alternatives?

In my thinking I have two choices.

1) First choice (which I read in a different post) is to use a stored
procedure as a constraint to go check the table in the other database.
Stored procedure? Possibly you could use a UDF for the task, but don't
do it. While techically possible, the performance penalty are
horrendeous for multi-row inserts.
2) Second choice would be to set up publish/subcribe and replicate the
table across the databases. THEN set up a Foreign Key.


In one word: don't.

It would be interesting to know, why you insist having two databases in
the first place, because that appears to be the root of the problem.

But if you must have it that have that way, use triggers. Note that
you need triggers on both tables. On the parent to check deletes
and updates, and one of the child to check insert and updates.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #3
Erland Sommarskog <so****@algonet.se> wrote in message news:<Xn**********************@127.0.0.1>...
[posted and mailed, please reply in news]

Jason (Ja*******@hotmail.com) writes:
I realize that FK cannot be created across databases. The question I
have is what are the alternatives?

In my thinking I have two choices.

1) First choice (which I read in a different post) is to use a stored
procedure as a constraint to go check the table in the other database.


Stored procedure? Possibly you could use a UDF for the task, but don't
do it. While techically possible, the performance penalty are
horrendeous for multi-row inserts.
2) Second choice would be to set up publish/subcribe and replicate the
table across the databases. THEN set up a Foreign Key.


In one word: don't.

It would be interesting to know, why you insist having two databases in
the first place, because that appears to be the root of the problem.

But if you must have it that have that way, use triggers. Note that
you need triggers on both tables. On the parent to check deletes
and updates, and one of the child to check insert and updates.


My reasoning (which could be completely wrong since I am new to
databasing) for multiple databases is a "logically" grouping of
tables. A table that could be used for different purposes would only
have to be created and maintained in one place. In my case I have a
"Supporting" database which has tables with information like country
listings and currency listings. My "Trading" database contains tables
with trades, orders, securities, exchanges, etc. If I needed, I could
create an "HR" database which would also reference tables in
"Supporting".

One of the columns in the Trading.Exchanges table is a row id to a row
in the Supporting.Countries table. While I guess I could just have
multiple copies of the same table in different databases, in my
opinion that is not a very clean solution.

- Jason
Jul 20 '05 #4
Jason (Ja*******@hotmail.com) writes:
My reasoning (which could be completely wrong since I am new to
databasing) for multiple databases is a "logically" grouping of
tables. A table that could be used for different purposes would only
have to be created and maintained in one place. In my case I have a
"Supporting" database which has tables with information like country
listings and currency listings. My "Trading" database contains tables
with trades, orders, securities, exchanges, etc. If I needed, I could
create an "HR" database which would also reference tables in
"Supporting".

Alright, I think I have a solution to your problem: cram it all into
one database.

I understand your reasoning, but using multiple database in an application
is cumbersome. For one: if you make hard-coded references to tables or
stored procedures in the other database, you have a gross problem if you
are asked to set up a second test environment on the same server.

Actually, the system I work with, falls into modules like you describe
above. You can identify system data like countries and currencies, base
data like securities and accounts, transaction data like notes and
positions, order data etc etc. Ok, so we are not considering to have
an HR module (if you mean Human Resources), but not all of our
customers gets all tables.

To this end, we have introduced a subsystem concept. A subsystem is
a set of tables, types, stored procedures etc, and one subsytstem can
depend on another. (But the dependencies should not be circular.) And
to support this, we have a tool, which you can find at
http://www.abaris.se/abaperls/ if you are curious. It's freeware.
One of the columns in the Trading.Exchanges table is a row id to a row
in the Supporting.Countries table. While I guess I could just have
multiple copies of the same table in different databases, in my
opinion that is not a very clean solution.


Having copies of same table in multiple SQL databases is not a problem.
That's just your binary code. Your source code for the table should be
in a version-control system.
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Vinodh Kumar P | last post by:
I understand the number of foreign keys allowed is restricted by the DBMS I use. In a general relational schema design perspective how many foreign keys a table shall have? If I have large number...
26
by: pb648174 | last post by:
I have a table called BidItem which has another table called BidAddendum related to it by foreign key. I have another table called BidFolder which is related to both BidItem and BidAddendum, based...
6
by: Brendan Jurd | last post by:
Hi all, I read on the manual page for Inheritance that: "A limitation of the inheritance feature is that indexes (including unique constraints) and foreign key constraints only apply to single...
5
by: Ross A. Finlayson | last post by:
Hi, I'm scratching together an Access database. The development box is Office 95, the deployment box Office 2003. So anyways I am griping about forms and global variables. Say for example...
1
by: Thomas T. Thai | last post by:
I'm looking for a better way to make use of foreign keys. Here is a sample setup: -- TESTING Foreign Keys create table mod ( mod_id int not null primary key, name varchar(32) not null...
9
by: sonal | last post by:
Hi all, I hv started with python just recently... and have been assigned to make an utility which would be used for data validations... In short we take up various comma separated data files for...
2
by: Chris | last post by:
I have a database column that stores a comma delimited list of foreign keys. Would someone show me how to do a join using the values from a list stored within a record? For example, a record in ...
1
by: apax999 | last post by:
Kinda new to SQL, using SQL Server 2005. I have some foreign keys in a couple of tables. I need to drop these tables, but can't since I'll get the error: Msg 3726, Level 16, State 1, Line...
6
by: bobdurie | last post by:
If i create a simple table with a foreign key constraint, does it create an implicit index on that given ID? I've been told this is done in some databases, but i need to know for sure if SQL...
2
JnrJnr
by: JnrJnr | last post by:
I have two SQL database tables. One contains various products(with unique primary keys) and the other contains information related to the products aswel as the product's foreign keys. What I want...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.