How To Use NOT NULL Constraint In SQL

In this article, we will learn how to use the NOT NULL constraint in SQL.

A column can hold NULL values by default. The NOT NULL constraint is used to enforce a column to NOT accept NULL values.

The NOT NULL constraint enforces a field to always contain a value, which means that without adding a value to this field we cannot insert a new record, or update a record.

 

NOT NULL – CREATE TABLE: 

Please check How To Create Table Using Query In SQL, to get a brief description of the CREATE TABLE statement.

Example

The subsequent statement would ensure that the “ID” and “Title” columns will NOT accept NULL values when the “Article” table is created:

CREATE TABLE Article (
    ID int NOT NULL,
    Title varchar(100) NOT NULL,
    Author varchar(100),
    Views int 
);

 

NOT NULL – ALTER TABLE: 

Please check How To Alter Table Using Query In SQL, to get a brief description of the ALTER TABLE statement.

Example

The subsequent statement would add a NOT NULL constraint on the “Views” column using ALTER TABLE statement:

ALTER TABLE Article 
ALTER COLUMN Views bigint NOT NULL;

 

Also, check How To Use TRUNCATE TABLE Statement In SQL

3 Comments

  1. Free Stuff

    Its like you read my mind! You seem to know a lot about this, like you wrote the book in it or something. I think that you can do with some pics to drive the message home a bit, but other than that, this is excellent blog. A fantastic read. I’ll definitely be back.

    0
    0
    Reply
    1. Thank You, I will do my best.

      0
      0
      Reply
  2. Hairstyles

    After research a number of of the blog posts on your website now, and I actually like your approach of blogging. I bookmarked it to my bookmark web site list and might be checking back soon. Pls take a look at my site as properly and let me know what you think.

    0
    0
    Reply

Submit a Comment

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

Subscribe

Select Categories