开发者

Is it wise to declare a VARCHAR with a value greater than 255 in MySQL?

开发者 https://www.devze.com 2023-02-04 14:26 出处:网络
I just noticed in the documentation that in versions greater than 5.0.3 of MySQL you can declare varchar\'s with larger values than 255. In the past I\'ve switched datatypes for an开发者_开发百科ythin

I just noticed in the documentation that in versions greater than 5.0.3 of MySQL you can declare varchar's with larger values than 255. In the past I've switched datatypes for an开发者_开发百科ything larger than 255 but I'm wondering if it's better practice now to define larger string values using varchar(1000) or whatever length is appropriate.

Is this common with other databases now as well, or is it best to stick with 255 as the max value and change datatypes above that?


As the answer @Eric pointed out suggests, VARCHARs are stored in table while TEXTs are stored in a separate file - the only truly important point that you have to keep in mind when designing a table structure is the row size limitation (MySQL limits each row / record to 65 KB).

I suggest you use VARCHARs for "one-liners" - anything that has a text input as its data source.


In my opinion, I would discourage to approach. When you need more than 255 characters, use TEXT are some more suitable.

Update: VARCHAR is now limited to 65535 bytes, but a row in MySQL cannot contain more than 65535 bytes.

You have to know that VARCHAR and fields like that are stored directly into your database when TEXT for example will be stored outisde the row why a pointer inside the row linking to it.

So if you want to use big VARCHAR, make sure they will not be too big and won't interfere with the rest of the data in the row.

For example, having mutltiple VARCHAR fields that can contains up to 65K char would be a bad idea.


The VARCHAR column is limited to 65,535 bytes, which doesn't always mean 65,535 characters depending on which character set you are using.

If your using the latin1 character set which is one byte per character you won't run into any issues because the length of the string in the same as the amount of storage needed.

If you use a character set that stores multi-byte characters you can only set the length to be what the character set will allow. For instance the utf8 character set can have a maximum length of 21,844 characters.

0

精彩评论

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