Setting default values in SQL is a powerful technique for ensuring that a column has a predefined value if no explicit value is provided during an insert operation. This feature is helpful when working with optional columns, reducing the need for null values and supporting consistent data entry.
In SQL, a default value is a preset value assigned to a column whenever a new row is inserted without specifying a value for that column. For example, if we define a column status
with a default value of "Pending"
, then when we add a new row without specifying status
, it will automatically be set to "Pending"
.
The ability to define default values reduces the need for nulls and ensures data consistency.
Default values can significantly enhance database design and data quality. Here are some key benefits:
In SQL, you can set a default value for a column by using the DEFAULT
keyword in the CREATE TABLE
or ALTER TABLE
statements.
CREATE TABLE table_name (
column_name data_type DEFAULT default_value,
...
);
ALTER TABLE table_name
MODIFY column_name data_type DEFAULT default_value;
column_name
: The name of the column.data_type
: The column’s data type (e.g., INT
, VARCHAR
).default_value
: The value to be used as the default.Let’s start by creating a new table called Orders
with a default value set for the status
column.
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
CustomerID INT,
OrderDate DATE,
Status VARCHAR(20) DEFAULT 'Pending'
);
In this example:
Status
column is assigned a default value of 'Pending'
.Status
INSERT INTO Orders (OrderID, CustomerID, OrderDate)
VALUES (1, 123, '2024-10-10');
OrderID | CustomerID | OrderDate | Status |
---|---|---|---|
1 | 123 | 2024-10-10 | Pending |
Status
was not specified, it defaults to 'Pending'
.2. Insert Specifying Status
INSERT INTO Orders (OrderID, CustomerID, OrderDate, Status)
VALUES (2, 456, '2024-10-11', 'Completed');
OrderID | CustomerID | OrderDate | Status |
---|---|---|---|
1 | 123 | 2024-10-10 | Pending |
2 | 456 | 2024-10-11 | Completed |
Here, Status
is set to 'Completed'
explicitly, overriding the default value.
You can add a default value to an existing table column using the ALTER TABLE
statement.
Suppose we have an existing Employees
table
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(100),
Department VARCHAR(50)
);
To add a default value of "General"
to the Department
column:
ALTER TABLE Employees
MODIFY Department VARCHAR(50) DEFAULT 'General';
Now, if you insert a new employee without specifying the department, it will default to "General"
.
Default values can vary depending on the data type of the column. Here are a few examples:
DEFAULT 'Not Specified'
DEFAULT 0
DEFAULT CURRENT_DATE
DEFAULT 1
1
might indicate true
or active
.SQL allows certain expressions as default values. For instance, you can use CURRENT_DATE
to automatically set a date column to the current date.
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
OrderDate DATE DEFAULT CURRENT_DATE
);
In this example:
OrderDate
, it defaults to the current date.If you want to override the default value, simply provide a value for that column during the INSERT
operation.
INSERT INTO Orders (OrderID, OrderDate) VALUES (3, '2024-12-01');
In this example:
OrderDate
is explicitly set to 2024-12-01
, ignoring the default CURRENT_DATE
.Default values are commonly used in scenarios like:
"Pending"
or "New"
for orders, tasks, etc.0
for inactive or 1
for active in Boolean or status fields.Consider an Inventory
table where items are added with a default InStock
value of TRUE
CREATE TABLE Inventory (
ItemID INT PRIMARY KEY,
ItemName VARCHAR(50),
Quantity INT DEFAULT 0,
InStock BOOLEAN DEFAULT TRUE
);
Setting default values in SQL is a fundamental technique for improving data consistency, reducing the need for null values, and simplifying data entry. By strategically using default values, you can create more robust, reliable databases that maintain data integrity with minimal manual intervention. Happy Coding!❤️