Home »
SQL
Use of DEFAULT Constraint in SQL
In this article, we are going to learn about DEFAULT constraint in SQL.
Submitted by Bharti Parmar, on January 17, 2019
DEFAULT constraint is used to insert default value into a column on a table and if no any value is stored in any place of a column then default value will be added into it.
How to use DEFAULT with example?
CREATE TABLE EMployees
(E_Id int NOT NULL,E_Name varchar(25) NOT NULL,
Contact number(10),Address varchar(50),
City varchar(20) DEFAULT 'Gwaloir');
We can also achieve DEFAULT value property by using GETDATE() function:
CREATE TABLE Employees
(E_Id int NOT NULL,E_name varchar(50),
E_Join_Date date DEFAULT GETDATE());
USE of ALTER to ADD DEFAULT constraint when table is already created:
There is different way to add DEFAULT constraint in different database language:
ALTER table EmployeesALTER E_name SET DEFAULT 'Bharti';
SQL
ALTER TABLE Employees ALTER COLUMN City SET DEFAULT 'Gwalior';
Oracle
ALTER TABLE Employees MODIFY Contact DEFAULT '0000';
How to DROP DEFAULT constraint from a table?
ALTER TABLE Employees ALTER COLUMN City DROP DEFAULT;
Conclusion:
In this article we have learnt what DEFAULT constraint is, how to use it with example and how we can alter and drop DEFAULT on a table? If you have any doubt/query, feel free to ask in comment section.