Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Data types ensure that numbers are stored in numeric columns and dates are stored in temporal columns. However, data types cannot enforce business rules such as: "Employee salary must be greater than ₹10,000", "Product discount percentage must be between 0 and 100", or "End date must be after Start date". To validate these business rules directly inside the database engine, you use CHECK Constraints.
A CHECK constraint specifies a boolean expression that must evaluate to TRUE (or UNKNOWN) for every inserted or updated row. If a statement causes the condition to evaluate to FALSE, MySQL rejects the operation with error 3819: Check constraint is violated.
CHECK constraints syntactically but silently ignored them! Active enforcement of CHECK constraints was officially implemented in MySQL 8.0.16.Let us test our constraint by attempting to insert an illegal discount:
The database rejected the bad data at the door, safeguarding your application against corrupted records!
You can use standard SQL operators, boolean logic, and scalar functions within CHECK expressions:
CHECK expression evaluates to UNKNOWN (due to a NULL column), MySQL considers the check satisfied and allows the insert! If a value must not be NULL, always pair your CHECK constraint with an explicit NOT NULL declaration.CHECK constraints?CHECK constraint expression to evaluate to FALSE?CHECK constraint syntax validates that an end_date column is strictly on or after a start_date column?CHECK constraint behave if the expression evaluates to UNKNOWN due to a NULL value?chk_salary from a table named employees in MySQL 8.0+?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| UNIQUE Constraint & Candidate Keys | Inserting Records with INSERT INTO |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.