通过本文主要向大家介绍了mysql等相关知识,希望本文的分享对您有所帮助
CHAR 0-255字节 定长字符串
VARCHAR 0-65535 字节 变长字符串
创建测试表
mysql> create table char_test(char_col char(10));
Query OK, 0 rows affected (0.30 sec)
查看mysql中的创建语句(这样可以看到更详细的创建语句,可以把INTEGER、BOOL等别名转换为MySQL的基本数据类型。);
mysql> show create table char_test;
+-----------+------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-----------+------------------------------------------------------------------------------------------------------+
| char_test | CREATE TABLE `char_test` (
`char_col` char(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-----------+------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
插入3行数据
mysql> insert into char_test(char_col) values
-> ('string1'),(' string2'),('string3 ');
Query OK, 3 rows affected (0.03 sec)
Records: 3 Duplicates: 0 Warnings: 0
这是后检索这些值的时候,发现string3的末尾空格被截断了;
mysql> select concat("'", char_col, "'" ) from char_test;
+-----------------------------+
| concat("'", char_col, "'" ) |
+-----------------------------+
| 'string1' |
| ' string2' |
| 'string3' |
+-----------------------------+
3 rows in set (0.00 sec)
如果用varchar(10)字段存储相同的值,可以得到如下结果:
mysql> select concat("'", varchar_col, "'" ) from varchar_test;
+--------------------------------+
| concat("'", varchar_col, "'" ) |
+--------------------------------+
| 'string1' |
| ' string2' |
| 'string3 ' |
+--------------------------------+
3 rows in set (0.00 sec)
参考:高性能MySQL(第3版). Baron Schwartz,Peter Zaitsev,Vadim Tkachenko 著;宁海元,周振兴,彭立勋 等 译