4.1.2. Constants

半世苍凉 提交于 2020-02-07 00:15:33
4.1.2. Constants
4.1.2. 常量
There are three kinds of implicitly-typed constants in PostgreSQL: strings, bit strings, and numbers.Constants can also be specified with explicit types, which can enable more accurate representation and more efficient handling by the system. These alternatives are discussed in the following subsections.
PostgreSQL中有三类隐式常量:字符串、位字符串和数字。常量也可以显式指定,这可以使系统实现更准确的表示和更有效的处理。 以下各节将讨论这些方案。
 
4.1.2.1. String Constants
4.1.2.1. 字符串常量
A string constant in SQL is an arbitrary sequence of characters bounded by single quotes ('), for example 'This is a string'. To include a single-quote character within a string constant, write two adjacent single quotes, e.g., 'Dianne''s horse'. Note that this is not the same as a double-quote character (").
SQL中的字符串常量是由单引号(')界定的任意字符序列,例如'This is a string'。 如果在字符串常量中包含有单引号字符,请写两个相邻的单引号,例如“ Dianne's horse”。 请注意,这不是双引号字符(“)。
 
Two string constants that are only separated by whitespace with at least one newline are concatenated and effectively treated as if the string had been written as one constant. For example:
将仅用空格和至少一个换行符分隔的两个字符串常量,视为一个常量。 例如:
 
SELECT 'foo'
'bar';
is equivalent to:
等效于:
 
SELECT 'foobar';
but:
但是:
 
SELECT 'foo' 'bar';
is not valid syntax. (This slightly bizarre behavior is specified by SQL; PostgreSQL is following the standard.)
是无效的语法。 (这种奇怪的行为由SQL指定; PostgreSQL遵循该标准。)
 
4.1.2.2. String Constants with C-style Escapes
4.1.2.2.  具有C样式转义符的字符串常量
PostgreSQL also accepts “escape” string constants, which are an extension to the SQL standard. An escape string constant is specified by writing the letter E (upper or lower case) just before the opening single quote, e.g., E'foo'. (When continuing an escape string constant across lines, write E only before the first opening quote.) Within an escape string, a backslash character (\) begins a C-like backslash escape sequence, in which the combination of backslash and following character(s) represent a special byte value, as shown in Table 4.1.
PostgreSQL还接受“转义”字符串常量,这是SQL标准的扩展。 通过在开头的单引号之前加字母E(大写或小写)来指定转义字符串常量,例如E'foo'。 (当跨行使用转义字符串常量时,仅需在第一个开始的引号之前写E。)在转义字符串中,反斜杠字符(\)开始一个类似于C的反斜杠转义序列,其中反斜杠和后跟的字符代表一个特殊的字节值,如表4.1所示。
 
Any other character following a backslash is taken literally. Thus, to include a backslash character, write two backslashes (\\). Also, a single quote can be included in an escape string by writing \',in addition to the normal way of ''.
反斜杠后的任何其他字符均按字面原意义使用。 因此,要包含反斜杠字符,可写两个反斜杠(\\)。 另外,除了使用常规的''(两个单引号)之外,还可以通过写\'将单引号包含在转义字符串中。
 
It is your responsibility that the byte sequences you create, especially when using the octal or hexadecimal escapes, compose valid characters in the server character set encoding. When the server encoding is UTF-8, then the Unicode escapes or the alternative Unicode escape syntax, explained in Section 4.1.2.3, should be used instead. (The alternative would be doing the UTF-8 encoding by hand and writing out the bytes, which would be very cumbersome.)
您创建的字节序列(尤其是在使用八进制或十六进制转义符时)需要在服务器字符集编码中能够组成有效字符。 如果服务器编码为UTF-8,则应改用第4.1.2.3节中说明Unicode转义或替代的Unicode转义语法。 (另一种方法是手工进行UTF-8编码并写出字节,这将非常麻烦。)
 
The Unicode escape syntax works fully only when the server encoding is UTF8. When other server encodings are used, only code points in the ASCII range (up to \u007F) can be specified. Both the 4-digit and the 8-digit form can be used to specify UTF-16 surrogate pairs to compose characters with code points larger than U+FFFF, although the availability of the 8-digit form technically makes this unnecessary. (When surrogate pairs are used when the server encoding is UTF8, they are first combined into a single code point that is then encoded in UTF-8.)
仅当服务器字符编码为UTF8时,Unicode转义语法才能完全起作用。 当使用其他服务器编码时,只能指定ASCII范围(最大\u007F)中的代码点。 4位和8位格式都可以用来指定UTF-16代理对,以组成代码点大于U + FFFF的字符,尽管从技术上讲8位格式的可用性使这并没有必要。(在服务器编码为UTF8的情况下使用代理对时,它们首先会组合成一个代码点,然后再以UTF-8进行编码。)
 
Caution
注意
If the configuration parameter standard_conforming_strings is off, then PostgreSQL recognizes backslash escapes in both regular and escape string constants. However, as of PostgreSQL 9.1, the default is on, meaning that backslash escapes are recognized only in escape string constants. This behavior is more standards-compliant, but might break applications which rely on the historical behavior, where backslash escapes were always recognized. As a workaround, you can set this parameter to off, but it is better to migrate away from using backslash escapes. If you need to use a backslash escape to represent a special character, write the string constant with an E.
如果参数standard_conforming_strings配置为off,则PostgreSQL会在常规和转义字符串常量中识别反斜杠转义。 但是,从PostgreSQL 9.1开始,该参数的默认值为on,这意味着仅在转义字符串常量中识别反斜杠转义。 虽然此行为更符合标准,但是可能会破坏依赖始终识别出反斜杠转义符的历史行为的应用程序。 解决方法是,可以将此参数设置为off,但是最好不要使用反斜杠转义来进行迁移。 如果需要使用反斜杠转义符来表示特殊字符,请使用E编写字符串常量。
 
In addition to standard_conforming_strings, the configuration parameters escape_string_warning and backslash_quote govern treatment of backslashes in string constants.
除了standard_conforming_strings外,参数escape_string_warning和backslash_quote也控制字符串常量中反斜杠的处理。
 
 The character with the code zero cannot be in a string constant.
代码为零的字符不能为字符串常量。
 
4.1.2.3. String Constants with Unicode Escapes
4.1.2.3.  具有Unicode转义符的字符串常量
PostgreSQL also supports another type of escape syntax for strings that allows specifying arbitrary Unicode characters by code point. A Unicode escape string constant starts with U& (upper or lower case letter U  followed by ampersand) immediately before the opening quote, without any spaces in between, for example U&'foo'. (Note that this creates an ambiguity with the operator &. Use spaces around the operator to avoid this problem.) Inside the quotes, Unicode characters can be specified in escaped form by writing a backslash followed by the four-digit hexadecimal code point number or alternatively a backslash followed by a plus sign followed by a six-digit hexadecimal code point number. For example, the string 'data' could be written as:
PostgreSQL还支持字符串的另一种转义语法,该语法允许按代码点指定任意Unicode字符。 Unicode转义字符串常量紧接在引号之前以U&(大写或小写字母U,后跟与号)开头,中间没有空格,例如U&'foo'。 (请注意,这会与运算符&产生歧义。请在运算符周围加空格以避免出现此问题。)在引号内,可以通过写反斜杠后跟四位数的十六进制代码点号或反斜杠后加+号跟六位数的十六进制代码点来指定Unicode字符。 或者,反斜杠后跟加号,后跟六位十六进制代码点编号。 例如,字符“data”可以写为:
 
U&'d\0061t\+000061'
The following less trivial example writes the Russian word “slon” (elephant) in Cyrillic letters:
以下简单的例子用西里尔字母写俄语单词“ slon”(大象):
 
U&'\0441\043B\043E\043D'
If a different escape character than backslash is desired, it can be specified using the UESCAPE clause after the string, for example:
如果需要与反斜杠不同的转义字符,则可以在字符串后使用UESCAPE子句来指定它,例如:
 
U&'d!0061t!+000061' UESCAPE '!'
The escape character can be any single character other than a hexadecimal digit, the plus sign, a single quote, a double quote, or a whitespace character.
转义字符可以是除十六进制数字,加号,单引号,双引号或空格字符以外的任何单个字符。
 
The Unicode escape syntax works only when the server encoding is UTF8. When other server encodings are used, only code points in the ASCII range (up to \007F) can be specified. Both the 4-digit and the 6-digit form can be used to specify UTF-16 surrogate pairs to compose characters with code points larger than U+FFFF, although the availability of the 6-digit form technically makes this unnecessary. (When surrogate pairs are used when the server encoding is UTF8, they are first combined into a single code point that is then encoded in UTF-8.)
Unicode转义语法仅在服务器编码为UTF8时有效。 当使服务器用其他字符编码时,只能指定ASCII范围(最大\ 007F)中的代码点。 4位和6位格式均可用于指定UTF-16代理对,以组成代码点大于U + FFFF的字符,尽管6位格式的可用性在技术上没有此必要。 (在服务器编码为UTF8的情况下使用代理对时,它们首先会组合成一个代码点,然后再以UTF-8进行编码。)
 
Also, the Unicode escape syntax for string constants only works when the configuration parameter standard_conforming_strings is turned on. This is because otherwise this syntax could confuse clients that parse the SQL statements to the point that it could lead to SQL injections and similar security issues. If the parameter is set to off, this syntax will be rejected with an error message.
同样的,仅在打开配置参数standard_conforming_strings时,字符串常量的Unicode转义语法才起作用。 这是因为否则的话该语法可能会使解析SQL语句的客户端感到困惑,以至于可能导致SQL注入和类似的安全问题。 如果该参数设置为off,则此语法将被拒绝并显示一条错误消息。
 
To include the escape character in the string literally, write it twice.
如果字符中含有转义字符,那么需要写两次以对转义字符进行转义。
 
4.1.2.4. Dollar-quoted String Constants
4.1.2.4. 以$包含的字符串常量
While the standard syntax for specifying string constants is usually convenient, it can be difficult to understand when the desired string contains many single quotes or backslashes, since each of those must be doubled. To allow more readable queries in such situations, PostgreSQL provides another way, called “dollar quoting”, to write string constants. A dollar-quoted string constant consists of a dollar sign ($), an optional “tag” of zero or more characters, another dollar sign, an arbitrary sequence of characters that makes up the string content, a dollar sign, the same tag that began this dollar quote, and a dollar sign. For example, here are two different ways to specify the string “Dianne's horse” using dollar quoting:
虽然指定字符串常量的标准语法很方便,但是当所需的字符串包含许多单引号或反斜杠时,就可能会很难理解,因为每个引号或反斜杠必须写两次。 为了在这种情况下提供更具可读性的查询,PostgreSQL提供了另一种称为“$引号”的方式来编写字符串常量。 以$的字符串常量包含一个美元符号($),一个零个或多个字符的可选“标记”,另一个美元符号,构成字符串内容的任意字符序列,一个美元符号以及与之前相同的标记和另一个美元符号。 例如,以下是两种使用美元引号指定字符串“ Dianne's horse”的方法:
 
$$Dianne's horse$$
$SomeTag$Dianne's horse$SomeTag$
Notice that inside the dollar-quoted string, single quotes can be used without needing to be escaped. Indeed, no characters inside a dollar-quoted string are ever escaped: the string content is always written literally. Backslashes are not special, and neither are dollar signs, unless they are part of a sequence matching the opening tag.
注意,在用$字符串中,可以使用单引号而不需要对其进行转义。 实际上,用美元引号引起的字符串中的任何字符都不会转义:字符串内容始终按字面意义编写。 反斜杠和美元符号都不是特殊的,除非它们是两个$符号之间的标记的一部分。
 
It is possible to nest dollar-quoted string constants by choosing different tags at each nesting level.This is most commonly used in writing function definitions. For example:
通过在每个嵌套级别选择不同的标记,可以嵌套用美元符号引起的字符串常量。这在编写函数定义时最常用。 例如:
 
$function$
BEGIN
RETURN ($1 ~ $q$[\t\r\n\v\\]$q$);
END;
$function$
Here, the sequence $q$[\t\r\n\v\\]$q$ represents a dollar-quoted literal string [\t\r\n\v\\], which will be recognized when the function body is executed by PostgreSQL. But since the  sequence does not match the outer dollar quoting delimiter $function$, it is just some more characters within the constant so far as the outer string is concerned.
在这里,当函数体被PostgreSQL执行时,序列$q$ [\t\r\n\v\\]$q$代表用$引起来的文字字符串[\t\r\n\v \\]。 但是由于该序列与外部美元引号分隔符$function$不匹配,因此就外部字符串而言,它只是常量中的一些其他字符。
 
The tag, if any, of a dollar-quoted string follows the same rules as an unquoted identifier, except that it cannot contain a dollar sign. Tags are case sensitive, so $tag$String content$tag$ is correct,but $TAG$String content$tag$ is not.
用美元引号的字符串的标签(如果有)遵循与标识符相同的规则,不同之处在于它不能包含美元符号。 标签区分大小写,因此$tag$ String content $tag$是正确的,但$TAG$ String content $tag$不正确。
 
A dollar-quoted string that follows a keyword or identifier must be separated from it by whitespace;otherwise the dollar quoting delimiter would be taken as part of the preceding identifier.
关键字或标识符后的$字符串必须用空格隔开;否则,$定界符将作为前面标识符的一部分。
 
Dollar quoting is not part of the SQL standard, but it is often a more convenient way to write complicated string literals than the standard-compliant single quote syntax. It is particularly useful when representing string constants inside other constants, as is often needed in procedural function definitions.With single-quote syntax, each backslash in the above example would have to be written as four backslashes, which would be reduced to two backslashes in parsing the original string constant, and then to one when the inner string constant is re-parsed during function execution.
美元引号虽然不是SQL标准的一部分,但是与标准兼容的单引号语法相比,美元引号通常是一种更方便的写复杂字符串文字的方法。 这在过程函数定义中经常需要用其他常量表示字符串常量时特别有用。使用单引号语法时,上例中的每个反斜杠都必须写成四个反斜杠,在解析原始字符串常量时在减少为两个反斜杠,然后在函数执行过程中重新解析内部字符串常量时将其较少为1个。
 
4.1.2.5. Bit-string Constants
4.1.2.5. 位字符串常量
Bit-string constants look like regular string constants with a B (upper or lower case) immediately before the opening quote (no intervening whitespace), e.g., B'1001'. The only characters allowed within bit-string constants are 0 and 1.
位字符串常量看起来像常规字符串常量,只是在引号之前(没有中间空格)带有B(大写或小写均可),例如B'1001'。 位字符串常量中唯一允许的字符是0和1。
 
Alternatively, bit-string constants can be specified in hexadecimal notation, using a leading X (upper or lower case), e.g., X'1FF'. This notation is equivalent to a bit-string constant with four binary digits for each hexadecimal digit.
或者,可以以十六进制表示法指定位串常量,使用前导X(大写或小写),例如X'1FF'。 此表示法等效于每个十六进制数字具有四个二进制数字的位字符串常量。
 
Both forms of bit-string constant can be continued across lines in the same way as regular string constants. Dollar quoting cannot be used in a bit-string constant.
两种形式的位字符串常量都可以以与常规字符串常量相同的方式跨行继续。 美元引号不能在位字符串常量中使用。
 
4.1.2.6. Numeric Constants
4.1.2.6. 数字常量
Numeric constants are accepted in these general forms:
可以以下列一般形式使用数字常量:
 
digits
digits.[digits][e[+-]digits]
[digits].digits[e[+-]digits]
digitse[+-]digits
 
where digits is one or more decimal digits (0 through 9). At least one digit must be before or after the decimal point, if one is used. At least one digit must follow the exponent marker (e), if one is present.There cannot be any spaces or other characters embedded in the constant. Note that any leading plus or minus sign is not actually considered part of the constant; it is an operator applied to the constant.
其中数字是一个或多个十进制数字(0到9)。 如果使用一位,则小数点之前或之后必须至少有一位数字。 指数标记(e)后面必须至少有一位数字(如果存在),该常数中不能嵌入任何空格或其他字符。 请注意,任何前导的正号或负号实际上并未视为常量的一部分; 它是应用于常量的运算符。
 
These are some examples of valid numeric constants:
这些是有效数字常量的一些示例:
 
42
3.5
4.
.001
37
5e2
1.925e-3
A numeric constant that contains neither a decimal point nor an exponent is initially presumed to be type integer if its value fits in type integer (32 bits); otherwise it is presumed to be type bigint if its value fits in type bigint (64 bits); otherwise it is taken to be type numeric. Constants that contain decimal points and/or exponents are always initially presumed to be type numeric.
如果一个既不包含小数点也不包含指数的数字常量的值适合integer(32位)类型,则假定该类型为integer;否则,如果其值适合bigint类型(64位),则假定它为bigint类型;否则,假定为numeric类型。 假定包含小数点和/或指数的常量为numeric类型。
 
The initially assigned data type of a numeric constant is just a starting point for the type resolution algorithms. In most cases the constant will be automatically coerced to the most appropriate type depending on context. When necessary, you can force a numeric value to be interpreted as a specific data type by casting it. For example, you can force a numeric value to be treated as type real (float4by writing:
初始分配的数值常量的数据类型只是类型解析算法的起点。 在大多数情况下,常量将根据上下文自动强制转换为最合适的类型。 必要时,可以通过将数值强制转换为特定的数据类型。 例如,您可以通过编写以下代码来强制将数值转换为real(float4)类型:
 
REAL '1.23' -- string style
1.23::REAL -- PostgreSQL (historical) style
 
These are actually just special cases of the general casting notations discussed next.
这些实际上只是接下来讨论的一般转换符号的特殊情况。
 
4.1.2.7. Constants of Other Types
4.1.2.7. 其他类型的常量
A constant of an arbitrary type can be entered using any one of the following notations:
可以使用以下任何一种方法输入任意类型的常量:
 
type 'string'
'string'::type
CAST ( 'string' AS type )
 
The string constant's text is passed to the input conversion routine for the type called type. The result is a constant of the indicated type. The explicit type cast can be omitted if there is no ambiguity as to the type the constant must be (for example, when it is assigned directly to a table column), in which case it is automatically coerced.
字符串常量的文本将传递给类型为type的输入转换例程。 结果是所指示类型的常量。 如果对于常量必须是哪种类型没有歧义,则可以省略显式类型强制转换(例如,当直接将常量分配给表列时),在这种情况下,它将被自动强制限制。
 
The string constant can be written using either regular SQL notation or dollar-quoting.
可以使用常规SQL表示法或美元引号编写字符串常量。
 
It is also possible to specify a type coercion using a function-like syntax:
也可以使用类似函数的语法来指定强制类型转换:
 
typename ( 'string' )
 
but not all type names can be used in this way; see Section 4.2.9 for details.
但并非所有类型名称都可以这种方式使用; 有关详细信息,请参见第4.2.9节
 
The ::, CAST(), and function-call syntaxes can also be used to specify run-time type conversions of arbitrary expressions, as discussed in Section 4.2.9. To avoid syntactic ambiguity, the type 'string' syntax can only be used to specify the type of a simple literal constant. Another restriction on the type 'string' syntax is that it does not work for array types; use :: or CAST() to specify the type of an array constant.
::,CAST()和函数调用语法也可以用于指定任意表达式的运行时类型转换,如第4.2.9节所述。 为避免语法歧义,'string'类型的语法只能用于指定简单文字常量的类型。 类型'string'语法的另一个限制是它不适用于数组类型;数组常量类型使用::或CAST()指定。
 
The CAST() syntax conforms to SQL. The type 'string' syntax is a generalization of the standard: SQL specifies this syntax only for a few data types, but PostgreSQL allows it for all types.The syntax with :: is historical PostgreSQL usage, as is the function-call syntax.
CAST()语法符合SQL标准。 类型'string'的语法是该标准的一般化:SQL仅针对几种数据类型指定此语法,而PostgreSQL允许所有类型使用。::的语法是PostgreSQL的历史用法,函数调用的语法也是如此。
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!