8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

13.3.4 BLOB 和 TEXT 类型13.3.4 The BLOB and TEXT Types

BLOB 是可以存储可变数量数据的二进制大对象。四种 BLOB 类型是 TINYBLOB、BLOB、MEDIUMBLOB 和 LONGBLOB。它们的区别仅在于可以存储值的最大长度。四种 TEXT 类型是 TINYTEXT、TEXT、MEDIUMTEXT 和 LONGTEXT。它们对应于四种 BLOB 类型,并且具有相同的最大长度和存储要求。请参阅 第 13.7 节,“数据类型存储要求”。A BLOB is a binary large object that can hold a variable amount of data. The four BLOB types are TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB. These differ only in the maximum length of the values they can hold. The four TEXT types are TINYTEXT, TEXT, MEDIUMTEXT, and LONGTEXT. These correspond to the four BLOB types and have the same maximum lengths and storage requirements. See Section 13.7, “Data Type Storage Requirements”.

BLOB 值被视为二进制字符串(字节字符串)。它们具有 binary 字符集和校对规则,比较和排序基于列值中字节的数值。TEXT 值被视为非二进制字符串(字符字符串)。它们具有非 binary 的字符集,值的排序和比较基于字符集的校对规则。BLOB values are treated as binary strings (byte strings). They have the binary character set and collation, and comparison and sorting are based on the numeric values of the bytes in column values. TEXT values are treated as nonbinary strings (character strings). They have a character set other than binary, and values are sorted and compared based on the collation of the character set.

如果未启用严格 SQL 模式,并且你向 BLOB 或 TEXT 列分配超过该列最大长度的值,该值将被截断以适应列长度,并生成一个警告。对于非空格字符的截断,可以使用严格 SQL 模式导致错误发生(而不是警告)并阻止插入该值。请参见 第 7.1.11 节,“服务器 SQL 模式”。If strict SQL mode is not enabled and you assign a value to a BLOB or TEXT column that exceeds the column's maximum length, the value is truncated to fit and a warning is generated. For truncation of nonspace characters, you can cause an error to occur (rather than a warning) and suppress insertion of the value by using strict SQL mode. See Section 7.1.11, “Server SQL Modes”.

无论 SQL 模式如何,对于插入到 TEXT 列的值,超出尾随空格的截断总会生成警告。Truncation of excess trailing spaces from values to be inserted into TEXT columns always generates a warning, regardless of the SQL mode.

对于 TEXT 和 BLOB 列,插入时不会进行填充,查询时也不会剥离任何字节。For TEXT and BLOB columns, there is no padding on insert and no bytes are stripped on select.

如果对一个 TEXT 列建立了索引,索引条目的比较会在末尾进行空格填充。这意味着,如果索引要求唯一值,对于那些仅在尾随空格数量上不同的值,会发生重复键错误。例如,如果表中包含 'a',尝试存储 'a ' 会导致重复键错误。对于 BLOB 列,则不适用这种情况。If a TEXT column is indexed, index entry comparisons are space-padded at the end. This means that, if the index requires unique values, duplicate-key errors occur for values that differ only in the number of trailing spaces. For example, if a table contains 'a', an attempt to store 'a ' causes a duplicate-key error. This is not true for BLOB columns.

In most respects, you can regard a BLOB column as a VARBINARY column that can be as large as you like. Similarly, you can regard a TEXT column as a VARCHAR column. BLOB以及TEXT differ from VARBINARY以及VARCHAR in the following ways:In most respects, you can regard a BLOB column as a VARBINARY column that can be as large as you like. Similarly, you can regard a TEXT column as a VARCHAR column. BLOB and TEXT differ from VARBINARY and VARCHAR in the following ways:

如果你在 TEXT 数据类型上使用 BINARY 属性,该列将被分配其字符集的二进制 (_bin) 排序规则。If you use the BINARY attribute with a TEXT data type, the column is assigned the binary (_bin) collation of the column character set.

LONG 和 LONG VARCHAR 映射到 MEDIUMTEXT 数据类型。这是一个兼容性功能。LONG and LONG VARCHAR map to the MEDIUMTEXT data type. This is a compatibility feature.

MySQL Connector/ODBC 将 BLOB 值定义为 LONGVARBINARY,将 TEXT 值定义为 LONGVARCHAR。MySQL Connector/ODBC defines BLOB values as LONGVARBINARY and TEXT values as LONGVARCHAR.

由于 BLOB 和 TEXT 值可能非常长,在使用它们时你可能会遇到一些限制:Because BLOB and TEXT values can be extremely long, you might encounter some constraints in using them:

每个 BLOB 或 TEXT 值在内部由单独分配的对象表示。这与所有其他数据类型形成对比,其他数据类型在表打开时每列只分配一次存储空间。Each BLOB or TEXT value is represented internally by a separately allocated object. This is in contrast to all other data types, for which storage is allocated once per column when the table is opened.

在某些情况下,可能希望将二进制数据(如媒体文件)存储在 BLOB 或 TEXT 列中。您可能会发现 MySQL 的字符串处理函数对于处理此类数据很有用。请参阅 第 14.8 节,“字符串函数和运算符”。出于安全和其他原因,通常更建议通过应用程序代码来实现,而不是授予应用程序用户 FILE 权限。您可以在 MySQL 论坛 (http://forums.mysql.com/) 中讨论各种语言和平台的具体情况。In some cases, it may be desirable to store binary data such as media files in BLOB or TEXT columns. You may find MySQL's string handling functions useful for working with such data. See Section 14.8, “String Functions and Operators”. For security and other reasons, it is usually preferable to do so using application code rather than giving application users the FILE privilege. You can discuss specifics for various languages and platforms in the MySQL Forums (http://forums.mysql.com/).

Note

在 mysql 客户端中,二进制字符串显示为十六进制表示,具体取决于 --binary-as-hex 的值。有关该选项的更多信息,请参阅 第 6.5.1 节,“mysql — MySQL 命令行客户端”。Within the mysql client, binary strings display using hexadecimal notation, depending on the value of the --binary-as-hex. For more information about that option, see Section 6.5.1, “mysql — The MySQL Command-Line Client”.