我能够在数据类型为VARCHAR(255)的MySQL列中存储超过255个字符(比如'a')。请澄清
答案 0 :(得分:1)
字符列的最大存储大小由宽度(即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)