Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005
CHAPTER 12 ■ ■ ■ Datatypes Choosing the right datatype seems so easy and straightforward, but many times I see it done incorrectly. The most basic decision—what type you use to store your data in—will have repercussions on your applications and data for years to come. Choosing the appropriate datatype is paramount and hard to change after the fact—that is, once you implement it you might be stuck with it for quite a while. In this chapter, we’ll take a look at all of the Oracle basic datatypes available and discuss how they are implemented and when each might be appropriate to use. We won’t examine user-defined datatypes, as they’re simply compound objects derived from the built-in Oracle datatypes. We’ll investigate what happens when you use the wrong datatype for the job, or even just the wrong parameters to the datatype (length, precision, scale, and so on). By the end of this chapter, you’ll have an understanding of the types available to you, how they’re implemented, when to use each type and, as important, why using the right type for the job is key. An Overview of Oracle Datatypes Oracle provides 22 different SQL data types for us to use. Briefly, they are as follows: • CHAR: A fixed-length character string that will be blank padded with spaces to its maximum length. A non-null CHAR(10) will always contain 10 bytes of information using the default National Language Support (NLS) settings. We will cover NLS implications in more detail shortly. A CHAR field may store up to 2,000 bytes of information. • NCHAR: A fixed-length character string that contains UNICODE formatted data. Unicode is a character-encoding standard developed by the Unicode Consortium with the aim of providing a universal way of encoding characters of any language, regardless of the computer system or platform being used. The NCHAR type allows a database to contain data in two different character sets: the CHAR type and NCHAR type use the database’s character set and the national character set, respectively. A non-null NCHAR(10) will always contain 10 characters of information (note that it differs from the CHAR type in this respect). An NCHAR field may store up to 2,000 bytes of information. 489
490 CHAPTER 12 ■ DATATYPES • VARCHAR2: Also currently synonymous with VARCHAR. This is a variable-length character string that differs from the CHAR type in that it is not blank padded to its maximum length. A VARCHAR2(10) may contain between 0 and 10 bytes of information using the default NLS settings. A VARCHAR2 may store up to 4,000 bytes of information. • NVARCHAR2: A variable-length character string that contains UNICODE formatted data. An NVARCHAR2(10) may contain between 0 and 10 characters of information. An NVARCHAR2 may store up to 4,000 bytes of information. • RAW: A variable-length binary datatype, meaning that no character set conversion will take place on data stored in this datatype. It is considered a string of binary bytes of information that will simply be stored by the database. It may store up to 2,000 bytes of information. • NUMBER: This datatype is capable of storing numbers with up to 38 digits of precision. These numbers may vary between 1.0✕10(–130) and up to but not including 1.0✕10(126). Each number is stored in a variable-length field that varies between 0 bytes (for NULL) and 22 bytes. Oracle NUMBER types are very precise—much more so than normal FLOAT and DOUBLE types found in many programming languages. • BINARY_FLOAT: This is a new type available only in Oracle 10g Release 1 and later. This is a 32-bit single-precision floating-point number. It can support at least 6 digits of precision and will consume 5 bytes of storage on disk. • BINARY_DOUBLE: This is a new type available only in Oracle 10g Release 1 and later. This is a 64-bit double-precision floating-point number. It can support at least 15 digits of precision and will consume 9 bytes of storage on disk. • LONG: This type is capable of storing up to 2GB of character data (2 gigabytes, not characters, as each character may take multiple bytes in a multibyte character set). As LONG types have many restrictions that we’ll discuss later and are provided for backward compatibility, it is strongly recommended you do not use them in new applications and, when possible, convert them from LONG to CLOB types in existing applications. • LONG RAW: The LONG RAW type is capable of storing up to 2GB of binary information. For the same reasons as noted for LONGs, it is recommended you use the BLOB type in all future development and, when possible, in existing applications as well. • DATE: This is a fixed-width 7-byte date/time datatype. It will always contain the seven attributes of the century, the year within the century, the month, the day of the month, the hour, the minute, and the second. • TIMESTAMP: This is a fixed-width 7- or 11-byte date/time datatype. It differs from the DATE datatype in that it may contain fractional seconds; up to 9 digits to the right of the decimal point may be preserved for TIMESTAMPs with fractional seconds. • TIMESTAMP WITH TIME ZONE: This is a fixed-width 13-byte TIMESTAMP as the preceding entry, but it also provides for TIME ZONE support. Additional information regarding the time zone is stored with the TIMESTAMP in the data, so the TIME ZONE originally inserted is preserved with the data.
- Page 484 and 485: CHAPTER 11 ■ INDEXES 439 an 8KB b
- Page 486 and 487: CHAPTER 11 ■ INDEXES 441 select *
- Page 488 and 489: CHAPTER 11 ■ INDEXES 443 select *
- Page 490 and 491: CHAPTER 11 ■ INDEXES 445 Indicate
- Page 492 and 493: CHAPTER 11 ■ INDEXES 447 an index
- Page 494 and 495: CHAPTER 11 ■ INDEXES 449 Table 11
- Page 496 and 497: CHAPTER 11 ■ INDEXES 451 9 1, 'M'
- Page 498 and 499: CHAPTER 11 ■ INDEXES 453 column w
- Page 500 and 501: CHAPTER 11 ■ INDEXES 455 Bitmap j
- 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 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 552 and 553: CHAPTER 12 ■ DATATYPES 507 NUMBER
- 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
CHAPTER 12<br />
■ ■ ■<br />
Datatypes<br />
Choosing the right datatype seems so easy <strong>and</strong> straightforward, but many times I see it<br />
done incorrectly. The most basic decision—what type you use to store your data in—will have<br />
repercussions on your applications <strong>and</strong> data for years to come. Choosing the appropriate<br />
datatype is paramount <strong>and</strong> hard to change after the fact—that is, once you implement it you<br />
might be stuck with it for quite a while.<br />
In this chapter, we’ll take a look at all of the <strong>Oracle</strong> basic datatypes available <strong>and</strong> discuss<br />
how they are implemented <strong>and</strong> when each might be appropriate to use. We won’t examine<br />
user-defined datatypes, as they’re simply compound objects derived from the built-in <strong>Oracle</strong><br />
datatypes. We’ll investigate what happens when you use the wrong datatype for the job, or<br />
even just the wrong parameters to the datatype (length, precision, scale, <strong>and</strong> so on). By the<br />
end of this chapter, you’ll have an underst<strong>and</strong>ing of the types available to you, how they’re<br />
implemented, when to use each type <strong>and</strong>, as important, why using the right type for the job<br />
is key.<br />
An Overview of <strong>Oracle</strong> Datatypes<br />
<strong>Oracle</strong> provides 22 different SQL data types for us to use. Briefly, they are as follows:<br />
• CHAR: A fixed-length character string that will be blank padded with spaces to its maximum<br />
length. A non-null CHAR(10) will always contain 10 bytes of information using the<br />
default National Language Support (NLS) settings. We will cover NLS implications in<br />
more detail shortly. A CHAR field may store up to 2,000 bytes of information.<br />
• NCHAR: A fixed-length character string that contains UNICODE formatted data. Unicode is<br />
a character-encoding st<strong>and</strong>ard developed by the Unicode Consortium with the aim of<br />
providing a universal way of encoding characters of any language, regardless of the<br />
computer system or platform being used. The NCHAR type allows a database to contain<br />
data in two different character sets: the CHAR type <strong>and</strong> NCHAR type use the database’s<br />
character set <strong>and</strong> the national character set, respectively. A non-null NCHAR(10) will<br />
always contain 10 characters of information (note that it differs from the CHAR type in<br />
this respect). An NCHAR field may store up to 2,000 bytes of information.<br />
489