Understanding DEFAULT Constraints in SQL: A Complete Guide

Rumman Ansari   Software Engineer   2024-07-19 02:14:02   5868  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

The DEFAULT constraint provides a default value to a column when the INSERT INTO statement does not provide a specific value.

Syntax: Altering an existing column to add a default constraint:

<span class="pln">
ALTER TABLE </span><span class="pun">{</span><span class="pln"> TABLE_NAME </span><span class="pun">}</span><span class="pln">
ADD CONSTRAINT </span><span class="pun">{</span><span class="pln"> CONSTRAINT_NAME </span><span class="pun">}</span><span class="pln">
DEFAULT </span><span class="pun">{</span><span class="pln"> DEFAULT_VALUE </span><span class="pun">}</span><span class="pln"> FOR </span><span class="pun">{</span><span class="pln"> EXISTING_COLUMN_NAME </span><span class="pun">}</span><span class="pln">
</span>

Syntax: Adding a new column, with default value, to an existing table:

<span class="pln">
ALTER TABLE </span><span class="pun">{</span><span class="pln"> TABLE_NAME </span><span class="pun">}</span><span class="pln"> 
ADD </span><span class="pun">{</span><span class="pln"> COLUMN_NAME </span><span class="pun">}</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> DATA_TYPE </span><span class="pun">}</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> NULL </span><span class="pun">|</span><span class="pln"> NOT NULL </span><span class="pun">}</span><span class="pln"> 
CONSTRAINT </span><span class="pun">{</span><span class="pln"> CONSTRAINT_NAME </span><span class="pun">}</span><span class="pln"> DEFAULT </span><span class="pun">{</span><span class="pln"> DEFAULT_VALUE </span><span class="pun">}</span><span class="pln">
</span>

Example

For example, the following SQL creates a new table called "Customers" and adds five columns. Here, the SALARY column is set to 10000.00 by default, so in case the INSERT INTO statement does not provide a value for this column, then by default this column would be set to 10000.00.

CREATE TABLE Customers(
   ID   INT              NOT NULL,
   NAME VARCHAR (20)     NOT NULL,
   AGE  INT              NOT NULL,
   ADDRESS  CHAR (25) ,
   SALARY   DECIMAL (18, 2) DEFAULT 10000.00,       
   PRIMARY KEY (ID)
);

If the CUSTOMERS 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 in the code block below.

ALTER TABLE Customers

MODIFY SALARY  DECIMAL (18, 2) DEFAULT 10000.00; 

Drop Default Constraint

To drop a DEFAULT constraint, use the following SQL query.

ALTER TABLE Customers
   ALTER COLUMN SALARY DROP DEFAULT;

The DEFAULT constraint is used to provide a default value for a column. The default value will be added to all new records IF no other value is specified.

Example for Default Constraints

Get the TablePerson from this tutorial

Code: The following command will add a default constraint, DF_TablePerson_GenderID.

<span class="pln">
ALTER TABLE </span><span class="typ">TablePerson</span><span class="pln">
ADD CONSTRAINT DF_TablePerson_GenderID
DEFAULT </span><span class="lit">1</span><span class="pln"> FOR </span><span class="typ">GenderID</span><span class="pln">
</span>

The insert statement below does not provide a value for GenderId column, so the default of 1 will be inserted for this record.

Code:

<span class="pln">
</span><span class="typ">Insert</span><span class="pln"> </span><span class="kwd">into</span><span class="pln"> </span><span class="typ">TablePerson</span><span class="pun">(</span><span class="pln">ID</span><span class="pun">,</span><span class="typ">Name</span><span class="pun">,</span><span class="typ">Email</span><span class="pun">)</span><span class="pln">
 values</span><span class="pun">(</span><span class="lit">5</span><span class="pun">,</span><span class="str">'Sam'</span><span class="pun">,</span><span class="str">'s@s.com'</span><span class="pun">)</span><span class="pln">
</span>

On the other hand, the following insert statement will insert NULL, instead of using the default.

Code: SQL Server

<span class="pln">
</span><span class="typ">Insert</span><span class="pln"> </span><span class="kwd">into</span><span class="pln"> </span><span class="typ">TablePerson</span><span class="pun">(</span><span class="pln">ID</span><span class="pun">,</span><span class="typ">Name</span><span class="pun">,</span><span class="typ">Email</span><span class="pun">,</span><span class="typ">GenderID</span><span class="pun">)</span><span class="pln">
 values </span><span class="pun">(</span><span class="lit">6</span><span class="pun">,</span><span class="str">'Dan'</span><span class="pun">,</span><span class="str">'d@d.com'</span><span class="pun">,</span><span class="pln">NULL</span><span class="pun">)</span><span class="pln">
</span>

To drop a constraint

Code: SQL Server

<span class="pln">
ALTER TABLE </span><span class="pun">{</span><span class="pln"> TABLE_NAME </span><span class="pun">}</span><span class="pln"> 
DROP CONSTRAINT </span><span class="pun">{</span><span class="pln"> CONSTRAINT_NAME </span><span class="pun">}</span><span class="pln">
</span>

No Questions Data Available.
No Program Data.

Stay Ahead of the Curve! Check out these trending topics and sharpen your skills.