BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

DEFAULT Constraint in SQL

Last Updated: September 10, 2022 by Chaitanya Singh | Filed Under: SQL

The DEFAULT constraint provides a default value to a column when there is no value provided while inserting a record into a table. Lets see how to specify this constraint and how it works.

Specify DEFAULT constraint while creating a table

Here we are creating a table “STUDENTS”, we have a requirement to set the exam fees to 10000 if fees is not specified while inserting a record (row) into the STUDENTS table. We can do so by using DEFAULT constraint. As you can see we have set the default value of EXAM_FEE column to 10000 using DEFAULT constraint.

CREATE TABLE STUDENTS(
       ROLL_NO   INT           NOT NULL,
       STU_NAME VARCHAR (35)   NOT NULL,
       STU_AGE  INT            NOT NULL,
       EXAM_FEE INT            DEFAULT 10000,
       STU_ADDRESS  VARCHAR (35) ,     
       PRIMARY KEY (ROLL_NO)
);

Specify DEFAULT constraint while creating a table

What if we want to set this constraint on a already existing table. For this we can ALTER Table statement like this:

Syntax:

ALTER TABLE <table_name>
   MODIFY <column_name> <column_data_type> DEFAULT <default_value>;

Example:

ALTER TABLE STUDENTS
   MODIFY EXAM_FEE INT DEFAULT 10000;

This way we can set constraint on a already created table.

How to drop DEFAULT Constraint

In the above sections, we have learnt the ways to set Constraint. Here we will see how to drop (delete) a Constraint:

Syntax:

ALTER TABLE <table_name>
  ALTER COLUMN <column_name> DROP DEFAULT;

Example:
Lets say we want to drop the constraint from STUDENTS table, which we have created in the above sections. We can do it like this.

ALTER TABLE CUSTOMERS
   ALTER COLUMN EXAM_FEE DROP DEFAULT;

Top Related Articles:

  1. NOT NULL Constraint in SQL
  2. Introduction to SQL
  3. SQL Tutorial for Beginners: Learn SQL
  4. SQL SELECT Statement
  5. SQL INSERT INTO SELECT Statement

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

SQL Tutorial

  • SQL Tutorial
  • SQL Introduction
  • SQL Syntax
  • SQL Data Types

SQL Database

  • SQL CREATE DB
  • SQL DROP DB
  • SQL Rename DB
  • SQL USE DB

SQL Queries

  • SQL Select
  • SQL Select Distinct
  • SQL Select Count
  • SQL Select Top
  • SQL Where
  • SQL AND, OR & NOT
  • SQL Order By
  • SQL Insert Into
  • SQL Insert Into SELECT
  • SQL Select Random
  • SQL Alias
  • SQL NULL Check
  • SQL Update
  • SQL Delete
  • SQL MIN, MAX
  • SQL SUM
  • SQL AVG

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap