What is Default Constraint in SQL?

Learn via video course
FREE
View all courses
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
Topics Covered

The DEFAULT constraint provides a default or a fixed value into the column of a table and gets overridden when the user enters a new value. The DEFAULT function is used when you insert a new row into the table without specifying any value for the column. After adding the DEFAULT constraint to the column of the table, each time when the user does not provide the value of the column then, the default value gets inserted.

In this post, you will learn about the DEFAULT constraint in SQL and how you can use it to insert a default value into a column of a table with the help of examples and use cases.

How to Add Default Constraints in SQL?

DEFAULT constraint in SQL can be added to the table in two ways:

1. While creating a table, we can add the DEFAULT constraint to the column of a table.

Suppose you want to create an employee table, having ID, NAME, and SALARY as the fields, and you don't want the SALARY field to be empty in any case, so we will specify the DEFAULT salary for each employee with the help of the DEFAULT constraint in SQL.

The syntax for adding the DEFAULT constraint while creating a table is:

Here, we have created a table named employee, having some columns as id, name, and salary. Also, you can see that against the column name, we have mentioned the data type of that column, and after that, we have written the constraint name that we want to apply to that column.

For the id column, we have added the NOT NULL and the PRIMARY KEY constraints, which are stating that the employee id can't be NULL and should behave as a PRIMARY KEY. Read more about NOT NULL in this article.

Let's see the salary field, where we have specified its data type as DECIMAL and have added the constraint of DEFAULT value. We have provided the default salary value of each employee as 10000.0, so for the cases, where we don't have the salary value of any employee, then for that employee, the default salary of 10000.0 rupees will be added.

Now, let's see the second way to add the DEFAULT constraint to the table.

2. If the employee table has already been created, then to add a DEFAULT constraint to the SALARY column, you would write a query like the one which is shown below:

Here, we have modified the already created table column SALARY, and have added the DEFAULT constraint of having the fixed default salary of each employee as 10000.0 rupees.

How to Remove Default Constraints in SQL?

To remove an existing DEFAULT constraint on any column, you can use the ALTER TABLE statement along with the DROP keyword in SQL. ALTER TABLE is used because the table is being modified or altered, and DROP is used to remove the constraint from the column of the table.

The syntax for dropping the DEFAULT constraint is:

You can use the following SQL query to drop the constraint in the employee table:

This will remove the DEFAULT constraint of the SALARY column in the employee table.

NOTE: Dropping the default constraint will be applied to the new rows, and it will not affect the current data in the table.

How to Insert Values for Default Constraint in SQL?

Let's take the above example of the employee table, where we have added the DEFAULT constraint to the SALARY column. Now, let's see how we can insert the values for the default constraint in SQL.

See the following INSERT statements that will add new employee details to the employee table:

Now, let's see how our table's data looks by using the SELECT command.

NOTE: * represents all the columns, which means we are selecting or retrieving all the data from the given table.

Output:

idnamesalary
1Kishore Krishna8000.2
2Gripa Shier78231.9
3Naveen Mishra10000.0
4Shipla Shukla29310.0
5Nandini Garg10000.0

As we can see from the output table above, for the employees having the id as 3 and 5, we have not provided any value for the salary field, so the default salary value of 10000.0 is being inserted, and for the other employees, we have given the salary value while inserting the data, so, the default value is not needed in those rows.

Examples for Default Constraint in SQL:

Let's take an example of building the table and dealing with the DEFAULT constraint from very scratch.

Let's create an employees table, having employee_id, employee_name, joining_date, location, and salary as the fields.

The syntax for creating the table is:

Now, let's insert some data into our table:

Now, let's see how our table's data looks by using the SELECT command.

Output:

emplpyee_idemployee_namejoining_datelocationsalary
1Kishore Krishna2021/10/22Noida8000.21
2Tina Mehta2022/09/21Chennai32344.72
3Shipri Goel2022/06/26Chennai100000.00
4Nalin Gupta2002/07/19Chennai10000.00
5Kanchan Goyal2022/06/26Jaipur20000.90

As we can see from the output table above, for the rows where the data for the joining_date has not been provided, we have added the GETDATE() function as the default value, which returns the current date and time. For the location field, we have added the default location for the employees as Chennai, and for the salary field, the default salary is 10000.00.

Let's suppose we want to add a new column as employee_retirement_age, along with the default value as 60.

To add a column in a table, we can use the following syntax:

In our case,

Now, let's insert some more data into our table:

Now, let's see how our table's data looks by using the SELECT command.

Output:

emplpyee_idemployee_namejoining_datelocationsalaryemployee_retirement_age
1Kishore Krishna2021/10/22Noida8000.21
2Tina Mehta2022/09/21Chennai32344.72
3Shipri Goel2022/06/26Chennai100000.00
4Nalin Gupta2002/07/19Chennai10000.00
5Kanchan Goyal2022/06/26Jaipur20000.90
6Kashish2009/01/02Beawer892300.0167
7Tina Mehta2022/09/21Chennai32344.7260

NOTE: The added default constraint will be applied to the new rows, and it will not affect the current data of the table.

As we can see for the rows where we have not added any value for the employee retirement age, the default value of 60 is being displayed.

We can drop the constraints from the columns if they are unnecessary.

Learn more about advanced concepts of SQL

Conclusion:

  • The main goal of the DEFAULT Constraint is to add a column with a default and a fixed value.

  • The DEFAULT constraint inserts a default value into a table column when you insert a new row into the table without specifying the value for the column.

  • The value will be added to all new records after the DEFAULT constraint has been added to the column of the table.

  • We can add the default constraint while creating the table, or you can add the default constraint to the existing table's column.

  • We can drop the default constraint by altering the table and dropping the constraint from the column of the table.

  • Dropping the default constraint will be applied to the new rows, and it will not affect the current data in the table.

  • The default value can be a literal value, integer value, decimal value, an expression, or a SQL Function, such as GETDATE(), which is used to return the current database system date and time.

  • The default value can also be a NULL value.