Primary Key vs Foreign Key
A primary key names each row in its own table. A foreign key sits in another table and points back to that name.
Primary Key
A column with a different value in each row. That is how one row is found.
A primary key is the column that gives each row in a table its own unique value. No two rows can have the same key, and it can never be empty. It is often a simple number, like id, that the database counts up by itself. Other tables use it to point at a row without confusion. Every table should have one.
Foreign Key
A column that points to a row in another table. The link is checked for you.
A foreign key is a column that points to a row in another table. It holds that row's primary key, so an order can point to its customer. The database checks the link. You cannot point at a row that is not there. It can also stop you from deleting a row that others point to. This is how a relational database ties its tables together.
In one line
- Primary Key
- A column with a different value in each row. That is how one row is found.
- Foreign Key
- A column that points to a row in another table. The link is checked for you.