MySQL十进制字段的正确默认值是多少? [英] What is the correct default value for a MySQL decimal field?

查看:237
本文介绍了MySQL十进制字段的正确默认值是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的MySQL数据库中有一个decimal字段.我将其定义为: decimal(1,1) UNSIGNED NULL.但是我想为它设置一个默认值,例如7.0,这就是我遇到的问题.每当我要设置此值时,都会出现此错误:

I have a decimal field in my MySQL database. I have defined it as this: decimal(1,1) UNSIGNED NULL. But I would like to set a default value for it like 7.0, and this is the problem I have. Whenever I want to set this value, I get this error:

Invalid default value ...

我也尝试将其设置为7,07,但是它导致了相同的错误. MySQL十进制字段的正确默认值是多少?

I also tried to set it as 7,0 and 7 but it resulted the same error. What is the correct default value for a MySQL decimal field?

注意:我正在将Navicat用于MySQL

推荐答案

在MySQL中,当声明DECIMAL(P,S)时:

In MySQL, when declaring DECIMAL(P,S) :

精度(P)表示存储的有效位数,小数位数(S)表示可存储的小数点后的位数.

The precision (P) represents the number of significant digits that are stored for values, and the scale (S) represents the number of digits that can be stored following the decimal point.

因此,在您的示例中,DECIMAL(1,1)表示最多1个数字,并且在点后最多1个数字...这实际上没有意义.

So in your example, DECIMAL(1,1) means at most 1 digit, and at most 1 digit after the dot... which doesn't really make sense.

为了更好地理解,这里有更多示例:

To better understand, here are more examples:

  • DECIMAL(5,2):5位数字,其中两个用于小数部分.因此,可能的值范围是-999.99到999.99
  • DECIMAL(5,0):不允许使用小数部分,因此它等效于最多5位数字的整数.
  • DECIMAL(5,2): 5 digits, two of them being used for the fractional part. Hence, possible values range from -999.99 to 999.99
  • DECIMAL(5,0): no fractional part allowed, so it is equivalent to an integer with maximum 5 digits.

对于UNSIGNED,其行为相同,但是使用减号会引发错误.

With UNSIGNED, the behavior is the same, but using a minus sign will throw an error.

这篇关于MySQL十进制字段的正确默认值是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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