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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Composite Foreign Key?

Author  Topic 

pacerier
Starting Member

5 Posts

Posted - 2009-12-07 : 22:40:43

I have a table (table A), and its primary key is a combination of column A and column B. so if i wanted to reference table A with table B's foreign key, does it mean that table B must have 2 columns each referencing each of table A's primary key?

however there may be problems in which table B's refcol A matches an allowed value in table A's col A and table B's refcol B matches an allowed value in table A's col B However the combination colA+colB VS combination refcolA + refcolB does not exist.

is there any better way to do this?

---

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-07 : 22:49:59
The foreign key does not have to match exactly the primary key. You could just reference one of the columns for the FK in the PK.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-12-07 : 22:53:45
Chop Suey?

Whoa

Care to post examples?

There should be no problem having composite keys

SOME people however, get around this by creating a FALSE, ARTIFICIAL "KEY" that represents the actual Natural Key

These are called Surrogates (IDENTITY or datetime Columns)

I Strongly suggest you learn to model data CORRECTLY and create these Natural keys



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-12-07 : 22:56:23
quote:
Originally posted by tkizer

The foreign key does not have to match exactly the primary key.


Ummmmmmmmmm....I could go with that in a non-identifying relationship...but you still would need to ref the whole key...IF it exists...

Otherwise, there is no real relationship



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

pacerier
Starting Member

5 Posts

Posted - 2009-12-08 : 00:51:33
i mean this:

TABLE1
PK1--PK2
-1----2
-2----7
-8----6
-1----9
-6----2

Primary key for table1 of course is a combination of attribute PK1 and PK2

now i want to reference Table 1 this is what i'm currently doing

TABLE2
FK1_referencing_PK1-----FK2_referencing_PK2
-------1-------------------------2
-------2-------------------------7
-------8-------------------------6
-------1-------------------------9
-------6-------------------------2

However when this is not good because I am able to insert a value such as:
-------6-------------------------9------
where 6 exist in PK1 and 9 exists in PK2 However 6 and 9 does not exist in combination.

is there anyway to fix this such that the database will reject such referenced nonexisting combinations of composite primary keys?

---
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-08 : 00:54:30
You just need to create a composite foreign key. It seems instead you have two foreign keys, one for each column in the PK.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

pacerier
Starting Member

5 Posts

Posted - 2009-12-08 : 02:34:49
sorry but how is that possible, or how does the syntax allows it to be possible?



---
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-08 : 11:10:37
Here's an example:

ALTER TABLE Table2
ADD CONSTRAINT FK_Table1_Table2 FOREIGN KEY (Column1, Column2)
REFERENCES Table1(Column1, Column2);

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -