SQL DEFAULT Constraint

A DEFAULT constraint in SQL is used to automatically assign a default value to a column if no value is provided during insertion.

Basic Syntax

CREATE TABLE table_name (
    column_name datatype DEFAULT default_value
);

Example (Orders Table)

CREATE TABLE Orders (
    order_id INT PRIMARY KEY,
    order_date DATE DEFAULT CURRENT_DATE,
    order_status VARCHAR(20) DEFAULT 'Pending'
);

Insert Without Specifying Default Columns

INSERT INTO Orders (order_id) VALUES (101);

Because:
order_date → automatically set to current date
order_status → automatically set to Pending


Why DEFAULT Constraint is Used
   • Saves time during data entry
   • Ensures consistent default values
   • Reduces NULL values

Short interview definition:
A DEFAULT constraint assigns a predefined value to a column when no value is specified during insertion.


Topics