Constraint to check on MySQL server-side if a phone number consists of only numbers or not and whether it has ten digits or not.
I have the following create table query where mobile
is the name of the field, storing the phone numbers of people: -
CREATE TABLE `users` (
--Other columns
`mobile` int UNSIGNED DEFAULT NULL,
) ENGINE=InnoDB;
As indicated, the mobile
column can be NULL. Hence, I can't apply direct length check constraints and other regex constraints. What would be a feasible check constraint, if any?
P.S. - I have assumed the phone numbers to be an INTEGER. I have read suggestions for making it varchar.
EDIT - (Take care of the version)
Prior to version 8.0, MySQL didn't enforce the check constraints. It allowed them to be written in CREATE TABLE statements, parsed them and ignored them.
Here's some useful documentation from MySQL Reference Manual:
Prior to MySQL 8.0.16, CREATE TABLE permits only the following limited version of table CHECK constraint syntax, which is parsed and ignored:
CHECK (expr)