What are primary keys, foreign keys and constraints?
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,UNIQUEandCHECKenforce 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.
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.
Turn this concept into a clear answer.
- Application validation improves messages, while database constraints protect the rule across all writers.
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.