MySQL中VARCHAR的单位

MySQL中VARCHAR的单位,第1张

VARCHAR是MySQL中常用的数据类型,但其单位究竟是字符还是字节呢?

经过查阅,MySQL的 官方文档 给出了答案:

4.1及之后的版本,VARCHAR的单位是字符;4.1之前的版本,VARCHAR的单位是字节。(As of version 4.1 , 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.)

同时官方文档也提到,4.1及之后的版本以UTF-8作为预定义字符集。(VARCHAR is shorthand for CHARACTER VARYING. NATIONAL VARCHAR is the standard SQL way to define that a VARCHAR column should use some predefined character set. MySQL 4.1 and up uses UTF8 as this predefined character set .)

在 Go 语言中,是不能用 len 函数来统计字符串中的字符个数的,这是因为在 Go 中,字符串是以 UTF-8 为格式进行存储的,在字符串上调用 len 函数,取得的是字符串包含的 byte 的个数。

那么如何在Go语言中获取字符串中的字符个数呢?有下面几种方法:

测试发现速度最快的是 utf8.RuneCountInString()

在 *** 作符命令下不能使用 运算表达式,可以使用 M,G等单位。

在mysql命令行中,可以使用 运算表达式, 不可以使用M,G等单位。

例: 在OS命令下:

[mysql@localhost mysql]$ mysql --max-allowed-packet=16M -u root -p123456

Warning: Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor. Commands end with or \g.

Your MySQL connection id is 13

Server version: 5.6.23-enterprise-commercial-advanced MySQL Enterprise Server - Advanced Edition (Commercial)

[mysql@localhost mysql]$ mysql --max-allowed-packet=16*1024*1024 -u root -p123456

Unknown suffix '*' used for variable 'max_allowed_packet' (value '16*1024*1024')

Warning: option 'max_allowed_packet': unsigned value 0 adjusted to 4096

mysql: Error while setting value '16*1024*1024' to 'max_allowed_packet'

在mysql命令行下

mysql>set global max_allowed_packet=16M

ERROR 1232 (42000): Incorrect argument type to variable 'max_allowed_packet'

mysql>set global max_allowed_packet=16*1024*1024

Query OK, 0 rows affected (0.00 sec)

Mysql中经常用来存储日期的数据类型有三种:Date、Datetime、Timestamp。

Date数据类型:用来存储没有时间的日期。Mysql获取和显示这个类型的格式为“YYYY-MM-DD”。支持的时间范围为“1000-00-00”到“9999-12-31”。

Datetime类型:存储既有日期又有时间的数据。存储和显示的格式为 “YYYY-MM-DD HH:MM:SS”。支持的时间范围是“1000-00-00 00:00:00”到“9999-12-31 23:59:59”。

Timestamp类型:也是存储既有日期又有时间的数据。存储和显示的格式跟Datetime一样。支持的时间范围是“1970-01-01 00:00:01”到“2038-01-19 03:14:07”。

所有不符合上面所述格式的数据都会被转换为相应类型的0值。(0000-00-00或者0000-00-00 00:00:00)


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/8646410.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-19
下一篇2023-04-19

发表评论

登录后才能评论

评论列表(0条)

    保存