gpt4 book ai didi

mysql - MySQL中VARCHAR(255)的存储大小是多少

转载 作者:行者123 更新时间:2023-11-29 12:19:09 24 4
gpt4 key购买 nike

我能够在数据类型为 VARCHAR(255) 的 MySQL 列中存储超过 255 个字符(例如“a”)。请澄清

最佳答案

字符列的最大存储大小由宽度定义(即 255)。您可以插入较短的值,但它们将被截断并显示警告。

如果您使用其中一种严格的 SQL 模式,它将产生错误而不是警告。

查看文档: https://dev.mysql.com/doc/refman/5.6/en/string-type-overview.html

mysql [localhost] {msandbox} (test) > CREATE TABLE t_varchar(a VARCHAR(127));
Query OK, 0 rows affected (0.02 sec)

mysql [localhost] {msandbox} (test) > SET sql_mode='';
Query OK, 0 rows affected (0.00 sec)

mysql [localhost] {msandbox} (test) > INSERT INTO t_varchar VALUES(REPEAT('a', 127));
Query OK, 1 row affected (0.00 sec)

mysql [localhost] {msandbox} (test) > INSERT INTO t_varchar VALUES(REPEAT('a', 128));
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql [localhost] {msandbox} (test) > SELECT LENGTH(a) FROM t_varchar;
+-----------+
| LENGTH(a) |
+-----------+
| 127 |
| 127 |
+-----------+
2 rows in set (0.00 sec)

mysql [localhost] {msandbox} (test) >
mysql [localhost] {msandbox} (test) > TRUNCATE TABLE t_varchar;
Query OK, 0 rows affected (0.00 sec)

mysql [localhost] {msandbox} (test) > SET sql_mode='STRICT_ALL_TABLES';
Query OK, 0 rows affected (0.00 sec)

mysql [localhost] {msandbox} (test) > INSERT INTO t_varchar VALUES(REPEAT('a', 127));
Query OK, 1 row affected (0.00 sec)

mysql [localhost] {msandbox} (test) > INSERT INTO t_varchar VALUES(REPEAT('a', 128));
ERROR 1406 (22001): Data too long for column 'a' at row 1
mysql [localhost] {msandbox} (test) > SELECT LENGTH(a) FROM t_varchar;
+-----------+
| LENGTH(a) |
+-----------+
| 127 |
+-----------+
1 row in set (0.00 sec)

关于mysql - MySQL中VARCHAR(255)的存储大小是多少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29294086/

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