Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between VARCHAR2(10 CHAR) and NVARCHAR2(10)

I've installed Oracle Database 10g Express Edition (Universal) with the default settings:

SELECT * FROM NLS_DATABASE_PARAMETERS; 
NLS_CHARACTERSET               AL32UTF8                                  NLS_NCHAR_CHARACTERSET         AL16UTF16                                 

Given that both CHAR and NCHAR data types seem to accept multi-byte strings, what is the exact difference between these two column definitions?

VARCHAR2(10 CHAR) NVARCHAR2(10) 
like image 494
Álvaro González Avatar asked Dec 22 '10 10:12

Álvaro González


People also ask

What is the difference between VARCHAR2 and NVARCHAR2?

VARCHAR2 vs. NVARCHAR2. First, the maximum size of VARCHAR2 can be in either bytes or characters, whereas the maximum size of NVARCHAR2 is only in characters. In addition, the maximum byte length of an NVARCHAR2 depends on the configured national character set.

What is the difference between VARCHAR2 10 and VARCHAR2 10 CHAR?

VARCHAR2(10 byte) will support up to 10 bytes of data, which could be as few as two characters in a multi-byte character sets. VARCHAR2(10 char) could support as much as 40 bytes of information and will support to up 10 characters of data.

What is the difference between VARCHAR2 byte and VARCHAR2 CHAR?

Varchar2(10) uses the current value of NLS_LENGTH_SEMANTICS to determine the limit for the string. If this is byte, then it's 10 bytes. If it's char, then it's 10 characters. In multibyte character sets these can be different!

What is the max length of NVARCHAR2 in Oracle?

The maximum length of an NVARCHAR2 column in bytes is 4000. Both the byte limit and the character limit must be met, so the maximum number of characters that is actually allowed in an NVARCHAR2 column is the number of characters that can be written in 4000 bytes.


1 Answers

The NVARCHAR2 datatype was introduced by Oracle for databases that want to use Unicode for some columns while keeping another character set for the rest of the database (which uses VARCHAR2). The NVARCHAR2 is a Unicode-only datatype.

One reason you may want to use NVARCHAR2 might be that your DB uses a non-Unicode character set and you still want to be able to store Unicode data for some columns without changing the primary character set. Another reason might be that you want to use two Unicode character set (AL32UTF8 for data that comes mostly from western Europe, AL16UTF16 for data that comes mostly from Asia for example) because different character sets won't store the same data equally efficiently.

Both columns in your example (Unicode VARCHAR2(10 CHAR) and NVARCHAR2(10)) would be able to store the same data, however the byte storage will be different. Some strings may be stored more efficiently in one or the other.

Note also that some features won't work with NVARCHAR2, see this SO question:

  • Oracle Text will not work with NVARCHAR2. What else might be unavailable?
like image 114
Vincent Malgrat Avatar answered Sep 21 '22 20:09

Vincent Malgrat