Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005
CHAPTER 12 ■ DATATYPES 507 NUMBER Type Syntax and Usage The syntax for the NUMBER type is straightforward: NUMBER( p,s ) where P and S are optional and are used to specify the following: • Precision, or the total number of digits. By default, the precision is 38 and has valid values in the range of 1 to 38. The character * may be used to represent 38 as well. • Scale, or the number of digits to the right of the decimal point. Valid values for the scale are –84 to 127, and its default value depends on whether or not the precision is specified. If no precision is specified, then scale defaults to the maximum range. If a precision is specified, then scale defaults to 0 (no digits to the right of the decimal point). So, for example, a column defined as NUMBER stores floating-point numbers (with decimal places), whereas a NUMBER(38) stores only integer data (no decimals), since the scale defaults to 0 in the second case. You should consider the precision and scale to be edits for your data—data integrity tools in a way. The precision and scale do not affect at all how the data is stored on disk, only what values are permitted and how numbers are to be rounded. For example, if a value exceeds the precision permitted, Oracle returns an error: ops$tkyte@ORA10GR1> create table t ( num_col number(5,0) ); Table created. ops$tkyte@ORA10GR1> insert into t (num_col) values ( 12345 ); 1 row created. ops$tkyte@ORA10GR1> insert into t (num_col) values ( 123456 ); insert into t (num_col) values ( 123456 ) * ERROR at line 1: ORA-01438: value larger than specified precision allows for this column So, you can use the precision to enforce some data integrity constraints. In this case, NUM_COL is a column that is not allowed to have more than five digits. The scale, on the other hand, is used to control “rounding” of the number, for example: ops$tkyte@ORA10GR1> create table t ( msg varchar2(10), num_col number(5,2) ); Table created. ops$tkyte@ORA10GR1> insert into t (msg,num_col) values ( '123.45', 123.45 ); 1 row created. ops$tkyte@ORA10GR1> insert into t (msg,num_col) values ( '123.456', 123.456 ); 1 row created. ops$tkyte@ORA10GR1> select * from t;
508 CHAPTER 12 ■ DATATYPES MSG NUM_COL ---------- ---------- 123.45 123.45 123.456 123.46 Notice how the number 123.456, with more than five digits, succeeded this time. That is because the scale we used in this example was used to round 123.456 to two digits, resulting in 123.46, and then 123.46 was validated against the precision, found to fit, and inserted. However, if we attempt the following insert, it fails: ops$tkyte@ORA10GR1> insert into t (msg,num_col) values ( '1234', 1234 ); insert into t (msg,num_col) values ( '1234', 1234 ) * ERROR at line 1: ORA-01438: value larger than specified precision allows for this column because the number 1234.00 has more than five digits in total. When you specify the scale of 2, at most three digits may be to the left of the decimal place and two to the right. Hence, that number does not fit. The NUMBER(5,2) column can hold all values between 999.99 and –999.99. It may seem strange to allow the scale to vary from –84 to 127. What purpose could a negative scale fulfill? It allows you to round values to the left of the decimal place. Just as the NUMBER(5,2) rounded values to the nearest .01, so a NUMBER(5,-2) would round to the nearest 100, for example: ops$tkyte@ORA10GR1> create table t ( msg varchar2(10), num_col number(5,-2) ); Table created. ops$tkyte@ORA10GR1> insert into t (msg,num_col) values ( '123.45', 123.45 ); 1 row created. ops$tkyte@ORA10GR1> insert into t (msg,num_col) values ( '123.456', 123.456 ); 1 row created. ops$tkyte@ORA10GR1> select * from t; MSG NUM_COL ---------- ---------- 123.45 100 123.456 100 The numbers were rounded up to the nearest 100. We still have five digits of precision, but there are now seven digits (including the trailing two 0s) permitted to the left of the decimal point: ops$tkyte@ORA10GR1> insert into t (msg,num_col) values ( '1234567', 1234567 ); 1 row created. ops$tkyte@ORA10GR1> select * from t;
- Page 502 and 503: CHAPTER 11 ■ INDEXES 457 INSERT a
- Page 504 and 505: CHAPTER 11 ■ INDEXES 459 7 l_last
- Page 506 and 507: CHAPTER 11 ■ INDEXES 461 ops$tkyt
- Page 508 and 509: CHAPTER 11 ■ INDEXES 463 If we co
- Page 510 and 511: CHAPTER 11 ■ INDEXES 465 ops$tkyt
- Page 512 and 513: CHAPTER 11 ■ INDEXES 467 Caveat o
- Page 514 and 515: CHAPTER 11 ■ INDEXES 469 ops$tkyt
- Page 516 and 517: CHAPTER 11 ■ INDEXES 471 Frequent
- Page 518 and 519: CHAPTER 11 ■ INDEXES 473 select *
- Page 520 and 521: CHAPTER 11 ■ INDEXES 475 If you s
- Page 522 and 523: CHAPTER 11 ■ INDEXES 477 we’ll
- Page 524 and 525: CHAPTER 11 ■ INDEXES 479 Predicat
- Page 526 and 527: CHAPTER 11 ■ INDEXES 481 ops$tkyt
- Page 528 and 529: CHAPTER 11 ■ INDEXES 483 ops$tkyt
- Page 530 and 531: CHAPTER 11 ■ INDEXES 485 This dem
- Page 532 and 533: CHAPTER 11 ■ INDEXES 487 SELECT /
- Page 534 and 535: CHAPTER 12 ■ ■ ■ Datatypes Ch
- Page 536 and 537: CHAPTER 12 ■ DATATYPES 491 • TI
- Page 538 and 539: CHAPTER 12 ■ DATATYPES 493 (in th
- Page 540 and 541: CHAPTER 12 ■ DATATYPES 495 That d
- Page 542 and 543: CHAPTER 12 ■ DATATYPES 497 ops$tk
- Page 544 and 545: CHAPTER 12 ■ DATATYPES 499 Table
- Page 546 and 547: CHAPTER 12 ■ DATATYPES 501 The IN
- Page 548 and 549: CHAPTER 12 ■ DATATYPES 503 ops$tk
- Page 550 and 551: CHAPTER 12 ■ DATATYPES 505 • BI
- Page 554 and 555: CHAPTER 12 ■ DATATYPES 509 MSG NU
- Page 556 and 557: CHAPTER 12 ■ DATATYPES 511 They a
- Page 558 and 559: CHAPTER 12 ■ DATATYPES 513 ■Not
- Page 560 and 561: CHAPTER 12 ■ DATATYPES 515 Coping
- Page 562 and 563: CHAPTER 12 ■ DATATYPES 517 Note t
- Page 564 and 565: CHAPTER 12 ■ DATATYPES 519 We are
- Page 566 and 567: CHAPTER 12 ■ DATATYPES 521 Format
- Page 568 and 569: CHAPTER 12 ■ DATATYPES 523 ops$tk
- Page 570 and 571: CHAPTER 12 ■ DATATYPES 525 You ca
- Page 572 and 573: CHAPTER 12 ■ DATATYPES 527 month
- Page 574 and 575: CHAPTER 12 ■ DATATYPES 529 DT2-DT
- Page 576 and 577: CHAPTER 12 ■ DATATYPES 531 DT TS
- Page 578 and 579: CHAPTER 12 ■ DATATYPES 533 ops$tk
- Page 580 and 581: CHAPTER 12 ■ DATATYPES 535 Since
- Page 582 and 583: CHAPTER 12 ■ DATATYPES 537 ops$tk
- Page 584 and 585: CHAPTER 12 ■ DATATYPES 539 ops$tk
- Page 586 and 587: CHAPTER 12 ■ DATATYPES 541 suppor
- Page 588 and 589: CHAPTER 12 ■ DATATYPES 543 Concep
- Page 590 and 591: CHAPTER 12 ■ DATATYPES 545 We can
- Page 592 and 593: CHAPTER 12 ■ DATATYPES 547 buffer
- Page 594 and 595: CHAPTER 12 ■ DATATYPES 549 Note t
- Page 596 and 597: CHAPTER 12 ■ DATATYPES 551 13 dbm
- Page 598 and 599: CHAPTER 12 ■ DATATYPES 553 equall
- Page 600 and 601: CHAPTER 12 ■ DATATYPES 555 ROWID/
508<br />
CHAPTER 12 ■ DATATYPES<br />
MSG<br />
NUM_COL<br />
---------- ----------<br />
123.45 123.45<br />
123.456 123.46<br />
Notice how the number 123.456, with more than five digits, succeeded this time. That is<br />
because the scale we used in this example was used to round 123.456 to two digits, resulting in<br />
123.46, <strong>and</strong> then 123.46 was validated against the precision, found to fit, <strong>and</strong> inserted. However,<br />
if we attempt the following insert, it fails:<br />
ops$tkyte@ORA10GR1> insert into t (msg,num_col) values ( '1234', 1234 );<br />
insert into t (msg,num_col) values ( '1234', 1234 )<br />
*<br />
ERROR at line 1:<br />
ORA-01438: value larger than specified precision allows for this column<br />
because the number 1234.00 has more than five digits in total. When you specify the scale of 2,<br />
at most three digits may be to the left of the decimal place <strong>and</strong> two to the right. Hence, that<br />
number does not fit. The NUMBER(5,2) column can hold all values between 999.99 <strong>and</strong> –999.99.<br />
It may seem strange to allow the scale to vary from –84 to 127. What purpose could a negative<br />
scale fulfill? It allows you to round values to the left of the decimal place. Just as the<br />
NUMBER(5,2) rounded values to the nearest .01, so a NUMBER(5,-2) would round to the nearest<br />
100, for example:<br />
ops$tkyte@ORA10GR1> create table t ( msg varchar2(10), num_col number(5,-2) );<br />
Table created.<br />
ops$tkyte@ORA10GR1> insert into t (msg,num_col) values ( '123.45', 123.45 );<br />
1 row created.<br />
ops$tkyte@ORA10GR1> insert into t (msg,num_col) values ( '123.456', 123.456 );<br />
1 row created.<br />
ops$tkyte@ORA10GR1> select * from t;<br />
MSG<br />
NUM_COL<br />
---------- ----------<br />
123.45 100<br />
123.456 100<br />
The numbers were rounded up to the nearest 100. We still have five digits of precision, but<br />
there are now seven digits (including the trailing two 0s) permitted to the left of the decimal<br />
point:<br />
ops$tkyte@ORA10GR1> insert into t (msg,num_col) values ( '1234567', 1234567 );<br />
1 row created.<br />
ops$tkyte@ORA10GR1> select * from t;