According to the official documentation, VARCHAR can be up to 65535 bytes (this also means that a record has only this field, because a MySQL row can only contain 65535 bytes).
However, it is not possible to index such a long VARCHAR. For MyISAM, the first 1000 bytes can be indexed, and for InnoDB, it is only 767 bytes. (source based on)
What we are talking about here is bytes, taking into account encoding factors (UTF-8 characters occupy 1-3 bytes, GBK characters occupy two bytes), and all VARCHAR information requires 1 more byte (i.e. empty string Also occupies 1 byte), it is easier to use VARCHAR(256).
@xuecan’s answer is very detailed, so I will add a few more. What you see should mostly be varchar(255) instead of 256. The reason is that in mysql versions below 4.1, the maximum length of varchar is limited to 255, and its data range can be 0~255 or 1~255 (according to different versions of the database) Certainly).
I heard that this problem is related to encoding~ Generally speaking, the number of bytes will not be too much. Considering the scalability of text length, text can be used for large texts. http://my.oschina.net/zimingforever/b...
According to the official documentation, VARCHAR can be up to 65535 bytes (this also means that a record has only this field, because a MySQL row can only contain 65535 bytes).
However, it is not possible to index such a long VARCHAR. For MyISAM, the first 1000 bytes can be indexed, and for InnoDB, it is only 767 bytes. (source based on)
What we are talking about here is bytes, taking into account encoding factors (UTF-8 characters occupy 1-3 bytes, GBK characters occupy two bytes), and all VARCHAR information requires 1 more byte (i.e. empty string Also occupies 1 byte), it is easier to use VARCHAR(256).
@xuecan’s answer is very detailed, so I will add a few more. What you see should mostly be varchar(255) instead of 256. The reason is that in mysql versions below 4.1, the maximum length of varchar is limited to 255, and its data range can be 0~255 or 1~255 (according to different versions of the database) Certainly).
I heard that this problem is related to encoding~ Generally speaking, the number of bytes will not be too much. Considering the scalability of text length, text can be used for large texts.
http://my.oschina.net/zimingforever/b...