How to Create CHECK Constraint in SQL

In this article I am going to explain about CHECK Constraints in SQL.
  • 2044

Introduction 

If you want to limit the value that are accepted by a column use CHECK constraint. Check constraint have some similarities like FOREIGN Key constraint because both constraint control the value that are entered in column.

Example

Using CHECK constraint you can limit the range of value entered in salary column. You can set range for salary from 10000 through 1000000 using it.

Statement that create a CHECK constraint in empinfo table

create table empinfo

(

empid int primary key,

emp_panno int unique,

name varchar(15),

age int,

city varchar(15),

salary money

CHECK(salary >=10000 and salary <=1000000) 

)

Statement that use empinfo table

Clipboard101.jpg


Clipboard04.jpg
© 2019 DotNetHeaven. All rights reserved.