Chinaunix首页 | 论坛 | 博客
  • 博客访问: 738564
  • 博文数量: 38
  • 博客积分: 587
  • 博客等级: 中士
  • 技术积分: 579
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-17 14:32
文章存档

2013年(15)

2012年(23)

分类: Mysql/postgreSQL

2012-04-18 11:09:48

在写这blog之前,我是没有理解完全:(.
一 知识拾遗:
1,int(11)和int(8)其存储所占都是4个字节,二者的存储是相同的,只有当对列加了zerofille定义后,int(n)这个n指定的长度才有意思。
2,zerofill顾名思义,“用0来填充,补齐位置”,比如一系统的订单号位置至少为8位数,不足则左边自动加0补齐,则可以定义为 order_id int(8) zerofill.
3, 如果对列添加了zerofill, 则这列自动加了unsigned, 即zerofill 相当于 unsigned zerofill;官方手册原话:
If you specify ZEROFILL for a numeric column, MySQL automatically adds the UNSIGNED attribute to the column
4, zerofill可以五种整型数据类型(tinyint到bigint), 不仅仅是int, 当然还有与int同名的integer.
二 知识实践:
 
1,创建测试表

  1. mysql> show create table t_zerofill;
  2. +------------+-----------------------------------------------------------------------------------------------------------------------------------------------+
  3. | Table | Create Table |
  4. +------------+-----------------------------------------------------------------------------------------------------------------------------------------------+
  5. | t_zerofill | CREATE TABLE `t_zerofill` (
  6. `order_id` int(5) unsigned zerofill NOT NULL,
  7. PRIMARY KEY (`order_id`)
  8. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
  9. +------------+-----------------------------------------------------------------------------------------------------------------------------------------------+

  10. 2 ,插入测试数据
  11. --测试数据中包括负数-234,插入时有Warnings
  12. mysql> insert into t_zerofill values(123),(12345),(123456),(-234);
  13. Query OK, 4 rows affected, 1 warning (0.00 sec)
  14. Records: 4 Duplicates: 0 Warnings: 1
  15. --查看Warnings可知负数-234越界了,从1中表结构有unsigned的就很明显此列不能有负数,因为sql_mode关系,
  16. -- 这里只出现Warnings。
  17. mysql> show warnings;
  18. +---------+------+---------------------------------------------------+
  19. | Level | Code | Message |
  20. +---------+------+---------------------------------------------------+
  21. | Warning | 1264 | Out of range value for column 'order_id' at row 4 |
  22. +---------+------+---------------------------------------------------+
  23. 1 row in set (0.00 sec)

  24. 3 查看测试结果
  25. -- 从测试结果可见,第二行插入数据123,不够5位,则左边补2个0;第4行6位数,超出5位的不管。
  26. mysql> select * from t_zerofill;
  27. +----------+
  28. | order_id |
  29. +----------+
  30. | 00000 |
  31. | 00123 |
  32. | 12345 |
  33. | 123456 |
  34. +----------+
  35. --查看下列执行计划的key_len为4,说明123也是占有4字节;
  36. mysql> explain select * from t_zerofill where order_id<12345;
  37. +----+-------------+------------+-------+---------------+---------+---------+------+------+--------------------------+
  38. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  39. +----+-------------+------------+-------+---------------+---------+---------+------+------+--------------------------+
  40. | 1 | SIMPLE | t_zerofill | index | PRIMARY | PRIMARY | 4 | NULL | 4 | Using where; Using index |
  41. +----+-------------+------------+-------+---------------+---------+---------+------+------+--------------------------+
还有如果不加zerofill, 所有tinyint(1)或者int(8)都没有意义, 和整型数据类型不加长度完全一样。

阅读(3146) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~