Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
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.
NOT NULL ConstraintThe 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.
NULL Exactly?NULL is not equal to zero (0).NULL is not equal to an empty string ("").NULL represents unknown, unrecorded, or missing data.NULL = NULL evaluates to UNKNOWN, not TRUE!DEFAULT ConstraintThe DEFAULT constraint supplies a predetermined fallback value if an INSERT statement does not explicitly provide a value for that column:
Output:
+------------+--------------------------------+-------------------+-----------------+------------------------+---------------+ | product_id | product_name | quantity_in_stock | is_discontinued | minimum_order_quantity | reorder_level | +------------+--------------------------------+-------------------+-----------------+------------------------+---------------+ | 1 | Wireless Mechanical Keyboard | 0 | 0 | 1 | 10 | +------------+--------------------------------+-------------------+-----------------+------------------------+---------------+
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!
shipped_date for an order that hasn't shipped yet), mark columns as NOT NULL. This simplifies query logic and enables the optimizer to utilize indexes more efficiently."" is a known string of zero length; NULL is an unknown value. Storing "" in a NOT NULL column succeeds, but may violate your business logic!NULL = NULL?DEFAULT 'Active'?status to set its default value to 'Pending'?NOT NULL improve database performance?UUID()) be written in a DEFAULT clause?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Foreign Key & Referential Integrity | UNIQUE Constraint & Candidate Keys |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.