MySQL — Default
Basic Default
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(100),
status VARCHAR(20) DEFAULT 'active',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Named Default
CREATE TABLE orders (
id INT PRIMARY KEY,
priority VARCHAR(10) DEFAULT 'medium' CONSTRAINT df_priority,
quantity INT DEFAULT 1
);
Adding Default After Creation
ALTER TABLE users ALTER COLUMN status SET DEFAULT 'active';
Dropping Default
ALTER TABLE users ALTER COLUMN status DROP DEFAULT;
Default Expressions
| Expression | Result |
|---|---|
DEFAULT 'active' | Fixed string |
DEFAULT CURRENT_TIMESTAMP | Current date/time |
DEFAULT 0 | Zero |
DEFAULT NULL | NULL |
DEFAULT (UUID()) | Generated UUID |
INSERT with DEFAULT
-- Uses the default value
INSERT INTO users (id, name) VALUES (1, 'Alice');
-- Explicitly use default
INSERT INTO users (id, name, status) VALUES (2, 'Bob', DEFAULT);
Mini Practice
Write SQL code that:
- Creates a table with DEFAULT constraints
- Uses CURRENT_TIMESTAMP as a default
- Inserts a row relying on the default
- Drops a default constraint
Up Next
Continue with Auto Increment — generating sequential numbers automatically.
Related Topics
Frequently Asked Questions about Default
What is Default in MySQL?
Default is a fundamental concept in MySQL. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Default?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Default.
Why is Default important in MySQL?
Default is essential for MySQL development. Understanding this concept will help you write better code and solve real-world problems more effectively.