SQL Day 04: Adding Default Constraint

LO: Adding Default Constraint

Prerequisite: Creating Tables

In creating a table chapter we have created the table named tblEmployee which have the not null columns that are we always need to provide the values for these columns to insert the values.


In tblEmployee table ID, Name and Email are not null where as GenderID allow nulls.
Let’s write the query to insert the values in tblEmployee and select the all records after the successful insert.

Insert into tblEmployee (ID, Name, Email) values (5,'David','david@test.com')
select * from tblEmployee



You should see the NULL is inserted for the GenderID column because we did not give any value while inserting the data.
Now, look at the tblGender table. We have specified value 3 for unknown.

Let’s take an example suppose you did not know the gender of the person and you want 3 to be inserted whenever the gender is not defined. To implement this you need to set the default constraint.

So basically Default constraint is used to insert the default value if no other value is specified, including NULL.

Adding Default Constraint in Existing Column

To add default constraint on existing column we need to use the alter command.

Syntax: 

alter table (table_name)
Add Constraint (constraint_name)
Default (default_value) For (column_name)

Example:

alter table tblEmployee
Add Constraint DF_tblEmployee_GenderID
Default 3 For GenderID

When you execute the above statement then default constraint get created and will display in Constraint folder.



Adding Default Constraint in Existing Table New Column

Let’s say we need to add the new column in the table with the default constraint then we need to write following query.

Syntax: 

alter table (table_name)
add (column_name) (Data_type) (Null | Not Null)
Constraint (constraint_name) default (default_value)

Now we again try to insert the values in the tblEmployee table and not give the GenderID.

Insert into tblEmployee (ID, Name, Email) values (5,'Tom','tom@test.com')
select * from tblEmployee 

Once the query is completed, this will insert the default value as 3 in GenderID column as initialized above.



Now we try to insert the values in tblEmployee with the GenderID value.

Insert into tblEmployee (ID, Name, Email, GenderID) values (5,'Marry','marry@test.com', 2)
select * from tblEmployee 

Once the query is completed, this will insert the value 2 for the record because we specified the value 2 for the GenderID so this will ignore the default value and will insert the 2 in the GenderID for the record.



Trick: Let’s see what will happen when we insert the GenderID value as NULL. Will this be inserted or the default value gets inserted?
The answer is NULL value will be inserted and the default value will NOT be executed because NULL is considered as value.


Dropping the Default Constraint

To drop the default constraint we need to alter the table and then drop the constraint.

Syntax:
alter table (table_name)
drop constraint (contraint_name)

Example:
alter table tblEmployee
drop constraint DF_tblEmployee_GenderID
That’s all for today. Thank You.

Comments

Popular posts from this blog

SQL Day 11: Group By in SQL Server

JAVA Script Executor Code to Click on an Element

SQL Day 2: Creating, Altering and Dropping a Database