如何在 MySQL 数据库中存储超过 255 个字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1476987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:11:02  来源:igfitidea点击:

How to store more than 255 char in MySQL database?

sqlmysql

提问by DNB5brims

I only get set the text field in MySQL to 255, if I want to store a data longer than 255 chars, what can I do?

我只将 MySQL 中的文本字段设置为 255,如果我想存储超过 255 个字符的数据,我该怎么办?

回答by Paul Dixon

Prior to MySQL 5.0.3, a VARCHARcould only store up to 255 characters.

在 MySQL 5.0.3 之前,VARCHAR最多只能存储 255 个字符。

  • To store up to 65535 (64KB) characters, use a TEXTcolumn.
  • To store up to 16777216 (16MB ) characters, use a MEDIUMTEXTcolumn.
  • To store up to 4294967296 (4GB) characters, use a LONGTEXTcolumn.
  • 要存储最多 65535 (64KB) 个字符,请使用TEXT列。
  • 要存储最多 16777216 (16MB) 个字符,请使用MEDIUMTEXT列。
  • 要存储最多 4294967296 (4GB) 个字符,请使用LONGTEXT列。

See the storage requirementssection of the manual for caveats on their usage.

有关其使用的注意事项,请参阅手册的存储要求部分。

Versions of MySQL after 5.0.3 can store up to 65535 chars in a VARCHAR (However you cannot store more than 65535 bytes in a single row).

MySQL 5.0.3 之后的版本最多可以在 VARCHAR 中存储 65535 个字符(但是,您不能在单行中存储超过 65535 个字节)。

回答by Quassnoi

Use TEXTdatatype:

使用TEXT数据类型:

CREATE TABLE t_text (value TEXT NOT NULL);

INSERT
INTO    t_text
SELECT  RPAD('', 1000, '*');

SELECT  LENGTH(value)
FROM    t_text;

---

1000

回答by simon

Change data type to varchar.

将数据类型更改为 varchar。

回答by simon

MySQL 4.0 -- Maximum length storage (Bytes) for String types:

MySQL 4.0 -- 字符串类型的最大长度存储(字节):

CHAR(255); 
VARCHAR(255); 
TINYBLOB, TINYTEXT < 2^8; 
BLOB, TEXT < 2^16; 
MEDIUMBLOB, MEDIUMTEXT < 2^24; 
LONGBLOB, LONGTEXT < 2^32;