Varchar is stored by byte, while nvarchar with "N" is stored by character.

Source: Internet
Author: User

Char varchar varchar2 is the most widely used string data type. How should we use it during database design?
First, we will analyze the description of the three data types:
1. Char
The length of a char is fixed and the maximum length is 2000 characters.
2. Varchar and varchar2
It is certain that varchar data types can be used in Oracle currently, however, I found a lot of information about their differences and did not find an accurate answer. The most common thing is that the varchar in Oracle is just the alias of varchar2, that is, synonym.
Some people also think that varchar is a variable string with a maximum length of 2000 (consistent with varchar in SQL Server), while varchar2 has a maximum length of 4000.
Once we know their characteristics, let's discuss their differences.
1. Char, varchar, varchar2
Since varchar and varchar2 are similar in usage, let's first discuss the differences between char and their usage:
Varchar and varchar2 save space than char and are slightly less efficient than char, that is, to achieve efficiency, a certain amount of space must be sacrificed, this is what we often say in database design, "Change space for efficiency '.
Although varchar and varchar2 save space than char, if a varchar and varchar2 columns are frequently modified and the length of the modified data is different each time, this will cause row migration (row migration) this causes redundant I/O, which should be avoided during database design and adjustment. In this case, it would be better to replace varchar and varchar2 with Char.
Of course, another case is that char can be used for almost unchanged fields such as ID card to achieve higher efficiency.
2. Varchar and varchar2
Here we will focus on the difference. We have basically understood the difference in SQL Server. We may all know that, at most, it is a review, but Oracle has added a varchar2 type, this is something you have never used before.
Because Oracle ensures that the data type is up and down compatible in any version, but varchar is not guaranteed, and varchar is generally available in other data types.
So I came to the following conclusion:
If you want to be compatible with the new version of Oracle, do not use varchar. If you want to be compatible with other databases except Oracle, do not use varchar2.
========================================================== ==========

The difference between nvarchar and varchar is that the storage method is different.
Varchar is stored by byte, while nvarchar with "N" is stored by character.
For example, varchar (40) can store 40 characters in length. When storing Chinese characters, because one Chinese character is equal to two bytes. therefore, varchar (40) can only store 20 Chinese characters.
Nvarchar (40) can store 40 Chinese characters, that is, 80 characters in length. nvarchar is relative to the stored character type. for example, some characters occupy three bytes.
The same is true for char and nchar.
Char is a fixed length, that is, when the number of characters you enter is smaller than the number you specify, char (8). If the number of characters you enter is smaller than 8, it will fill in null values later. When the number of characters you enter is greater than the specified number, it intercepts the characters that exceed the limit.

Varchar is a variable-length type. Its storage space changes with the actual number of characters you enter, but the maximum length cannot exceed the length you specify.

Nvarchar is a variable-length UNICODE character. It differs significantly from Char in that it differs from varchar in that it has a large storage space and can contain more characters in the same length.
The character set is selected when SQL Server is installed and cannot be changed. The Unicode data type allows a column to store any character defined by the Unicode standard, including all characters defined by different character sets. Unicode data types must be twice the storage space of Non-Unicode data types.

Unicode data is stored using the nchar, nvarchar, and ntext Data Types in SQL Server. This data type can be used for columns that store characters from multiple character sets. The nvarchar type is used when the number of Unicode characters in each column is different (up to 4000. When each item in a column has a fixed length (up to 4000 Unicode characters), The nchar type is used. When any item in the column exceeds 4000 Unicode characters, the ntext type is used.
Nvarchar (n ):
Unicode data with a variable length of n characters. The value of N must be between 1 and 4,000. The storage size of bytes is twice the number of input characters. The length of the input data can be zero.

Varchar [(n)]:
Variable-length and non-UNICODE character data with a length of n Bytes. N must be a value between 1 and 8,000. The storage size is the actual length of the input data bytes, rather than n Bytes. The length of the input data can be zero.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.