1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Chapter4.SQL Syntax</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="sql.html" title="PartII.The SQL Language">
<link rel="prev" href="sql.html" title="PartII.The SQL Language">
<link rel="next" href="sql-expressions.html" title="4.2.Value Expressions">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="chapter" lang="en" id="sql-syntax">
<div class="titlepage"><div><div><h2 class="title">
<a name="sql-syntax"></a>Chapter4.SQL Syntax</h2></div></div></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<dl>
<dt><span class="sect1"><a href="sql-syntax.html#sql-syntax-lexical">4.1. Lexical Structure</a></span></dt>
<dd><dl>
<dt><span class="sect2"><a href="sql-syntax.html#sql-syntax-identifiers">4.1.1. Identifiers and Key Words</a></span></dt>
<dt><span class="sect2"><a href="sql-syntax.html#sql-syntax-constants">4.1.2. Constants</a></span></dt>
<dt><span class="sect2"><a href="sql-syntax.html#sql-syntax-operators">4.1.3. Operators</a></span></dt>
<dt><span class="sect2"><a href="sql-syntax.html#id572121">4.1.4. Special Characters</a></span></dt>
<dt><span class="sect2"><a href="sql-syntax.html#sql-syntax-comments">4.1.5. Comments</a></span></dt>
<dt><span class="sect2"><a href="sql-syntax.html#sql-precedence">4.1.6. Lexical Precedence</a></span></dt>
</dl></dd>
<dt><span class="sect1"><a href="sql-expressions.html">4.2. Value Expressions</a></span></dt>
<dd><dl>
<dt><span class="sect2"><a href="sql-expressions.html#id572958">4.2.1. Column References</a></span></dt>
<dt><span class="sect2"><a href="sql-expressions.html#id573036">4.2.2. Positional Parameters</a></span></dt>
<dt><span class="sect2"><a href="sql-expressions.html#id573100">4.2.3. Subscripts</a></span></dt>
<dt><span class="sect2"><a href="sql-expressions.html#id573192">4.2.4. Field Selection</a></span></dt>
<dt><span class="sect2"><a href="sql-expressions.html#id573238">4.2.5. Operator Invocations</a></span></dt>
<dt><span class="sect2"><a href="sql-expressions.html#id573353">4.2.6. Function Calls</a></span></dt>
<dt><span class="sect2"><a href="sql-expressions.html#syntax-aggregates">4.2.7. Aggregate Expressions</a></span></dt>
<dt><span class="sect2"><a href="sql-expressions.html#sql-syntax-type-casts">4.2.8. Type Casts</a></span></dt>
<dt><span class="sect2"><a href="sql-expressions.html#sql-syntax-scalar-subqueries">4.2.9. Scalar Subqueries</a></span></dt>
<dt><span class="sect2"><a href="sql-expressions.html#sql-syntax-array-constructors">4.2.10. Array Constructors</a></span></dt>
<dt><span class="sect2"><a href="sql-expressions.html#sql-syntax-row-constructors">4.2.11. Row Constructors</a></span></dt>
<dt><span class="sect2"><a href="sql-expressions.html#syntax-express-eval">4.2.12. Expression Evaluation Rules</a></span></dt>
</dl></dd>
</dl>
</div>
<a name="id570532"></a><p> This chapter describes the syntax of SQL. It forms the foundation
for understanding the following chapters which will go into detail
about how the SQL commands are applied to define and modify data.
</p>
<p> We also advise users who are already familiar with SQL to read this
chapter carefully because there are several rules and concepts that
are implemented inconsistently among SQL databases or that are
specific to <span class="productname">PostgreSQL</span>.
</p>
<div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="sql-syntax-lexical"></a>4.1.Lexical Structure</h2></div></div></div>
<a name="id570572"></a><p> SQL input consists of a sequence of
<em class="firstterm">commands</em>. A command is composed of a
sequence of <em class="firstterm">tokens</em>, terminated by a
semicolon (“<span class="quote">;</span>”). The end of the input stream also
terminates a command. Which tokens are valid depends on the syntax
of the particular command.
</p>
<p> A token can be a <em class="firstterm">key word</em>, an
<em class="firstterm">identifier</em>, a <em class="firstterm">quoted
identifier</em>, a <em class="firstterm">literal</em> (or
constant), or a special character symbol. Tokens are normally
separated by whitespace (space, tab, newline), but need not be if
there is no ambiguity (which is generally only the case if a
special character is adjacent to some other token type).
</p>
<p> Additionally, <em class="firstterm">comments</em> can occur in SQL
input. They are not tokens, they are effectively equivalent to
whitespace.
</p>
<p> For example, the following is (syntactically) valid SQL input:
</p>
<pre class="programlisting">SELECT * FROM MY_TABLE;
UPDATE MY_TABLE SET A = 5;
INSERT INTO MY_TABLE VALUES (3, 'hi there');</pre>
<p>
This is a sequence of three commands, one per line (although this
is not required; more than one command can be on a line, and
commands can usefully be split across lines).
</p>
<p> The SQL syntax is not very consistent regarding what tokens
identify commands and which are operands or parameters. The first
few tokens are generally the command name, so in the above example
we would usually speak of a “<span class="quote">SELECT</span>”, an
“<span class="quote">UPDATE</span>”, and an “<span class="quote">INSERT</span>” command. But
for instance the <code class="command">UPDATE</code> command always requires
a <code class="token">SET</code> token to appear in a certain position, and
this particular variation of <code class="command">INSERT</code> also
requires a <code class="token">VALUES</code> in order to be complete. The
precise syntax rules for each command are described in <a href="reference.html" title="PartVI.Reference">PartVI, “Reference”</a>.
</p>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="sql-syntax-identifiers"></a>4.1.1.Identifiers and Key Words</h3></div></div></div>
<a name="id570706"></a><a name="id570720"></a><a name="id570733"></a><p> Tokens such as <code class="token">SELECT</code>, <code class="token">UPDATE</code>, or
<code class="token">VALUES</code> in the example above are examples of
<em class="firstterm">key words</em>, that is, words that have a fixed
meaning in the SQL language. The tokens <code class="token">MY_TABLE</code>
and <code class="token">A</code> are examples of
<em class="firstterm">identifiers</em>. They identify names of
tables, columns, or other database objects, depending on the
command they are used in. Therefore they are sometimes simply
called “<span class="quote">names</span>”. Key words and identifiers have the
same lexical structure, meaning that one cannot know whether a
token is an identifier or a key word without knowing the language.
A complete list of key words can be found in <a href="sql-keywords-appendix.html" title="AppendixC.SQL Key Words">AppendixC, <i><acronym class="acronym">SQL</acronym> Key Words</i></a>.
</p>
<p> SQL identifiers and key words must begin with a letter
(<code class="literal">a</code>-<code class="literal">z</code>, but also letters with
diacritical marks and non-Latin letters) or an underscore
(<code class="literal">_</code>). Subsequent characters in an identifier or
key word can be letters, underscores, digits
(<code class="literal">0</code>-<code class="literal">9</code>), or dollar signs
(<code class="literal">$</code>). Note that dollar signs are not allowed in identifiers
according to the letter of the SQL standard, so their use may render
applications less portable.
The SQL standard will not define a key word that contains
digits or starts or ends with an underscore, so identifiers of this
form are safe against possible conflict with future extensions of the
standard.
</p>
<p> <a name="id570853"></a>
The system uses no more than <code class="symbol">NAMEDATALEN</code>-1
characters of an identifier; longer names can be written in
commands, but they will be truncated. By default,
<code class="symbol">NAMEDATALEN</code> is 64 so the maximum identifier
length is 63. If this limit is problematic, it can be raised by
changing the <code class="symbol">NAMEDATALEN</code> constant in
<code class="filename">src/include/postgres_ext.h</code>.
</p>
<p> <a name="id570890"></a>
Identifier and key word names are case insensitive. Therefore
</p>
<pre class="programlisting">UPDATE MY_TABLE SET A = 5;</pre>
<p>
can equivalently be written as
</p>
<pre class="programlisting">uPDaTE my_TabLE SeT a = 5;</pre>
<p>
A convention often used is to write key words in upper
case and names in lower case, e.g.,
</p>
<pre class="programlisting">UPDATE my_table SET a = 5;</pre>
<p>
</p>
<p> <a name="id570927"></a>
There is a second kind of identifier: the <em class="firstterm">delimited
identifier</em> or <em class="firstterm">quoted
identifier</em>. It is formed by enclosing an arbitrary
sequence of characters in double-quotes
(<code class="literal">"</code>). A delimited
identifier is always an identifier, never a key word. So
<code class="literal">"select"</code> could be used to refer to a column or
table named “<span class="quote">select</span>”, whereas an unquoted
<code class="literal">select</code> would be taken as a key word and
would therefore provoke a parse error when used where a table or
column name is expected. The example can be written with quoted
identifiers like this:
</p>
<pre class="programlisting">UPDATE "my_table" SET "a" = 5;</pre>
<p>
</p>
<p> Quoted identifiers can contain any character other than a double
quote itself. (To include a double quote, write two double quotes.)
This allows constructing table or column names that would
otherwise not be possible, such as ones containing spaces or
ampersands. The length limitation still applies.
</p>
<p> Quoting an identifier also makes it case-sensitive, whereas
unquoted names are always folded to lower case. For example, the
identifiers <code class="literal">FOO</code>, <code class="literal">foo</code>, and
<code class="literal">"foo"</code> are considered the same by
<span class="productname">PostgreSQL</span>, but
<code class="literal">"Foo"</code> and <code class="literal">"FOO"</code> are
different from these three and each other. (The folding of
unquoted names to lower case in <span class="productname">PostgreSQL</span> is
incompatible with the SQL standard, which says that unquoted names
should be folded to upper case. Thus, <code class="literal">foo</code>
should be equivalent to <code class="literal">"FOO"</code> not
<code class="literal">"foo"</code> according to the standard. If you want
to write portable applications you are advised to always quote a
particular name or never quote it.)
</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="sql-syntax-constants"></a>4.1.2.Constants</h3></div></div></div>
<a name="id571080"></a><p> There are three kinds of <em class="firstterm">implicitly-typed
constants</em> in <span class="productname">PostgreSQL</span>:
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.
</p>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="sql-syntax-strings"></a>4.1.2.1.String Constants</h4></div></div></div>
<a name="id571118"></a><p> <a name="id571135"></a>
A string constant in SQL is an arbitrary sequence of characters
bounded by single quotes (<code class="literal">'</code>), for example
<code class="literal">'This is a string'</code>. The standard-compliant way of
writing a single-quote character within a string constant is to
write two adjacent single quotes, e.g.
<code class="literal">'Dianne''s horse'</code>.
<span class="productname">PostgreSQL</span> also allows single quotes
to be escaped with a backslash (<code class="literal">\'</code>). However,
future versions of <span class="productname">PostgreSQL</span> will not
allow this, so applications using backslashes should convert to the
standard-compliant method outlined above.
</p>
<p> Another <span class="productname">PostgreSQL</span> extension is that
C-style backslash escapes are available: <code class="literal">\b</code> is a
backspace, <code class="literal">\f</code> is a form feed,
<code class="literal">\n</code> is a newline, <code class="literal">\r</code> is a
carriage return, <code class="literal">\t</code> is a tab. Also supported is
<code class="literal">\<em class="replaceable"><code>digits</code></em></code>, where
<em class="replaceable"><code>digits</code></em> represents an octal byte value, and
<code class="literal">\x<em class="replaceable"><code>hexdigits</code></em></code>, where
<em class="replaceable"><code>hexdigits</code></em> represents a hexadecimal byte value.
(It is your responsibility that the byte sequences you create are
valid characters in the server character set encoding.) Any other
character following a backslash is taken literally. Thus, to
include a backslash in a string constant, write two backslashes.
</p>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
<h3 class="title">Note</h3>
<p> While ordinary strings now support C-style backslash escapes,
future versions will generate warnings for such usage and
eventually treat backslashes as literal characters to be
standard-conforming. The proper way to specify escape processing is
to use the escape string syntax to indicate that escape
processing is desired. Escape string syntax is specified by writing
the letter <code class="literal">E</code> (upper or lower case) just before
the string, e.g. <code class="literal">E'\041'</code>. This method will work in all
future versions of <span class="productname">PostgreSQL</span>.
</p>
</div>
<p> The character with the code zero cannot be in a string constant.
</p>
<p> Two string constants that are only separated by whitespace
<span class="emphasis"><em>with at least one newline</em></span> are concatenated
and effectively treated as if the string had been written in one
constant. For example:
</p>
<pre class="programlisting">SELECT 'foo'
'bar';</pre>
<p>
is equivalent to
</p>
<pre class="programlisting">SELECT 'foobar';</pre>
<p>
but
</p>
<pre class="programlisting">SELECT 'foo' 'bar';</pre>
<p>
is not valid syntax. (This slightly bizarre behavior is specified
by <acronym class="acronym">SQL</acronym>; <span class="productname">PostgreSQL</span> is
following the standard.)
</p>
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="sql-syntax-dollar-quoting"></a>4.1.2.2.Dollar-Quoted String Constants</h4></div></div></div>
<a name="id571351"></a><p> 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,
<span class="productname">PostgreSQL</span> provides another way, called
“<span class="quote">dollar quoting</span>”, to write string constants.
A dollar-quoted string constant
consists of a dollar sign (<code class="literal">$</code>), an optional
“<span class="quote">tag</span>” 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 “<span class="quote">Dianne's horse</span>”
using dollar quoting:
</p>
<pre class="programlisting">$$Dianne's horse$$
$SomeTag$Dianne's horse$SomeTag$</pre>
<p>
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.
</p>
<p> 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:
</p>
<pre class="programlisting">$function$
BEGIN
RETURN ($1 ~ $q$[\t\r\n\v\\]$q$);
END;
$function$</pre>
<p>
Here, the sequence <code class="literal">$q$[\t\r\n\v\\]$q$</code> represents a
dollar-quoted literal string <code class="literal">[\t\r\n\v\\]</code>, which will
be recognized when the function body is executed by
<span class="productname">PostgreSQL</span>. But since the sequence does not match
the outer dollar quoting delimiter <code class="literal">$function$</code>, it is
just some more characters within the constant so far as the outer
string is concerned.
</p>
<p> 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 <code class="literal">$tag$String content$tag$</code>
is correct, but <code class="literal">$TAG$String content$tag$</code> is not.
</p>
<p> 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.
</p>
<p> 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.
</p>
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="sql-syntax-bit-strings"></a>4.1.2.3.Bit-String Constants</h4></div></div></div>
<a name="id571505"></a><p> Bit-string constants look like regular string constants with a
<code class="literal">B</code> (upper or lower case) immediately before the
opening quote (no intervening whitespace), e.g.,
<code class="literal">B'1001'</code>. The only characters allowed within
bit-string constants are <code class="literal">0</code> and
<code class="literal">1</code>.
</p>
<p> Alternatively, bit-string constants can be specified in hexadecimal
notation, using a leading <code class="literal">X</code> (upper or lower case),
e.g., <code class="literal">X'1FF'</code>. This notation is equivalent to
a bit-string constant with four binary digits for each hexadecimal digit.
</p>
<p> 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.
</p>
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="id571577"></a>4.1.2.4.Numeric Constants</h4></div></div></div>
<a name="id571581"></a><p> Numeric constants are accepted in these general forms:
</p>
<pre class="synopsis"><em class="replaceable"><code>digits</code></em>
<em class="replaceable"><code>digits</code></em>.[<span class="optional"><em class="replaceable"><code>digits</code></em></span>][<span class="optional">e[<span class="optional">+-</span>]<em class="replaceable"><code>digits</code></em></span>]
[<span class="optional"><em class="replaceable"><code>digits</code></em></span>].<em class="replaceable"><code>digits</code></em>[<span class="optional">e[<span class="optional">+-</span>]<em class="replaceable"><code>digits</code></em></span>]
<em class="replaceable"><code>digits</code></em>e[<span class="optional">+-</span>]<em class="replaceable"><code>digits</code></em></pre>
<p>
where <em class="replaceable"><code>digits</code></em> 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 (<code class="literal">e</code>), if one is present.
There may not 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.
</p>
<p> These are some examples of valid numeric constants:
</p>
<div class="literallayout"><p>42<br>
3.5<br>
4.<br>
.001<br>
5e2<br>
1.925e-3</p></div>
<p>
</p>
<p> <a name="id571678"></a>
<a name="id571685"></a>
<a name="id571692"></a>
A numeric constant that contains neither a decimal point nor an
exponent is initially presumed to be type <code class="type">integer</code> if its
value fits in type <code class="type">integer</code> (32 bits); otherwise it is
presumed to be type <code class="type">bigint</code> if its
value fits in type <code class="type">bigint</code> (64 bits); otherwise it is
taken to be type <code class="type">numeric</code>. Constants that contain decimal
points and/or exponents are always initially presumed to be type
<code class="type">numeric</code>.
</p>
<p> 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.<a name="id571740"></a>
For example, you can force a numeric value to be treated as type
<code class="type">real</code> (<code class="type">float4</code>) by writing
</p>
<pre class="programlisting">REAL '1.23' -- string style
1.23::REAL -- PostgreSQL (historical) style</pre>
<p>
These are actually just special cases of the general casting
notations discussed next.
</p>
</div>
<div class="sect3" lang="en">
<div class="titlepage"><div><div><h4 class="title">
<a name="sql-syntax-constants-generic"></a>4.1.2.5.Constants of Other Types</h4></div></div></div>
<a name="id571775"></a><p> A constant of an <span class="emphasis"><em>arbitrary</em></span> type can be
entered using any one of the following notations:
</p>
<pre class="synopsis"><em class="replaceable"><code>type</code></em> '<em class="replaceable"><code>string</code></em>'
'<em class="replaceable"><code>string</code></em>'::<em class="replaceable"><code>type</code></em>
CAST ( '<em class="replaceable"><code>string</code></em>' AS <em class="replaceable"><code>type</code></em> )</pre>
<p>
The string constant's text is passed to the input conversion
routine for the type called <em class="replaceable"><code>type</code></em>. The
result is a constant of the indicated type. The explicit type
cast may 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.
</p>
<p> The string constant can be written using either regular SQL
notation or dollar-quoting.
</p>
<p> It is also possible to specify a type coercion using a function-like
syntax:
</p>
<pre class="synopsis"><em class="replaceable"><code>typename</code></em> ( '<em class="replaceable"><code>string</code></em>' )</pre>
<p>
but not all type names may be used in this way; see <a href="sql-expressions.html#sql-syntax-type-casts" title="4.2.8.Type Casts">Section4.2.8, “Type Casts”</a> for details.
</p>
<p> The <code class="literal">::</code>, <code class="literal">CAST()</code>, and
function-call syntaxes can also be used to specify run-time type
conversions of arbitrary expressions, as discussed in <a href="sql-expressions.html#sql-syntax-type-casts" title="4.2.8.Type Casts">Section4.2.8, “Type Casts”</a>. But the form
<code class="literal"><em class="replaceable"><code>type</code></em> '<em class="replaceable"><code>string</code></em>'</code>
can only be used to specify the type of a literal constant.
Another restriction on
<code class="literal"><em class="replaceable"><code>type</code></em> '<em class="replaceable"><code>string</code></em>'</code>
is that it does not work for array types; use <code class="literal">::</code>
or <code class="literal">CAST()</code> to specify the type of an array constant.
</p>
<p> The <code class="literal">CAST()</code> syntax conforms to SQL. The
<code class="literal"><em class="replaceable"><code>type</code></em> '<em class="replaceable"><code>string</code></em>'</code>
syntax is a generalization of the standard: SQL specifies this syntax only
for a few data types, but <span class="productname">PostgreSQL</span> allows it
for all types. The syntax with
<code class="literal">::</code> is historical <span class="productname">PostgreSQL</span>
usage, as is the function-call syntax.
</p>
</div>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="sql-syntax-operators"></a>4.1.3.Operators</h3></div></div></div>
<a name="id571984"></a><p> An operator name is a sequence of up to <code class="symbol">NAMEDATALEN</code>-1
(63 by default) characters from the following list:
</p>
<div class="literallayout"><p>+-*/<>=~!@#%^&|`?</p></div>
<p>
There are a few restrictions on operator names, however:
</p>
<div class="itemizedlist"><ul type="disc">
<li><p> <code class="literal">--</code> and <code class="literal">/*</code> cannot appear
anywhere in an operator name, since they will be taken as the
start of a comment.
</p></li>
<li>
<p> A multiple-character operator name cannot end in <code class="literal">+</code> or <code class="literal">-</code>,
unless the name also contains at least one of these characters:
</p>
<div class="literallayout"><p>~!@#%^&|`?</p></div>
<p>
For example, <code class="literal">@-</code> is an allowed operator name,
but <code class="literal">*-</code> is not. This restriction allows
<span class="productname">PostgreSQL</span> to parse SQL-compliant
queries without requiring spaces between tokens.
</p>
</li>
</ul></div>
<p>
</p>
<p> When working with non-SQL-standard operator names, you will usually
need to separate adjacent operators with spaces to avoid ambiguity.
For example, if you have defined a left unary operator named <code class="literal">@</code>,
you cannot write <code class="literal">X*@Y</code>; you must write
<code class="literal">X* @Y</code> to ensure that
<span class="productname">PostgreSQL</span> reads it as two operator names
not one.
</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="id572121"></a>4.1.4.Special Characters</h3></div></div></div>
<p> Some characters that are not alphanumeric have a special meaning
that is different from being an operator. Details on the usage can
be found at the location where the respective syntax element is
described. This section only exists to advise the existence and
summarize the purposes of these characters.
</p>
<div class="itemizedlist"><ul type="disc">
<li><p> A dollar sign (<code class="literal">$</code>) followed by digits is used
to represent a positional parameter in the body of a function
definition or a prepared statement. In other contexts the
dollar sign may be part of an identifier or a dollar-quoted string
constant.
</p></li>
<li><p> Parentheses (<code class="literal">()</code>) have their usual meaning to
group expressions and enforce precedence. In some cases
parentheses are required as part of the fixed syntax of a
particular SQL command.
</p></li>
<li><p> Brackets (<code class="literal">[]</code>) are used to select the elements
of an array. See <a href="arrays.html" title="8.10.Arrays">Section8.10, “Arrays”</a> for more information
on arrays.
</p></li>
<li><p> Commas (<code class="literal">,</code>) are used in some syntactical
constructs to separate the elements of a list.
</p></li>
<li><p> The semicolon (<code class="literal">;</code>) terminates an SQL command.
It cannot appear anywhere within a command, except within a
string constant or quoted identifier.
</p></li>
<li><p> The colon (<code class="literal">:</code>) is used to select
“<span class="quote">slices</span>” from arrays. (See <a href="arrays.html" title="8.10.Arrays">Section8.10, “Arrays”</a>.) In certain SQL dialects (such as Embedded
SQL), the colon is used to prefix variable names.
</p></li>
<li><p> The asterisk (<code class="literal">*</code>) is used in some contexts to denote
all the fields of a table row or composite value. It also
has a special meaning when used as the argument of the
<code class="function">COUNT</code> aggregate function.
</p></li>
<li><p> The period (<code class="literal">.</code>) is used in numeric
constants, and to separate schema, table, and column names.
</p></li>
</ul></div>
<p>
</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="sql-syntax-comments"></a>4.1.5.Comments</h3></div></div></div>
<a name="id572268"></a><p> A comment is an arbitrary sequence of characters beginning with
double dashes and extending to the end of the line, e.g.:
</p>
<pre class="programlisting">-- This is a standard SQL comment</pre>
<p>
</p>
<p> Alternatively, C-style block comments can be used:
</p>
<pre class="programlisting">/* multiline comment
* with nesting: /* nested block comment */
*/</pre>
<p>
where the comment begins with <code class="literal">/*</code> and extends to
the matching occurrence of <code class="literal">*/</code>. These block
comments nest, as specified in the SQL standard but unlike C, so that one can
comment out larger blocks of code that may contain existing block
comments.
</p>
<p> A comment is removed from the input stream before further syntax
analysis and is effectively replaced by whitespace.
</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage"><div><div><h3 class="title">
<a name="sql-precedence"></a>4.1.6.Lexical Precedence</h3></div></div></div>
<a name="id572336"></a><p> <a href="sql-syntax.html#sql-precedence-table" title="Table4.1.Operator Precedence (decreasing)">Table4.1, “Operator Precedence (decreasing)”</a> shows the precedence and
associativity of the operators in <span class="productname">PostgreSQL</span>.
Most operators have the same precedence and are left-associative.
The precedence and associativity of the operators is hard-wired
into the parser. This may lead to non-intuitive behavior; for
example the Boolean operators <code class="literal"><</code> and
<code class="literal">></code> have a different precedence than the Boolean
operators <code class="literal"><=</code> and <code class="literal">>=</code>. Also, you will
sometimes need to add parentheses when using combinations of
binary and unary operators. For instance
</p>
<pre class="programlisting">SELECT 5 ! - 6;</pre>
<p>
will be parsed as
</p>
<pre class="programlisting">SELECT 5 ! (- 6);</pre>
<p>
because the parser has no idea [mdash ] until it is too late
[mdash ] that <code class="token">!</code> is defined as a postfix operator,
not an infix one. To get the desired behavior in this case, you
must write
</p>
<pre class="programlisting">SELECT (5 !) - 6;</pre>
<p>
This is the price one pays for extensibility.
</p>
<div class="table">
<a name="sql-precedence-table"></a><p class="title"><b>Table4.1.Operator Precedence (decreasing)</b></p>
<div class="table-contents"><table summary="Operator Precedence (decreasing)" border="1">
<colgroup>
<col>
<col>
<col>
</colgroup>
<thead><tr>
<th>Operator/Element</th>
<th>Associativity</th>
<th>Description</th>
</tr></thead>
<tbody>
<tr>
<td><code class="token">.</code></td>
<td>left</td>
<td>table/column name separator</td>
</tr>
<tr>
<td><code class="token">::</code></td>
<td>left</td>
<td>
<span class="productname">PostgreSQL</span>-style typecast</td>
</tr>
<tr>
<td>
<code class="token">[</code> <code class="token">]</code>
</td>
<td>left</td>
<td>array element selection</td>
</tr>
<tr>
<td><code class="token">-</code></td>
<td>right</td>
<td>unary minus</td>
</tr>
<tr>
<td><code class="token">^</code></td>
<td>left</td>
<td>exponentiation</td>
</tr>
<tr>
<td>
<code class="token">*</code> <code class="token">/</code> <code class="token">%</code>
</td>
<td>left</td>
<td>multiplication, division, modulo</td>
</tr>
<tr>
<td>
<code class="token">+</code> <code class="token">-</code>
</td>
<td>left</td>
<td>addition, subtraction</td>
</tr>
<tr>
<td><code class="token">IS</code></td>
<td></td>
<td>
<code class="literal">IS TRUE</code>, <code class="literal">IS FALSE</code>, <code class="literal">IS UNKNOWN</code>, <code class="literal">IS NULL</code>
</td>
</tr>
<tr>
<td><code class="token">ISNULL</code></td>
<td></td>
<td>test for null</td>
</tr>
<tr>
<td><code class="token">NOTNULL</code></td>
<td></td>
<td>test for not null</td>
</tr>
<tr>
<td>(any other)</td>
<td>left</td>
<td>all other native and user-defined operators</td>
</tr>
<tr>
<td><code class="token">IN</code></td>
<td></td>
<td>set membership</td>
</tr>
<tr>
<td><code class="token">BETWEEN</code></td>
<td></td>
<td>range containment</td>
</tr>
<tr>
<td><code class="token">OVERLAPS</code></td>
<td></td>
<td>time interval overlap</td>
</tr>
<tr>
<td>
<code class="token">LIKE</code> <code class="token">ILIKE</code> <code class="token">SIMILAR</code>
</td>
<td></td>
<td>string pattern matching</td>
</tr>
<tr>
<td>
<code class="token"><</code> <code class="token">></code>
</td>
<td></td>
<td>less than, greater than</td>
</tr>
<tr>
<td><code class="token">=</code></td>
<td>right</td>
<td>equality, assignment</td>
</tr>
<tr>
<td><code class="token">NOT</code></td>
<td>right</td>
<td>logical negation</td>
</tr>
<tr>
<td><code class="token">AND</code></td>
<td>left</td>
<td>logical conjunction</td>
</tr>
<tr>
<td><code class="token">OR</code></td>
<td>left</td>
<td>logical disjunction</td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break"><p> Note that the operator precedence rules also apply to user-defined
operators that have the same names as the built-in operators
mentioned above. For example, if you define a
“<span class="quote">+</span>” operator for some custom data type it will have
the same precedence as the built-in “<span class="quote">+</span>” operator, no
matter what yours does.
</p>
<p> When a schema-qualified operator name is used in the
<code class="literal">OPERATOR</code> syntax, as for example in
</p>
<pre class="programlisting">SELECT 3 OPERATOR(pg_catalog.+) 4;</pre>
<p>
the <code class="literal">OPERATOR</code> construct is taken to have the default precedence
shown in <a href="sql-syntax.html#sql-precedence-table" title="Table4.1.Operator Precedence (decreasing)">Table4.1, “Operator Precedence (decreasing)”</a> for “<span class="quote">any other</span>” operator. This is true no matter
which specific operator name appears inside <code class="literal">OPERATOR()</code>.
</p>
</div>
</div>
</div></body>
</html>
|