2 Day Developer > Managing Database Objects > Using Datatypes > Storing Character Data
Storing Character Data |
Previous |
Next |
This section contains the following topics:
You can use the following SQL datatypes to store character (alphanumeric) data:
The VARCHAR2
datatype stores variable-length character literals.
When creating a VARCHAR2
column in a table, you must specify a string length between 1 and 4000 bytes for the VARCHAR2
column. Set the size to the maximum number of characters to be stored in the column. For example, a column to hold the last name of employees can be restricted to 25 bytes by defining it as VARCHAR2(25)
.
For each row, Oracle Database XE stores each value in the column as a variable-length field unless a value exceeds the column's maximum length, in which case Oracle returns an error. Using VARCHAR2
saves on space used by the table. For most cases where you need to store character data, you would use the VARCHAR2
datatype.
The CHAR
datatype stores fixed-length character literals.
When creating a CHAR
column in a table, you must specify a string length between 1 and 2000 bytes for the CHAR
column. For each row, Oracle Database XE stores each value in the column as a fixed-length field. If the value of the character data is less than specified length of the column, then the value is blank-padded to the fixed length. If a value is too large, Oracle Database XE returns an error.
NCHAR
and NVARCHAR2
datatypes store only Unicode character data.
The NVARCHAR2
datatype stores variable-length Unicode character literals. The NCHAR
datatype stores fixed-length Unicode character literals. See Working in a Global Environment for information about using Unicode data and globalization support.
See Also:
|
When deciding which datatype to use for a column that will store character data in a table, consider the following:
Space usage
To store data more efficiently, use the VARCHAR2
datatype. The CHAR
datatype adds blanks to maintain a fixed column length for all column values, whereas the VARCHAR2
datatype does not add extra blanks.
Comparison semantics
Use the CHAR
datatype when trailing blanks are not important in string comparisons. Use the VARCHAR2
datatype when trailing blanks are important in string comparisons.
Future compatibility
The CHAR
and VARCHAR2
datatypes are fully supported.
See Also: Oracle Database SQL Reference for more information about comparison semantics for these datatypes |