問題描述
有誰知道我可以通過使用 4 個字節而不是使用 8 個字節和 bigint 來保存無符號整數(0 到 4294967295)的任何解決方法嗎?
Does anyone know of any work around by which i can save unsigned integers (0 to 4294967295) simply using 4 bytes instead of using 8 bytes and bigint?
我知道我們可以創建用戶定義的數據類型并對它們創建一個約束以不允許負值,但這仍然不允許我輸入超過 2147483647 的值.我只想使用 4 個字節,但能夠保存更大的整數值大于 2147483647 但小于 4294967295.
I know we can create user defined datatypes and create a constraint on them to not allow negative values but that still does not allow me to enter values over 2147483647. I only want to use 4 bytes but be able to save integer values greater than 2147483647 but less than 4294967295.
可能的重復:SQL Server 中的 4 字節無符號整數?
推薦答案
沒有可用的無符號類型,因此您可以使用 UDT 創建一個,或者選擇更大的數據類型.如果您在 UDT 中執行此操作,您將再次超過 4 個字節.
There is no unsigned type available to you, so you could create one using the UDT, or opt for the larger data type. If you do it in a UDT you are going to exceed the 4 bytes again.
最極端的做法是在您讀取存儲值后通過添加 -2^31 自動將偏移量應用于它,但這是一種真正的 hacky 方法,并且會使查看代碼等的任何人感到困惑,而不是提及錯誤/遺漏的可能性.我根本不推薦 hack.
The extreme hack would be to apply an offset automatically to your stored value after you read it, by adding -2^31 but this is a real hacky way to go about it and confusing for anyone viewing the code etc, not to mention the potential for mistakes / things being missed. I wouldn't recommend the hack at all.
這篇關于sql server 4 字節無符號整數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!