What are SQL constraints?
Why Interviewers Ask This
Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid MySQL / SQL basics — a prerequisite for any developer role.
Answer
SQL constraints enforce rules on data in tables, ensuring data integrity and accuracy. Types: (1) NOT NULL — column cannot contain NULL: name VARCHAR(100) NOT NULL; (2) UNIQUE — all values in a column must be distinct (NULLs are allowed — MySQL allows multiple NULLs in a unique column): email VARCHAR(255) UNIQUE; (3) PRIMARY KEY — combination of NOT NULL + UNIQUE, uniquely identifies each row; (4) FOREIGN KEY — references a primary key in another table, enforces referential integrity; (5) CHECK — validates values meet a condition (fully supported in MySQL 8.0+): CHECK (age >= 18); (6) DEFAULT — provides a default value when none is specified: status VARCHAR(20) DEFAULT "active"; (7) AUTO_INCREMENT — automatically generates incrementing integer values (MySQL-specific, not a standard constraint but commonly discussed with them). Constraints can be defined at column level (inline) or table level (after all columns, named constraints). Use CONSTRAINT constraint_name to name constraints for clear error messages.
Common Mistake
Rushing to answer is a common mistake. Take two seconds to structure your response: definition → example → trade-off. This structure makes complex MySQL / SQL answers easy to follow.