转自https://www.cnblogs.com/wangzhongqiu/p/6424827.html

用法:

mysql> CREATE TABLE t ( a INT UNSIGNED, b INT UNSIGNED )

探索一:正负数问题

拿tinyint字段来举例,unsigned后,字段的取值范围是0-255,而signed的范围是-128 - 127。 那么如果我们在明确不需要负值存在的情况下,通常是不要设置signed来支持负数的。 因为只支持正数会让存储空间大一倍呢(当然我这种表达可能不准确)。 假设我们使用tinyint来存储一些状态值。 0表示删除,1表示待付款,2表示已付款,3...。 突然来个需求要加订单取消,一些有代码洁癖的人就想,那就将定义为:-1表示取消吧。 但是就因为有了-1,我们说起来应该可以从0存到255的,结果就变为了0-127。 所以一般情况下,我们不建议这样设置

字段设置为unsigned后有一个问题是:

当select a - b from t时,a为10,b为12,那么这时就会出现异常情况:ERROR 1690 (22003): BIGINT UNSIGNED value is out of range in '(`test`.`t`.`a` - `test`.`t`.`b`)'

所以注意这种情况即可

探索二:性能问题

严格讲,在性能上是有细微的差别的。 unsigned的性能更好,当只存储正整数的情况下。 因为,当unsigned时,假设查询值在500以下的数据,那么MySQL会将范围定义为:0-500,而如果是signed,则查询范围为:-2147483648 - 500。 参考文章:http://rakesh.sankar-b.com/2010/08/25/mysql-unsigned-int-to-signed-int-performance-tips-index/

里面讲到:

Let’s say you want to know the list of customers who have purchased an item of quantity 500 or less. Following is the query you might be used to get these results:

SELECT *

FROM customer

WHERE quantity <= 500

Cool, the above query will yield you the list of customers who have purchased an item of quantity 500 or less. Right, what is the big deal, it should return fast, but consider when you have a table with millions of records then this query might be slow in returning you the results.

Yes, that is true, you can always add an “ index ” to the “quantity” field and improve the performance – exactly, this should improve the performance of processing the query much better than without an “index”.

Without “unsigned”:

Process flow, since the quantity field is an “ int ” and you have an  index  of this field, MySQL  will define the range as  -2147483648 to 500  and it will get the result based on this range.

With “unsigned”:

Process flow, since the quantity field is an “ int ” with “ unsigned ” and you have an index of this field,  MySQL  will define the range as  0 to 500  and it will get the result based on this range.

Now compare the difference yourself and tell me, for sure it will improve the performance of the your query. Since we know we never store any negative (signed values) in the quantity field and the default behavior of “ int ” is “ signed “, it’s always better to write a full-syntax while creating a table.

总的说来,设置unsigned最大的差异是字段取值范围的变化。 所以基于这点来对字段的unsigned或者signed是比较明智的决定

以上

参考文献:

http://verysimple.com/2006/10/22/mysql-data-type-optimization-tips/

http://rakesh.sankar-b.com/2010/08/25/mysql-unsigned-int-to-signed-int-performance-tips-index/

http://dev.mysql.com/doc/refman/5.7/en/numeric-type-overview.html

http://www.cnblogs.com/blankqdb/archive/2012/11/03/blank_qdb.html

int signed in mysql_【转】mysql 中int类型字段unsigned和signed的区别相关推荐

  1. mysql int 11 最大多少_types - mysql中int(11)列的大小(以字节为单位)是多少?...

    types - mysql中int(11)列的大小(以字节为单位)是多少? 以字节为单位的mysql中int(11)列的大小是多少? 并且可以存储在此列中的最大值? Gaurav asked 2019 ...

  2. mysql中signed是什么类型_mysql 中int类型字段unsigned和signed的探索

    转自:http://www.0791quanquan.com/news_keji/topic_816453/ 探索一:正负数问题 拿tinyint字段来举例,unsigned后,字段的取值范围是0-2 ...

  3. unsigned int mysql_mysql 中int类型字段unsigned和signed的探索

    转自:http://www.0791quanquan.com/news_keji/topic_816453/ 探索一:正负数问题 拿tinyint字段来举例,unsigned后,字段的取值范围是0-2 ...

  4. MySQL中varchar类型字段隐式转换造成多删除数据

    例如一个表中字段是varchar类型: desc test; +-------+-------------+------+-----+---------+----------------+ | Fie ...

  5. 解决Mysql中longtext类型字段数据丢失问题

    一.问题背景 一个数据库表的某个字段设置成了longtext类型,但是向数据库表中更新数据时,却发现部分数据丢失.相应字段的数据大小超过了4M,longtext类型可存储4G左右数据,按常理说不会出现 ...

  6. python中int是什么的缩写_python中int是什么类型

    python中的基本数据类型 1:虽然python中的变量不需要声明,但使用时必须赋值 整形变量 浮点型变量 字符型 2:可以一个给多个变量赋值,也可以多个给多个变量赋值 3:python3中有6个标 ...

  7. python mysql写入速度加快_解决python写入mysql中datetime类型遇到的问题

    解决python写入mysql中datetime类型遇到的问题 发布时间:2020-08-31 16:46:47 来源:脚本之家 阅读:89 作者:WilliamDescant 刚开始使用python ...

  8. python mysql驱动写入datetime类型的数据_解决python写入mysql中datetime类型遇到的问题...

    刚开始使用python,还不太熟练,遇到一个datetime数据类型的问题: 在mysql数据库中,有一个datetime类型的字段用于存储记录的日期时间值.python程序中有对应的一个dateti ...

  9. mysql中int最大多少,MySQL中int最大值深入讲解

    MySQL中int最大值深入讲解 导语 前两天看到的问题,展开写一下. 字节 我们都知道计算机是以二进制为基础.存储的基本单位是 Bit,也称为比特.二进制位.1bit 可以表示 0 或者 1 两个数 ...

最新文章

  1. 使用 QT 时遇到的问题及解决办法
  2. Bug错误openssl_encrypt()
  3. [CSAcademy]Cycle Tree
  4. Linux I2C设备驱动编写(二)
  5. stm32 非debug模式程序无法运行
  6. 版本控制-代码和文档等用SVN管理
  7. EVE-NG模拟器综合
  8. 好用的FTP下载工具 flashfxp工具
  9. 用SmartUpload实现文件上传,下载,删除
  10. 程序员最爱的 10 个在线社区,你去过几个?
  11. 使用Python爬虫爬取淘宝商品并分析
  12. Gitee代码提交 自用
  13. WIN10管理员权限设置、更改用户名被“拒绝访问”
  14. win10安装TeamView 提示rollback framework could not be initialized
  15. Kaggle泰坦尼克号-决策树Top 3%-0基础代码详解
  16. 项目十大管理之成本管理
  17. SAP AS ABAP 7.52 SP04, Developer Edition 免费下载
  18. 二分查找(整数二分)
  19. STM32中断分配——抢占优先级与响应优先级
  20. 讯飞语音转文字_录音实时转文字就是如此简单 讯飞智能录音笔SR701评测

热门文章

  1. Rancher创始人谈Docker,创新愈发困难,未来将何去何从?
  2. key redis 遍历_快乐运维Redis大数据量查询与清理
  3. android应用控制百度地图,Android中应用百度地图API开发地图APP实例-显示百度地图...
  4. (企业案例)Nacos Config 进阶使用
  5. Apache JMeter 测试 HTTP接口
  6. flowable 图片缓存
  7. RPC 中 参数传递 ImputStream 流会关闭
  8. 小程序开发(9)-之地图组件map、导航
  9. 拼的html页面乱,页面分页html拼接
  10. 字典 学生成绩等级_python-列表及字典进阶