Home » SQL

Use of CHECK Constraint in SQL

In this article, we are going to learn how to use CHECK constraint in SQL?
Submitted by Bharti Parmar, on January 09, 2019

Basically, CHECK constraint is used to LIMIT in columns for the range of values. We can use this constraint for single or multiple columns.

In single column, CHECK allows LIMIT on some values in a column and in multiple columns, CHECK allows LIMIT on firm columns based in other column in the row on a table.

How to use 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;

Conclusion:

In this article, we have learnt what is CHECK constraint, How to use it with example and how we can alter and drop CHECk on a table?





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.