为什么MySQL忽略了列的数据类型长度?

发布于 2025-02-03 16:26:42 字数 394 浏览 3 评论 0原文

我正在使用MySQL 8.0,并且正在尝试将URL存储在列中。每当我尝试插入超过191个字符的URL时,我都会收到以下错误:

1406 Data too long for column 'url' at row 1

列数据类型为varchar(1000),URL为〜450个字符。我尝试插入其他字符串和任何191个字符,并且在工作中正常,但是在191个字符上没有什么能做的。

我尝试将此列的数据类型更改为其他字符串格式,例如文本,longText和blob。我还尝试在其他表格中插入191个字符的字符串,并且还限制为191个字符。

我知道UTF8MB4会导致Varchar(255)实际为191,但是我不应该用Varchar(1000)覆盖它吗?数据库中是否有可能引起此问题的设置?

I am using MySQL 8.0 and I am trying to store a URL in a column. Every time I try to insert a URL longer than 191 characters, I get the following error:

1406 Data too long for column 'url' at row 1

The column data type is VARCHAR(1000) and the URL is ~450 characters. I have tried inserting other strings and anything 191 characters and under works fine but nothing does over 191 chars.

I have tried changing the datatype for this column to other string formats like TEXT, LONGTEXT, and BLOB. I have also tried inserting strings over 191 characters in other tables and they are also limited to 191 characters.

I know utf8mb4 causes VARCHAR(255) to actually be 191 but I shouldn't it be possible to override that with VARCHAR(1000)? Is there a setting in the database that can cause this issue?

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

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

发布评论

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

评论(1

三生殊途 2025-02-10 16:26:42

取自 eshere

On a database that uses a utf8mb collation, each character takes 4 bytes.
On innodb, indexes (the prefixes to be exact) can contain up to 767 bytes.

So, the max indexable length on utf8mb + innodb is 767/4=191 characters.

因此,您最好使用text格式为您的URL。

更新:

根据此答案,问题是在Charset中,是utf8mb4utf8,整理相同。

Taken from here:

On a database that uses a utf8mb collation, each character takes 4 bytes.
On innodb, indexes (the prefixes to be exact) can contain up to 767 bytes.

So, the max indexable length on utf8mb + innodb is 767/4=191 characters.

So you better to use TEXT format for your URLs.

UPDATED:

According to this answer, the problem is in charset, which is utf8mb4 when it needs to be utf8, same for collation.

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