用于保存百分比值的适当数据类型? [英] Appropriate datatype for holding percent values?

查看:34
本文介绍了用于保存百分比值的适当数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

保持百分比值范围从 0.00% 到 100.00% 的最佳数据类型是什么?

What is the best datatype for holding percent values ranging from 0.00% to 100.00%?

推荐答案

假设百分比有两位小数,您使用的数据类型取决于您计划如何存储百分比.如果您要存储它们的小数等价物(例如 100.00% 存储为 1.0000),我会将数据存储在 decimal(5,4) 数据类型中,并带有 CHECK确保值永远不会超过 1.0000(假设这是上限)并且永远不会低于 0(假设这是下限)的约束.如果您要存储它们的面值(例如 100.00% 存储为 100.00),那么您应该使用带有适当 CHECK 约束的 decimal(5,2).结合一个好的列名,它可以让其他开发者清楚数据是什么以及数据是如何存储在列中的.

Assuming two decimal places on your percentages, the data type you use depends on how you plan to store your percentages. If you are going to store their fractional equivalent (e.g. 100.00% stored as 1.0000), I would store the data in a decimal(5,4) data type with a CHECK constraint that ensures that the values never exceed 1.0000 (assuming that is the cap) and never go below 0 (assuming that is the floor). If you are going to store their face value (e.g. 100.00% is stored as 100.00), then you should use decimal(5,2) with an appropriate CHECK constraint. Combined with a good column name, it makes it clear to other developers what the data is and how the data is stored in the column.

这篇关于用于保存百分比值的适当数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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