Use of CHECK Constraint in SQL

Use of CHECK Constraint in SQL

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?

DURGESH

I Love python, so I like machine learning a Lot and on the other hand, I like building apps and fun games I post blogs on my website for Tech enthusiast to learn and Share Information With The World.

Leave a Reply

Your email address will not be published. Required fields are marked *

3 × 2 =

Recent Content