[My]SQL VARCHAR 大小和空终止
免责声明:总的来说,我对 SQL 和数据库非常陌生。
我需要创建一个最多存储 32 个字符的文本数据的字段。 “VARCHAR(32)
”是否意味着我的数据有恰好 32 个字符? 我需要为空终止保留一个额外的字符吗?
我进行了简单的测试,看来这是一个所见即所得的缓冲区。 然而,我想从真正知道自己在做什么的人那里得到具体的答案。
我有 C[++] 背景,所以这个问题在我脑海中敲响了警钟。
Disclaimer: I'm very new to SQL and databases in general.
I need to create a field that will store a maximum of 32 characters of text data. Does "VARCHAR(32)
" mean that I have exactly 32 characters for my data? Do I need to reserve an extra character for null-termination?
I conducted a simple test and it seems that this is a WYSIWYG buffer. However, I wanted to get a concrete answer from people who actually know what they're doing.
I have a C[++] background, so this question is raising alarm bells in my head.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您有 32 个角色可供使用。 SQL 并不像某些编程语言那样关心以 null 结尾的字符串。
Yes, you have 32 characters at your disposal. SQL does not concern itself with nul terminated strings like some programming languages do.
您的 VARCHAR 规范大小是数据的最大大小,因此在本例中为 32 个字符。 然而,VARCHARS 是一个动态字段,因此实际使用的物理存储只是数据的大小加上一两个字节。
如果你把一个10个字符的字符串放入VARCHAR(32)中,物理存储将是11或12个字节(手册会告诉你确切的公式)。
然而,当 MySQL 处理结果集时(即在 SELECT 之后),将为每条记录的该字段在内存中分配 32 个字节。
Your VARCHAR specification size is the max size of your data, so in this case, 32 characters. However, VARCHARS are a dynamic field, so the actual physical storage used is only the size of your data, plus one or two bytes.
If you put a 10-character string into a VARCHAR(32), the physical storage will be 11 or 12 bytes (the manual will tell you the exact formula).
However, when MySQL is dealing with result sets (ie. after a SELECT), 32 bytes will be allocated in memory for that field for every record.