표 8-4. 문자 자료형
| 이름 | 설명 |
|---|---|
| character varying(n), varchar(n) | 길이 제한 있는 가변 길이 문자열 |
| character(n), char(n) | 공백 채움 고정 길이 문자열 |
| text | 길이 제한 없는 가변 길이 문자열 |
PostgreSQL에서 쓸 수 있는 범용 문자 자료형들은 표 8-4에서 소개하고 있다.
SQL 문자열 자료형으로 기본적으로 다음과 같은 두 가지 자료형을 사용할 수 있다: character varying(n) 또는 character(n), 여기서 n 값은 양수다. 두 형식 다 n 글자 개수(바이트가 아님) 만큼 저장 할 수 있음을 뜻한다. n characters (not bytes) in length. 이 길이보다 더 긴 문자열을 입력하려고 하면 오류가 생긴다. 한편 입력 되는 문자열의 끝부분에 오는 공백 문자열은 문자열 최대 길이값까지 저장되며 나머지는 무시된다. (이는 SQL 표준안에 지정된 이상한 표준안을 따르기 때문이다) 지정한 최대 길이값보다 적은 문자열을 저장하려고 하면, character 자료형인 경우는 남은 길이를 공백으로 채워 저장하며, character varying 자료형은 그 입력된 문자열만 저장한다.
character varying(n) 또는 character(n) 형 변환을 할 때, 원본 자료형의 글자수가 형변환 할 때 지정한 n 값보다 클 경우 오류 없이 최대 길이 만큼 잘린다. (이 또한 SQL 표준안을 따르기 위해서 이렇게 처리한다.)
varchar(n)형과, char(n)형은 각각 character varying(n)형과, character(n)형의 별칭이다. 최대 글자수 지정 없이 character 형태로 사용하면, character(1)형으로 처리되며, character varying 일 경우는 글자수 제한 없는 문자열로 처리된다. (두번째 경우는 PostgreSQL 확장기능이다.
덧붙여, PostgreSQL에서는 text 자료형을 제공한다. 이 자료형은 길이 제한이 없는 문자열을 저장할 수 있다. 이 자료형은 SQL 표준은 아니지만, 많은 다른 SQL 데이터베이스 관리 시스템에서 지원하고 있다.
(요기서 부터가 PostgreSQL에서 내부적으로 다루는 문자열 처리 방법) Values of type character are physically padded with spaces to the specified width n, and are stored and displayed that way. However, the padding spaces are treated as semantically insignificant. Trailing spaces are disregarded when comparing two values of type character, and they will be removed when converting a character value to one of the other string types. Note that trailing spaces are semantically significant in character varying and text values.
The storage requirement for a short string (up to 126 bytes) is 1 byte plus the actual string, which includes the space padding in the case of character. Longer strings have 4 bytes of overhead instead of 1. Long strings are compressed by the system automatically, so the physical requirement on disk might be less. Very long values are also stored in background tables so that they do not interfere with rapid access to shorter column values. In any case, the longest possible character string that can be stored is about 1 GB. (The maximum value that will be allowed for n in the data type declaration is less than that. It wouldn't be useful to change this because with multibyte character encodings the number of characters and bytes can be quite different. If you desire to store long strings with no specific upper limit, use text or character varying without a length specifier, rather than making up an arbitrary length limit.)
작은 정보: 가장 중요한 이야기: 이 세가지 자료형의 처리하는데, 있어 성능 차이가 없다는 것! There is no performance difference among these three types, apart from increased storage space when using the blank-padded type, and a few extra CPU cycles to check the length when storing into a length-constrained column. While character(n) has performance advantages in some other database systems, there is no such advantage in PostgreSQL; in fact character(n) is usually the slowest of the three because of its additional storage costs. In most situations text or character varying should be used instead.
Refer to 4.1.2.1절 for information about the syntax of string literals, and to 9장 for information about available operators and functions. The database character set determines the character set used to store textual values; for more information on character set support, refer to 22.2절.
예 8-1. Using the character types
CREATE TABLE test1 (a character(4));
INSERT INTO test1 VALUES ('ok');
SELECT a, char_length(a) FROM test1; -- (1)
a | char_length
------+-------------
ok | 2
CREATE TABLE test2 (b varchar(5));
INSERT INTO test2 VALUES ('ok');
INSERT INTO test2 VALUES ('good ');
INSERT INTO test2 VALUES ('too long');
ERROR: value too long for type character varying(5)
INSERT INTO test2 VALUES ('too long'::varchar(5)); -- explicit truncation
SELECT b, char_length(b) FROM test2;
b | char_length
-------+-------------
ok | 2
good | 5
too l | 5There are two other fixed-length character types in PostgreSQL, shown in 표 8-5. The name type exists only for the storage of identifiers in the internal system catalogs and is not intended for use by the general user. Its length is currently defined as 64 bytes (63 usable characters plus terminator) but should be referenced using the constant NAMEDATALEN in C source code. The length is set at compile time (and is therefore adjustable for special uses); the default maximum length might change in a future release. The type "char" (note the quotes) is different from char(1) in that it only uses one byte of storage. It is internally used in the system catalogs as a simplistic enumeration type.