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 andmore 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 ('), forexample '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 adouble-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 concatenatedand 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 thestandard.)
是无效的语法。 (这种奇怪的行为由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. Anescape string constant is specified by writing the letter E (upper or lower case) just before the openingsingle quote, e.g., E'foo'. (When continuing an escape string constant across lines, write E onlybefore the first opening quote.) Within an escape string, a backslash character (\) begins a C-likebackslash escape sequence, in which the combination of backslash and following character(s) representa special byte value, as shown in Table 4.1.
PostgreSQL还接受“转义”字符串常量,这是SQL标准的扩展。通过在开头的单引号之前加字母E(大写或小写)来指定转义字符串常量,例如E'foo'。 (当跨行使用转义字符串常量时,仅需在第一个开始的引号之前写E。)在转义字符串中,反斜杠字符(\)开始一个类似于C的反斜杠转义序列,其中反斜杠和后跟的字符代表一个特殊的字节值,如表4.1所示。
postgres=# select e'ni\nhao';
?column?
----------
ni      +
hao
(1 row)
postgres=#
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 hexadecimalescapes, compose valid characters in the server character set encoding. When the server encodingis UTF-8, then the Unicode escapes or the alternative Unicode escape syntax, explained inSection 4.1.2.3, should be used instead. (The alternative would be doing the UTF-8 encoding by handand writing out the bytes, which would be very cumbersome.)
您创建的字节序列(尤其是在使用八进制或十六进制转义符时)需要在服务器字符集编码中能够组成有效字符。 如果服务器编码为UTF-8,则应改用第4.1.2.3节中说明Unicode转义或替代的Unicode转义语法,然后数据库服务会自动进行转换。
Caution
注意
If the configuration parameter standard_conforming_strings is off, then PostgreSQLrecognizes backslash escapes in both regular and escape string constants. However, asof PostgreSQL 9.1, the default is on, meaning that backslash escapes are recognizedonly in escape string constants. This behavior is more standards-compliant, but mightbreak applications which rely on the historical behavior, where backslash escapes werealways recognized. As a workaround, you can set this parameter to off, but it is betterto migrate away from using backslash escapes. If you need to use a backslash escapeto represent a special character, write the string constant with an E.
如果参数standard_conforming_strings配置为off,则PostgreSQL会在常规和转义字符串常量中识别反斜杠转义。 但是,从PostgreSQL 9.1开始,该参数的默认值为on,这意味着仅在转义字符串常量中识别反斜杠转义。 虽然此行为更符合标准,但是可能会破坏依赖始终识别出反斜杠转义符的历史行为的应用程序。 解决方法是,可以将此参数设置为off,但是最好不要使用反斜杠转义来进行迁移。 如果需要使用反斜杠转义符来表示特殊字符,请使用E编写字符串常量。
postgres=# show standard_conforming_strings;
standard_conforming_strings
-----------------------------
on
(1 row)
postgres=# select '\';
?column?
----------
\
(1 row)
postgres=#
postgres=# show standard_conforming_strings;
standard_conforming_strings
-----------------------------
off
(1 row)
postgres=# select '\';
postgres'#
postgres'#
In addition to standard_conforming_strings, the configuration parametersescape_string_warning and backslash_quote govern treatment of backslashes in stringconstants.
除了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 arbitraryUnicode characters by code point. A Unicode escape string constant starts with U& (upper or lowercase letter U  followed by ampersand) immediately before the opening quote, without any spaces inbetween, for example U&'foo'. (Note that this creates an ambiguity with the operator &. Use spacesaround the operator to avoid this problem.) Inside the quotes, Unicode characters can be specifiedin escaped form by writing a backslash followed by the four-digit hexadecimal code point numberor alternatively a backslash followed by a plus sign followed by a six-digit hexadecimal code pointnumber. 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 clauseafter 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 singlequote, a double quote, or a whitespace character.
转义字符可以是除十六进制数字,加号,单引号,双引号或空格字符以外的任何单个字符。
To include the escape character in the string literally, write it twice.
如果字符中含有转义字符,那么需要写两次以对转义字符进行转义。
Also, the Unicode escape syntax for string constants only works when the configuration parameterstandard_conforming_strings is turned on. This is because otherwise this syntax could confuse clientsthat parse the SQL statements to the point that it could lead to SQL injections and similar securityissues. If the parameter is set to off, this syntax will be rejected with an error message.
同样的,仅在打开配置参数standard_conforming_strings时,字符串常量的Unicode转义语法才起作用。 这是因为否则的话该语法可能会使解析SQL语句的客户端感到困惑,以至于可能导致SQL注入和类似的安全问题。 如果该参数设置为off,则此语法将被拒绝并显示一条错误消息。
postgres=# select u&'foo';
2020-12-17 21:25:11.037 CST [32233] ERROR:  unsafe use of string constant with Unicode escapes at character 8
2020-12-17 21:25:11.037 CST [32233] DETAIL:  String constants with Unicode escapes cannot be used when standard_conforming_strings is off.
2020-12-17 21:25:11.037 CST [32233] STATEMENT:  select u&'foo';
ERROR:  unsafe use of string constant with Unicode escapes
LINE 1: select u&'foo';
^
DETAIL:  String constants with Unicode escapes cannot be used when standard_conforming_strings is off.
postgres=#
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 tounderstand when the desired string contains many single quotes or backslashes, since each of thosemust be doubled. To allow more readable queries in such situations, PostgreSQL provides anotherway, called “dollar quoting”, to write string constants. A dollar-quoted string constant consists of adollar sign ($), an optional “tag” of zero or more characters, another dollar sign, an arbitrary sequenceof 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” usingdollar 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 writtenliterally. Backslashes are not special, and neither are dollar signs, unless they are part of a sequencematching the opening tag.
注意,在用$字符串中,可以使用单引号而不需要对其进行转义。 实际上,用美元引号引起的字符串中的任何字符都不会转义:字符串内容始终按字面意义编写。 反斜杠和美元符号都不是特殊的,除非它们是两个$符号之间的标记的一部分。
postgres=# select $$that's a cat.$$;
?column?
---------------
that's a cat.
(1 row)
postgres=#
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 thesequence does not match the outer dollar quoting delimiter $function$, it is just some more characterswithin 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 itcannot 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 complicatedstring literals than the standard-compliant single quote syntax. It is particularly useful whenrepresenting 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 fourbackslashes, which would be reduced to two backslashes in parsing the original string constant, andthen 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) immediatelybefore the opening quote (no intervening whitespace), e.g., B'1001'. The only characters allowedwithin 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 (upperor lower case), e.g., X'1FF'. This notation is equivalent to a bit-string constant with four binary digitsfor each hexadecimal digit.
或者,可以以十六进制表示法指定位串常量,使用前导X(大写或小写),例如X'1FF'。 此表示法等效于每个十六进制数字具有四个二进制数字的位串常量。
Both forms of bit-string constant can be continued across lines in the same way as regular stringconstants. 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 thedecimal 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 plusor minus sign is not actually considered part of the constant; it is an operator applied to the constant.
其中digits是一个或多个十进制数字(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 betype integer if its value fits in type integer (32 bits); otherwise it is presumed to be type bigintif its value fits in type bigint (64 bits); otherwise it is taken to be type numeric. Constants thatcontain 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 resolutionalgorithms. In most cases the constant will be automatically coerced to the most appropriate type dependingon context. When necessary, you can force a numeric value to be interpreted as a specific datatype by casting it. For example, you can force a numeric value to be treated as type real (float4)by 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 resultis a constant of the indicated type. The explicit type cast can be omitted if there is no ambiguity as tothe type the constant must be (for example, when it is assigned directly to a table column), in whichcase 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 conversionsof 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 restrictionon the type 'string' syntax is that it does not work for array types; use :: or CAST() to specifythe 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 thestandard: 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的历史用法,函数调用的语法也是如此。

4.1.2. Constants相关推荐

  1. Codeforces Beta Round #96 (Div. 1) D. Constants in the language of Shakespeare 贪心

    D. Constants in the language of Shakespeare Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codef ...

  2. 常量和指针(Pointers and Constants)

    常量和指针(Pointers and Constants) --const 修饰的指针解惑 一般遇到用const修饰的常量涉及到指针就会比较麻烦,容易把头搞晕,有个简单的技巧就是从右向左看,下面我举例 ...

  3. 深入浅出OOP(五): C#访问修饰符(Public/Private/Protected/Internal/Sealed/Constants)

    访问修饰符(或者叫访问控制符)是面向对象语言的特性之一,用于对类.类成员函数.类成员变量进行访问控制.同时,访问控制符也是语法保留关键字,用于封装组件. Public, Private, Protec ...

  4. 八十五、store数据,actionCreators 与 constants 的拆分和redux-immutable的使用

    2020/11/22. 周日.今天又是奋斗的一天. @Author:Runsen 你是否将所有 JavaScript 脚本放在一个大文件中,并在所有页面上使用这个文件?如果是这样,你可能需要考虑使用代 ...

  5. [C++] 用Xcode来写C++程序[3] Constants

    用Xcode来写C++程序[3] Constants 以下是一些基本数据的含义: 75 // int 75u // unsigned int 75l // long 75ul // unsigned ...

  6. Application Constants

    Application: Application类是Android框架中提供的一个类.本身程序员不需要创建它,只需要继承它既可.并在manifest中进行注册. 它给我们提供了一个一般不会被销毁的全局 ...

  7. TYPES、DATA、TYPE、LIKE、CONSTANTS、STATICS、TABLES

    TYPES.DATA.TYPE.LIKE. 42 创建数据类型与变量... 44 TYPES 语句... 44 DATA 语句... 46 CONSTANTS 语句... 48 STATICS 语句. ...

  8. python中arcsec_Python SciPy 常数(Constants)

    1.SciPy常数 由于SciPy更加专注于科学实现,因此它提供了许多内置的科学常数. 当您使用数据科学时,这些常量可能会有所帮助. PI是科学常数的一个例子. 例如: 打印PI的常数值:from s ...

  9. 1.3 C++常量 (Constants )

     1.3 常量 (Constants ) 一个常量(constant)是一个有固定值的表达式. 字(Literals) 字是用来在程序源码中表达特定的值.在前面的内容中我们已经用了很多的字来给变量 ...

最新文章

  1. ADO.NET复习——自己编写SqlHelper类
  2. 利用sql报错帮助进行sql注入
  3. swagger ui remove springboot paths
  4. HTML5 Audio(音频)
  5. 真正的男人要勇于承担责任......
  6. 终于……我的游戏………简体版就要发布了!
  7. java list分批_Java实用笔记——mybatis批量导入
  8. parse_str 相反函数
  9. u9系统的使用方法仓库_【用友u9使用教程】
  10. html布局源代码实例,网页实际案例-从设计到代码实现全过程(一)
  11. kettle(PDI)安装使用过程,并部署到Linux下执行定时作业
  12. 如何分析加上SE壳的.net程序
  13. 将java封装的实体类数据生成excel供下载
  14. COMP SCI 4094/4194/7094 - Distributed Databases and Data Mining
  15. RabbitMQ - 4种Exchange类型
  16. 2021 ICCV论文分享 | 遮挡边界检测
  17. FDM打印机使用总结(二)
  18. 2007年世界杀毒软件排行榜
  19. 8.ASP.NET Core中的Kestrel Web服务器
  20. 一文带你读完《推荐系统实践》

热门文章

  1. ffmpeg C++推流
  2. 一个神奇的测试_被套住就要说真话!《神奇女侠》的作者真的发明过测谎仪
  3. 佐治亚理工计算机科学专业排名,佐治亚理工学院计算机科学硕士专业排名
  4. 怎样改变照片大小?免费在线图片压缩方法
  5. Java练习之坦克大战!!!复制可以直接用!!!文章最后有飞机大战代码!!!
  6. mysql 按日期分组求和
  7. 面试题(javamysql)
  8. 防火墙NAT综合实验——nat控制,豁免,远程,DMZ区域(带命令)
  9. 零基础手把手教你做FMEDA
  10. python中元组的特点_Python中的元组介绍