• A key is any subset of the attributes or columns from a relational database table.
  • A superkey is any key that uniquely identifies an entity or row, including the entire entity itself.
  • A candidate key is a minimal superkey, that is, a superkey from which no attributes may be removed without making it no longer a superkey. For this reason, entire entities are seldom candidate keys (except in many-to-many relationships with no attributes). Examples of candidate keys in the real world are (social_security_number), (username), or (first_name, middle_initial, last_name, date_of_birth, address). Candidate keys are so-called because they're candidates for being primary keys.
  • A primary key is the candidate key chosen by the database designer to identify the entity. Inserting an entity with an identical primary key to another entity in the same table is a key violation.
  • A foreign key is a key that references a primary key in another table (or, in recursive relationships, the same table). Referencing a nonexistent key is another type of key violation, as is deleting an entity that another entity references (unless, of course, you on delete cascade the key). Standard SQL does not support constraints on foreign keys; DBMS vendors implement them in incompatible ways.
Here's a Venn diagram of the concepts.
,-------------------------------------.
|      Keys                           |
|  ,-------------------------------.  |
|  |   Superkeys        ,-------.  |  |
|  |  ,-----------------+-.Whole|  |  |
|  |  |  Candidate keys | | rows|  |  |
|  |  |                 | |     |  |  |
|  |  |  o Primary key  `-+-----'  |  |
|  |  |                   |        |  |
|  |  `-------------------'        |  |
|  `-------------------------------'  |
`-------------------------------------'

Common primary keys