đ¯ The DEFAULT constraint in SQL is used to automatically assign a predefined value to a column when no value is provided during an INSERT operation. It helps ensure consistency, reduces manual data entry, and prevents unnecessary NULL values.
đ What is the DEFAULT Constraint?
A DEFAULT constraint specifies a value that the database should use automatically whenever an INSERT statement omits that column. If a value is explicitly provided, the supplied value is stored instead of the default.
Information
đ¯ Why Use DEFAULT?
The DEFAULT constraint simplifies data entry and enforces consistent default values across records.
- đ Automatically populate common values.
- đ Reduce repetitive data entry.
- đ Improve data consistency.
- đ Minimize unnecessary NULL values.
- đ Simplify application development.
đ Basic Syntax
DEFAULT Constraint Syntax
CREATE TABLE table_name
(
column_name data_type DEFAULT default_value
);đĄ Create a Table with DEFAULT
Create a Students table where the Status column automatically stores 'Active' if no value is provided.
DEFAULT Example
CREATE TABLE Students
(
StudentID INT PRIMARY KEY,
Name VARCHAR(100),
Department VARCHAR(100),
Status VARCHAR(20) DEFAULT 'Active'
);â INSERT Without Providing the Default Column
Since no value is supplied for Status, the default value is used automatically.
Using the Default Value
INSERT INTO Students
(StudentID, Name, Department)
VALUES
(101, 'Alice', 'Computer Science');Result:
| StudentID | Name | Department | Status |
|---|---|---|---|
| 101 | Alice | Computer Science | Active |
âī¸ INSERT with an Explicit Value
If a value is explicitly provided, it overrides the default.
Override the Default
INSERT INTO Students
(StudentID, Name, Department, Status)
VALUES
(102, 'Bob', 'Mathematics', 'Inactive');The Status column stores 'Inactive' instead of the default value.
đ Using Built-in Functions as DEFAULT Values
Many database systems allow built-in functions as default values, such as the current date or current timestamp.
DEFAULT CURRENT_DATE
CREATE TABLE Orders
(
OrderID INT PRIMARY KEY,
CustomerName VARCHAR(100),
OrderDate DATE DEFAULT CURRENT_DATE
);DEFAULT CURRENT_TIMESTAMP
CREATE TABLE LoginHistory
(
LoginID INT PRIMARY KEY,
Username VARCHAR(100),
LoginTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);Important
â Add a DEFAULT Constraint to an Existing Table
You can add a default value after a table has already been created. The syntax differs among database systems.
SQL Server Example
ALTER TABLE Students
ADD CONSTRAINT DF_Students_Status
DEFAULT 'Active' FOR Status;MySQL Example
ALTER TABLE Students
ALTER Status
SET DEFAULT 'Active';â Remove a DEFAULT Constraint
Removing a default constraint is database-specific.
MySQL Example
ALTER TABLE Students
ALTER Status
DROP DEFAULT;Warning
đ Common DEFAULT Examples
| Column | Default Value | Purpose |
|---|---|---|
| Status | 'Active' | Automatically assign an active status. |
| Quantity | 0 | Initialize numeric values. |
| Country | 'India' | Provide a common default location. |
| CreatedDate | CURRENT_DATE | Store the creation date automatically. |
| CreatedAt | CURRENT_TIMESTAMP | Record the creation timestamp. |
âī¸ DEFAULT vs NOT NULL
| Feature | DEFAULT | NOT NULL |
|---|---|---|
| Automatically Provides a Value | â Yes | â No |
| Requires a Value | â Not necessarily | â Yes |
| Prevents NULL | Only if the default is used. | â Always |
| Main Purpose | Assign a default value. | Require a value. |
đŧ Real-World Example
In an e-commerce application, every newly created order should initially have the status 'Pending' unless another status is explicitly specified.
Orders Table
CREATE TABLE Orders
(
OrderID INT PRIMARY KEY,
CustomerName VARCHAR(100),
OrderStatus VARCHAR(20) DEFAULT 'Pending',
OrderDate DATE DEFAULT CURRENT_DATE
);This design automatically assigns a default order status and records the order date when a new order is created.
â ī¸ Common Mistakes
- â Assuming the default value replaces explicitly supplied values.
- â Expecting existing rows to automatically receive a newly added default.
- â Using invalid expressions as default values.
- â Assuming every database supports the same default functions and syntax.
Warning
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ DEFAULT automatically supplies a value when one is not provided.
- đ Explicitly provided values always override the default.
- đ Built-in functions such as CURRENT_DATE or CURRENT_TIMESTAMP are commonly used as defaults.
- đ Default syntax varies slightly across SQL database systems.
- đ Existing rows are generally unaffected when a new default is added.
- đ DEFAULT improves consistency and reduces repetitive data entry.