超出 DOUBLE 范围的小数字

Small number out of DOUBLE range

我使用以下定义在 mariaDB 上创建了一个 table。注意经度和纬度字段。

Create Table geo_data (
    geo_data_id int NOT NULL AUTO_INCREMENT,
    place_id int NOT NULL,
    longitude DOUBLE(18,18) SIGNED,
    latitude DOUBLE(18,18) SIGNED,
    Primary Key (geo_data_id),
    Foreign Key (place_id) References place (place_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

当我尝试使用

将数据插入 geo_data table 时
Insert into geo_data (place_id, longitude, latitude) Values (1, 1.2, 3.4);

我收到以下错误消息:

Error: ER_WARN_DATA_OUT_OF_RANGE: Out of range value for column 'longitude' at row 1

我想我在这里遗漏了一些东西,因为我不相信 1.2 会以任何方式超出 Double(18,18) 的范围。那么这里到底发生了什么?

您的列定义为 DOUBLE(18,18)。第一个数是scale(整数的总位数,包括小数);第二个是 precision(小数位数)。

为小数位和精度赋予相同的值意味着您的值不能大于1(所有 18 位都是小数)。

您想将精度降低到更小的值,以便为 non-decimal 位数字留出空间,例如:DOUBLE(18, 6),这为您提供了 12 non-decimal 个位置。