Copyright

What is a Foreign Key in SQL?

Instructor: Kaitlin Oglesby
If you want to link SQL tables to individual records, making your database much more efficient and responsive, then you will need knowledge of foreign keys. We'll be comparing primary and foreign keys and looking at an example of the latter.

What is a Foreign Key?

If you have been using SQL or any database system for long, you know that one of the biggest advantages to a database is that, unlike a spreadsheet, it can draw data from a variety of different tables quite easily. However, to do this it must have an easy way to know which tables to draw data from. In short, it needs a key. A foreign key is a field that directly identifies another table. As you might imagine, this makes using foreign keys a very useful skill and one of the aspects of SQL that helps to set it apart from other programs.

Primary vs. Foreign Keys

Before we go any further, let's make sure that we understand the difference between a primary key and a foreign key. A primary key provides an absolute reference point to a record on a row. For example, ID numbers for employees are often primary keys. Meanwhile, a foreign key allows links to be made between different tables. A primary key may be the ID number on a main table of employees, but a name or an ID number would be a foreign key on the subsequent tables that describe other aspects of the employee.

Linking Tables

That may sound a bit confusing, so let's take a minute to walk through it. Like I said earlier, foreign keys are great to keep track of information on subordinate tables that link back to a main table. Sometimes you'll see these referred to as child and parent tables, respectively.

Figure 1 shows an example of how tables could be linked. The image is from Microsoft Access and shows the primary key (artistID) for the table tblArtist is a foreign key in the table tblAlbum:


Figure 1: Foreign Key Example
SQL foreign key example


To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support