Home » SQL

Use of DEFAULT Constraint in SQL

In this article, we are going to learn about DEFAULT constraint in SQL.
Submitted by Bharti Parmar, on January 17, 2019

DEFAULT constraint is used to insert default value into a column on a table and if no any value is stored in any place of a column then default value will be added into it.

How to use DEFAULT with example?

CREATE TABLE EMployees
(E_Id int NOT NULL,E_Name varchar(25) NOT NULL,
Contact number(10),Address varchar(50),
City varchar(20) DEFAULT 'Gwaloir');

We can also achieve DEFAULT value property by using GETDATE() function:

CREATE TABLE Employees
(E_Id int NOT NULL,E_name varchar(50),
E_Join_Date date DEFAULT GETDATE());

USE of ALTER to ADD DEFAULT constraint when table is already created:

There is different way to add DEFAULT constraint in different database language:

  • MYSQL
  •     ALTER table EmployeesALTER E_name SET DEFAULT 'Bharti';
    
  • SQL
  •     ALTER TABLE Employees ALTER COLUMN City SET DEFAULT 'Gwalior';
    
  • Oracle
  •     ALTER TABLE Employees MODIFY Contact DEFAULT '0000';
    

How to DROP DEFAULT constraint from a table?

    ALTER TABLE Employees ALTER COLUMN City DROP DEFAULT;

Conclusion:

In this article we have learnt what DEFAULT constraint is, how to use it with example and how we can alter and drop DEFAULT on a table? If you have any doubt/query, feel free to ask in comment section.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.