표 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 | 5
There 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.