Examples of CHECK Constraints

In this, we are going to explore how check constraint actually works in MYSQL. We will see how it will behave if have try to enter any records which do not follow its imposed condition.

Example 1: Inserting a Row with Rank Grater than 100

In this example, we are going to see how check constraint will handle an insertion of record which do not follow its imposed condition.

Query:

INSERT INTO w3wiki(id,name,questions,rank)
VALUES(05,'Vivek',150,120);

Output:

rank > 100

Explanation: In the above image, we can clearly see than an error appeared. It because we have tried to enter a row with rank 120, which is grater than 100. Therefore failing to satisfy the check constraint condition will result in an error.

Example 2: Inserting a Row with Questions Less than 100

In this example, we will try to insert a row with questions less than 100. Like in the previous example, this will us an error too. In order to insert a row successfully into our table, we need to satisfy both the conditions.

Query:

INSERT INTO w3wiki(id,name,questions,rank)
VALUES(05,'Vivek',15,12);

Output:

question < 100

Explanation: In the above image, we can clearly notice an error. This error is caused as we try to enter a row with question less than 100 which is against our imposed condition. Therefore it throws us an error, just like in did in the previous example.

MYSQL CHECK Constraint

MySQL is a very famous and widely used open-source RDBMS. It is used to store, retrieve, and manage structured data efficiently. It is used in both types of applications i.e. large and small scale applications. In MySQL, the CHECK constraint enforces a condition on the column(s) of a table. It makes sure that a specific type of data only gets inserted into the table.

In this article, we are going to explore various use cases of MYSQL check constraints. We are going to cover all the basic points with clear and concise examples along with their explanations.

Similar Reads

MYSQL CHECK Constraint

In MYSQL, the Check constraint is used to impose conditions on what type of data to be inserted into our table. It helps in maintaining the accuracy and consistency of the data. It helps in avoiding the entry of data that does not follow our specified conditions....

Creating a table with CHECK Constraint

In this, we are heading to create a table in our database. We will create a table named ‘geeksforgeeks‘. We will apply a check constraint on two of its columns....

Examples of CHECK Constraints

In this, we are going to explore how check constraint actually works in MYSQL. We will see how it will behave if have try to enter any records which do not follow its imposed condition....

Drop the CHECK constraint

In order to drop CHECK constraint from our existing table, we will be using ALTER and DROP clause. ALTER Clause is used to modify the structure of the table and DROP Clause is used remove a database object....

Adding CHECK Constraints to an Existing Table

We are going to add a CHECK constraint to our existing table. As our table have no check constraint, we add one with the below query....

Conclusion

Overall, check constraint is use to impose a condition on the data which are going to be inserted into our table. We can clearly prevent on adding irrelevant data to our table. We have seen many examples related to check constraints, how to remove check constraint from existing table and adding check constraint to the existing table. Now you can write all the queries related to check constraint with ease can can get the desired output....