Ok. So I know what a primary key in DB is. If you have a table in a database, a primary key is a single value that is unique to each row in your table. For example:
id | name | whatever ------------------------- 1 Alice .... 2 Bob .... 45 Eve .... 988 .... ....
So I need a good, simple example to explain what exactly a foreign key is. Because I just don’t get it 🙂
Edit: OK it’s pretty easy, I guess I was over-complicating the problem.
So one final question, the only restriction on foreign keys is that it they are a valid primary key value in the table I am referring to?
A foreign key is a field that points to a primary key of another table.
Example:
You can see that Users.UserRoleID is a foreign key which points to the primary key UserRoles.UserRoleID
The use of foreign keys makes setting up relationships on other tables simple, allowing you to link together the data of multiple tables in a nice way:
Example:
Output would then be: