What is Check Constraint in MySQL | How to create Check Constraint in MySQL - MySQL Developer Tutorial

What is Check Constraint in MySQL | How to create Check Constraint in MySQL

Check Constraint  is table constraint that restricts the data you can add to table. In this video we get to know that Check Constraint can be used while we create table in MySQL but Check Constraint does not work in MySQL. If we need to implement Check Constraint in MySQL, we will be using Trigger.
In MariaDB Check Constraint works and we have showed by example how you can implement Check Constraint in MariaDB.

In below example, we are create Check Constraint on Age column which will only accept values less than 135. Also we have add the Check Constraint for Gender column which will only accept 'M' or 'F' value.


CREATE TABLE customer (
  `idcustomer` int(11) NOT NULL,
  `firstname` varchar(50) DEFAULT NULL,
  `lastname` varchar(30) DEFAULT NULL,
  `age` int(11)  NOT NULL CHECK (age 135),
  `phonenumber` char(11) DEFAULT NULL,
  `dob` date DEFAULT NULL,
  `gender` char(1) NOT NULL Check (gender in ('M','F'))
  ) ;



Check Constraint in MariaDB - How to create Check Constraint in MariaDB

3 comments: