SQL -Structured Query Language - Sql Key & Constraint - Default Constraint Tutorial
DEFAULT Constraint is used, when we want to place a default value if no value is inserted.
For example, Suppose we want that the status of the employee should be active if no other value is inserted.
The syntax for Default Constraint-
CREATE TABLE user_detail
(
id int,
first_name varchar(50),
status varchar(50) DEFAULT 'ACTIVE'
);
In this, we define status ( value - ACTIVE ) with DEFAULT constraints while creating a user_detail table. With a DEFAULT constraint, it will insert value - ACTIVE, when no value is inserted in a status column.
Syntax For Adding DEFAULT Constraint With Alter Command-
ALTER TABLE user_detail
MODIFY status varchar(50) DEFAULT 'ACTIVE';
This syntax will add a DEFAULT Constraint in status with the value 'ACTIVE' after table creation.
Syntax For Dropping DEFAULT Constraint With Alter Command-
ALTER TABLE user_detail
ALTER status DROP DEFAULT;
This syntax will drop a DEFAULT Constraint from its column name.