mysql varchar

本文介绍在MySQL中当插入到VARCHAR字段的数据超出定义长度时的行为。数据不会导致错误,但会触发警告,并按定义的最大长度进行截断。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当插入的字符数超过varchar(n)限制的长度时,字符会被截断,不会出错,但会有警告

mysql> create table aa(name varchar(12));
Query OK, 0 rows affected (0.06 sec)

mysql> insert into aa value('qwertyuiopasdf');
Query OK, 1 row affected, 1 warning (0.03 sec)

mysql> show warnings;
+---------+------+-------------------------------------------+
| Level | Code | Message |
+---------+------+-------------------------------------------+
| Warning | 1265 | Data truncated for column 'name' at row 1 |
+---------+------+-------------------------------------------+
1 row in set (0.00 sec)

mysql> select * from aa;
+--------------+
| name |
+--------------+
| qwertyuiopas |
+--------------+



转载于:https://www.cnblogs.com/iLoveMyD/archive/2012/03/28/2420998.html

MySQL中,varchar是一种用于存储可变长度字符数据的数据类型。它可以存储最大长度为65535个字节的字符数据,但是需要注意以下几点: 1. 如果在一个表中只有一个varchar字段,并且该字段设置为NOT NULL,那么该字段的最大长度为65533个字节,即65535-2=65533byte[^1]。 2. 如果在一个表中有多个字段,并且其中一个字段是varchar类型,那么该表的行大小不能超过65535个字节,不包括BLOB类型的字段。如果超过了这个限制,你需要将一些字段改为TEXT或BLOB类型。 下面是一些示例: 1. 创建一个表t4,其中包含一个int字段c,一个char(30)字段c2,和一个varchar(21812)字段c3: ```mysql mysql> create table t4(c int, c2 char(30), c3 varchar(21812)) charset=utf8; Query OK, 0 rows affected (0.05 sec) ``` 2. 创建一个表t5,其中包含一个int字段c,一个char(30)字段c2,和一个varchar(21813)字段c3。由于行大小超过了限制,会报错: ```mysql mysql> create table t5(c int, c2 char(30), c3 varchar(21813)) charset=utf8; ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. You have to change some columns to TEXT or BLOBs ``` 3. 创建一个表t2,其中只有一个varchar字段name,并且该字段设置为NOT NULL。该字段的最大长度为65533个字节: ```mysql mysql> create table t2 ( name varchar(65533) not null) charset=latin1; Query OK, 0 rows affected (0.03 sec) ``` 4. 创建一个表t3,其中只有一个varchar字段name,并且该字段设置为NOT NULL。由于字段长度超过了限制,会报错: ```mysql mysql> create table t3 ( name varchar(65534) not null) charset=latin1; ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. You have to change some columns to TEXT or BLOBs ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值