Take the 2-minute tour ×
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.

Note: this is not the same as this SO question which is about adding redundant information in the tables themselves.

Does the final statement in the below series of statements buy me anything in terms of indexing / query optimization or is it totally useless and an anti-pattern? From a referential integrity viewpoint it is useless but might there be some benefit in indexing, or query processing speed for instance, e.g. in a JOIN only between the "child" and "grandfather" tables?

CREATE TABLE grandfather (i INTEGER);
ALTER TABLE  grandfather ADD PRIMARY KEY (i);

CREATE TABLE father  (i, j INTEGER);
ALTER TABLE  father ADD PRIMARY KEY (i, j);
ALTER TABLE  father ADD CONSTRAINT father_2_grandfather FOREIGN KEY (i) REFERENCES grandfather(i);

CREATE TABLE child (i, j, k INTEGER);
ALTER TABLE  child ADD PRIMARY KEY (i, j, k);
ALTER TABLE  child ADD CONSTRAINT child_2_father      FOREIGN KEY (i, j) REFERENCES father(i, j);
-- is the below statement totally useless ?
ALTER TABLE  child ADD CONSTRAINT child_2_grandfather FOREIGN KEY (i)    REFERENCES grandfather(i);
share|improve this question
2  
It's not useful at all I think. The only exception might be to help brain-dead ORMs by explicitly telling them that there is an (implicit but trusted) foreign key relationship from child to grandfather through the father an the other 2 FKs. –  ypercube Feb 15 at 0:44
add comment

migrated from stackoverflow.com Feb 15 at 16:42

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

1 Answer

(For similar relations simple hierarchy are more sensible... (id, parent))

Apart from that the last foreign key only slows down a bit the DML queries.

share|improve this answer
add comment

Your Answer

 
discard

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.