In this article, we will figure out how to utilize CHECK requirement in SQL?
Fundamentally, CHECK requirement is utilized to LIMIT in segments for the scope of values. We can utilize this limitation for single or different segments.
In single segment, CHECK permits LIMIT on certain values in a section and in numerous segments, CHECK permits LIMIT on firm segments situated in other segment in the line on a table.
How to utilize CHECK with example?
1) For single column
CREATE TABLE Conpany (E_Id int NOT NULL CHECK (E_Id>0),E_Name varchar(255) NOT NULL, Contact number(10),Address varchar(255),City varchar(255));
2) For multiple Columns
CREATE TABLE company (E_Id int NOT NULL,E_Name varchar(255) NOT NULL,contact number(10), Address varchar(255),City varchar(255), CONSTRAINT chk_emp CHECK (E_Id>0 AND City='Gwalior'));
USE of ALTER to ADD CHECK constraint in an already created table
1) For single column
ALTER TABLE company ADD CHECK (E_Id>0);
2) For multiple columns
ALTER TABLE company ADD CONSTRAINT chk_emp CHECK (E_Id>0 AND E_name='Bharti');
How to DROP CHECK constraint from a table?
ALTER TABLE company DROP CONSTRAINT chk_emp;
End:
In this article, we have realized what is CHECK imperative, How to utilize it with example and how we can change and drop CHECK on a table?