CHECK constraint in sql server


CHECK- The CHECK responsible for enables a condition to check the value being entered into a record

Example-
In the following case, we create a PRODUCT table it has a PRICE column and its value should be greater than 20.00,
so that you cannot have any product which price is less than or equal to 20.00

CREATE TABLE PRODUCT
(
ID INT NOT NULL IDENTITY(1,1),
SKU VARCHAR(20) NOT NULL,
TITLE VARCHAR(200) NOT NULL,
PRICE MONEY NOT NULL CHECK (PRICE>20.00),
DESCRIPTION VARCHAR(2000)  NULL,
DTCREATE DATETIME NULL
CONSTRAINT pk_ID PRIMARY KEY (ID)
)

If a table already exists, then use the following query to create check constraints.

ALTER TABLE PRODUCT
ADD CHECK (PRICE>20.00)

Related Posts

What is the Use of isNaN Function in JavaScript? A Comprehensive Explanation for Effective Input Validation

In the world of JavaScript, input validation is a critical aspect of ensuring that user-provided data is processed correctly. One indispensa...