Middle
What is a Constraint in a database?
sobes.tech AI
Answer from AI
A constraint in a database is a rule applied to data in a table to ensure its integrity and correctness. Constraints help prevent the entry of incorrect or conflicting data.
Main types of constraints:
- PRIMARY KEY — a unique identifier for a row, cannot be NULL.
- FOREIGN KEY — ensures referential integrity between tables.
- UNIQUE — guarantees the uniqueness of values in a column.
- NOT NULL — prohibits NULL values in a column.
- CHECK — sets a condition that values must satisfy.
For example, a NOT NULL constraint on the email field guarantees that each customer will have an email specified.