开发者

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

I've been doing some searching on the Internet, but I can't for the life figure this out:

Can I force a MySQL table column to ONLY accept a set number of characters (e.g. 5 numbers ONLY, not 4 numbers nor 6 n开发者_JS百科umbers)? I've read the MySQL documentation for some various data types but haven't really discovered anything yet.

In a rather unfortunate move, I can't perform reg-ex checking in PHP that easily right now, and would like to take the easy way out by forcing this check in the database itself. Thanks in advance!


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


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


Is it not possible for you to check the length of string for that column in php before sending it to database?

If you still need it in database level then, there is function Length in mysql. you might want to check it out.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜