The difference between char, varchar, nvarchar in the database

1. char
     Fixed length, up to n characters.
 
2. varchar
     A mutable string of maximum length n.
(n is an integer, different databases, the maximum length n is different)
 
The difference between char and varchar:
     varchar saves space, but is slightly less efficient than char.
     It is said that varchar saves space than char because varchar is a variable string. For example, using varchar(5) to store the string "abc" only takes up 3 bytes of storage space, while using char(5) to store it takes up 5 bytes ("abc").
     It is said that varchar is slightly less efficient than char, because when varchar data is modified, data migration (ie: redundant I/O) may be caused due to different data lengths. Among them, Oracle's expression for this redundant I/O description is: "Row Migration".
3. nvarchar
     The characteristics of nvarchar need to be compared with varchar.
     The difference between nvarchar and varchar is mainly in the way of storing data:
     1). varchar: store data in bytes
          varchar(6), can store up to 6 bytes of data, such as: "hahaha", "abcdef"...
Note: How many bytes a Chinese character occupies in the database depends on the encoding method of unicode, for example: utf8 occupies 3 bytes in mysql, and Chinese_PRC_CI_AS in sqlserver occupies 2 bytes...
     2). nvarchar: store data by character
          nvarchar(6), can store up to 6 characters/Chinese data, such as: "hahahahaha", "abcdef"...
          The maximum actual byte length of nvarchar(m) storage = n*m (n depends on the encoding method). If nvarchar stores English characters, the byte length of n is also stored according to the encoding method. That is to say, if you use nvarchar to store English characters, it will waste more than half of the storage space
 
Summarize:
      1. The performance gap between char and varchar is very small and can be ignored.
     2. In large data volume applications, the use of char and nvarchar may lead to a lot of waste of storage space.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326448814&siteId=291194637