资料库:什麽是 unsigned integer

前言

一开始在设计资料库时常常不确定那个 type 要怎麽设,也不知道什麽是 unsigned integer ...

什麽是 unsigned integer

简单来说就是没有「负号」的整数资料类型,以 32-bit 整数为例,有号和无号能存的范围不同:

  • 32-bit signed int: https://chart.googleapis.com/chart?cht=tx&chl=-2%5E%7B31%7D ~ https://chart.googleapis.com/chart?cht=tx&chl=2%5E%7B31%7D-1
  • 32-bit unsigned int: https://chart.googleapis.com/chart?cht=tx&chl=0 ~ https://chart.googleapis.com/chart?cht=tx&chl=2%5E%7B32%7D-1

32-bit unsgined 因为有一个位元是存正负号(正0负1),所以范围才会是正负有 https://chart.googleapis.com/chart?cht=tx&chl=2%5E%7B31%7D 个可能;减一是因为有 0,所以正数范围是 https://chart.googleapis.com/chart?cht=tx&chl=0https://chart.googleapis.com/chart?cht=tx&chl=2%5E%7B32%7D-1 ,确实是 https://chart.googleapis.com/chart?cht=tx&chl=2%5E%7B31%7D 个数。

二进制的 $-2^{31}$ (signed int)

[1][000...0] #第一个为1,後面31个0

二进制的 $2^{31}-1$ (signed int)

[0][111...1] #第一个为0,後面31个1

二进制的 -1 (signed int)

[1][111...1] #32个1

有兴趣可以到这个网站玩玩看binaryconvert

那资料库什麽时候用 unsigned integer

简单来说,就看你那个栏位会不会有负数

例如 Laravel schema builder blueprint 提供的 id() 就是 unsigned 的 big integer

$table->id();

increments() 则是 unsigned integer

$table->increments('id');

更多请看官方文件


<<:  C# 入门之代码结构初解

>>:  建立前端开发准则,让团队能够有效率的开发好维护的程序码(by 均一前端工程师宜陞)

[2021铁人赛 Day08] General Skills 05

引言 昨天学习到的工具是 nc netcat ,可以想成网路版本的 cat , 但可以双向沟通,最...

D17 - 彭彭的课程# Python Module 模组的载入与使用(2)

今天波金钟奖大家有没有收看 现在很多明星我都认不出来了QQ 时代的眼泪阿 好的今天紧接昨天的第二部份...

C#学习笔记4:C#的基本运算

这是我一边学习一边写下的笔记,如果内容有错,恳请在下方留言跟我说,我会非常感谢的!!! 基本运算 一...

[ Day 10 ] - 传值与传址

传值与传址 先来看案例 案例一 let a = 50; let b = a; console.log...

[NestJS 带你飞!] DAY23 - Authentication (上)

相信各位在使用各大网站提供的功能时,都会需要注册帐号来获得更多的使用体验,比如:google、fac...