最小的mysql类型,适应单个小数 [英] smallest mysql type that accommodates single decimal

查看:157
本文介绍了最小的mysql类型,适应单个小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是数据库新手。我设置一个mysql表。其中一个字段将接受以0.5为增量的值。例如0.5,1.0,1.5,2.0,.... 200.5等。

Database newbie here. I'm setting up a mysql table. One of the fields will accept a value in increment of a 0.5. e.g. 0.5, 1.0, 1.5, 2.0, .... 200.5, etc.

它不捕获小数。

`value` int(10), 

可以容纳这个值的最小类型,考虑它只有一个小数。

What would be the smallest type that can accommodate this value, considering it's only a single decimal.

我也考虑过,因为十进制将总是0.5,如果有的话,我可以将它存储在一个单独的布尔字段?所以我会有2个字段。这是一个愚蠢的还是有点过于复杂的想法?我不知道它是否真的节省了我任何内存,它可能会变慢,现在我访问2个字段,而不是1

I also was considering that because the decimal will always be 0.5 if at all, I could store it in a separate boolean field? So I would have 2 fields instead. Is this a stupid or somewhat over complicated idea? I don't know if it really saves me any memory, and it might get slower now that I'm accessing 2 fields instead of 1

`value` int(10), 
`half` bool,  //or something similar to boolean


b $ b

你的建议是什么?是第一个选项更好,这种情况下最小的数据类型是什么,会得到我的0.5?

What are your suggestions guys? Is the first option better, and what's the smallest data type in that case that would get me the 0.5?

推荐答案

想查看 DECIMAL(P, S) 类型。

You'll want to look at the DECIMAL(P,S) type.

为此,P是精度,S是比例。您可以将P视为总数(小数点前后)的数字,S表示小数点后面的数字。

For that, P is the precision and S is the scale. You can think of P as how many digits there are in total (both before and after the decimal point), and S as how many of the digits are after the decimal point.

例如,要存储从-999.99到999.99,你需要5位数的精度和2的scale,因此你会使用 DECIMAL(5,2)

So for instance, to store from -999.99 to 999.99, you'd need 5 digits of precision and a scale of 2, therefore you'd use DECIMAL(5, 2).

在你的情况下,你需要一个 DECIMAL(n,1),其中n是在小数点前需要多少位数,小数点后为+ 1。

In your case, you'd need a DECIMAL(n, 1), where n is how many digits you need before the decimal point + 1 for the decimal.

这篇关于最小的mysql类型,适应单个小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆