tags:

views:

87

answers:

2

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

A: 

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

jspcal
There are constraints under MySQL?
zneak
-1: Just plain wrong
hobodave
Your original answer was "Yes you can do it with constraints". You completely changed the body of your answer to CYA.
hobodave
@hobodave: you're *completely wrong*. obviously you've never heard of triggers (http://forge.mysql.com/wiki/Triggers#Emulating_Check_Constraints) which is how mysql implements constraints. read and learn before you downvote incorrectly.
jspcal
+2  A: 

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.

hobodave
"you must use PHP to limit the length" - this is *false*. obviously you've never heard of triggers, which is how mysql implements constraints: http://forge.mysql.com/wiki/Triggers#Emulating_Check_Constraints. read and learn.
jspcal