When designing a database, you often want to ensure that certain columns automatically have a default value if no value is provided during an INSERT operation. The DEFAULT
constraint is a powerful tool in SQL that allows you to set default values for columns when a value is not explicitly provided.
The DEFAULT
constraint provides a default value for a column when no value is specified during an INSERT operation. This can be helpful for columns where a default value makes sense, such as the status of an order, the creation date of a record, or a predefined number.
DEFAULT
value is automatically used.DEFAULT
values ensure that columns do not contain NULL
unless explicitly allowed, improving data consistency.You can define the DEFAULT
constraint when creating or modifying a table. It can be used to assign a constant value, or even a function call (like CURRENT_DATE
or NOW()
) for dynamic default values.
CREATE TABLE table_name (
column1 datatype DEFAULT default_value,
column2 datatype DEFAULT default_value,
...
);
column1, column2
: The columns where the DEFAULT
constraint is applied.default_value
: The default value to be assigned if no value is provided during an INSERT operation.
CREATE TABLE orders (
order_id INT PRIMARY KEY,
order_date DATE DEFAULT CURRENT_DATE,
status VARCHAR(50) DEFAULT 'Pending'
);
In this example:
order_date
will automatically be set to the current date if no date is provided.status
will default to 'Pending'
if no status is provided.
CREATE TABLE products (
product_id INT PRIMARY KEY,
product_name VARCHAR(100),
stock_quantity INT DEFAULT 0
);
Here, if no stock_quantity
is provided during an INSERT, the value will default to 0.
When inserting data into a table with a DEFAULT
constraint, you can omit the column(s) that have a default value, and the database will automatically assign the default value.
INSERT INTO orders (order_id, status)
VALUES (1, 'Shipped');
In this case:
order_date
will automatically be set to the current date (because of DEFAULT CURRENT_DATE
).status
is explicitly set to 'Shipped'
, overriding the default value.
INSERT INTO products (product_id, product_name)
VALUES (1, 'Laptop');
In this case:
stock_quantity
will automatically default to 0
(because of the DEFAULT 0
constraint).If you need to add a DEFAULT
constraint to an existing column in a table, you can do so using the ALTER TABLE
statement.
ALTER TABLE table_name
ALTER COLUMN column_name SET DEFAULT default_value;
ALTER TABLE employees
ALTER COLUMN hire_date SET DEFAULT CURRENT_DATE;
This adds a DEFAULT
value of the current date to the hire_date
column of the employees
table.
If you no longer need the default value for a column, you can remove the DEFAULT
constraint using the ALTER TABLE
statement.
ALTER TABLE table_name
ALTER COLUMN column_name DROP DEFAULT;
ALTER TABLE products
ALTER COLUMN stock_quantity DROP DEFAULT;
This removes the DEFAULT
value for the stock_quantity
column, meaning the column will no longer automatically receive a default value.
The DEFAULT
constraint offers several advantages that help streamline data entry and ensure data consistency:
DEFAULT
constraint can be used to define values that follow business rules (e.g., setting a default status to "Pending" when creating an order).NULL
unless specifically allowed.The DEFAULT
constraint is commonly used in the following scenarios:
Timestamps: Automatically setting the current timestamp or date when a record is created.
CREATE TABLE logs (
log_id INT PRIMARY KEY,
log_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Status Fields: Setting a default status when records are created.
CREATE TABLE tasks (
task_id INT PRIMARY KEY,
task_name VARCHAR(100),
status VARCHAR(50) DEFAULT 'Not Started'
);
Numeric Counters: Initializing numeric columns to a default value like 0, useful for counters or quantities.
CREATE TABLE inventory (
item_id INT PRIMARY KEY,
item_name VARCHAR(100),
stock_quantity INT DEFAULT 0
);
Flags: Setting default boolean or flag values, such as marking a field as active by default.
CREATE TABLE users (
user_id INT PRIMARY KEY,
username VARCHAR(100),
is_active BOOLEAN DEFAULT TRUE
);
While the DEFAULT
constraint is incredibly useful, there are a few things to keep in mind:
DEFAULT
constraint works well for static values but can’t dynamically generate data based on other fields or complex expressions.DEFAULT
constraint is typically used to provide a default value for non-NULL columns, but you can't apply it to columns that allow NULL
unless you specifically want to prevent NULL
values.DEFAULT
constraint is supported by most relational databases, but syntax may vary slightly depending on the database system.