How to ensure that a MySQL column accepts only X number of characters?

I search the Internet, but I canโ€™t understand what kind of life it is:

Can I make a MySQL table column ONLY accept a given number of characters (e.g. 5 numbers ONLY, not 4 numbers or 6 numbers)? I read the MySQL documentation for some different data types, but havenโ€™t found anything yet.

In a pretty bad move, I canโ€™t do the reg-ex check in PHP, which is easy now, and would like to make a simple exit by forcing this check in the database itself. Thanks in advance!

+4
source share
3 answers

you need to write a code layer to do something like trigger BEFORE INSERT

+1
source

you can probably do this with a check constraint in the table

+1
source

Can't check the row length for this column in php before sending to the database?

If you still need this at the database level, then mysql has a Length function. you can check it out.

+1
source

All Articles