小数超出DOUBLE范围

问题描述

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

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,Primary Key (geo_data_id),Foreign Key (place_id) References place (place_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

当我尝试使用将数据插入geo_data表中时

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位数字均为小数)。

您希望将精度降低到较小的水平,以便为非小数位留出空间,例如:DOUBLE(18,6),它为您提供12个非小数位。