mysql中unsigned類型欄位出現65535錯誤

來源:互聯網
上載者:User

$sql = "UPDATE `dzxbbs_common_member` SET newprompt=newprompt-1 WHERE uid='{$uid}'";

`dzxbbs_common_member` 表中 newprompt的類型是 smallint unsigned  長度為6的類型。

當newprompt值為0時,執行這條sql語句,得到了意想不到的結果newprompt欄位值變成了65535。

出現這個問題的伺服器版本是 mysql 5.1.38-log。

解決這個需要設定這個參數 sql_mode="NO_UNSIGNED_SUBTRACTION" 啟用這個選項後兩個UNSIGNED類型相減返回SIGNED類型。

我在本地測試的結果是報錯
#1690 – BIGINT UNSIGNED value is out of range in '(`test`.`test`.`view` – 1)'

伺服器版本: mysql 5.5.16

開源電商系統ecshop商品庫存也存在突然變成65535的問題

補充:unsigned相關欄位長度

unsigned 既為非負數,用此類型可以增加資料長度! 
例如如果 tinyint最大是127,那 tinyint unsigned 最大 就可以到 127 * 2 
unsigned 屬性只針對整型,而binary屬性只用於char 和varchar。

類型
說明
tinyint
非常小 整數
smallint
較小整數
mediumint
中等大小整數
int
標準整數
bigint
較大整數
float
單精確度浮點數
double
雙精確度浮點數
decimal
一個串 浮點數


每種數值 類型 名稱和取值範圍如表2所示。
類型說明
取值範圍
tinyint[(m)]
有符號值:-128 到127(- 27 到27 - 1)
無符號值:0到255(0 到28 - 1) 1個位元組
smallint[(m)]
有符號值:-32768 到32767(- 215 到215 - 1)
無符號值:0到 65535(0 到21 6 - 1) 2個位元組
mediumint[(m)]
有符號值:-8388608 到8388607(- 22 3 到22 3 - 1 )
無符號值:0到16777215(0 到22 4 - 1) 3個位元組
int[(m)]
有符號值:-2147683648 到2147683647(- 231 到231- 1)
無符號值:0到4294967295(0 到232 - 1) 4個位元組
bigint[(m)]
有符號值:-9223372036854775808 到9223373036854775807(- 263到263-1)
無符號值:0到18446744073709551615(0到264 – 1) 8個位元組
float[(m, d)]
最小非零值:±1.175494351e - 38
double[(m,d)]
最小非零值:±2.2250738585072014e - 308
decimal (m, d)
可變;其值 範圍依賴於m 和d
表2:數值列 類型 取值範圍
各種 類型值所需 儲存量如表3所示。
類型說明
儲存需求
tinyint[(m)]
1位元組
smallint[(m)]
2位元組
mediumint[(m)]
3位元組
int[(m)]
4位元組
bigint[(m)]
8位元組
float[(m, d)]
4位元組
double[(m, d)]
8位元組
decimal (m, d)
m位元組( mysql < 3.23),m+2位元組( mysql > 3.23 )

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.