217

What is the equivalent of varchar(max) in MySQL?

David Basarab
  • 72,212
  • 42
  • 129
  • 156

5 Answers5

235

The max length of a varchar is subject to the max row size in MySQL, which is 64KB (not counting BLOBs):

VARCHAR(65535)

However, note that the limit is lower if you use a multi-byte character set:

VARCHAR(21844) CHARACTER SET utf8

Here are some examples:

The maximum row size is 65535, but a varchar also includes a byte or two to encode the length of a given string. So you actually can't declare a varchar of the maximum row size, even if it's the only column in the table.

mysql> CREATE TABLE foo ( v VARCHAR(65534) );
ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs

But if we try decreasing lengths, we find the greatest length that works:

mysql> CREATE TABLE foo ( v VARCHAR(65532) );
Query OK, 0 rows affected (0.01 sec)

Now if we try to use a multibyte charset at the table level, we find that it counts each character as multiple bytes. UTF8 strings don't necessarily use multiple bytes per string, but MySQL can't assume you'll restrict all your future inserts to single-byte characters.

mysql> CREATE TABLE foo ( v VARCHAR(65532) ) CHARSET=utf8;
ERROR 1074 (42000): Column length too big for column 'v' (max = 21845); use BLOB or TEXT instead

In spite of what the last error told us, InnoDB still doesn't like a length of 21845.

mysql> CREATE TABLE foo ( v VARCHAR(21845) ) CHARSET=utf8;
ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs

This makes perfect sense, if you calculate that 21845*3 = 65535, which wouldn't have worked anyway. Whereas 21844*3 = 65532, which does work.

mysql> CREATE TABLE foo ( v VARCHAR(21844) ) CHARSET=utf8;
Query OK, 0 rows affected (0.32 sec)
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • 22
    I'd also suggest using TEXT – Adi Jun 04 '12 at 09:50
  • 3
    @AdnanShammout: Sure, unless you want to declare a `DEFAULT` value for the column. Can't do that with TEXT or BLOB types. – Bill Karwin Jun 04 '12 at 16:53
  • Will using Text give you more than 65535 length? – Dan W Jan 18 '13 at 10:41
  • 6
    @DanW, `TEXT` length limit is 64K. `MEDIUMTEXT` length limit is 16M. `LONGTEXT` length limit is 4G. – Bill Karwin Jan 18 '13 at 16:36
  • 4
    The biggest problem with this is that if you create a column of that size, you can only have that **one** column in your table. The *MAX* row size in MySql is 64k, so if you have a 64k column, that's all you get. This same constraint does not apply to Sql Server's `varchar(max)` column type. – joshperry Feb 08 '13 at 19:01
  • 1
    @joshperry, yes but TEXT/BLOB columns count only a small amount (9-12 bytes) toward that 64KB row size limit, so if you need a large VARCHAR as well as a number of other columns, it's better to use TEXT. – Bill Karwin Feb 08 '13 at 19:21
  • What if the table is already set to use utf8? Does that mean the max is `VARCHAR(21844)`? – 2rs2ts Jan 10 '14 at 15:48
  • 1
    @2rs2ts, yes, if the default charset at the database or table level is utf8, then you get an error if you try to declare any varchar longer than 21844. I'll edit the answer above to show an example. – Bill Karwin Jan 10 '14 at 15:57
86

TLDR; MySql does not have an equivalent concept of varchar(max), this is a MS SQL Server feature.

What is VARCHAR(max)?

varchar(max) is a feature of Microsoft SQL Server.

The amount of data that a column could store in Microsoft SQL server versions prior to version 2005 was limited to 8KB. In order to store more than 8KB you would have to use TEXT, NTEXT, or BLOB columns types, these column types stored their data as a collection of 8K pages separate from the table data pages; they supported storing up to 2GB per row.

The big caveat to these column types was that they usually required special functions and statements to access and modify the data (e.g. READTEXT, WRITETEXT, and UPDATETEXT)

In SQL Server 2005, varchar(max) was introduced to unify the data and queries used to retrieve and modify data in large columns. The data for varchar(max) columns is stored inline with the table data pages.

As the data in the MAX column fills an 8KB data page an overflow page is allocated and the previous page points to it forming a linked list. Unlike TEXT, NTEXT, and BLOB the varchar(max) column type supports all the same query semantics as other column types.

So varchar(MAX) really means varchar(AS_MUCH_AS_I_WANT_TO_STUFF_IN_HERE_JUST_KEEP_GROWING) and not varchar(MAX_SIZE_OF_A_COLUMN).

MySql does not have an equivalent idiom.

In order to get the same amount of storage as a varchar(max) in MySql you would still need to resort to a BLOB column type. This article discusses a very effective method of storing large amounts of data in MySql efficiently.

dylan-myers
  • 323
  • 3
  • 18
joshperry
  • 41,167
  • 16
  • 88
  • 103
  • 1
    This has more to do with SQL Server than MySQL. – Kermit Feb 08 '13 at 16:01
  • 17
    @njk Yes, but in order to explain what the "equivalent" in MySql (or the lack of an equivalent in this case) necessitated describing exactly what `varchar(max)` **really** means. – joshperry Feb 08 '13 at 18:56
32

The max length of a varchar is

65535

divided by the max byte length of a character in the character set the column is set to (e.g. utf8=3 bytes, ucs2=2, latin1=1).

minus 2 bytes to store the length

minus the length of all the other columns

minus 1 byte for every 8 columns that are nullable. If your column is null/not null this gets stored as one bit in a byte/bytes called the null mask, 1 bit per column that is nullable.

ʞɔıu
  • 47,148
  • 35
  • 106
  • 149
  • I like the format of your answer but it seems to be missing some useful information that the top answer provides regarding charsets and stuff – Joe Phillips Apr 17 '18 at 20:46
9

For Sql Server

alter table prg_ar_report_colors add Text_Color_Code VARCHAR(max);

For MySql

alter table prg_ar_report_colors add Text_Color_Code longtext;

For Oracle

alter table prg_ar_report_colors add Text_Color_Code CLOB;

Dinanath Parit
  • 165
  • 3
  • 4
  • 3
    note that longtext, text and blob are stored differently than varchar. varchar is stored with the other fields, while text is stored on a separate file in the server. it has pros and cons so careful which one to choose – santiago arizti Apr 06 '18 at 15:34
3

Mysql Converting column from VARCHAR to TEXT when under limit size!!!

mysql> CREATE TABLE varchars1(ch3 varchar(6),ch1 varchar(3),ch varchar(4000000))
;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> SHOW WARNINGS;
+-------+------+---------------------------------------------+
| Level | Code | Message                                     |
+-------+------+---------------------------------------------+
| Note  | 1246 | Converting column 'ch' from VARCHAR to TEXT |
+-------+------+---------------------------------------------+
1 row in set (0.00 sec)

mysql>
zloctb
  • 10,592
  • 8
  • 70
  • 89