MySQL DEFAULT Constraint
Introduction
The DEFAULT constraint assigns a default value to a column when no value is specified during insertion.
This ensures that the column always has a value.
Example
CREATE TABLE users (
id INT,
status VARCHAR(20) DEFAULT 'active'
);
If no value is provided for statusIt will automatically be set to active.
Example Insert
INSERT INTO users (id) VALUES (1);
Result:
| id | status |
|---|---|
| 1 | active |
Benefits
-
Prevents missing values
-
Simplifies data insertion
-
Maintains consistency
Key Points
-
DEFAULT assigns automatic values
-
Applied when no value is provided
-
Helps maintain consistent data