ITPub博客

首页 > 数据库 > MySQL > MySQL中varchar和char定义长度是字符,与Oracle nvarchar2类似

MySQL中varchar和char定义长度是字符,与Oracle nvarchar2类似

原创 MySQL 作者:denglt 时间:2014-02-18 17:44:58 0 删除 编辑
今天测试了下MySQL的字符类型varchar,发现与Oracle nvarchar2类型相似,定义的是字符长度,而非字节长度。

mysql> select @@version;
+------------------+
| @@version        |
+------------------+
| 5.6.14-ndb-7.3.3 |
+------------------+
1 row in set (0.00 sec)

mysql> desc test1;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(10) | YES  |     | NULL    |       |
| name2 | char(10)    | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

 

mysql> select length(name),name,length(name2),name2 from test1;
+--------------+--------------------------------+---------------+--------------------------------+
| length(name) | name                           | length(name2) | name2                          |
+--------------+--------------------------------+---------------+--------------------------------+
|           18 | 邓小平好人人人人人             |            18 | 邓小平好人人人人人             |
|           10 | 邓小平好人                     |            10 | 邓小平好人                     |
|           10 | 0123456789                     |            10 | 0123456789                     |
|           20 | 邓小平好人人人人人人           |            20 | 邓小平好人人人人人人           |
|            6 | 邓小平                         |             6 | 邓小平                         |
|           12 | 邓小平好人人                   |            12 | 邓小平好人人                   |
+--------------+--------------------------------+---------------+--------------------------------+
6 rows in set (0.00 sec)



mysql> insert into test1 (name) values('12345678901');
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> insert into test1 (name2) values('12345678901');
ERROR 1406 (22001): Data too long for column 'name2' at row 1
 
 
网上的另一篇说明这个的文章:http://cau99.blog.51cto.com/1855224/383023/

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/195110/viewspace-1083351/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2010-11-04

  • 博文量
    118
  • 访问量
    708736