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:
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:
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.