Italian Trulli

Default Constraints in SQL with Example

Test Tribe
.

 SQL Default- How to use Default in SQL?


SQL Default Constraints 

In SQL Default constraints are used to set the fixed value or default value for a column. The default value will be added to all new records when the INSERT INTO statement does not provide a specific value.

Syntax Of Default Constraints
The Basic syntax of default constraints is given Below.

CREATE TABLE Employee ( EID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int, Address varchar(255) DEFAULT 'Andheri East' );

 
Example of Default Constraints
An example of a default constraint is given below that helps you to understand the default easily.

Suppose we create a table Employee and we want if the user does not enter the data in the address then the system will set by default the office address in the address column of the employee table.

SQL Command for Default

CREATE TABLE Employee ( EID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int, Address varchar(255) DEFAULT 'Andheri East' );
The above SQL sets a DEFAULT value for the "Address" column when the "Employee" table is created


How to use Default Constraints in SQL 


SQL Default on Create Table 
CREATE TABLE Employee ( EID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int, Address varchar(255) DEFAULT 'Andheri East' );

SQL Default on Alter Table 

MY SQL 
ALTER TABLE Employee 
ALTER Address SET DEFAULT 'Andheri East';
SQL Server 
ALTER TABLE Employee ADD CONSTRAINT df_City DEFAULT 'Andheri East' FOR Addesss;


Drop SQL Default 

ALTER TABLE Employee ALTER Address DROP DEFAULT;


Hope !!! The above tutorial on " DEFAULT CONSTRAINT IN SQL "Is helpful for you...

Team,
QA acharya

Tags: SQL Default, Default constraints, How to use Default Constraints 

SQL Default Constraints With Example

  

Post a Comment

0 Comments