开发者

Which is the correct text datatype to use in MySQL?

There's VARCHAR, CHAR, TEXT, BIGTEXT, BLOB...

Which is the correct datatype to use when storing small 开发者_如何学JAVAtext fields (ie. something like first_name)?


I usually go with VARCHAR for most small text fields, unless I know the field is going to be a fixed size, like a U.S. state abbreviation, where I'd use CHAR(2).


"Correct" is a strong word, but most people would probably use VARCHAR. CHAR is also an option, but it's not as frequently used in my experience. This page explains the difference.

One of the notable differences between CHAR and VARCHAR in the latest versions of MySQL is that CHAR is incapable of storing trailing spaces (because the column is automatically padded to its max length by spaces). So if you store something like 'abc ', it will be retrieved as 'abc'. This might not matter for most applications, but it's something to keep in mind. Similarly, prior to 5.03, trailing whitespaces are stripped from VARCHAR fields before insertion. So if you need to store arbitrary byte values and don't want to worry about different behaviors between MySQL versions, you should use one of the BLOB types.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜