What is the equivalent of varchar(max) in MySQL?
相关问题
- sqlyog export query result as csv
- NOT DISTINCT query in mySQL
- MySQL: conduct a basic search
- Why sometimes there is one of more gap(s) in the v
- mySQL alter table on update, current timestamp
Mysql Converting column from VARCHAR to TEXT when under limit size!!!
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.
The max length of a varchar in MySQL 5.6.12 is 4294967295.
The max length of a varchar is subject to the max row size in MySQL, which is 64KB (not counting BLOBs):
However, note that the limit is lower if you use a multi-byte character set:
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.
But if we try decreasing lengths, we find the greatest length that works:
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.
In spite of what the last error told us, InnoDB still doesn't like a length of 21845.
This makes perfect sense, if you calculate that 21845*3 = 65535, which wouldn't have worked anyway. Whereas 21844*3 = 65532, which does work.
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 had to use
TEXT
,NTEXT
, orBLOB
columns types, these column types stored their data as a collection of 8K pages separate from the 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
, andUPDATETEXT
)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 forvarchar(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
, andBLOB
thevarchar(max)
column type supports all the same query semantics as other column types.So
varchar(MAX)
really meansvarchar(AS_MUCH_AS_I_WANT_TO_STUFF_IN_HERE_JUST_KEEP_GROWING)
and notvarchar(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 aBLOB
column type. This article discusses a very effective method of storing large amounts of data in MySql efficiently.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;