开发者

Minimum for varchar?

开发者 https://www.devze.com 2022-12-17 10:49 出处:网络
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?

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 fie开发者_JAVA百科ld 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


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.


better to do it in php, mysql would require some triggers to do the constraints.

0

精彩评论

暂无评论...
验证码 换一张
取 消