MySQL学习总结之路(第一章:服务与数据库管理)
MySQL学习总结之路(第二章:表)
MySQL学习总结之路(第三章:数据类型)
MySQL学习总结之路(第四章:运算符)
MySQL学习总结之路(第五章:函数)
MySQL学习总结之路(第六章:表类型【存储引擎】的选择)
MySQL学习总结之路(第七章:选择合适的数据类型)
CHAR 和 VARCHAR 的对比
值 | CHAR(4) | 存储需求 | VARCHAR(4) | 存储需求 |
'' | ' ' | 4 个字节 | '' | 1 个字节 |
'ab' | 'ab ' | 4 个字节 | 'ab ' | 3 个字节 |
'abcd' | 'abcd' | 4 个字节 | 'abcd' | 5 个字节 |
'abcdefgh' | 'abcd' | 4 个字节 | 'abcd' | 5 个字节 |
从 CHAR(4)和 VARCHAR(4)列检索的值并不总是相同,因为检索时从 CHAR 列删除了尾部的空格。
mysql> CREATE TABLE vc (v VARCHAR(4), c CHAR(4)); Query OK, 0 rows affected (0.55 sec) mysql> INSERT INTO vc VALUES ('ab ', 'ab '); Query OK, 1 row affected (0.00 sec) mysql> SELECT CONCAT(v, '+'), CONCAT(c, '+') FROM vc; +----------------+----------------+ | CONCAT(v, '+') | CONCAT(c, '+') | +----------------+----------------+ | ab + | ab+ | +----------------+----------------+ 1 row in set (0.04 sec)
区别: BLOB 能用来保存二进制数据;TEXT 只能保存字符数据
mysql> create table t (id varchar(100),context text); Query OK, 0 rows affected (0.38 sec)
mysql> insert into t values(1,repeat('haha',100)); Query OK, 1 row affected (0.08 sec) mysql> insert into t values(2,repeat('haha',100)); Query OK, 1 row affected (0.00 sec) mysql> insert into t values(3,repeat('haha',100)); Query OK, 1 row affected (0.00 sec) mysql> insert into t select * from t; Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 . . . . mysql> insert into t select * from t; Query OK, 196608 rows affected (4.05 sec) Records: 196608 Duplicates: 0 Warnings: 0
查看文件物理大小:
mysql> delete from t where id =1; Query OK, 131072 rows affected (2.13 sec)
mysql> optimize table t; +--------+----------+----------+----------+ | Table | Op | Msg_type | Msg_text | +--------+----------+----------+----------+ | test.t | optimize | status | OK | +--------+----------+----------+----------+ 1 row in set (0.69 sec)
可以发现,表的数据文件大大缩小,“空洞”空间已经被回收。
c1 列的值由 131072.32 变成了 131072.31,这是上面的数值在使用单精度浮点数表示时,产生了误差。这是浮点数特有的问题。
因此在精度要求比较高的应用中(比如货币)要使用定点数而不是浮点数来保存数据。
mysql> CREATE TABLE test (c1 float(10,2),c2 decimal(10,2)); Query OK, 0 rows affected (0.41 sec) mysql> insert into test values(131072.32,131072.32); Query OK, 1 row affected (0.04 sec) mysql> select * from test; +-----------+-----------+ | c1 | c2 | +-----------+-----------+ | 131072.31 | 131072.32 | +-----------+-----------+ 1 row in set (0.00 sec)
浮点型数据保存小数时,要注意四舍五入的问题,并尽量保留足够的小数位,避免存储的数据不准确。
mysql> create table t (f float( 8,1)); Query OK, 0 rows affected (0.12 sec) mysql> desc t; +-------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------+------+-----+---------+-------+ | f | float(8,1) | YES | | NULL | | +-------+------------+------+-----+---------+-------+ 1 row in set (0.00 sec) mysql> insert into t values (1.23456); Query OK, 1 row affected (0.00 sec) mysql> select * from t; +------+ | f | +------+ | 1.2 | +------+ 1 row in set (0.00 sec) mysql> insert into t values (1.25456); Query OK, 1 row affected (0.03 sec) mysql> select * from t; +------+ | f | +------+ | 1.2 | | 1.3 | +------+ 2 rows in set (0.00 sec)