Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

General Discussion

Thomas Joseph Meneses
Thomas Joseph Meneses
9,838 Points

Do foreign keys affect SQL JOIN operations?

Learning about normalization and joining tables, as well as about foreign keys, really piqued my programmer's curiosity. Both concepts seem to go hand-in-hand, particularly that both mean to define and then use table relationships. And yet wherever I looked, no one seemed to be asking this question (not even in the almighty Stack Overflow).

Can the constraint of having foreign keys allow in some way the SQL engine to JOIN the respective parent and child tables in a way that's somehow better than not having foreign keys defined at all? Although the constraints are defined, it seems as though a JOIN still requires the coder to set the related columns through ON [table1].[col1] = [table2].[col2].

If there is now way to depend on the previously defined foreign key when doing a JOIN, then it is in my opinion a violation of DRY since you already defined the relationship, and yet need to define it again when JOINing the related tables.