1
1
<!--
2
- $Header: /cvsroot/pgsql/doc/src/sgml/syntax.sgml,v 1.56 2002/01/07 02:29:13 petere Exp $
2
+ $Header: /cvsroot/pgsql/doc/src/sgml/syntax.sgml,v 1.57 2002/01/09 23:38:06 tgl Exp $
3
3
-->
4
4
5
5
<chapter id="sql-syntax">
@@ -370,7 +370,7 @@ REAL '1.23' -- string style
370
370
'<replaceable>string</replaceable>'::<replaceable>type</replaceable>
371
371
CAST ( '<replaceable>string</replaceable>' AS <replaceable>type</replaceable> )
372
372
</synopsis>
373
- Thevalue inside the string is passed to the input conversion
373
+ The string's text is passed to the input conversion
374
374
routine for the type called <replaceable>type</replaceable>. The
375
375
result is a constant of the indicated type. The explicit type
376
376
cast may be omitted if there is no ambiguity as to the type the
@@ -383,25 +383,23 @@ CAST ( '<replaceable>string</replaceable>' AS <replaceable>type</replaceable> )
383
383
It is also possible to specify a type coercion using a function-like
384
384
syntax:
385
385
<synopsis>
386
- <replaceable>typename</replaceable> ( <replaceable>value </replaceable> )
386
+ <replaceable>typename</replaceable> (' <replaceable>string </replaceable>' )
387
387
</synopsis>
388
- although this only works for types whose names are also valid as
389
- function names. For example, <literal>double precision</literal>
390
- can't be used this way, but the equivalent <literal>float8</literal>
391
- can. Also, the names <literal>interval</>, <literal>time</>, and
392
- <literal>timestamp</> can only be used in this context if they are
393
- double-quoted, because of parser conflicts. Therefore, the use of
394
- the function-like cast syntax leads to inconsistencies and should
395
- probably be avoided in new applications.
388
+ but not all type names may be used in this way; see <xref
389
+ linkend="sql-syntax-type-casts"> for details.
396
390
</para>
397
391
398
392
<para>
399
393
The <literal>::</literal>, <literal>CAST()</literal>, and
400
- function-call syntaxes can also be used to specify the type of
401
- arbitrary expressions, but the form
402
- <replaceable>type</replaceable>
403
- '<replaceable>string</replaceable>' can only be used to specify
404
- the type of a literal constant.
394
+ function-call syntaxes can also be used to specify runtime type
395
+ conversions of arbitrary expressions, as discussed in <xref
396
+ linkend="sql-syntax-type-casts">. But the form
397
+ <replaceable>type</replaceable> '<replaceable>string</replaceable>'
398
+ can only be used to specify the type of a literal constant.
399
+ Another restriction on
400
+ <replaceable>type</replaceable> '<replaceable>string</replaceable>'
401
+ is that it does not work for array types; use <literal>::</literal>
402
+ or <literal>CAST()</literal> to specify the type of an array constant.
405
403
</para>
406
404
</sect3>
407
405
@@ -793,64 +791,50 @@ CAST ( '<replaceable>string</replaceable>' AS <replaceable>type</replaceable> )
793
791
794
792
<listitem>
795
793
<para>
796
- A column reference
794
+ A column reference.
797
795
</para>
798
796
</listitem>
799
797
800
798
<listitem>
801
799
<para>
802
- An operator invocation:
803
- <simplelist>
804
- <member><replaceable>expression</replaceable> <replaceable>operator</replaceable> <replaceable>expression</replaceable> (binary infix operator)</member>
805
- <member><replaceable>operator</replaceable> <replaceable>expression</replaceable> (unary prefix operator)</member>
806
- <member><replaceable>expression</replaceable> <replaceable>operator</replaceable> (unary postfix operator)</member>
807
- </simplelist>
808
- where <replaceable>operator</replaceable> follows the syntax
809
- rules of <xref linkend="sql-syntax-operators"> or is one of the
810
- tokens <token>AND</token>, <token>OR</token>, and
811
- <token>NOT</token>. Which particular operators exist and whether
812
- they are unary or binary depends on what operators have been
813
- defined by the system or the user. <xref linkend="functions">
814
- describes the built-in operators.
800
+ A positional parameter reference, in the body of a function declaration.
815
801
</para>
816
802
</listitem>
817
803
818
804
<listitem>
819
- <synopsis>( <replaceable>expression</replaceable> )</synopsis>
820
805
<para>
821
- Parentheses are used to group subexpressions and override precedence .
806
+ An operator invocation .
822
807
</para>
823
808
</listitem>
824
809
825
810
<listitem>
826
811
<para>
827
- Apositional parameter reference, in the body of a functiondeclaration .
812
+ A functioncall .
828
813
</para>
829
814
</listitem>
830
815
831
816
<listitem>
832
817
<para>
833
- A function call
818
+ An aggregate expression.
834
819
</para>
835
820
</listitem>
836
821
837
822
<listitem>
838
823
<para>
839
- An aggregate expression
824
+ A type cast.
840
825
</para>
841
826
</listitem>
842
827
843
828
<listitem>
844
829
<para>
845
- A scalar subquery. This is an ordinary
846
- <command>SELECT</command> in parentheses that returns exactly one
847
- row with one column. It is an error to use a subquery that
848
- returns more than one row or more than one column in the context
849
- of a value expression. (But if, during a particular execution, the
850
- subquery returns no rows, the scalar result is taken to be NULL.)
851
- The subquery can refer to variables from the surrounding query,
852
- which will act as constants during any one evaluation of the subquery.
853
- See also <xref linkend="functions-subquery">.
830
+ A scalar subquery.
831
+ </para>
832
+ </listitem>
833
+
834
+ <listitem>
835
+ <synopsis>( <replaceable>expression</replaceable> )</synopsis>
836
+ <para>
837
+ Parentheses are used to group subexpressions and override precedence.
854
838
</para>
855
839
</listitem>
856
840
</itemizedlist>
@@ -885,14 +869,14 @@ CAST ( '<replaceable>string</replaceable>' AS <replaceable>type</replaceable> )
885
869
the key words <literal>NEW</literal> or <literal>OLD</literal>.
886
870
(NEW and OLD can only appear in the action portion of a rule,
887
871
while other correlation names can be used in any SQL statement.)
888
- The correlation name can be omitted if the column name is unique
872
+ The correlation name and separating dot may be omitted if the column name
873
+ is unique
889
874
across all the tables being used in the current query. If
890
875
<replaceable>column</replaceable> is of an array type, then the
891
876
optional <replaceable>subscript</replaceable> selects a specific
892
- element in the array. If no subscript is provided, then the whole
893
- array is selected. Refer to the description of the particular
894
- commands in the <citetitle>PostgreSQL Reference Manual</citetitle>
895
- for the allowed syntax in each case.
877
+ element or elements in the array. If no subscript is provided, then the
878
+ whole array is selected. (See <xref linkend="arrays"> for more about
879
+ arrays.)
896
880
</para>
897
881
</sect2>
898
882
@@ -923,6 +907,26 @@ CREATE FUNCTION dept (text) RETURNS dept
923
907
</para>
924
908
</sect2>
925
909
910
+ <sect2>
911
+ <title>Operator Invocations</title>
912
+
913
+ <para>
914
+ There are three possible syntaxes for an operator invocation:
915
+ <simplelist>
916
+ <member><replaceable>expression</replaceable> <replaceable>operator</replaceable> <replaceable>expression</replaceable> (binary infix operator)</member>
917
+ <member><replaceable>operator</replaceable> <replaceable>expression</replaceable> (unary prefix operator)</member>
918
+ <member><replaceable>expression</replaceable> <replaceable>operator</replaceable> (unary postfix operator)</member>
919
+ </simplelist>
920
+ where the <replaceable>operator</replaceable> token follows the syntax
921
+ rules of <xref linkend="sql-syntax-operators"> or is one of the
922
+ tokens <token>AND</token>, <token>OR</token>, and
923
+ <token>NOT</token>. Which particular operators exist and whether
924
+ they are unary or binary depends on what operators have been
925
+ defined by the system or the user. <xref linkend="functions">
926
+ describes the built-in operators.
927
+ </para>
928
+ </sect2>
929
+
926
930
<sect2>
927
931
<title>Function Calls</title>
928
932
@@ -973,7 +977,7 @@ sqrt(2)
973
977
974
978
where <replaceable>aggregate_name</replaceable> is a previously
975
979
defined aggregate, and <replaceable>expression</replaceable> is
976
- any expression that does not itself contain an aggregate
980
+ anyvalue expression that does not itself contain an aggregate
977
981
expression.
978
982
</para>
979
983
@@ -1006,6 +1010,90 @@ sqrt(2)
1006
1010
</para>
1007
1011
</sect2>
1008
1012
1013
+ <sect2 id="sql-syntax-type-casts">
1014
+ <title>Type Casts</title>
1015
+
1016
+ <indexterm>
1017
+ <primary>data types</primary>
1018
+ <secondary>type casts</secondary>
1019
+ </indexterm>
1020
+
1021
+ <para>
1022
+ A type cast specifies a conversion from one datatype to another.
1023
+ <productname>PostgreSQL</productname> accepts two equivalent syntaxes
1024
+ for type casts:
1025
+ <synopsis>
1026
+ CAST ( <replaceable>expression</replaceable> AS <replaceable>type</replaceable> )
1027
+ <replaceable>expression</replaceable>::<replaceable>type</replaceable>
1028
+ </synopsis>
1029
+ The <literal>CAST</> syntax conforms to SQL92; the syntax with
1030
+ <literal>::</literal> is historical <productname>PostgreSQL</productname>
1031
+ usage.
1032
+ </para>
1033
+
1034
+ <para>
1035
+ When a cast is applied to a value expression of a known type, it
1036
+ represents a run-time type conversion. The cast will succeed only
1037
+ if a suitable type conversion function is available. Notice that this
1038
+ is subtly different from the use of casts with constants, as shown in
1039
+ <xref linkend="sql-syntax-constants-generic">. A cast applied to an
1040
+ unadorned string literal represents the initial assignment of a type
1041
+ to a literal constant value, and so it will succeed for any type
1042
+ (if the string literal's contents are acceptable input syntax for the
1043
+ datatype).
1044
+ </para>
1045
+
1046
+ <para>
1047
+ An explicit type cast may be omitted if there is no ambiguity as to the
1048
+ type that a value expression must produce (for example, when it is
1049
+ assigned to a table column); the system will automatically apply a
1050
+ type cast in such cases.
1051
+ </para>
1052
+
1053
+ <para>
1054
+ It is also possible to specify a type cast using a function-like
1055
+ syntax:
1056
+ <synopsis>
1057
+ <replaceable>typename</replaceable> ( <replaceable>expression</replaceable> )
1058
+ </synopsis>
1059
+ However, this only works for types whose names are also valid as
1060
+ function names. For example, <literal>double precision</literal>
1061
+ can't be used this way, but the equivalent <literal>float8</literal>
1062
+ can. Also, the names <literal>interval</>, <literal>time</>, and
1063
+ <literal>timestamp</> can only be used in this fashion if they are
1064
+ double-quoted, because of parser conflicts. Therefore, the use of
1065
+ the function-like cast syntax leads to inconsistencies and should
1066
+ probably be avoided in new applications.
1067
+ </para>
1068
+ </sect2>
1069
+
1070
+ <sect2>
1071
+ <title>Scalar Subqueries</title>
1072
+
1073
+ <para>
1074
+ A scalar subquery is an ordinary
1075
+ <command>SELECT</command> in parentheses that returns exactly one
1076
+ row with one column. The <command>SELECT</command> query is executed
1077
+ and the single returned value is used in the surrounding value expression.
1078
+ It is an error to use a query that
1079
+ returns more than one row or more than one column as a scalar subquery.
1080
+ (But if, during a particular execution, the subquery returns no rows,
1081
+ there is no error; the scalar result is taken to be NULL.)
1082
+ The subquery can refer to variables from the surrounding query,
1083
+ which will act as constants during any one evaluation of the subquery.
1084
+ See also <xref linkend="functions-subquery">.
1085
+ </para>
1086
+
1087
+ <para>
1088
+ For example, the following finds the largest city population in each
1089
+ state:
1090
+ <programlisting>
1091
+ SELECT name, (SELECT max(pop) FROM cities WHERE cities.state = states.name)
1092
+ FROM states;
1093
+ </programlisting>
1094
+ </para>
1095
+ </sect2>
1096
+
1009
1097
</sect1>
1010
1098
1011
1099