You are on page 1of 1

8 Chapter 1: Creating

1.5.1 A String Is a String: BINARY, CHARACTER, LONG


All character and binary columns are stored as varying length character strings
regardless of how they are declared. The maximum length specifies a limit on
the byte size of the data portion of the string, with a default of 1 byte. The
LONG VARCHAR and LONG BINARY types have an implied maximum
length of 2GB.
<string_type> ::= <char_type> [ "(" <maximum_length> ")" ]
| LONG BINARY
| LONG VARCHAR
<char_type> ::= BINARY
| CHAR [ VARYING ]
| CHARACTER [ VARYING ]
| VARBINARY
| VARCHAR
<maximum_length> ::= integer literal in the range 1 to 32767

Tip: All these data types, including LONG VARCHAR and LONG BINARY, may
be used for local and global variables in stored procedures and other SQL
scripts, as well as for columns in tables.

Storage requirements depend on the current length of each column value rather
than the maximum length. Long strings are split and require more overhead than
short strings, whereas short strings are stored efficiently even if they are
declared as LONG VARCHAR. Heres how it works: String values up to 254
bytes in length are always stored together with the other columns in the row.
When the length grows to 255 bytes or larger the value is partitioned into two
pieces; the first piece is 254 bytes long and remains where it was, while the
remainder is called a blob continuation and is placed on one or more separate
pages called extension pages. These extension pages are kept separate so that a
query or sequential scan that doesnt need to look at the long values wont have
to retrieve all these pages. This arrangement is described in more detail in Sec-
tion 10.6.2, Table Fragmentation.
From a SQL programming point of view, a string is a string in SQL Any-
where 9 and you dont have to worry about the declared data type. For example,
if you think all company names will fit into 30 characters but you are concerned
about exceptions, there is no performance penalty for using CHARACTER
( 100 ) or even 1000. Similarly, a description column that will usually require
only a few hundred characters can be declared as LONG VARCHAR to handle
those special cases; your database wont grow in size until you actually store
very long values.
Exactly the same data may be stored in either CHARACTER or BINARY
columns. In particular, the zero byte (hexadecimal 00) may be stored in a
CHARACTER column and it is treated as data, not a string terminator.

Tip: In some programming environments the zero byte string terminator is


called null. This is not the same as the database NULL value implemented by
SQL Anywhere 9; database NULLs require special handling when they are used
in applications.

You might also like