user725913
user725913

Reputation:

Oracle Number data type and its strictness

I'm very new to oracle but I've been learning about it in class and I've recently come across the number data type. I've read the documentation regarding scale and precision (s,p) but I still am not positive regarding the type's proper usage.

Let's say I want to store percent values as a decimal. The decimal can be anywhere from 0 to 1 and may have up to 3 numbers following the decimal place.

Some possible decimals to be stored may include:

.66
.553
1.00

If I were to make the column NUMBER(4,3) would that work EVEN IF there were only two numbers? See below

.22
.10
.35
etc...

In short, does the number type require that the EXACT sizes are met? Eg. Would NUMBER(4,3) ABSOLUTELY REQUIRE that the data inserted is 4 numbers, 3 of them falling after the decimal place?

Thanks for the help!

Upvotes: 1

Views: 16405

Answers (1)

Justin Cave
Justin Cave

Reputation: 231651

Data types for columns limit the range of values that can be stored. But they don't do anything to force a particular length or precision.

Just like you can store a single character or a NULL in a VARCHAR2(100) column, you can store numbers with less than 3 digits of precision and less than 4 digits of scale. For example, the values 0 and 6.1 are both perfectly valid for a NUMBER(4,3) column. If you insert a value that has too many digits of precision, the value will be silently rounded to the precision specified in the column definition.

SQL> create table foo (
  2    col1 number(4,3)
  3  );

Table created.

SQL> insert into foo values( 9.999 );

1 row created.

SQL> insert into foo values (0);

1 row created.

SQL> insert into foo values( 6.1 );

1 row created.

SQL> insert into foo values( 1.2345 );

1 row created.

SQL> select * from foo;

      COL1
----------
     9.999
         0
       6.1
     1.235

Upvotes: 2

Related Questions