unsigned DECIMAL(3,2) 在 MySQL 中使用了多少字节

How many bytes does an unsigned DECIMAL(3,2) use in MySQL

我想知道 SQL 中无符号 DECIMAL(3,2) 使用了多少字节。我在网上找到的文档不是很清楚,尤其是当你有一个未签名的 DECIMAL 时。

documentation 看起来很清楚:

Values for DECIMAL columns are stored using a binary format that packs nine decimal digits into 4 bytes. The storage requirements for the integer and fractional parts of each value are determined separately. Each multiple of nine digits requires 4 bytes, and any remaining digits left over require some fraction of 4 bytes. The storage required for remaining digits is given by the following table.

所以,DECIMAL(3, 2)的整数部分有1位,小数部分有2位。根据table,1-2位需要一个字节。因此总数是两个字节。

通常正确或非常接近的简单答案:

DECIMAL(m,n) 的存储要求是 m/2

在您的情况下,3/2 = 1.5 非常接近。在这种情况下,四舍五入得到正确的“2”。