Check in DBMS
DBMS CHECK
On this page we will discuss about Check in DBMS.Check constraint ensures that the data entered by the user for that column is within the range of values or possible values specified.CHECK IN DBMS
- Suppose in real-time if you want to give access to an application only if the age entered by the user is greater than 18 this is done at the back-end by using a
check
constraint - Check constraint ensures that the data entered by the user for that column is within the range of values or possible values specified.
Syntax for check constraint
CREATE TABLE STUDENT ( column1 datatype(size), column2 datatype(size), column3 datatype(size), CHECK (condition) );
Example for check constraint
CREATE TABLE STUDENT (
ID int ,
Name varchar(255) ,
Age int,
CHECK (Age>=18)
);
- As we have used a check constraint as (Age>=18) which means values entered by the user for this age column while inserting the data must be less than or equal to 18 otherwise an error is shown
- Simply, the only possible values that the age column will accept is [0 -17].
Check constraint at table level
CREATE TABLE student {
ID int ,
Name varchar(255) ,
Program interface specificationAge int,
City varchar(255),
CONSTRAINT CHK_Person CHECK (Age>=18 AND City='hyderabad')
);
- You can apply check constraint to more than one column at a time
- In the above example we have used two conditions with the check constraint Age>=18 AND City=’ Hyderabad’ , as a result, both the columns age and city will be checked while data is entered i.e the only possible values that are accepted for the column age are 0 -17 and city must be only Hyderabad
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment