gpt4 book ai didi

mysql - 如何在 mysql 5.1 中限制 varchar?

转载 作者:太空宇宙 更新时间:2023-11-03 10:51:44 25 4
gpt4 key购买 nike

我需要在 mysql 5.1 中创建可以存储用户反馈的列。它不应该太长,所以我认为不会超过 1000 个 UTF-8 字符。

问题是如何在 mysql 5.1 中有效地表示它。

现在我有:

`description` varchar NOT NULL,

但是如何限制 varchar 最多容纳 1000 个 UTF-8 字符?

最佳答案

来自 the documentation :

Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 255 before MySQL 5.0.3, and 0 to 65,535 in 5.0.3 and later versions. The effective maximum length of a VARCHAR in MySQL 5.0.3 and later is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used.

这意味着您可以在 VARCHAR 列中存储最多 65,535 个字节。然而,从String Type Overview :

MySQL interprets length specifications in character column definitions in character units. (Before MySQL 4.1, column lengths were interpreted in bytes.) This applies to CHAR, VARCHAR, and the TEXT types.

因此,使用 UTF8 排序规则声明您的表并将 varchar 的长度设置为 1,000 个字符,MySQL 将在幕后为您完成工作。

关于mysql - 如何在 mysql 5.1 中限制 varchar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24350217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com