Detailed comparison with the data type varchar

Oracle in varchar2 field set up to 4000 , if there are more database design field is set 4000 .

In oracle Three of the more common types: VARCHAR2 (byte) , VARCHAR2 (char) , NVARCHAR2 () .

First of all, we must always remember clearly: either varchar2 or NVARCHAR2 , the maximum number of bytes is 4000 .

VARCHAR2 (byte) : is the default representation, for example, we write: VARCHAR2 (100) , equivalent to VARCHAR2 (100 byte) , represents the maximum number of bytes is 100 , this field can accommodate up to 100 bytes, emphasizing space . Because it is byte we describe, therefore, save kanji characters, be wary. If you are using a database GBK encoding, then a character will occupy 2 bytes, can store up to 50 characters, if your database using a UTF8 encoding, then a character will occupy 3 bytes, can store up to 33 is characters.

VARCHAR2 (char) : represents the maximum number of characters is 100 , this field can accommodate up to 100 characters, the number of stressed. Suppose we write VARCHAR2 (100 char) , then either the numbers, letters, characters, both as a character, write up to 100 , it being understood, the more characters, the greater the space occupied, following the same principles on top of database coding. For example: deposit a Chinese character, the underlying accounting for 2 or 3 bytes, into a letter, representing 1 byte, definitely not some article said 1 letters or numbers also account for 2 or 3 bytes!

NVARCHAR2 () : no byte , char points, similar VARCHAR2 (char) , but NVARCHAR2 () shielding coding database, no matter what kind of coding, NVARCHAR2 () in the two bytes are a character.

General tutorials, also to this, but if more step thinking, you will find a fatal problem.

Practical applications, such an approach is likely to occur: VARCHAR2 (1400 char) , our subjective view, this field is no longer than 1400 characters, which means that we may deposit 1399 characters, looks like a very correct way .

However, if this 1399 characters are Chinese characters, characters in length and not exceed 1400 , everything looks normal, but in fact we lost a part of the data, and why?

Since 1399 Chinese characters, according to the UTF8 encoding it (99% of projects are UTF8 encoding it .. ) , take up 1399 * 3 = 4197 bytes, while the article begins to say, no matter what the char , the maximum length is 4000 byte, a not more, so the extra 197 bytes, will be erased, and the entire process without any error, your data so evaporate!

So, for GBK in terms of database coding, security wording is: VARCHAR2 (2000 char) , NVARCHAR2 (2000) , for UTF8 in terms of database coding, security wording is: VARCHAR2 (1333 char) , NVARCHAR2 (2000) .

Guess you like

Origin www.cnblogs.com/wanlige/p/12425282.html