Skip to Main Content
  • Questions
  • Witch you prefer for defining type Integers Number,number(p) or number(p,0)?

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Nabil.

Asked: January 13, 2014 - 9:32 pm UTC

Last updated: January 14, 2014 - 10:06 pm UTC

Version: 11.2.0

Viewed 1000+ times

You Asked

Hi
I want to transfer my work from ms access database to oracle.
Witch you prefer for defining type Integers Number,number(p) or number(p,0)?.
Witch is better for performance when there are calculations on those feilds.

thank you

and Tom said...

the performance is going to be the same - assuming the same data.

The precision and scale are simply "edits" - constraints on your data. They do not affect how the data is stored on disk, only what values may be placed in there.

So - that said, you would use the precision and scale as a data integrity constraint. If you have "money" that you are storing, you might use:

ops$tkyte%ORA11GR2> set numformat 999999999999.99

ops$tkyte%ORA11GR2> create table t ( x number(*,2) );
Table created.


ops$tkyte%ORA11GR2> insert into t values ( 123432432.99 );
1 row created.

ops$tkyte%ORA11GR2> insert into t values ( 123432432.9999 );
1 row created.

ops$tkyte%ORA11GR2> select * from t;

               X
----------------
    123432432.99
    123432433.00


please do take note of the rounding that takes place! If that is not desirable - you should:

ops$tkyte%ORA11GR2> create table t ( x number constraint x_must_be_money check(x=round(x,2)) );

Table created.

ops$tkyte%ORA11GR2> 
ops$tkyte%ORA11GR2> insert into t values ( 123432432.99 );

1 row created.

ops$tkyte%ORA11GR2> insert into t values ( 123432432.9999 );
insert into t values ( 123432432.9999 )
*
ERROR at line 1:
ORA-02290: check constraint (OPS$TKYTE.X_MUST_BE_MONEY) violated


ops$tkyte%ORA11GR2> select * from t;

               X
----------------
    123432432.99



using number(x,2) will just ensure that all data inserted in there has at most 2 decimals places - it an edit.

And the edit will not affect performance at all - since 1111.22 stored in a number, number(*,2), number(6,2) would all be stored precisely the same:

ops$tkyte%ORA11GR2> create table t ( x number, y number(*,2), z number(6,2) );

Table created.

ops$tkyte%ORA11GR2> insert into t values ( 1111.22, 1111.22, 1111.22 );

1 row created.

ops$tkyte%ORA11GR2> select x,dump(x,16), y,dump(y,16), z,dump(z,16) from t;

               X
----------------
DUMP(X,16)
-------------------------------------------------------------------------------
               Y
----------------
DUMP(Y,16)
-------------------------------------------------------------------------------
               Z
----------------
DUMP(Z,16)
-------------------------------------------------------------------------------
         1111.22
Typ=2 Len=4: c2,c,c,17
         1111.22
Typ=2 Len=4: c2,c,c,17
         1111.22
Typ=2 Len=4: c2,c,c,17




Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Oracle 11GR2 Documentation

A reader, January 15, 2014 - 12:27 pm UTC

Is there any reason that the use of '*' to specify an arbitrary numeric precision is no longer shown in the 11G documentation?

It is shown in the concepts manual for 10g http://docs.oracle.com/cd/B19306_01/server.102/b14220/datatype.htm#i16209

Thanks!