简体   繁体   English

Oracle中数字的默认精度和比例是多少?

[英]What is the default Precision and Scale for a Number in Oracle?

When creating a column of type NUMBER in Oracle, you have the option of not specifying a precision or scale. 在Oracle中创建NUMBER类型的列时,您可以选择不指定精度或比例。 What do these default do if you don't specify them? 如果您不指定它们,这些默认值会怎样?

NUMBER (precision, scale) NUMBER(精度,比例)

If a precision is not specified, the column stores values as given. 如果未指定精度,则列将值存储为给定值。 If no scale is specified, the scale is zero. 如果未指定比例,则比例为零。

A lot more info at: 更多信息:

http://download.oracle.com/docs/cd/B28359_01/server.111/b28318/datatype.htm#CNCPT1832 http://download.oracle.com/docs/cd/B28359_01/server.111/b28318/datatype.htm#CNCPT1832

The NUMBER type can be specified in different styles : 可以使用不同的样式指定NUMBER类型:

Resulting  Resulting  Precision
Specification   Precision  Scale      Check      Comment
―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
NUMBER          NULL       NULL       NO         'maximum range and precision',
                                                 values are stored 'as given'
NUMBER(P, S)    P          S          YES        Error code: ORA-01438
NUMBER(P)       P          0          YES        Error code: ORA-01438
NUMBER(*, S)    38         S          NO

Where the precision is the total number of digits and scale is the number of digits right or left (negative scale) of the decimal point. 精度是总位数,刻度是小数点右或左(负刻度)的位数。

Oracle specifies ORA-01438 as Oracle将ORA-01438指定为

value larger than specified precision allowed for this column 值大于此列允许的指定精度

As noted in the table, this integrity check is only active if the precision is explicitly specified. 如表中所示,此完整性检查仅在显式指定精度时才有效。 Otherwise Oracle silently rounds the inserted or updated value using some unspecified method. 否则,Oracle使用一些未指定的方法静默舍入插入或更新的值。

I believe the default precision is 38, default scale is zero. 我相信默认精度为38,默认比例为零。 However the actual size of an instance of this column, is dynamic. 但是,此列的实例的实际大小是动态的。 It will take as much space as needed to store the value, or max 21 bytes. 它将需要尽可能多的空间来存储值,或最多21个字节。

Oracle stores numbers in the following way: 1 byte for power, 1 byte for the first significand digit (that is one before the separator), the rest for the other digits. Oracle以下列方式存储数字: 1 byte用于电源, 1 byte用于第一个有效数字(即分隔符之前的一个),其余数字用于其他数字。

By digits here Oracle means centesimal digits (ie base 100 ) digits Oracle表示centesimal digits (即base 100

SQL> INSERT INTO t_numtest VALUES (LPAD('9', 125, '9'))
  2  /

1 row inserted

SQL> INSERT INTO t_numtest VALUES (LPAD('7', 125, '7'))
  2  /

1 row inserted

SQL> INSERT INTO t_numtest VALUES (LPAD('9', 126, '9'))
  2  /

INSERT INTO t_numtest VALUES (LPAD('9', 126, '9'))

ORA-01426: numeric overflow

SQL> SELECT DUMP(num) FROM t_numtest;

DUMP(NUM)
--------------------------------------------------------------------------------
Typ=2 Len=2: 255,11
Typ=2 Len=21: 255,8,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,79

As we can see, the maximal number here is 7.(7) * 10^124 , and he have 19 centesimal digits for precision, or 38 decimal digits. 我们可以看到,这里的最大数是7.(7) * 10^124 ,并且他有19 38十进制数字用于精度,或38十进制数字。

实际上,你总是可以自己测试它。

CREATE TABLE CUSTOMERS ( CUSTOMER_ID NUMBER NOT NULL, JOIN_DATE DATE NOT NULL, CUSTOMER_STATUS VARCHAR2(8) NOT NULL, CUSTOMER_NAME VARCHAR2(20) NOT NULL, CREDITRATING VARCHAR2(10) ) ;

select column_name, data_type, nullable, data_length, data_precision, data_scale from user_tab_columns where table_name ='CUSTOMERS';

I expand on spectra's answer so people don't have to try it for themselves. 我扩展了光谱的答案,所以人们不必为自己尝试。

This was done on Oracle Database 11g Express Edition Release 11.2.0.2.0 - Production. 这是在Oracle Database 11g Express Edition 11.2.0.2.0版 - 生产版上完成的。

CREATE TABLE CUSTOMERS
(
  CUSTOMER_ID NUMBER NOT NULL,
  FOO FLOAT NOT NULL,
  JOIN_DATE DATE NOT NULL,
  CUSTOMER_STATUS VARCHAR2(8) NOT NULL,
  CUSTOMER_NAME VARCHAR2(20) NOT NULL,
  CREDITRATING VARCHAR2(10)
);

select column_name, data_type, nullable, data_length, data_precision, data_scale
from user_tab_columns where table_name ='CUSTOMERS'; 

Which yields 哪个收益率

COLUMN_NAME      DATA_TYPE  NULLABLE DATA_LENGTH DATA_PRECISION DATA_SCALE
CUSTOMER_ID      NUMBER     N        22        
FOO              FLOAT      N        22          126    
JOIN_DATE        DATE       N        7        
CUSTOMER_STATUS  VARCHAR2   N        8        
CUSTOMER_NAME    VARCHAR2   N        20        
CREDITRATING     VARCHAR2   Y        10    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM