CHECK constraints in SQL: Judge the statement: "Common types of SQL CHECK constraints include range checks and limiting column values to allowed sets."
-
ACorrect
-
BIncorrect
-
COnly true with DEFERRABLE constraints
-
DOnly valid in ANSI SQL, not in vendors
-
EApplies only to numeric data types
Answer
Correct Answer: Correct
Explanation
Introduction / Context: CHECK constraints are declarative integrity rules enforced by the DBMS to restrict allowed values in a column or tuple. They are a key tool for maintaining data quality at the database level rather than relying solely on application code.
Given Data / Assumptions:
- The statement references two common patterns: range checks and enumerated/domain checks.
- We assume a mainstream SQL DBMS supporting CHECK (e.g., PostgreSQL, SQL Server, Oracle, MySQL 8+ with CHECK enforced).
Concept / Approach: A CHECK constraint evaluates a Boolean expression per row. Typical constraints include ranges (e.g., salary between 0 and 1e7), set membership (e.g., status in ('NEW','SHIPPED','CLOSED')), simple relational expressions (start_date <= end_date), and pattern checks (LIKE, basic functions). These are classic, widely used patterns for enforcing domain integrity.
Step-by-Step Solution:
Range example: CHECK (age >= 0 AND age <= 130)Domain example: CHECK (state IN ('CA','NY','TX'))Relational example: CHECK (start_date <= end_date)DBMS ensures any INSERT/UPDATE violating the expression is rejected.Verification / Alternative check: Inspect system catalogs to confirm constraints are stored and enforced; test with sample inserts to see violations blocked.
Why Other Options Are Wrong:
- Incorrect: Disagrees with standard practice.
- Deferrable only: Many CHECKs are NOT DEFERRABLE; deferrability is optional.
- Only ANSI, not vendors: Major vendors support CHECK; behavior nuances exist but concept holds.
- Only numeric: Strings, dates, and other types work too.
Common Pitfalls: Overusing triggers for validations that belong in CHECK; writing constraints that rely on subqueries (often disallowed in some DBMSs) instead of simpler per-row expressions.
Final Answer: Correct