Explain primary key, foreign key, index, constraint and transaction as if I know nothing.
Answer
- Primary key: column(s) that uniquely identify each row (no duplicates, no null).
- Foreign key: a column that points to the primary key of another table, creating a relationship and preventing orphan rows.
- Index: an extra sorted structure that makes searches faster, at the cost of slower writes and more space.
- Constraint: a rule the database enforces (NOT NULL, UNIQUE, CHECK, FOREIGN KEY).
- Transaction: a group of operations that either all succeed or all fail (ACID), e.g. a money transfer.
- Together they keep data correct (integrity) and queries fast.
Practice this question in Repaso