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 table use ALTER TABLE.

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

You may also like



Guides


vsn 3.1