alternative
  • Home (current)
  • About
  • Tutorial
    Technologies
    C#
    Deep Learning
    Statistics for AIML
    Natural Language Processing
    Machine Learning
    SQL -Structured Query Language
    Python
    Ethical Hacking
    Placement Preparation
    Quantitative Aptitude
    View All Tutorial
  • Quiz
    C#
    SQL -Structured Query Language
    Quantitative Aptitude
    Java
    View All Quiz Course
  • Q & A
    C#
    Quantitative Aptitude
    Java
    View All Q & A course
  • Programs
  • Articles
    Identity And Access Management
    Artificial Intelligence & Machine Learning Project
    How to publish your local website on github pages with a custom domain name?
    How to download and install Xampp on Window Operating System ?
    How To Download And Install MySql Workbench
    How to install Pycharm ?
    How to install Python ?
    How to download and install Visual Studio IDE taking an example of C# (C Sharp)
    View All Post
  • Tools
    Program Compiler
    Sql Compiler
    Replace Multiple Text
    Meta Data From Multiple Url
  • Contact
  • User
    Login
    Register

SQL -Structured Query Language - Sql Key & Constraint - Check Constraint Tutorial

CHECK Constraint is used, when we want to limit the value range that is going to be inserted in a column.

For example, Suppose we want that the data of employee should be inserted whose age is greater than 20.

Then the CHECK constraint will check, whether the age is greater than 20 or not. If not, then the data will not be inserted.

 

The syntax for Check Constraint-

create table user_detail

(

    id int,

    first_name varchar(50),
  
    age int CHECK(age>20)

);

 

create table user_detail

(

    id int,

    first_name varchar(50),
  
    age int,
  
    CONSTRAINT CHK_age CHECK (age>20)

);

 

Run Query

In this, we define age with CHECK constraints with age is greater than 20 while creating a user_detail table. With a CHECK constraint, it doesn’t allow inserting an invalid record.

It will give an error (CHECK constraint failed: user_detail) while inserting the age less or equal to 20.

 

Syntax For Adding CHECK Constraint With Alter Command-

ALTER TABLE user_detail
ADD CHECK (age>20);
ALTER TABLE user_detail
ADD CONSTRAINT CHK_age CHECK (age>20);

This syntax will add a CHECK Constraint in age after table creation. If it gives an error make sure that records should not contain an invalid value other than the defined condition.

 

Syntax For Dropping Check Constraint With Alter Command-

ALTER TABLE user_detail
DROP CHECK CHK_age;

This syntax will drop a CHECK Constraint with its constraint name.

SQL -Structured Query Language

SQL -Structured Query Language

  • Introduction
  • Overview
    • How To Download And Install MySql Workbench
    • SQL Syntax
  • SQL Database
    • Create Database
    • Drop Database
    • Use / Select Database
  • SQL Table
    • Create Table
    • Alter - Rename Table
    • Alter - Add Column
    • Alter - Modify Column
    • Alter - Rename Column
    • Alter - Drop Column
    • Copy Table
    • Drop Table
  • SQL Insert
    • Insert Record
  • Sql Update
    • Update Record
  • Sql Select
    • Select Record
    • Select - Count
    • Select - Average
    • Select - Sum
    • Select - Min Max
    • Select - Distinct
    • Select - Limit
    • Select - First
    • Select - Last
  • Sql Clause
    • Like Clause
    • And Operator
    • Or Operator
    • Between & Not Between Operator
    • As or Alias
    • In Clause
    • Group By Clause
    • Having Clause
  • Sql Order By
    • Ascending
    • Descending
    • Random
    • Ascending Descending Multiple Column
  • SQL Joins
    • Joins and its type
    • Inner Join
    • Left Join
    • Right Join
    • Full Join
    • Cross Join
  • SQL View
    • Create View
    • Update View
    • Delete View
  • SQL Delete
    • Delete Table
    • Truncate Table
  • Sql Key & Constraint
    • Primary Key
    • Foreign Key
    • Unique Key
    • Not Null Constraint
    • Check Constraint
    • Default Constraint
  • Normalization
    • Overview
    • 1 NF
    • 2 NF
    • 3 NF
    • Boyce Codd normal form ( BCNF )

About Fresherbell

Best learning portal that provides you great learning experience of various technologies with modern compilation tools and technique

Important Links

Don't hesitate to give us a call or send us a contact form message

Terms & Conditions
Privacy Policy
Contact Us

Social Media

© Untitled. All rights reserved. Demo Images: Unsplash. Design: HTML5 UP.