NOT NULL and DEFAULT Constraints
NOT NULL and DEFAULT Constraints: Preventing Missing Data
In database design, missing or unrecorded information is represented by NULL. However, allowing NULL everywhere leads to complex, bug-prone queries where every filter must check IS NOT NULL. To build reliable databases, you must know when to strictly forbid missing data using NOT NULL and when to supply automatic fallbacks using DEFAULT.
1. The NOT NULL Constraint
The NOT NULL constraint enforces that a column must always be assigned a valid value during an INSERT or UPDATE statement. Attempting to insert a NULL value into a NOT NULL column triggers an error.
What is NULL Exactly?
NULLis not equal to zero (0).NULLis not equal to an empty string ("").NULLrepresents unknown, unrecorded, or missing data.- In SQL three-valued logic,
NULL = NULLevaluates toUNKNOWN, notTRUE!
2. The DEFAULT Constraint
The DEFAULT constraint supplies a predetermined fallback value if an INSERT statement does not explicitly provide a value for that column:
Testing Defaults During Insertion:
Output:
+------------+--------------------------------+-------------------+-----------------+------------------------+---------------+ | product_id | product_name | quantity_in_stock | is_discontinued | minimum_order_quantity | reorder_level | +------------+--------------------------------+-------------------+-----------------+------------------------+---------------+ | 1 | Wireless Mechanical Keyboard | 0 | 0 | 1 | 10 | +------------+--------------------------------+-------------------+-----------------+------------------------+---------------+
3. Dynamic Default Expressions in MySQL 8.0+
Traditionally, DEFAULT only accepted literal constants (e.g., DEFAULT 0, DEFAULT 'Active'). In modern MySQL 8.0+, you can use dynamic expressions enclosed in parentheses!
4. Modifying Constraints on Existing Tables
5. Best Practices & Common Pitfalls
- Default to NOT NULL Everywhere Possible: Unless a column genuinely represents optional or unknown information (e.g.,
shipped_datefor an order that hasn't shipped yet), mark columns asNOT NULL. This simplifies query logic and enables the optimizer to utilize indexes more efficiently. - Do Not Confuse Empty String with NULL: An empty string
""is a known string of zero length;NULLis an unknown value. Storing""in aNOT NULLcolumn succeeds, but may violate your business logic!
Multiple Choice Questions
1. In SQL three-valued logic, what is the result of the comparison expression NULL = NULL?
A. TRUE B. FALSE C. UNKNOWN D. 0 Answer: C Explanation: In standard SQL logic, NULL represents an unknown value, so comparing two unknowns yields UNKNOWN, not TRUE.
2. What happens if an INSERT statement does not provide a value for a column configured with DEFAULT 'Active'?
A. The insert query fails B. The string 'Active' is automatically assigned to that column C. The column is assigned NULL D. The column is left blank Answer: B Explanation: The DEFAULT constraint automatically populates the column with its specified default value when an INSERT omits it.
3. Which SQL statement alters an existing column named status to set its default value to 'Pending'?
A. UPDATE users SET DEFAULT = 'Pending'; B. ALTER TABLE users ALTER COLUMN status SET DEFAULT 'Pending'; C. SET DEFAULT 'Pending' ON users.status; D. CHANGE DEFAULT TO 'Pending' ON status; Answer: B Explanation: ALTER TABLE table_name ALTER COLUMN column_name SET DEFAULT 'value'; is the standard syntax to assign a new default value.
4. Why does marking columns as NOT NULL improve database performance?
A. It compresses data to 1 bit B. It eliminates null-check overhead and allows the query optimizer to utilize more compact index representations C. It doubles the CPU clock rate D. It prevents users from updating records Answer: B Explanation: Nullable columns require extra null-mask bits in index entries and table records, complicating query execution plans.
5. In MySQL 8.0+, how must dynamic function expressions (like UUID()) be written in a DEFAULT clause?
A. Enclosed in double quotes: DEFAULT "UUID()" B. Enclosed in parentheses: DEFAULT (UUID()) C. Preceded by an at sign: DEFAULT @UUID() D. Function defaults are not supported Answer: B Explanation: MySQL 8.0 requires that expression-based defaults be enclosed within parentheses, e.g., DEFAULT (UUID()).
UNIQUE Constraint & Candidate Keys
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Foreign Key & Referential Integrity | UNIQUE Constraint & Candidate Keys |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.