05.11.2015 Views

Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 12 ■ DATATYPES 499<br />

Table 12-1. Four Basic String Types<br />

String Type<br />

Description<br />

VARCHAR2( ) is a number between 1 <strong>and</strong> 4,000 for up to 4,000<br />

bytes of storage. In the following section, we’ll examine in<br />

detail the differences <strong>and</strong> nuances of the BYTE versus CHAR<br />

modifier in that clause.<br />

CHAR( ) is a number between 1 <strong>and</strong> 2,000 for up to 2,000<br />

bytes of storage.<br />

NVARCHAR2( )<br />

is a number greater than 0 whose upper bound is<br />

dictated by your national character set.<br />

NCHAR( )<br />

is a number greater than 0 whose upper bound is<br />

dictated by your national character set.<br />

Bytes or Characters<br />

The VARCHAR2 <strong>and</strong> CHAR types support two methods of specifying lengths:<br />

• In bytes: VARCHAR2(10 byte). This will support up to 10 bytes of data, which could be as<br />

few as two characters in a multibyte character set.<br />

• In characters: VARCHAR2(10 char). This will support to up 10 characters of data, which<br />

could be as much as 40 bytes of information.<br />

When using a multibyte character set such as UTF8, you would be well advised to use the<br />

CHAR modifier in the VARCHAR2/CHAR definition—that is, use VARCHAR2(80 CHAR), not VARCHAR2(80),<br />

since your intention is likely to define a column that can in fact store 80 characters of data. You<br />

may also use the session or system parameter NLS_LENGTH_SEMANTICS to change the default<br />

behavior from BYTE to CHAR. I do not recommend changing this setting at the system level;<br />

rather, use it as part of an ALTER SESSION setting in your database schema installation scripts.<br />

Any application that requires a database to have a specific set of NLS settings makes for an<br />

“unfriendly” application. Such applications, generally, cannot be installed into a database<br />

with other applications that do not desire these settings, but rely on the defaults to be in place.<br />

One other important thing to remember is that the upper bound of the number of bytes<br />

stored in a VARCHAR2 is 4,000. However, even if you specify VARCHAR2(4000 CHAR), you may not<br />

be able to fit 4,000 characters into that field. In fact, you may be able to fit as few as 1,000<br />

characters in that field if all of the characters take 4 bytes to be represented in your chosen<br />

character set!<br />

The following small example demonstrates the differences between BYTE <strong>and</strong> CHAR, <strong>and</strong><br />

how the upper bounds come into play. We’ll create a table with three columns, the first two of<br />

which will be 1 byte <strong>and</strong> 1 character, respectively, with the last column being 4,000 characters.<br />

Notice that we’re performing this test on a multibyte character set database using the character<br />

set AL32UTF8, which supports the latest version of the Unicode st<strong>and</strong>ard <strong>and</strong> encodes<br />

characters in a variable-length fashion using from 1 to 4 bytes for each character:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!