The DEFAULT constraint is used to assign a default value to a column when a new record is inserted, provided no value is explicitly specified for that column. It ensures that columns have a predefined value when data is inserted, making data entry more efficient and helping enforce consistency.
Key Characteristics of the DEFAULT Constraint:
- Automatic Value Assignment: If no value is provided for the column during an
INSERToperation, the default value will be used. - Use of System Functions: You can use system functions like
CURRENT_DATE()orNOW()to set default values based on the current date or time. - Optional Value: If a value is provided during an insert, it will override the default value.
Using the DEFAULT Constraint
1. DEFAULT Constraint on Table Creation
You can define the DEFAULT constraint when creating a table. This will automatically assign the default value to the column if no value is provided.
Example: DEFAULT Constraint on a Column
CREATE TABLE Persons (
ID INT NOT NULL,
LastName VARCHAR(255) NOT NULL,
FirstName VARCHAR(255),
Age INT,
City VARCHAR(255) DEFAULT 'Sandnes'
);
In this example:
- The
Citycolumn has a default value of'Sandnes'. If no value is provided for theCitycolumn during an insert, the value'Sandnes'will be used.
Example: Using System Functions with DEFAULT
CREATE TABLE Orders (
ID INT NOT NULL,
OrderNumber INT NOT NULL,
OrderDate DATE DEFAULT CURRENT_DATE()
);
In this example:
- The
OrderDatecolumn has a default value set to the current date using theCURRENT_DATE()function. This ensures that if noOrderDateis provided, the current date will be inserted automatically.
2. DEFAULT Constraint on Existing Table
You can add a DEFAULT constraint to an existing column using the ALTER TABLE statement.
Example: Add a DEFAULT Constraint on a Column
ALTER TABLE Persons
ALTER COLUMN City SET DEFAULT 'Sandnes';
In this example:
- The
Citycolumn is given a default value of'Sandnes'after the table has already been created. If no value is provided forCity,'Sandnes'will be used by default.
3. Dropping a DEFAULT Constraint
If you no longer want a column to have a default value, you can remove the DEFAULT constraint using the ALTER TABLE statement.
Example: Drop a DEFAULT Constraint
ALTER TABLE Persons
ALTER COLUMN City DROP DEFAULT;
In this example:
- The
DEFAULTconstraint on theCitycolumn is removed, and the column will no longer automatically populate with a default value.
Important Notes:
- Only One Default: A column can only have one
DEFAULTvalue. If you attempt to define more than one default value for the same column, an error will occur. - NULL Values: If you define a column with a
DEFAULTvalue, and the column is nullable, the default value will be used only when the column is not provided a value duringINSERT. If the column is explicitly set toNULL, the default value will not be applied. - Automatic Value: The
DEFAULTvalue is only applied if no value is provided. If a value is supplied during anINSERTstatement, that value will be used instead of the default.
Summary of DEFAULT Constraint Usage:
- On Table Creation: The
DEFAULTconstraint is defined when creating the table to assign default values to columns. - On Table Modification: You can add or modify a
DEFAULTconstraint using theALTER TABLEstatement after the table is created. - Dropping a DEFAULT Constraint: If you want to remove a
DEFAULTconstraint from a column, you can do so using theALTER TABLEstatement.
The DEFAULT constraint is particularly useful for ensuring that columns always have a value, even if the user does not explicitly provide one, and can be especially helpful for dates, statuses, or other commonly used values.