SQL

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

Yasin Panwala

Yasin Panwala is a Web Developer and Author at TheCodeHubs. He has experience in Web Developing and Designing and also in Writing. He has got his skills in working on technologies like .NET Core, ADO.NET, AJAX, Angular, AngularJS, ASP.NET, ASP.NET MVC, Bootstrap, C#, CSS, Entity Framework, Express.js, GraphQL, HTML, JavaScript, JQuery, JSON, LINQ, Microsoft Office, MongoDB, MySQL, Node.js, PostgreSQL, SQL, SQL Server, TypeORM, TypeScript, Visual Basic .NET, Web API. He also got his skills in working with different integration and some known versioning tools. He is always ready to learn new things and he always tries his best on tasks that are assigned to him and gives the best possible outputs.

View Comments

  • 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.

  • 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.

Recent Posts

Testing hk

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Operation

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

TETS NEW

test

3 years ago