Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the next best thing after VARCHAR(255)

Tags:

mysql

I have a table with about 2 million records in it. I have a field called message which is setup as varchar(160) and I need something bigger that varchar(255) because I need to be able to store about 500 characters in it, what is the next best datatype to use without increasing the size of the db drastically?

thanks

like image 252
user1005319 Avatar asked Apr 11 '12 19:04

user1005319


People also ask

Is VARCHAR 255 the max?

The effective maximum length of a VARCHAR is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used. Make sure you are aware of the effects of a multi-byte character set. VARCHAR(255) stores 255 characters, which may be more than 255 bytes.

What does VARCHAR 512 mean?

This means, that in a varchar column for each row one additional byte is used to store how long the string in the row actually is. So the difference between varchar(1024) and varchar(512) is, that your data gets truncated when you try to insert more than 1024 or 512 bytes. Note: bytes, not characters.

Why do we write VARCHAR 255 in SQL?

One should use Varchar over Char if your input is going to be varying lengths. Like a name field.. Set it to what you believe will be the max length.. SQL will only allocate what you use, as opposed to char which will always allocate the max needed.

Which is faster VARCHAR or text?

In most circumstances, VARCHAR provides better performance, it's more flexible, and can be fully indexed. If you need to store longer strings, use MEDIUMTEXT or LONGTEXT, but be aware that very large amounts of data can be stored in columns of these types.


2 Answers

If you're on 5.0.3 or later a varchar can be 65535 characters.

11.4.1. The CHAR and VARCHAR Types

MySQL: Large VARCHAR vs. TEXT

like image 41
idstam Avatar answered Sep 24 '22 22:09

idstam


Starting with MySQL 5.0.3, you can simply use VARCHAR(500).

M represents the maximum column length in characters. In MySQL 5.0, the range of M is 0 to 255 before MySQL 5.0.3, and 0 to 65,535 in MySQL 5.0.3 and later.

like image 166
Sergey Kalinichenko Avatar answered Sep 24 '22 22:09

Sergey Kalinichenko