MySQL 数据库中存储文章的字段/数据类型

发布于 2024-10-09 14:54:34 字数 221 浏览 0 评论 0原文

我不禁相信这个话题已经被写了一遍又一遍,但我很难找到任何好的、可靠的信息。

我应该使用什么数据类型来存储 200 到 400 个字的文本?篇幅较长、接近两三千字的文章又如何呢?

哪些选项会影响我的决定?我不打算搜索这些数据,但我不能完全排除我稍后可能想要搜索的可能性。

不幸的是,我的背景是 MS Access,唯一的选择是备注字段。对于 MySQL 来说,事情似乎并不那么简单。

I can't help but believe this topic has been written about over and over again but I'm having trouble finding any good, solid information.

What data type should I use to store 200 to 400 words of text? What about longer articles that could approach two or three thousand words?

What options should affect my decision? I don't plan to search this data but I can't completely rule out the possibility that I may want to do that later.

Unfortunately my background is MS Access where the only option for this was a memo field. It doesn't appear to be quite so simple with MySQL.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嘿嘿嘿 2024-10-16 14:54:34

如果您使用的是 MySQL 5.0.3 或更高版本,请转至 VARCHAR。它可以容纳 65k 字节。只要每行只有 1 个 long VARCHAR,就应该没问题。

否则就用文字。

来自mysql手册:

BLOB 和 TEXT 与 VARBINARY 不同
和 VARCHAR 的方式如下:

没有删除尾随空格
当值为 BLOB 和 TEXT 列时
存储或检索。 MySQL之前
5.0.3,这与 VARBINARY 和 VARCHAR 不同,后者的尾随空格是
存储值时删除。

比较时,TEXT 是空格扩展的
准确地拟合比较对象
例如 CHAR 和 VARCHAR。

对于 BLOB 和 TEXT 列上的索引,
您必须指定索引前缀
长度。对于 CHAR 和 VARCHAR,前缀
长度是可选的。参见第 7.5.1 节,
“列索引”。

BLOB 和 TEXT 列不能有
默认值。

也很高兴知道(来自手册):

BLOB 或 TEXT 列的实例
查询的结果是
使用临时表进行处理
导致服务器使用表
磁盘而不是内存中,因为
MEMORY存储引擎不支持
这些数据类型

在制定使用 TEXT 的查询时确实应该考虑

If you're using MySQL 5.0.3 or later, go VARCHAR. It can hold 65k bytes. As long as you have only 1 long VARCHAR per row, you should be fine.

Otherwise go with text.

From the mysql manual:

BLOB and TEXT differ from VARBINARY
and VARCHAR in the following ways:

There is no trailing-space removal for
BLOB and TEXT columns when values are
stored or retrieved. Before MySQL
5.0.3, this differs from VARBINARY and VARCHAR, for which trailing spaces are
removed when values are stored.

On comparisons, TEXT is space extended
to fit the compared object, exactly
like CHAR and VARCHAR.

For indexes on BLOB and TEXT columns,
you must specify an index prefix
length. For CHAR and VARCHAR, a prefix
length is optional. See Section 7.5.1,
“Column Indexes”.

BLOB and TEXT columns cannot have
DEFAULT values.

Also nice to know (from the manual):

Instances of BLOB or TEXT columns in
the result of a query that is
processed using a temporary table
causes the server to use a table on
disk rather than in memory because the
MEMORY storage engine does not support
those data types

which you really should take into account when formulating queries which use TEXT.

老子叫无熙 2024-10-16 14:54:34

TEXT 字段应该足够大以存储大多数文章。似乎与Access的Memo类型差不多。它最多可以容纳 65535 个字符,这大约是...我不知道...平均 10-12,000 个单词?

A TEXT field should be big enough to store most articles. Seems to be about equivalent to Access's Memo type. It can hold up to 65535 chars, which would be somewhere around...i dunno...10-12,000 words, on average?

筱果果 2024-10-16 14:54:34

TEXT 数据类型对于您的情况来说是一个安全的选择,VARCHAR 通常在需要索引或需要存储明确定义的值(IP地址、邮政编码等)。

The TEXT data type is a safe bet for your situation, VARCHARs are usually used when they need to be indexed or there is a well-defined value to be stored (IP address, zip code, etc).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文