Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

If you have table a (column x) which is 1 to many with table b (columns x, y) which is 1 to many with table c (columns y, z), it's pretty straight forward to define the foreign keys.

If for performance reasons, table c was altered to be (columns x, y, z) is there a way to specify that table c (columns x, y) have a foreign key relationship to table b's (columns x, y)?

Previously table c contained 2 single column foreign keys x --> table a and y --> table b. While this is correct, it creates erroneous results when trying to use commercial tools to create database diagrams.

share|improve this question

migrated from May 20 '13 at 1:29

This question came from our site for professional and enthusiast programmers.

up vote 1 down vote accepted

I have no problems:

create table a(x int primary key);
create table b(x int references a, y int, primary key(x,y));
create table c(x int, y int, z int, foreign key(x,y) references b);
share|improve this answer
i'll try that. x is not a primary key in b right now. – dan- May 20 '13 at 18:39
Post the DDL statements of your tables. – msi77 May 20 '13 at 19:12
thanks. that worked. my mistake was not including both columns in the primary key for the intermediate table. – dan- May 20 '13 at 19:19

You can create a composite foreign key from B to C:

alter table TableC
     add constraint FK_TableC_XY
     foreign key (x, y)
     references TableB (x, y)

That's better than two foreign keys: this will ensure that the combination of (x, y) is present, and not just the individual values.

share|improve this answer
actually, thats what i want to do but sql server will not let me because table b (x) has already been declared as a foreign key from table a (x). – dan- May 20 '13 at 13:56
as a follow on to the question which would be higher performing: a composite key which can be validated from one table or two foreign keys which can be validated from two different tables. – dan- May 22 '13 at 15:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.