You are on page 1of 20

Character Data Types

David Berry
http://buildingbettersoftware.blogspot.com/
Module Introduction

Fixed length vs.


Character data
variable width
types overview
character types

Character sets,
Oracle RAW data
VARCHAR2 and
type
NVARCHAR2 types
Character Data Types in Oracle

Space padded Variable length

Default
CHAR VARCHAR2
character set

Unicode
NCHAR NVARCHAR2
character set
Column Specifications

CREATE TABLE world_cities


(
country_code CHAR(2),
country_name VARCHAR2(40 byte),
captital_city VARCHAR2(40 char),
national_country_name NCHAR(2),
national_country_name NVARCHAR2(40),
national_capital_name NVARCHAR2(40)
);
Character Column Maximums

CHAR,
2000 bytes
NCHAR

4000 bytes
VARCHAR2,
NVARCHAR2 32,767 bytes
(Oracle 12c - MAX_STRING_SIZE=EXTENDED)
Attempt to insert/update
Oracle will throw an error
column with value longer
(data is not auto-truncated)
than column size
CHAR vs. VARCHAR2

Field Type: VARCHAR2(16)

I D A H O

5 bytes of data

Field Type: CHAR2(16)

I D A H O

16 bytes of data
Problems With Space Padded Data

Additional Trim data Require


storage before use padding of
bind variables
Character Data Type Recommendation

Use VARCHAR2/NVARCHAR2 in tables you create

Understand CHAR behavior when you encounter these


CHAR/VARCHAR2 vs. NCHAR/NVARCHAR2
What are the differences?
How do I choose when to use each one?
National Language Settings and Strings

CHAR, VARCHAR2 NCHAR, NVARCHAR2

Data is stored in the default Data is stored in national


character set of the database character set of the database
(Unicode)
NLS Character Sets

SELECT *
FROM nls_database_parameters
WHERE parameter IN
('NLS_CHARACTERSET', 'NLS_NCHAR_CHARACTERSET');

PARAMETER VALUE
------------------------- --------------------
NLS_CHARACTERSET WE8MSWIN1252
NLS_NCHAR_CHARACTERSET AL16UTF16
Character Set Properties

Character Set Properties


WE8MSWIN1252 Western European characters
One byte per character

Most ASCII characters are one byte


AL32UTF8
Good when ASCII characters most common

AL16UTF16 Most characters are two bytes


ASCII characters are two bytes
Character Set Implications

Multi-byte characters cannot be stored in single byte


Data integrity character set columns

Data storage Character set impacts length of data on disk

Column data Character set impacts what length of strings can be


length stored
Character Set Implications on Data Type Choice

• Data stored in default character set


CHAR
• Best choice if you only need characters in this
VARCHAR2 character set

• Data is stored in national character set


NCHAR (Unicode)
NVARCHAR2 • Use when you need characters outside of
default character set
Character Sets and Data Type Selection

CHAR/VARCHAR2 limited to characters in default


What characters character set
can be stored
NCHAR/NVARCHAR2 can store virtually any character

Number of bytes per character varies by


character set
Storage
considerations Affects length of string you can store

Affects amount of space taken up by row


VARCHAR vs. VARCHAR2

VARCHAR data type currently synonymous with VARCHAR2

VARCHAR behavior in Oracle may change at some point in


the future

Oracle recommends always using VARCHAR2


RAW Data Type

Purpose Store raw binary data (an array of bytes)

2000 bytes
Maximum Size 32,767 bytes
(Oracle 12c - MAX_STRING_SIZE=EXTENDED)
RAW Data Type

CREATE TABLE data_transfer_files


(
file_id NUMBER(6),
file_receive_time DATE,
file_size NUMBER(10),
file_checksum RAW(64)
);
Basic of character data types

Differences between fixed and


variable length character data
Module types
Summary Differences between VARCHAR2
and NVARCHAR2 data types

Oracle RAW data type

You might also like