Skip to main content

NOT NULL constraint in sql server

NOT NULL- This constraint is responsible for a column to confirm that a column cannot have a null value.

Example-

Following we create new table PRODUCT and add six columns where ID, SKU, TITLE, PRICE specify not to accept NULLs.


CREATE TABLE PRODUCT
(
  ID INT NOT NULL,
  SKU VARCHAR(20) NOT NULL,
  TITLE VARCHAR(200) NOT NULL,
  PRICE MONEY NOT NULL,
  DESCRIPTION VARCHAR(2000)  NULL,
  DTCREATE DATETIME NULL
)


If PRODUCT table already created, then to add a NOT NULL constraint to SKU column we use the following query.


ALTER TABLE PRODUCT ALTER COLUMN SKU  VARCHAR (20) NOT NULL