8.3. Character Types

8.3. Character Types
8.3. Character Types
uploading.4e448015.gifDump failed to re-upload canceled
Table 8.4 shows the general-purpose character types available in PostgreSQL.
Table 8.4 shows the PostgreSQL common character data type.
 
SQL defines two primary character types: character varying(n) and character(n), where n is a positive integer. Both of these types can store strings up to n characters (not bytes) in length. An attempt to store a longer string into a column of these types will result in an error, unless the excess characters are all spaces, in which case the string will be truncated to the maximum length. (This somewhat bizarre exception is required by the SQL standard.) If the string to be stored is shorter than the declared length, values of type character will be space-padded; values of type character varying will simply store the shorter string.
SQL defines two major types of characters: a positive integer character varying (n) and character (n), where n is. Two data types can save up to n characters (not bytes) to create a character. If you try to insert long value error, unless the excess characters are all spaces, in this case, the string will be truncated. (This bizarre abnormality specified by the SQL standard.) If the stored string is shorter than the defined length, then the character will fill the space to a defined length, the character varying only the value is stored.
 
If one explicitly casts a value to character varying(n) or character(n), then an overlength value will be truncated to n characters without raising an error. (This too is required by the SQL standard.)
If the value is cast to character varying (n) or character (n), so long cut off part directly without an error. (This is also the requirements of the standard SQL.)
 
The notations varchar(n) and char(n) are aliases for character varying(n) and character(n), respectively. character without length specifier is equivalent to character(1)If character varying is used without length specifier, the type accepts strings of any size. The latter is a PostgreSQL extension.
varchar (n) and char (n) are character varying (n) and character (n) alias. If the character is not limited length, for the character (1). If the character varying length is not defined, then it can enter a string of any length. The latter is implemented as a PostgreSQL extension.
 
In addition, PostgreSQL provides the text type, which stores strings of any length. Although the type text is not in the SQL standard, several other SQL database management systems have it as well.
Further, PostgreSQL also provides a text string data type that can hold any length. Although the SQL standard and no text type, but in many other SQL database management systems also have this type.
 
Values of type character are physically padded with spaces to the specified width n, and are stored and displayed that way. However, trailing spaces are treated as semantically insignificant and disregarded when comparing two values of type character. In collations where whitespace is significant,this behavior can produce unexpected results; for example SELECT 'a '::CHAR(2) collate "C" < E'a\n'::CHAR(2) returns true, even though C locale would consider a space to be greater than a newline. Trailing spaces are 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, and when using pattern matching, that is LIKE and regular expressions.
value is actually character spaces to a specified length n, but also so that the storage and display. However,  when comparing the two values character types, trailing spaces are treated as semantically insignificant and can be ignored. Blank collation is important, this behavior can produce unexpected results; for example, the SELECT 'A' :: CHAR (2) COLLATE  < "C" E'a \ n-':: CHAR (2) returns true, even if the C locale think space is greater than line breaks as well. When you convert a character value to another string type will delete trailing spaces. Please note that trailing spaces in the character varying and text values and the use of pattern matching (ie LIKE and regular expressions) are important semantically.
 
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.)
Storage requirements short strings (up to 126 bytes) is 1 byte plus the actual string, including filling spaces in the case of the character. Overhead longer string is 4 bytes instead of 1 byte. Longer string compression performed automatically by the system, and therefore the demand for physical disk may be less. Very long values ​​are also stored in background tables so they do not interfere with rapid access to the shorter column values. In any case, the string may be stored up to about 1 GB. Maximum value (data type declaration n is less than the allowed value. Unnecessary to change this value, because using the multi-byte character code, the character, and the number of bytes may be different. If you want to store the upper limit is not particular long string, use without a length specifier text or character varying type, rather than to set an arbitrary length limit.)
 
Tip
Tips
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.
There is no performance difference between the three types, types except when padded with spaces to increase the storage space, and the length limit stored in the column, there are some additional length to check by the CPU. Although the character (n) has performance advantages in some other database systems, but in PostgreSQL has no such advantage. In fact, character (n) is usually the slowest of the three, because it requires additional storage costs. In most cases, you should use text or character varying types.
 
Refer to Section 4.1.2.1 for information about the syntax of string literals, and to Chapter 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 Section 23.3.
For information about the syntax of string literals, see Section 4.1.2.1 ; information about available operators and functions, see Chapter 9 . Database character set determines the character set used to store text values; for more information on character set support, see Section 23.3 .
 
Example 8.1. Using the Character Types
Example 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
 
1 The char_length function is discussed in Section 9.4.
Char_length function in 9.4 discussion.
 
There are two other fixed-length character types in PostgreSQL, shown in Table 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.
There are two types of characters PostgreSQL fixed length, see Table 8.5. name is only used in the storage system identifier of the internal table, the user should not use this type of ships. Currently, its length is defined as 64 bytes (63 plus available characters terminator), you should use a constant NAMEDATALEN C source code referenced. This length is set at compile time (and therefore can be adjusted for any particular purpose); the default maximum length may change in future releases. Type "char" (Note that the quotes) is different from the char (1) that it uses only one byte of storage space. It is used as a simple enumeration type within the system directory.
 
uploading.4e448015.gifDump failed to re-upload canceled
Published 341 original articles · won praise 54 · views 880 000 +

Guess you like

Origin blog.csdn.net/ghostliming/article/details/104628351