programing tip

SQL Server에서 char, nchar, varchar 및 nvarchar의 차이점은 무엇입니까?

itbloger 2020. 10. 3. 10:04
반응형

SQL Server에서 char, nchar, varchar 및 nvarchar의 차이점은 무엇입니까?


무엇을 의미 nvarchar합니까?

차이점은 무엇이며 char, nchar, varchar, 및 nvarcharSQL Server의는?


정리하기 위해 ... 또는 요약 ...

  • ncharnvarchar저장할 수있는 유니 코드 문자를.
  • char유니 코드를 저장할 수없는 문자를.varchar
  • charnchar되는 길이 고정 됩니다 저장 공간을 확보 당신이 모든 공간을 사용하지 않는 경우에도 사용자가 지정한 문자의 번호를.
  • varchar하고 nvarchar있는 가변 길이 에만 저장하는 문자 공간을 사용합니다. 같은 스토리지를 보유하지 않습니다 charnchar .

nchar그리고 nvarchar당신이 필요로하는 경우에만이를 사용하는 것이 현명 할 수 있도록 저장 공간으로 두 번 차지합니다 유니 코드 지원.


지금까지의 모든 대답은 그것이 varchar단일 바이트이고 nvarchar이중 바이트 임을 나타냅니다 . 이 작업의 첫 번째 부분은 실제로 아래 그림과 같이 데이터 정렬에 의존 합니다.

DECLARE @T TABLE
(
C1 VARCHAR(20) COLLATE Chinese_Traditional_Stroke_Order_100_CS_AS_KS_WS,
C2 NVARCHAR(20)COLLATE  Chinese_Traditional_Stroke_Order_100_CS_AS_KS_WS
)

INSERT INTO @T 
    VALUES (N'中华人民共和国',N'中华人民共和国'),
           (N'abc',N'abc');

SELECT C1,
       C2,
       LEN(C1)        AS [LEN(C1)],
       DATALENGTH(C1) AS [DATALENGTH(C1)],
       LEN(C2)        AS [LEN(C2)],
       DATALENGTH(C2) AS [DATALENGTH(C2)]
FROM   @T  

보고

여기에 이미지 설명 입력

점을 유의 문자가 아직 표현되지 않은 VARCHAR버전을 자동으로 대체되었다 ?.

실제로 해당 데이터 정렬에서 단일 바이트로 표현할 수있는 중국어 문자는 없습니다. 유일한 단일 바이트 문자는 일반적인 서양 ASCII 집합입니다.

이 때문에 그것은 행을 삽입 할 수있다 nvarchar(X)(A)에 열 varchar(X)truncation 에러 실패하는 (X는 두 경우에 동일 번호를 나타낸다).

SQL Server 2012는 .NET Framework를 지원하는 SC (보조 문자) 데이터 정렬을 추가합니다 UTF-16. 이러한 데이터 정렬에서 단일 nvarchar문자는 2 바이트 또는 4 바이트를 차지할 수 있습니다.


nchar 및 char는 nvarchar 및 varchar와 마찬가지로 서로 정확히 동일한 방식으로 작동합니다. 유일한 차이점은 nchar / nvarchar는 유니 코드 문자 (확장 문자 집합 사용이 필요한 경우 필수)를 저장하지만 varchar는 저장하지 않는다는 것입니다.

유니 코드 문자에는 더 많은 저장소가 필요하기 때문에 nchar / nvarchar 필드는 두 배의 공간을 차지합니다 (예를 들어 이전 버전의 SQL Server에서 nvarchar 필드의 최대 크기는 4000 임).

이 질문의 중복 이 하나 .


더 많은 것을 추가하려면 : nchar- 데이터에 후행 공백을 추가합니다. nvarchar- 데이터에 후행 공백을 추가하지 않습니다.

따라서 'nchar'필드로 데이터 세트를 필터링하려는 경우 RTRIM을 사용하여 공백을 제거 할 수 있습니다. 예를 들어, BRAND라는 nchar (10) 필드는 NIKE라는 단어를 저장합니다. 단어 오른쪽에 6 개의 공백을 추가합니다. 따라서 필터링 할 때 표현식은 다음과 같아야합니다. RTRIM (Fields! BRAND.Value) = "NIKE"

내가 지금 조금 어려움을 겪고 있었기 때문에 이것이 누군가를 도울 수 있기를 바랍니다!


기존 답변을 요약하고 수정하려는 나의 시도 :

First, char and nchar will always use a fixed amount of storage space, even when the string to be stored is smaller than the available space, whereas varchar and nvarchar will use only as much storage space as is needed to store that string (plus two bytes of overhead, presumably to store the string length). So remember, "var" means "variable", as in variable space.

The second major point to understand is that, nchar and nvarchar store strings using exactly two bytes per character, whereas char and varchar use an encoding determined by the collation code page, which will usually be exactly one byte per character (though there are exceptions, see below). By using two bytes per character, a very wide range of characters can be stored, so the basic thing to remember here is that nchar and nvarchar tend to be a much better choice when you want internationalization support, which you probably do.

Now for some some finer points.

First, nchar and nvarchar columns always store data using UCS-2. This means that exactly two bytes per character will be used, and any Unicode character in the Basic Multilingual Plane (BMP) can be stored by an nchar or nvarchar field. However, it is not the case that any Unicode character can be stored. For example, according to Wikipedia, the code points for Egyptian hieroglyphs fall outside of the BMP. There are, therefore, Unicode strings that can be represented in UTF-8 and other true Unicode encodings that cannot be stored in a SQL Server nchar or nvarchar field, and strings written in Egyptian hieroglyphs would be among them. Fortunately your users probably don't write in that script, but it's something to keep in mind!

Another confusing but interesting point that other posters have highlighted is that char and varchar fields may use two bytes per character for certain characters if the collation code page requires it. (Martin Smith gives an excellent example in which he shows how Chinese_Traditional_Stroke_Order_100_CS_AS_KS_WS exhibits this behavior. Check it out.)

UPDATE: As of SQL Server 2012, there are finally code pages for UTF-16, for example Latin1_General_100_CI_AS_SC, which can truly cover the entire Unicode range.


  • char: fixed-length character data with a maximum length of 8000 characters.
  • nchar: fixed-length unicode data with a maximum length of 4000 characters.
  • Char = 8 bit length
  • NChar = 16 bit length

nchar[(n)] (national character)

  • Fixed-length Unicode string data.
  • n defines the string length and must be a value from 1 through 4,000.
  • The storage size is two times n bytes.

nvarchar [(n | max)] (national character varying.)

  • Variable-length Unicode string data.
  • n defines the string length and can be a value from 1 through 4,000.
  • max indicates that the maximum storage size is 2^31-1 bytes (2 GB).
  • The storage size, in bytes, is two times the actual length of data entered + 2 bytes

char [(n)] (character)

  • Fixed-length, non-Unicode string data.
  • n defines the string length and must be a value from 1 through 8,000.
  • The storage size is n bytes.

varchar [(n | max)] (character varying)

  • Variable-length, non-Unicode string data.
  • n defines the string length and can be a value from 1 through 8,000.
  • max indicates that the maximum storage size is 2^31-1 bytes (2 GB).
  • The storage size is the actual length of the data entered + 2 bytes.

The differences are:

  1. n[var]char stores unicode while [var]char just stores single-byte characters.
  2. [n]char requires a fixed number of characters of the exact length while [n]varchar accepts a variable number of characters up to and including the defined length.

Another difference is length. Both nchar and nvarchar can be up to 4,000 characters long. And char and varchar can be up to 8000 characters long. But for SQL Server you can also use a [n]varchar(max) which can handle up to 2,147,483,648 characters. (Two gigabytes, a signed 4-byte integer.)


nchar requires more space than nvarchar.

eg,

A char(100) will always store 100 characters even if you only enter 5, the remaining 95 chars will be padded with spaces. Storing 5 characters in a varchar(100) will save 5 characters.


nchar (10)은 길이가 10 인 고정 길이 유니 코드 문자열입니다. nvarchar (10)은 최대 길이가 10 인 가변 길이 유니 코드 문자열입니다. 일반적으로 모든 데이터 값이 10 자이고 후자가 있으면 전자를 사용합니다. 길이가 다른 경우.


  • nchar는 고정 길이이며 유니 코드 문자를 포함 할 수 있습니다. 문자 당 2 바이트 저장소를 사용합니다.

  • varchar는 가변 길이이며 유니 코드 문자를 포함 할 수 없습니다. 문자 당 1 바이트 저장소를 사용합니다.


NVARCHAR 은 유니 코드 문자를 저장할 수 있으며 문자 당 2 바이트를 사용합니다.

참고 URL : https://stackoverflow.com/questions/176514/what-is-the-difference-between-char-nchar-varchar-and-nvarchar-in-sql-server

반응형