LearnContact
Lesson 1815 min read

Default Constraint

Learn how the DEFAULT constraint automatically supplies a fallback value for a column when no value is provided on insert.

Introduction

Not every column needs a value supplied on every insert. Many columns have a sensible "normal" value that should be used automatically when nothing else is specified — like a new user account starting as "active," or a row recording the exact moment it was created.

The DEFAULT constraint lets you configure exactly that: a fallback value MySQL fills in automatically whenever a column is left out of an INSERT statement.

What You Will Learn
  • What the DEFAULT constraint does.
  • How to set a default value for a column.
  • How to default a timestamp column to the current date and time.
  • What happens when you omit a defaulted column from an INSERT.
  • How to override a default when you do want to supply your own value.

What is a Default Constraint?

DEFAULT specifies a value that MySQL automatically stores in a column when an INSERT statement does not explicitly provide one. It does not stop you from supplying your own value — it only kicks in when the column is left out entirely.

Note

DEFAULT is different from NOT NULL. NOT NULL only rejects missing values; DEFAULT actually supplies one automatically, so the column never ends up empty in the first place.

Defining a Default Value

You attach DEFAULT directly to a column definition, followed by the value you want MySQL to use.

CREATE TABLE students (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    status VARCHAR(20) DEFAULT 'active'
);

Whenever a new row is inserted without specifying "status," MySQL will automatically store the text 'active' in that column.

Worked Example: Status Column

Let's insert a row while deliberately leaving out the "status" column.

INSERT INTO students (id, name) VALUES (1, 'Alex');

SELECT * FROM students;
Result
+----+------+--------+
| id | name | status |
+----+------+--------+
| 1  | Alex | active |
+----+------+--------+

Notice the "status" column was never mentioned in the INSERT statement, yet it was automatically filled with 'active' — the default we defined.

Defaulting Timestamps

A very common use of DEFAULT is recording when a row was created, using CURRENT_TIMESTAMP instead of a literal value.

CREATE TABLE students (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    status VARCHAR(20) DEFAULT 'active',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

INSERT INTO students (id, name) VALUES (2, 'Sam');

SELECT * FROM students WHERE id = 2;
Result
+----+------+--------+---------------------+
| id | name | status | created_at          |
+----+------+--------+---------------------+
| 2  | Sam  | active | 2026-07-26 10:15:32 |
+----+------+--------+---------------------+

MySQL evaluated CURRENT_TIMESTAMP at the exact moment the row was inserted, so "created_at" automatically records when that student row was added — no application code needed to compute it.

Overriding a Default

A default value is only used when the column is omitted. If you explicitly provide a value, MySQL uses that instead.

INSERT INTO students (id, name, status) VALUES (3, 'Priya', 'inactive');

SELECT * FROM students WHERE id = 3;
Result
+----+-------+----------+---------------------+
| id | name  | status   | created_at          |
+----+-------+----------+---------------------+
| 3  | Priya | inactive | 2026-07-26 10:16:05 |
+----+-------+----------+---------------------+

Here "status" is 'inactive' because we explicitly supplied it, while "created_at" still fell back to its default since we did not mention it.

Common Mistakes

Avoid These Mistakes
  • Thinking DEFAULT fills in a value even when you explicitly pass NULL — it does not; an explicit NULL is still stored as NULL (unless the column is also NOT NULL, which would cause an error).
  • Forgetting to quote string defaults, e.g. writing DEFAULT active instead of DEFAULT 'active'.
  • Expecting DEFAULT CURRENT_TIMESTAMP to update on every UPDATE — by itself it only applies on INSERT; MySQL needs ON UPDATE CURRENT_TIMESTAMP added separately for that.
  • Using a default that hides missing data, when an error would actually be more appropriate for that column.

Key Takeaways

  • DEFAULT supplies an automatic fallback value when a column is left out of an INSERT.
  • It only applies when the column is omitted, not when NULL is explicitly given.
  • DEFAULT CURRENT_TIMESTAMP is the standard way to auto-record when a row was created.
  • You can always override a default by explicitly providing your own value.
  • DEFAULT reduces repetitive application code and keeps sensible fallback values consistent.

Summary

The DEFAULT constraint removes the burden of always specifying every column value. It quietly supplies sensible fallback values — like an 'active' status or the current timestamp — whenever an INSERT statement omits a column.

In this lesson, you saw how to define defaults, watched MySQL apply them automatically on insert, and learned how to override them when needed. Next, you'll learn how the CHECK constraint validates data before MySQL allows it to be stored.

Lesson 18 Completed
  • You understand what the DEFAULT constraint does.
  • You can set default values, including CURRENT_TIMESTAMP.
  • You know defaults only apply when a column is omitted.
  • You are ready to learn about the CHECK constraint.
Next Lesson →

Check Constraint