Oct 21, 2014

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,
DISCRIPTION VARCHAR(2000)  NULL,
DTCREATE DATETIME NULL
)

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

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