LESSON 11 / 24·2 min readFoundation

What are primary keys, foreign keys and constraints?

01 Understand02 Recall03 Prepare

Understand the concept

Keys identify records and relationships, while constraints tell the database which stored states are allowed. A primary key identifies each row, a foreign key validates a relationship, and other constraints enforce required or unique values and conditions. Database enforcement protects data even when different application paths write concurrently.

Important ideas

  • Primary key: unique, non-NULL row identity.

  • Foreign key: ensures a referenced record exists.

  • NOT NULL, UNIQUE and CHECK enforce required values and data rules.

  • A foreign key does not automatically choose the right delete behavior; decide whether related rows should restrict, cascade or become NULL.

PAUSE & RECALL

Can you explain it without the notes?

What are primary keys, foreign keys and constraints?

Use your own words. Explain an example and one limitation before checking the explanation.

Check your explanation

Keys identify records and relationships, while constraints tell the database which stored states are allowed. A primary key identifies each row, a foreign key validates a relationship, and other constraints enforce required or unique values and conditions. Database enforcement protects data even when different application paths write concurrently.

CONNECT IT TO YOUR INTERVIEW

Turn this concept into a clear answer.

  • Application validation improves messages, while database constraints protect the rule across all writers.
Practice this concept
YOUR NEXT STEP

Keep connecting the ideas.

Next lesson: What are 1NF, 2NF and 3NF?

Adapted from 05_SQL_POSTGRESQL.md · “What are primary keys, foreign keys and constraints?”. Examples are study fragments; framework behavior can depend on your version.