Saturday 19 December 2015

CHECK constraint in SQL

CHECK constraint in SQL verifies the value in the column to be according to specification.
Example:
  1. CHECK (Balance >= 1000)
  2. CHECK (Balance BETWEEN 1000 and 7000)
  3. CHECK (Balance IN (1000, 2000, 4000, 7000, 9000)
In Query 3, the Balance can have value 1000 or 2000 or 4000 or 7000 or 9000 only.
Note:
  1.  We can impose one or more constraint on a column.
  2.  Except NOT NULL constraint, UNIQUE, CHECK and FOREIGN KEY allow NULL values in to column. To restrict them you can explicitly impose a NOT NULL constraint on those columns.
CREATE Table BANK (CustomerId Int Primary Key, Cname Varchar (50), Balance Decimal (7,2) NOT NULL Constraint Bal_Chk  CHECK (Balance >=1000)



No comments:

Post a Comment