Differences between SQL Server char, nchar, varchar and nvarchar

Source: Internet
Author: User

I visited the wooden study and reviewed char, nchar, varchar, and nvarchar, So I recorded it and hoped to share it with others, so that I could easily query it later. Of course, this is not plagiarism.

1. CHAR: it occupies 1 byte for English (ASCII) characters and 2 bytes for a Chinese character. It is convenient to store fixed-length data for char, And the indexing efficiency on the char field is extremely high, for example, if char (10) is defined, whether or not the data you store reaches 10 bytes will occupy 10 bytes of space. Because it is a fixed length, the speed is high. For example, if char (10) is defined, no matter whether the data you store reaches 10 bytes, it takes up 10 bytes of space. Because it is a fixed length, the speed is high.

2. varchar stores variable-length data. If the possible value of a field is not fixed, we only know that it cannot exceed 10 characters. defining it as varchar (10) is the most cost-effective. The actual length of the varchar type is the actual length of its value plus 1. Why "+ 1? This byte is used to save the actual length.

Varchar type: the type of varchar is not filled with spaces, but Char is filled up until it is full, such as varchar (100), but its value is only "Qian ", therefore, the value stored in the database is "Qian", and char is different, for example, char (100). Its value is "Qian ", in fact, it is "Qian" in the database (there are 96 spaces after Qian, that is, it is filled with 100 bytes ).
Note: Since char is of a fixed length, it is much faster than varchar! However, it is a little troublesome for the program to process it. We need to use functions such as trim to remove spaces on both sides!

3. What is the difference between the nchar and nvarchar types? For conversion from other types of characters, such as Chinese characters and phonetic symbols, each English (ASCII) character occupies two bytes and one Chinese Character occupies two bytes, all characters occupy 2 bytes.

Varchar (n): variable-length character data type. The maximum storage length is 8,000 characters.

Nvarchar (n): a variable-length Unicode data with a maximum length of 4,000 characters. The size of bytes is twice the number of input characters, that is, it stores data in two bytes. If data is stored in a single word segment, it also occupies storage in double bytes. Space .

Varchar is generally applicable to English and numbers. nvarchar is applicable to Chinese and other characters, where n represents Unicode constants and can solve the conversion problem between multilingual character sets.

 

Related Article

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.