</>
Skip to content
MySQL lessons (39/48)

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

ExpressionResult
DEFAULT 'active'Fixed string
DEFAULT CURRENT_TIMESTAMPCurrent date/time
DEFAULT 0Zero
DEFAULT NULLNULL
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:

  1. Creates a table with DEFAULT constraints
  2. Uses CURRENT_TIMESTAMP as a default
  3. Inserts a row relying on the default
  4. 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.