Dofactory.com
Dofactory.com
Earn income with your data and sql skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.

SQL NOT NULL Constraint

NOT NULL specifies that NULL values are not accepted in a column.

This constraint ensures that invalid data cannot enter the column.

Adding a NULL value to a NOT NULL column will cause an error.

Example

#

In this table, the FirstName and LastName columns do not accepts NULL values.

CREATE TABLE Customer (
  Id INT IDENTITY,
  FirstName NVARCHAR(50) NOT NULL,
  LastName NVARCHAR(50) NOT NULL,
  City NVARCHAR(40),
  CONSTRAINT PK_Customer PRIMARY KEY(Id)
)

Note: The City column does accept NULL values.


Add NOT NULL

#

To add a NOT NULL constraint to an existing column use ALTER TABLE.

ALTER TABLE Customer
  ALTER COLUMN City NVARCHAR(40) NOT NULL

You may also like



Last updated on Dec 21, 2023

Earn income with your data and sql skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.