beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

NOT NULL Constraint in SQL

By Chaitanya Singh | Filed Under: SQL

NOT NULL constraint makes sure that a column does not hold NULL value. When we don’t provide value for a particular column while inserting a record into a table, by default it takes NULL value. By specifying NULL constraint, we can be sure that a particular column(s) cannot have NULL values.

How to specify the NULL constraint while creating table

Here I am creating a table “STUDENTS”. I have specified NOT NULL constraint for columns ROLL_NO, STU_NAME and STU_AGE which means you must provide the value for these three fields while inserting/updating records in this table. It enforces these column(s) not to accept null values.

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

Specify the NULL constraint for already existing table

In the above section we learnt how to specify the NULL constraint while creating a table. However we can specify this constraint on a already present table also. For this we need to use ALTER TABLE statement.

ALTER TABLE STUDENTS
   MODIFY STU_ADDRESS  VARCHAR (235) NOT NULL;

After this STU_ADDRESS column will not accept any null values.

Enjoyed this post? Try these related posts

  1. Group By clause in SQL
  2. SQL SELECT MIN, MAX Functions
  3. SQL SELECT AVG() Function
  4. SQL Select Random Rows from Table
  5. SQL – CREATE TABLE Statement
  6. SQL SELECT SUM() Function

Leave a Reply Cancel reply

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

SQL Tutorial

  • SQL Introduction
  • SQL Syntax
  • 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

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap