Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minimum for varchar?

Tags:

mysql

I thought you could set varchar as varchar(min,max), but I must have mistaken. What's the syntax to set a min and max? Or do I have to use PHP to do that?

Update: Let me add more context. I'm making a field for usernames, so I thought I could set the minimum and maximum for varchar in MySQL. Seems I can only set the length which is 0 to 255. I'm guessing this means I have to use PHP to add the minimum restriction

like image 374
Strawberry Avatar asked Dec 22 '22 06:12

Strawberry


1 Answers

You cannot set a minimum length for a VARCHAR field in MySQL. Despite jspcal's answer, there is no such min length constraint.

The max is simply VARCHAR(max)

Yes, you must use PHP to limit the length. Personally, I use application code to perform all my constraint validations, and only rely on the DB as a last resort/failsafe.

It's better to detect invalid data in your application and return a useful error message, than to catch a DB exception and have to interpret/translate that into the appropriate error message.

like image 159
hobodave Avatar answered Jan 08 '23 04:01

hobodave