What are SQL Default Constraint with examples?

SQL Default Constraint :

In my previous articles i have explained about the different constraints in SQL.This article gives you idea about one of the important constraint of SQL which is SQL Default Constraint with real industry examples.This constraint is different from all other constraints.There are lot of times user needs to add the default value to the specific column of the table.Its really difficult to all default values to the column. In spite of that user can directly set the default value as a SQL Default Constraint so that user will not face any difficulty in inserting or updating the default values.SQL Default Constraint is basically used to insert default value.

‘SQL Constraints’ means rules and regulations to normalize the data

The following section gives user the idea about SQL Default Constraint with real industry examples.It helps the user to understand the concept of SQL Default Constraint with examples.

How to add SQL Default Constraints?

The DEFAULT constraint is used to insert a default value into a column.

The default value will be added to all new records, if no other value is specified.

The following SQL creates a DEFAULT constraint on the “Class” column when the “Student” Table is created:

SQL Default constraint

CREATE TABLE Student

(

RollNo Number (10),

Subject Varchar2 (15),

Fees Number (10),

Class Number (5) DEFAULT 5,

DOJ Date Default SYSDATE

);

INSERT INTO Student (RollNo, Subject, Fees)

Values (105, ‘Sci’, 5000);

To DROP a DEFAULT SQL Constraints:

Note: To DROP a Default constraint you need to make is as NOT NULL first, then apply DROP Command.

ALTER TABLE Student MODIFY Class NOT NULL;

Syntax:

 

ALTER TABLE <Table_Name>

DROP CONSTRAINT <Constraint_Name>;

Example:

ALTER TABLE Student

DROP Constraint SYS_C005844;

SQL DEFAULT SQL Constraint on ALTER TABLE:

To create a DEFAULT constraint on the “Subject” column when the table is already created, use the following SQL:

Example:

ALTER TABLE Student

MODIFY Subject DEFAULT ‘English’;

Hope You Like this article on SQL Default Constraint with real industry example.This article will help user to understand the concept of SQL Default Constraints with different examples.If you like this article or if you have any suggestions for this article kindly comment it on comment section.