Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases SQL Basics Finding the Data You Want Searching Tables with 'WHERE'

What is a Foreign Key?

Andrew discussed that foreign keys are unique ID's that are.... shared across tables? So if I had an object named squid that had an ID of 1 that ID number will always refer to squid across tables in a database?

Am I missing anything or leaving anything out about what a foreign key is?

2 Answers

Steven Parker
Steven Parker
231,272 Points

The term "foreign key" refers to a relationship between the values in a column and the key values of another table. If you declare a column in some table as a foreign key for the "squid" table, then each item in it would contain a value that would match an ID value in the "squid" table.

The database enforces this and will not allow you to enter any value in the foreign key column that doesn't already exist as an ID in the "squid" table.

Asher Orr
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Asher Orr
Python Development Techdegree Graduate 9,410 Points

I was also confused about this, A X.

Thanks for your comment, Steven!

If I understand correctly, the PKID in the "books" table serves as the foreign key for books_id in the "loans" table.

The books_ID column in the "loans" table will always match the PKID column in books. The database designer declared this so the data is tightly coupled (as Andrew says, the power of SQL is in utilizing the relationships tables have with one another.)

Is that correct, or am I missing something here? Steven- this comes up in the video at 5:55 exactly.

Steven Parker
Steven Parker
231,272 Points

Sounds like you got it!   :+1:

Foreign Key refers to keys that are id belonging to another table.