SQL Topics
DEFAULT
title: DEFAULT
The DEFAULT constraint assigns a predefined value to a column when you insert a row without providing a value for that column.
It reduces repetitive INSERT logic and ensures consistent data across records.
What a DEFAULT Constraint Does
DEFAULT supplies a value automatically during INSERT.
CREATE TABLE Tasks (
TaskID INT PRIMARY KEY,
Title VARCHAR(255),
Status VARCHAR(20) DEFAULT 'Pending',
CreatedAt DATE DEFAULT CURRENT_DATE
);INSERT INTO Tasks (TaskID, Title)
VALUES (1, 'Review Database Schema');The Status becomes 'Pending' and CreatedAt becomes today's date.
Defining a DEFAULT Constraint
Set it during table creation:
Status VARCHAR(20) DEFAULT 'Pending'Add it later:
ALTER TABLE Tasks
ALTER Status SET DEFAULT 'Pending';Remove it later:
ALTER TABLE Tasks
ALTER Status DROP DEFAULT;DEFAULT Values
DEFAULT supports:
- Constants:
'Active',0,'N/A' - Expressions:
CURRENT_DATE,CURRENT_TIMESTAMP,NOW()
Real-World Uses
| Column | Example DEFAULT |
|---|---|
Status | 'Active' |
Role | 'User' |
Priority | 'Medium' |
CreatedAt | CURRENT_DATE |
IsDeleted | FALSE |
Pseudocode example:
Frequently Asked Questions
Q: Does DEFAULT apply to UPDATE statements? A: No. DEFAULT applies only when a column is omitted in an INSERT. Use COALESCE in an UPDATE if needed.
Q: Can DEFAULT return different values per row? A: Yes, when using expressions like CURRENT_DATE or NOW(), the database evaluates the expression per row.
Q: What happens if I explicitly insert NULL? A: DEFAULT is ignored. The column stores NULL, which will fail if it is also NOT NULL.
Summary
DEFAULTprovides automatic values when none are supplied.- Use it to reduce boilerplate and enforce consistent initial states.
- Combine it with
NOT NULLto guarantee a usable value. - Common defaults include status flags and timestamps.
Next Step
See Composite Keys to combine multiple columns into a primary key, or return to the SQL Constraints overview.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for DEFAULT.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this SQL topic.
Search Terms
sql, sql complete guide, sql tutorial, sql notes, complete, guide, constraints, default
Related SQL Topics