2000-03-31 05:27:42 +02:00
|
|
|
<!--
|
2000-12-17 06:47:57 +01:00
|
|
|
$Header: /cvsroot/pgsql/doc/src/sgml/syntax.sgml,v 1.28 2000/12/17 05:47:57 tgl Exp $
|
2000-03-31 05:27:42 +02:00
|
|
|
-->
|
|
|
|
|
1999-06-09 15:44:59 +02:00
|
|
|
<chapter id="syntax">
|
1999-05-12 09:32:47 +02:00
|
|
|
<title>SQL Syntax</title>
|
|
|
|
|
|
|
|
<abstract>
|
|
|
|
<para>
|
1999-07-22 17:11:05 +02:00
|
|
|
A description of the general syntax of SQL.
|
1999-05-12 09:32:47 +02:00
|
|
|
</para>
|
|
|
|
</abstract>
|
|
|
|
|
1999-07-22 17:11:05 +02:00
|
|
|
<para>
|
|
|
|
<acronym>SQL</acronym> manipulates sets of data. The language is
|
|
|
|
composed of various <firstterm>key words</firstterm>. Arithmetic
|
|
|
|
and procedural expressions are allowed. We will cover these topics
|
|
|
|
in this chapter; subsequent chapters will include details on data
|
|
|
|
types, functions, and operators.
|
|
|
|
</para>
|
|
|
|
|
2000-09-29 22:21:34 +02:00
|
|
|
<sect1 id="sql-keywords">
|
1999-05-12 09:32:47 +02:00
|
|
|
<title>Key Words</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<acronym>SQL92</acronym> defines <firstterm>key words</firstterm>
|
|
|
|
for the language
|
|
|
|
which have specific meaning. Some key words are
|
|
|
|
<firstterm>reserved</firstterm>, which indicates that they are
|
|
|
|
restricted to appear in only certain contexts. Other key words are
|
|
|
|
<firstterm>not restricted</firstterm>, which indicates that in certain
|
|
|
|
contexts they
|
|
|
|
have a specific meaning but are not otherwise constrained.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<productname>Postgres</productname> implements an extended subset of the
|
|
|
|
<acronym>SQL92</acronym> and <acronym>SQL3</acronym> languages. Some language
|
|
|
|
elements are not as restricted in this implementation as is
|
|
|
|
called for in the language standards, in part due
|
|
|
|
to the extensibility features of <productname>Postgres</productname>.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Information on <acronym>SQL92</acronym> and <acronym>SQL3</acronym> key words
|
|
|
|
is derived from <xref linkend="DATE97" endterm="DATE97">.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<sect2>
|
|
|
|
<title>Reserved Key Words</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<acronym>SQL92</acronym> and <acronym>SQL3</acronym> have
|
|
|
|
<firstterm>reserved key words</firstterm> which are not allowed
|
|
|
|
as identifiers and not allowed in any usage other than as fundamental
|
|
|
|
tokens in <acronym>SQL</acronym> statements.
|
|
|
|
<productname>Postgres</productname> has additional key words
|
|
|
|
which have similar restrictions. In particular, these key words
|
|
|
|
are not allowed as column or table names, though in some cases
|
|
|
|
they are allowed to be column labels (i.e. in AS clauses).
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<tip>
|
|
|
|
<para>
|
2000-12-17 06:47:57 +01:00
|
|
|
Any string can be used as an identifier if surrounded by
|
1999-05-12 09:32:47 +02:00
|
|
|
double quotes (<quote>like this!</quote>). Some care is required since
|
|
|
|
such an identifier will be case sensitive
|
2000-05-02 22:02:03 +02:00
|
|
|
and will retain embedded whitespace and most other special characters.
|
1999-05-12 09:32:47 +02:00
|
|
|
</para>
|
|
|
|
</tip>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The following are <productname>Postgres</productname>
|
|
|
|
reserved words which are neither <acronym>SQL92</acronym>
|
|
|
|
nor <acronym>SQL3</acronym> reserved words. These are allowed
|
|
|
|
to be present as column labels, but not as identifiers:
|
|
|
|
|
|
|
|
<programlisting>
|
1998-09-30 07:41:54 +02:00
|
|
|
ABORT ANALYZE
|
|
|
|
BINARY
|
|
|
|
CLUSTER CONSTRAINT COPY
|
|
|
|
DO
|
|
|
|
EXPLAIN EXTEND
|
|
|
|
LISTEN LOAD LOCK
|
|
|
|
MOVE
|
|
|
|
NEW NONE NOTIFY
|
2000-05-02 22:02:03 +02:00
|
|
|
OFFSET
|
1998-09-30 07:41:54 +02:00
|
|
|
RESET
|
|
|
|
SETOF SHOW
|
|
|
|
UNLISTEN UNTIL
|
|
|
|
VACUUM VERBOSE
|
1999-05-12 09:32:47 +02:00
|
|
|
</programlisting>
|
|
|
|
</para>
|
1998-09-30 07:41:54 +02:00
|
|
|
|
1999-05-12 09:32:47 +02:00
|
|
|
<para>
|
|
|
|
The following are <productname>Postgres</productname>
|
|
|
|
reserved words which are also <acronym>SQL92</acronym>
|
|
|
|
or <acronym>SQL3</acronym> reserved words, and which
|
|
|
|
are allowed to be present as column labels, but not as identifiers:
|
1998-09-30 07:41:54 +02:00
|
|
|
|
1999-05-12 09:32:47 +02:00
|
|
|
<programlisting>
|
2000-05-02 22:02:03 +02:00
|
|
|
ALL ANY ASC BETWEEN BIT BOTH
|
|
|
|
CASE CAST CHAR CHARACTER CHECK COALESCE COLLATE COLUMN
|
|
|
|
CONSTRAINT CROSS CURRENT CURRENT_DATE CURRENT_TIME
|
|
|
|
CURRENT_TIMESTAMP CURRENT_USER
|
|
|
|
DEC DECIMAL DEFAULT DESC DISTINCT
|
|
|
|
ELSE END EXCEPT EXISTS EXTRACT
|
|
|
|
FALSE FLOAT FOR FOREIGN FROM FULL
|
1999-05-12 09:32:47 +02:00
|
|
|
GLOBAL GROUP
|
2000-05-02 22:02:03 +02:00
|
|
|
HAVING
|
|
|
|
IN INNER INTERSECT INTO IS
|
|
|
|
JOIN
|
|
|
|
LEADING LEFT LIKE LOCAL
|
|
|
|
NATURAL NCHAR NOT NULL NULLIF NUMERIC
|
|
|
|
ON OR ORDER OUTER OVERLAPS
|
|
|
|
POSITION PRECISION PRIMARY PUBLIC
|
|
|
|
REFERENCES RIGHT
|
|
|
|
SELECT SESSION_USER SOME SUBSTRING
|
|
|
|
TABLE THEN TO TRANSACTION TRIM TRUE
|
|
|
|
UNION UNIQUE USER
|
|
|
|
VARCHAR
|
|
|
|
WHEN WHERE
|
1999-05-12 09:32:47 +02:00
|
|
|
</programlisting>
|
1998-09-30 07:41:54 +02:00
|
|
|
|
1999-05-12 09:32:47 +02:00
|
|
|
The following are <productname>Postgres</productname>
|
|
|
|
reserved words which are also <acronym>SQL92</acronym>
|
|
|
|
or <acronym>SQL3</acronym> reserved words:
|
1998-09-30 07:41:54 +02:00
|
|
|
|
1999-05-12 09:32:47 +02:00
|
|
|
<programlisting>
|
2000-05-02 22:02:03 +02:00
|
|
|
ADD ALTER AND AS
|
|
|
|
BEGIN BY
|
|
|
|
CASCADE CLOSE COMMIT CREATE CURSOR
|
1999-10-12 15:58:53 +02:00
|
|
|
DECLARE DEFAULT DELETE DESC DISTINCT DROP
|
1998-12-18 17:13:45 +01:00
|
|
|
EXECUTE EXISTS EXTRACT
|
1998-09-30 07:41:54 +02:00
|
|
|
FETCH FLOAT FOR FROM FULL
|
|
|
|
GRANT
|
|
|
|
HAVING
|
2000-07-14 17:26:21 +02:00
|
|
|
IN INNER INSERT INTERVAL INTO INOUT IS
|
1998-09-30 07:41:54 +02:00
|
|
|
JOIN
|
|
|
|
LEADING LEFT LIKE LOCAL
|
1999-10-12 15:58:53 +02:00
|
|
|
NAMES NATIONAL NATURAL NCHAR NO NOT NULL
|
2000-07-14 17:26:21 +02:00
|
|
|
ON OR OUT OUTER
|
1998-09-30 07:41:54 +02:00
|
|
|
PARTIAL PRIMARY PRIVILEGES PROCEDURE PUBLIC
|
|
|
|
REFERENCES REVOKE RIGHT ROLLBACK
|
2000-07-14 17:26:21 +02:00
|
|
|
SELECT SESSION SET SUBSTRING
|
1998-10-14 18:28:53 +02:00
|
|
|
TO TRAILING TRIM
|
1999-10-12 15:58:53 +02:00
|
|
|
UNION UNIQUE UPDATE USING
|
1998-09-30 07:41:54 +02:00
|
|
|
VALUES VARCHAR VARYING VIEW
|
2000-07-14 17:26:21 +02:00
|
|
|
WHERE WITH WITHOUT WORK
|
1999-05-12 09:32:47 +02:00
|
|
|
</programlisting>
|
|
|
|
</para>
|
1998-09-30 07:41:54 +02:00
|
|
|
|
1999-05-12 09:32:47 +02:00
|
|
|
<para>
|
|
|
|
The following are <acronym>SQL92</acronym> reserved key words which
|
|
|
|
are not <productname>Postgres</productname> reserved key words, but which
|
|
|
|
if used as function names are always translated into the function
|
2000-05-02 22:02:03 +02:00
|
|
|
<function>CHAR_LENGTH</function>:
|
1998-09-30 07:41:54 +02:00
|
|
|
|
1999-05-12 09:32:47 +02:00
|
|
|
<programlisting>
|
2000-05-02 22:02:03 +02:00
|
|
|
CHARACTER_LENGTH
|
1999-05-12 09:32:47 +02:00
|
|
|
</programlisting>
|
|
|
|
</para>
|
1998-09-30 07:41:54 +02:00
|
|
|
|
1999-05-12 09:32:47 +02:00
|
|
|
<para>
|
|
|
|
The following are <acronym>SQL92</acronym> or <acronym>SQL3</acronym>
|
|
|
|
reserved key words which
|
|
|
|
are not <productname>Postgres</productname> reserved key words, but
|
|
|
|
if used as type names are always translated into an alternate, native type:
|
1998-09-30 07:41:54 +02:00
|
|
|
|
1999-05-12 09:32:47 +02:00
|
|
|
<programlisting>
|
1998-09-30 07:41:54 +02:00
|
|
|
BOOLEAN DOUBLE FLOAT INT INTEGER INTERVAL REAL SMALLINT
|
1999-05-12 09:32:47 +02:00
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
|
2000-05-02 22:02:03 +02:00
|
|
|
<para>
|
|
|
|
The following are not keywords of any kind, but when used in the
|
|
|
|
context of a type name are translated into a native
|
|
|
|
<productname>Postgres</productname> type, and when used in the
|
|
|
|
context of a function name are translated into a native function:
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
DATETIME TIMESPAN
|
|
|
|
</programlisting>
|
|
|
|
|
|
|
|
(translated to <type>TIMESTAMP</type> and <type>INTERVAL</type>,
|
|
|
|
respectively). This feature is intended to help with
|
|
|
|
transitioning to v7.0, and will be removed in the next full
|
|
|
|
release (likely v7.1).
|
|
|
|
</para>
|
|
|
|
|
1999-05-12 09:32:47 +02:00
|
|
|
<para>
|
|
|
|
The following are either <acronym>SQL92</acronym>
|
|
|
|
or <acronym>SQL3</acronym> reserved key words
|
|
|
|
which are not key words in <productname>Postgres</productname>.
|
|
|
|
These have no proscribed usage in <productname>Postgres</productname>
|
2000-05-02 22:02:03 +02:00
|
|
|
at the time of writing (v7.0) but may become reserved key words in the
|
1999-05-12 09:32:47 +02:00
|
|
|
future:
|
|
|
|
|
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
Some of these key words represent functions in <acronym>SQL92</acronym>.
|
|
|
|
These functions are defined in <productname>Postgres</productname>,
|
|
|
|
but the parser does not consider the names to be key words and they are allowed
|
|
|
|
in other contexts.
|
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
|
|
|
|
<programlisting>
|
1998-09-30 07:41:54 +02:00
|
|
|
ALLOCATE ARE ASSERTION AT AUTHORIZATION AVG
|
2000-05-02 22:02:03 +02:00
|
|
|
BIT_LENGTH
|
|
|
|
CASCADED CATALOG CHAR_LENGTH CHARACTER_LENGTH COLLATION
|
|
|
|
CONNECT CONNECTION CONTINUE CONVERT CORRESPONDING COUNT
|
|
|
|
CURRENT_SESSION
|
1999-10-01 17:21:08 +02:00
|
|
|
DATE DEALLOCATE DEC DESCRIBE DESCRIPTOR
|
1999-09-29 07:22:25 +02:00
|
|
|
DIAGNOSTICS DISCONNECT DOMAIN
|
1999-10-12 15:58:53 +02:00
|
|
|
ESCAPE EXCEPT EXCEPTION EXEC EXTERNAL
|
1998-09-30 07:41:54 +02:00
|
|
|
FIRST FOUND
|
1999-05-12 09:32:47 +02:00
|
|
|
GET GO GOTO
|
1999-10-01 17:21:08 +02:00
|
|
|
IDENTITY INDICATOR INPUT INTERSECT
|
1999-09-29 07:22:25 +02:00
|
|
|
LAST LOWER
|
1998-09-30 07:41:54 +02:00
|
|
|
MAX MIN MODULE
|
|
|
|
OCTET_LENGTH OPEN OUTPUT OVERLAPS
|
|
|
|
PREPARE PRESERVE
|
1999-10-01 17:21:08 +02:00
|
|
|
ROWS
|
1999-10-12 15:58:53 +02:00
|
|
|
SCHEMA SECTION SESSION SIZE SOME
|
1998-12-18 17:13:45 +01:00
|
|
|
SQL SQLCODE SQLERROR SQLSTATE SUM SYSTEM_USER
|
|
|
|
TEMPORARY TRANSLATE TRANSLATION
|
1998-09-30 07:41:54 +02:00
|
|
|
UNKNOWN UPPER USAGE
|
|
|
|
VALUE
|
1998-12-18 17:13:45 +01:00
|
|
|
WHENEVER WRITE
|
1999-05-12 09:32:47 +02:00
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
</sect2>
|
|
|
|
|
|
|
|
<sect2>
|
|
|
|
<title>Non-reserved Keywords</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<acronym>SQL92</acronym> and <acronym>SQL3</acronym> have
|
|
|
|
<firstterm>non-reserved keywords</firstterm> which have
|
1999-09-01 04:37:40 +02:00
|
|
|
a prescribed meaning in the language but which are also allowed
|
1999-05-12 09:32:47 +02:00
|
|
|
as identifiers.
|
|
|
|
<productname>Postgres</productname> has additional keywords
|
|
|
|
which allow similar unrestricted usage.
|
|
|
|
In particular, these keywords
|
|
|
|
are allowed as column or table names.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The following are <productname>Postgres</productname>
|
|
|
|
non-reserved key words which are neither <acronym>SQL92</acronym>
|
|
|
|
nor <acronym>SQL3</acronym> non-reserved key words:
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
ACCESS AFTER AGGREGATE
|
1998-09-30 07:41:54 +02:00
|
|
|
BACKWARD BEFORE
|
2000-05-02 22:02:03 +02:00
|
|
|
CACHE COMMENT CREATEDB CREATEUSER CYCLE
|
1998-09-30 07:41:54 +02:00
|
|
|
DATABASE DELIMITERS
|
1999-05-12 09:32:47 +02:00
|
|
|
EACH ENCODING EXCLUSIVE
|
2000-05-02 22:02:03 +02:00
|
|
|
FORCE FORWARD FUNCTION
|
1998-09-30 07:41:54 +02:00
|
|
|
HANDLER
|
|
|
|
INCREMENT INDEX INHERITS INSENSITIVE INSTEAD ISNULL
|
|
|
|
LANCOMPILER LOCATION
|
1999-05-12 09:32:47 +02:00
|
|
|
MAXVALUE MINVALUE MODE
|
2000-05-02 22:02:03 +02:00
|
|
|
NOCREATEDB NOCREATEUSER NOTHING NOTIFY NOTNULL
|
1998-09-30 07:41:54 +02:00
|
|
|
OIDS OPERATOR
|
|
|
|
PASSWORD PROCEDURAL
|
2000-05-02 22:02:03 +02:00
|
|
|
RECIPE REINDEX RENAME RETURNS ROW RULE
|
1999-05-12 09:32:47 +02:00
|
|
|
SEQUENCE SERIAL SHARE START STATEMENT STDIN STDOUT
|
2000-05-02 22:02:03 +02:00
|
|
|
TEMP TRUSTED
|
|
|
|
UNLISTEN UNTIL
|
1998-09-30 07:41:54 +02:00
|
|
|
VALID VERSION
|
1999-05-12 09:32:47 +02:00
|
|
|
</programlisting>
|
|
|
|
</para>
|
1998-09-30 07:41:54 +02:00
|
|
|
|
1999-05-12 09:32:47 +02:00
|
|
|
<para>
|
|
|
|
The following are <productname>Postgres</productname>
|
|
|
|
non-reserved key words which are <acronym>SQL92</acronym>
|
|
|
|
or <acronym>SQL3</acronym> reserved key words:
|
1998-09-30 07:41:54 +02:00
|
|
|
|
1999-05-12 09:32:47 +02:00
|
|
|
<programlisting>
|
1998-09-30 07:41:54 +02:00
|
|
|
ABSOLUTE ACTION
|
2000-07-14 17:26:21 +02:00
|
|
|
CHARACTERISTICS CONSTRAINTS
|
1999-10-01 17:21:08 +02:00
|
|
|
DAY DEFERRABLE DEFERRED
|
1998-09-30 07:41:54 +02:00
|
|
|
HOUR
|
1999-10-01 17:21:08 +02:00
|
|
|
IMMEDIATE INITIALLY INSENSITIVE ISOLATION
|
1998-09-30 07:41:54 +02:00
|
|
|
KEY
|
1999-09-29 07:22:25 +02:00
|
|
|
LANGUAGE LEVEL
|
1998-09-30 07:41:54 +02:00
|
|
|
MATCH MINUTE MONTH
|
|
|
|
NEXT
|
|
|
|
OF ONLY OPTION
|
2000-07-14 17:26:21 +02:00
|
|
|
PATH PENDANT PRIOR PRIVILEGES
|
1999-10-01 17:21:08 +02:00
|
|
|
READ RELATIVE RESTRICT
|
2000-07-14 17:26:21 +02:00
|
|
|
SCHEMA SCROLL SECOND
|
1998-10-14 18:28:53 +02:00
|
|
|
TIME TIMESTAMP TIMEZONE_HOUR TIMEZONE_MINUTE TRIGGER
|
1998-09-30 07:41:54 +02:00
|
|
|
YEAR
|
|
|
|
ZONE
|
1999-05-12 09:32:47 +02:00
|
|
|
</programlisting>
|
|
|
|
</para>
|
1998-09-30 07:41:54 +02:00
|
|
|
|
1999-05-12 09:32:47 +02:00
|
|
|
<para>
|
|
|
|
The following are <productname>Postgres</productname>
|
|
|
|
non-reserved key words which are also either <acronym>SQL92</acronym>
|
|
|
|
or <acronym>SQL3</acronym> non-reserved key words:
|
1998-09-30 07:41:54 +02:00
|
|
|
|
1999-05-12 09:32:47 +02:00
|
|
|
<programlisting>
|
|
|
|
COMMITTED SERIALIZABLE TYPE
|
|
|
|
</programlisting>
|
|
|
|
</para>
|
1998-09-30 07:41:54 +02:00
|
|
|
|
1999-05-12 09:32:47 +02:00
|
|
|
<para>
|
|
|
|
The following are either <acronym>SQL92</acronym>
|
|
|
|
or <acronym>SQL3</acronym> non-reserved key words which are not
|
|
|
|
key words of any kind in <productname>Postgres</productname>:
|
1998-09-30 07:41:54 +02:00
|
|
|
|
1999-05-12 09:32:47 +02:00
|
|
|
<programlisting>
|
1998-09-30 07:41:54 +02:00
|
|
|
ADA
|
|
|
|
C CATALOG_NAME CHARACTER_SET_CATALOG CHARACTER_SET_NAME
|
1999-09-29 07:22:25 +02:00
|
|
|
CHARACTER_SET_SCHEMA CLASS_ORIGIN COBOL COLLATION_CATALOG
|
|
|
|
COLLATION_NAME COLLATION_SCHEMA COLUMN_NAME
|
|
|
|
COMMAND_FUNCTION CONDITION_NUMBER
|
|
|
|
CONNECTION_NAME CONSTRAINT_CATALOG CONSTRAINT_NAME
|
|
|
|
CONSTRAINT_SCHEMA CURSOR_NAME
|
1998-09-30 07:41:54 +02:00
|
|
|
DATA DATE_TIME_INTERVAL_CODE DATE_TIME_INTERVAL_PRECISION
|
1999-09-29 07:22:25 +02:00
|
|
|
DYNAMIC_FUNCTION
|
1998-09-30 07:41:54 +02:00
|
|
|
FORTRAN
|
|
|
|
LENGTH
|
|
|
|
MESSAGE_LENGTH MESSAGE_OCTET_LENGTH MORE MUMPS
|
|
|
|
NAME NULLABLE NUMBER
|
|
|
|
PAD PASCAL PLI
|
|
|
|
REPEATABLE RETURNED_LENGTH RETURNED_OCTET_LENGTH
|
1999-09-29 07:22:25 +02:00
|
|
|
RETURNED_SQLSTATE ROW_COUNT
|
|
|
|
SCALE SCHEMA_NAME SERVER_NAME SPACE SUBCLASS_ORIGIN
|
1998-09-30 07:41:54 +02:00
|
|
|
TABLE_NAME
|
|
|
|
UNCOMMITTED UNNAMED
|
1999-05-12 09:32:47 +02:00
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
</sect2>
|
|
|
|
</sect1>
|
|
|
|
|
2000-09-29 22:21:34 +02:00
|
|
|
<sect1 id="sql-comments">
|
1999-07-22 17:11:05 +02:00
|
|
|
<title>Comments</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
A <firstterm>comment</firstterm>
|
2000-03-18 19:03:12 +01:00
|
|
|
is an arbitrary sequence of characters beginning with double dashes
|
|
|
|
and extending to the end of the line, e.g.:
|
1999-07-22 17:11:05 +02:00
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
-- This is a standard SQL comment
|
|
|
|
</programlisting>
|
2000-07-14 17:26:21 +02:00
|
|
|
</para>
|
1999-07-22 17:11:05 +02:00
|
|
|
|
2000-07-14 17:26:21 +02:00
|
|
|
<para>
|
|
|
|
We also support C-style block comments, e.g.:
|
1999-07-22 17:11:05 +02:00
|
|
|
|
|
|
|
<programlisting>
|
2000-07-14 17:26:21 +02:00
|
|
|
/* multi-line comment
|
|
|
|
* with nesting: /* nested block comment */
|
1999-09-01 04:37:40 +02:00
|
|
|
*/
|
1999-07-22 17:11:05 +02:00
|
|
|
</programlisting>
|
2000-03-18 19:03:12 +01:00
|
|
|
|
2000-07-14 17:26:21 +02:00
|
|
|
where the comment begins with "<literal>/*</literal>" and extends
|
2000-12-17 06:47:57 +01:00
|
|
|
to the matching occurrence of "<literal>*/</literal>". These block
|
2000-07-14 17:26:21 +02:00
|
|
|
comments nest, as specified in SQL99, so that one can comment out
|
|
|
|
larger blocks of code which may contain existing block comments.
|
1999-07-22 17:11:05 +02:00
|
|
|
</para>
|
|
|
|
</sect1>
|
|
|
|
|
2000-09-29 22:21:34 +02:00
|
|
|
<sect1 id="sql-names">
|
1999-07-22 17:11:05 +02:00
|
|
|
<title>Names</title>
|
|
|
|
|
|
|
|
<para>
|
2000-03-31 01:42:34 +02:00
|
|
|
Names in SQL must begin with a letter
|
|
|
|
(<literal>a</literal>-<literal>z</literal>) or underscore
|
|
|
|
(<literal>_</literal>).
|
|
|
|
Subsequent characters in a name can be letters, digits
|
|
|
|
(<literal>0</literal>-<literal>9</literal>),
|
|
|
|
or underscores. The system uses no more than NAMEDATALEN-1 characters
|
|
|
|
of a name; longer names can be written in queries, but they will be
|
|
|
|
truncated.
|
|
|
|
By default, NAMEDATALEN is 32 so the maximum name length is 31 (but
|
|
|
|
at the time the system is built, NAMEDATALEN can be changed in
|
2000-12-17 06:47:57 +01:00
|
|
|
<filename>src/include/postgres_ext.h</filename>).
|
1999-09-01 04:37:40 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2000-03-18 19:03:12 +01:00
|
|
|
Names containing other characters may be formed by surrounding them
|
2000-03-31 01:42:34 +02:00
|
|
|
with double quotes (<literal>"</literal>). For example, table or column
|
|
|
|
names may contain
|
2000-03-18 19:03:12 +01:00
|
|
|
otherwise disallowed characters such as spaces, ampersands, etc. if
|
|
|
|
quoted. Quoting a name also makes it case-sensitive,
|
|
|
|
whereas unquoted names are always folded to lower case. For example,
|
|
|
|
the names <literal>FOO</literal>, <literal>foo</literal>
|
|
|
|
and <literal>"foo"</literal> are
|
|
|
|
considered the same by <productname>Postgres</productname>, but
|
|
|
|
<literal>"Foo"</literal> is a different name.
|
1999-07-22 17:11:05 +02:00
|
|
|
</para>
|
2000-03-31 01:42:34 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
Double quotes can also be used to protect a name that would otherwise
|
|
|
|
be taken to be an SQL keyword. For example, <literal>IN</literal>
|
|
|
|
is a keyword but <literal>"IN"</literal> is a name.
|
|
|
|
</para>
|
1999-07-22 17:11:05 +02:00
|
|
|
</sect1>
|
|
|
|
|
2000-09-29 22:21:34 +02:00
|
|
|
<sect1 id="sql-constants">
|
1999-07-22 17:11:05 +02:00
|
|
|
<title>Constants</title>
|
|
|
|
|
|
|
|
<para>
|
2000-12-17 06:47:57 +01:00
|
|
|
There are three kinds of <firstterm>implicitly typed constants</firstterm>
|
|
|
|
in <productname>Postgres</productname>: strings, integers,
|
1999-09-29 07:22:25 +02:00
|
|
|
and floating point numbers. Constants can
|
|
|
|
also be specified with explicit types, which can enable more
|
|
|
|
accurate representation and more efficient handling by the
|
|
|
|
backend. The implicit constants are described below; explicit
|
|
|
|
constants are discussed afterwards.
|
1999-07-22 17:11:05 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<sect2>
|
|
|
|
<title>String Constants</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<firstterm>Strings</firstterm>
|
|
|
|
in SQL are arbitrary sequences of ASCII characters bounded by single
|
1999-09-01 04:37:40 +02:00
|
|
|
quotes ("'", e.g. <literal>'This is a string'</literal>).
|
1999-07-22 17:11:05 +02:00
|
|
|
SQL92 allows single quotes to be embedded in strings by typing two
|
2000-01-25 08:04:04 +01:00
|
|
|
adjacent single quotes (e.g. <literal>'Dianne''s horse'</literal>).
|
|
|
|
In <productname>Postgres</productname> single quotes may alternatively
|
|
|
|
be escaped with a backslash ("\", e.g.
|
|
|
|
<literal>'Dianne\'s horse'</literal>). To include a
|
|
|
|
backslash in a string constant, type two backslashes.
|
|
|
|
Non-printing characters may also be embedded within strings by
|
|
|
|
prepending them with a backslash
|
|
|
|
(e.g. <literal>'\<replaceable>tab</replaceable>'</literal>).
|
1999-07-22 17:11:05 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
</sect2>
|
|
|
|
|
|
|
|
<sect2>
|
|
|
|
<title>Integer Constants</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<firstterm>Integer constants</firstterm>
|
2000-12-17 06:47:57 +01:00
|
|
|
in SQL are sequences of ASCII digits with no decimal point.
|
|
|
|
The range of legal values depends on which integer datatype is
|
|
|
|
used, but the plain <literal>integer</literal> type accepts values
|
|
|
|
ranging from -2147483648 to +2147483647.
|
1999-09-01 04:37:40 +02:00
|
|
|
</para>
|
1999-07-22 17:11:05 +02:00
|
|
|
</sect2>
|
|
|
|
|
|
|
|
<sect2>
|
|
|
|
<title>Floating Point Constants</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<firstterm>Floating point constants</firstterm>
|
|
|
|
consist of an integer part, a decimal point, and a fraction part or
|
|
|
|
scientific notation of the following format:
|
|
|
|
|
|
|
|
<synopsis>
|
|
|
|
{<replaceable>dig</replaceable>}.{<replaceable>dig</replaceable>} [e [+-] {<replaceable>dig</replaceable>}]
|
|
|
|
</synopsis>
|
|
|
|
|
|
|
|
where <replaceable>dig</replaceable> is one or more digits.
|
|
|
|
You must include at least one <replaceable>dig</replaceable> after the
|
|
|
|
period and after the [+-] if you use those options. An exponent with
|
|
|
|
a missing mantissa has a mantissa of 1 inserted. There may be no
|
1999-09-01 04:37:40 +02:00
|
|
|
extra characters embedded in the string.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Floating point constaints are of type
|
|
|
|
<type>float8</type>. <type>float4</type> can be specified
|
|
|
|
explicitly by using <acronym>SQL92</acronym> string notation or
|
|
|
|
<productname>Postgres</productname> type notation:
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
float4 '1.23' -- string style
|
|
|
|
'1.23'::float4 -- Postgres (historical) style
|
|
|
|
</programlisting>
|
1999-07-22 17:11:05 +02:00
|
|
|
</para>
|
|
|
|
</sect2>
|
|
|
|
|
|
|
|
<sect2>
|
|
|
|
<title>Constants of Postgres User-Defined Types</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
A constant of an
|
|
|
|
<emphasis>arbitrary</emphasis>
|
1999-09-29 07:22:25 +02:00
|
|
|
type can be entered using any one of the following notations:
|
1999-07-22 17:11:05 +02:00
|
|
|
|
|
|
|
<synopsis>
|
|
|
|
<replaceable>type</replaceable> '<replaceable>string</replaceable>'
|
|
|
|
'<replaceable>string</replaceable>'::<replaceable>type</replaceable>
|
2000-08-07 21:53:27 +02:00
|
|
|
CAST ( '<replaceable>string</replaceable>' AS <replaceable>type</replaceable> )
|
1999-07-22 17:11:05 +02:00
|
|
|
</synopsis>
|
|
|
|
|
|
|
|
The value inside the string is passed to the input
|
1999-09-01 04:37:40 +02:00
|
|
|
conversion routine for the type called
|
|
|
|
<replaceable>type</replaceable>. The result is a
|
1999-07-22 17:11:05 +02:00
|
|
|
constant of the indicated type. The explicit typecast may be omitted
|
|
|
|
if there is no ambiguity as to the type the constant must be, in which
|
|
|
|
case it is automatically coerced.
|
|
|
|
</para>
|
2000-12-17 06:47:57 +01:00
|
|
|
|
|
|
|
<para>
|
|
|
|
It is also possible to specify a type coercion using a function-like
|
|
|
|
syntax:
|
|
|
|
|
|
|
|
<synopsis>
|
|
|
|
<replaceable>typename</replaceable> ( <replaceable>value</replaceable> )
|
|
|
|
</synopsis>
|
|
|
|
|
|
|
|
although this only works for types whose names are also valid as
|
|
|
|
function names. (For example, <literal>double precision</literal>
|
|
|
|
can't be used this way --- but the equivalent <literal>float8</literal>
|
|
|
|
can.)
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The <literal>::</literal>, <literal>CAST()</literal>, and function-call
|
|
|
|
syntaxes can also be used to specify run-time type conversions. But
|
|
|
|
the form <replaceable>type</replaceable>
|
|
|
|
'<replaceable>string</replaceable>' can only be used to specify the
|
|
|
|
type of a literal constant.
|
|
|
|
</para>
|
1999-07-22 17:11:05 +02:00
|
|
|
</sect2>
|
|
|
|
|
|
|
|
<sect2>
|
|
|
|
<title>Array constants</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<firstterm>Array constants</firstterm>
|
2000-12-17 06:47:57 +01:00
|
|
|
are n-dimensional arrays of any Postgres datatype.
|
|
|
|
The general format of an array constant is the following:
|
1999-07-22 17:11:05 +02:00
|
|
|
|
|
|
|
<synopsis>
|
2000-12-17 06:47:57 +01:00
|
|
|
{ <replaceable>val1</replaceable> <replaceable>delim</replaceable> <replaceable>val2</replaceable> <replaceable>delim</replaceable> ... }
|
1999-07-22 17:11:05 +02:00
|
|
|
</synopsis>
|
|
|
|
|
|
|
|
where <replaceable>delim</replaceable>
|
2000-12-17 06:47:57 +01:00
|
|
|
is the delimiter character for the type, as recorded in its
|
|
|
|
<literal>pg_type</literal> class entry.
|
|
|
|
(For all built-in types, this is the comma character ",".)
|
|
|
|
Each <replaceable>val</replaceable> is either a constant
|
|
|
|
of the array element type, or a sub-array.
|
|
|
|
An example of an array constant is
|
1999-07-22 17:11:05 +02:00
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
{{1,2,3},{4,5,6},{7,8,9}}
|
|
|
|
</programlisting>
|
|
|
|
|
|
|
|
This constant is a two-dimensional, 3 by 3 array consisting of three
|
|
|
|
sub-arrays of integers.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2000-12-17 06:47:57 +01:00
|
|
|
Individual array elements can be placed between single-quote
|
|
|
|
marks to avoid ambiguity problems with respect to leading white space.
|
|
|
|
Without quote marks, the array-value parser will skip white space.
|
|
|
|
Note that to write a quote mark inside a string literal that is to
|
|
|
|
become an array value, you must double the quote mark as described
|
|
|
|
previously.
|
1999-07-22 17:11:05 +02:00
|
|
|
</para>
|
|
|
|
</sect2>
|
|
|
|
</sect1>
|
|
|
|
|
2000-09-29 22:21:34 +02:00
|
|
|
<sect1 id="sql-columns">
|
1999-07-22 17:11:05 +02:00
|
|
|
<title>Fields and Columns</title>
|
|
|
|
|
|
|
|
<sect2>
|
|
|
|
<title>Fields</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
A <firstterm>field</firstterm>
|
2000-12-17 06:47:57 +01:00
|
|
|
is either a user-defined attribute of a given class or one of the
|
|
|
|
following system-defined attributes:
|
1999-07-22 17:11:05 +02:00
|
|
|
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
|
|
|
<term>oid</term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
stands for the unique identifier of an instance which is added by
|
2000-12-17 06:47:57 +01:00
|
|
|
Postgres to all instances automatically. OIDs are not reused and are
|
|
|
|
32-bit quantities.
|
1999-07-22 17:11:05 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term>xmin</term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The identity of the inserting transaction.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term>xmax</term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The identity of the deleting transaction.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term>cmin</term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2000-12-17 06:47:57 +01:00
|
|
|
The command identifier within the inserting transaction.
|
1999-07-22 17:11:05 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term>cmax</term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2000-12-17 06:47:57 +01:00
|
|
|
The command identifier within the deleting transaction.
|
1999-07-22 17:11:05 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2000-12-17 06:47:57 +01:00
|
|
|
For further information on the system attributes consult
|
1999-07-22 17:11:05 +02:00
|
|
|
<xref linkend="STON87a" endterm="STON87a">.
|
2000-12-17 06:47:57 +01:00
|
|
|
Transaction and command identifiers are 32 bit quantities.
|
1999-07-22 17:11:05 +02:00
|
|
|
</para>
|
|
|
|
</sect2>
|
|
|
|
|
|
|
|
<sect2>
|
|
|
|
<title>Columns</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
A <firstterm>column</firstterm> is a construct of the form:
|
|
|
|
|
|
|
|
<synopsis>
|
2000-12-17 06:47:57 +01:00
|
|
|
<replaceable>instance</replaceable>{.<replaceable>composite_field</replaceable>}.<replaceable>field</replaceable> `['<replaceable>subscript</replaceable>`]'
|
1999-07-22 17:11:05 +02:00
|
|
|
</synopsis>
|
|
|
|
|
|
|
|
<replaceable>instance</replaceable>
|
|
|
|
identifies a particular class and can be thought of as standing for
|
|
|
|
the instances of that class. An instance variable is either a class
|
2000-12-17 06:47:57 +01:00
|
|
|
name, an alias for a class defined by means of a FROM clause,
|
|
|
|
or the keyword NEW or OLD.
|
|
|
|
(NEW and OLD can only appear in the action portion of a rule, while
|
|
|
|
other instance variables can be used in any SQL statement.) The
|
|
|
|
instance name can be omitted if the first field name is unique
|
|
|
|
across all the classes being used in the current query.
|
1999-07-22 17:11:05 +02:00
|
|
|
<replaceable>composite_field</replaceable>
|
|
|
|
is a field of of one of the Postgres composite types,
|
2000-12-17 06:47:57 +01:00
|
|
|
while successive composite fields select attributes in the
|
1999-07-22 17:11:05 +02:00
|
|
|
class(s) to which the composite field evaluates. Lastly,
|
|
|
|
<replaceable>field</replaceable>
|
|
|
|
is a normal (base type) field in the class(s) last addressed. If
|
|
|
|
<replaceable>field</replaceable>
|
2000-12-17 06:47:57 +01:00
|
|
|
is of an array type,
|
|
|
|
then the optional <replaceable>subscript</replaceable>
|
|
|
|
selects a specific element in the array. If no subscript is
|
|
|
|
provided, then the whole array is selected.
|
1999-07-22 17:11:05 +02:00
|
|
|
</para>
|
|
|
|
</sect2>
|
|
|
|
</sect1>
|
|
|
|
|
2000-09-29 22:21:34 +02:00
|
|
|
<sect1 id="sql-operators">
|
1999-07-22 17:11:05 +02:00
|
|
|
<title>Operators</title>
|
|
|
|
|
|
|
|
<para>
|
2000-12-17 06:47:57 +01:00
|
|
|
Any built-in or user-defined operator may be used in SQL.
|
|
|
|
For the list of built-in operators consult <xref linkend="functions">.
|
1999-07-22 17:11:05 +02:00
|
|
|
For a list of user-defined operators consult your system administrator
|
|
|
|
or run a query on the <literal>pg_operator</literal> class.
|
|
|
|
Parentheses may be used for arbitrary grouping of operators in expressions.
|
|
|
|
</para>
|
|
|
|
</sect1>
|
|
|
|
|
2000-09-29 22:21:34 +02:00
|
|
|
<sect1 id="sql-expressions">
|
1999-05-12 09:32:47 +02:00
|
|
|
<title>Expressions</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<acronym>SQL92</acronym> allows <firstterm>expressions</firstterm>
|
1999-07-22 17:11:05 +02:00
|
|
|
to transform data in tables. Expressions may contain operators
|
2000-12-14 23:30:56 +01:00
|
|
|
and functions.
|
1999-07-22 17:11:05 +02:00
|
|
|
</para>
|
1999-05-12 09:32:47 +02:00
|
|
|
|
1999-07-22 17:11:05 +02:00
|
|
|
<para>
|
|
|
|
An expression is one of the following:
|
|
|
|
|
|
|
|
<simplelist>
|
|
|
|
<member>constant</member>
|
2000-12-17 06:47:57 +01:00
|
|
|
<member>column</member>
|
|
|
|
<member><replaceable>expression</replaceable> <replaceable>binary_operator</replaceable> <replaceable>expression</replaceable></member>
|
|
|
|
<member><replaceable>expression</replaceable> <replaceable>right_unary_operator</replaceable></member>
|
|
|
|
<member><replaceable>left_unary_operator</replaceable> <replaceable>expression</replaceable></member>
|
|
|
|
<member>( <replaceable>expression</replaceable> )</member>
|
1999-07-22 17:11:05 +02:00
|
|
|
<member>parameter</member>
|
1999-12-13 18:39:38 +01:00
|
|
|
<member>functional expression</member>
|
|
|
|
<member>aggregate expression</member>
|
1999-07-22 17:11:05 +02:00
|
|
|
</simplelist>
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2000-12-17 06:47:57 +01:00
|
|
|
We have already discussed constants and columns. The three kinds of
|
1999-12-13 18:39:38 +01:00
|
|
|
operator expressions indicate respectively binary (infix), right-unary
|
|
|
|
(suffix) and left-unary (prefix) operators. The following sections
|
|
|
|
discuss the remaining options.
|
1999-05-12 09:32:47 +02:00
|
|
|
</para>
|
1999-07-22 17:11:05 +02:00
|
|
|
|
|
|
|
<sect2>
|
|
|
|
<title>Parameters</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
A <firstterm>parameter</firstterm>
|
|
|
|
is used to indicate a parameter in a SQL function. Typically this
|
2000-12-17 06:47:57 +01:00
|
|
|
is used in SQL function definition statements. The form of a
|
1999-07-22 17:11:05 +02:00
|
|
|
parameter is:
|
|
|
|
|
|
|
|
<synopsis>
|
|
|
|
$<replaceable class="parameter">number</replaceable>
|
|
|
|
</synopsis>
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
For example, consider the definition of a function,
|
|
|
|
<function>dept</function>, as
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
CREATE FUNCTION dept (name)
|
|
|
|
RETURNS dept
|
2000-12-17 06:47:57 +01:00
|
|
|
AS 'select * from dept where name = $1'
|
1999-07-22 17:11:05 +02:00
|
|
|
LANGUAGE 'sql';
|
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
</sect2>
|
|
|
|
|
|
|
|
<sect2>
|
|
|
|
<title>Functional Expressions</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
A <firstterm>functional expression</firstterm>
|
|
|
|
is the name of a legal SQL function, followed by its argument list
|
|
|
|
enclosed in parentheses:
|
|
|
|
|
|
|
|
<synopsis>
|
2000-12-17 06:47:57 +01:00
|
|
|
<replaceable>function</replaceable> (<replaceable>expression</replaceable> [, <replaceable>expression</replaceable> ... ] )
|
1999-07-22 17:11:05 +02:00
|
|
|
</synopsis>
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
For example, the following computes the square root of an employee
|
|
|
|
salary:
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
sqrt(emp.salary)
|
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
</sect2>
|
|
|
|
|
2000-12-14 23:30:56 +01:00
|
|
|
<sect2 id="syntax-aggregates">
|
|
|
|
<title>Aggregate Expressions</title>
|
1999-07-22 17:11:05 +02:00
|
|
|
|
|
|
|
<para>
|
1999-12-13 18:39:38 +01:00
|
|
|
An <firstterm>aggregate expression</firstterm> represents the application
|
|
|
|
of an aggregate function across the rows selected by a query.
|
|
|
|
An aggregate function reduces multiple inputs to a single output value,
|
|
|
|
such as the sum or average of the inputs.
|
|
|
|
The syntax of an aggregate expression is one of the following:
|
1999-07-22 17:11:05 +02:00
|
|
|
|
1999-12-13 18:39:38 +01:00
|
|
|
<simplelist>
|
|
|
|
<member><replaceable>aggregate_name</replaceable> (<replaceable>expression</replaceable>)</member>
|
2000-02-21 02:20:41 +01:00
|
|
|
<member><replaceable>aggregate_name</replaceable> (ALL <replaceable>expression</replaceable>)</member>
|
1999-12-13 18:39:38 +01:00
|
|
|
<member><replaceable>aggregate_name</replaceable> (DISTINCT <replaceable>expression</replaceable>)</member>
|
|
|
|
<member><replaceable>aggregate_name</replaceable> ( * )</member>
|
|
|
|
</simplelist>
|
|
|
|
|
|
|
|
where <replaceable>aggregate_name</replaceable> is a previously defined
|
|
|
|
aggregate, and <replaceable>expression</replaceable> is any expression
|
|
|
|
that doesn't itself contain an aggregate expression.
|
|
|
|
</para>
|
1999-07-22 17:11:05 +02:00
|
|
|
|
1999-12-13 18:39:38 +01:00
|
|
|
<para>
|
|
|
|
The first form of aggregate expression invokes the aggregate across all
|
|
|
|
input rows for which the given expression yields a non-null value.
|
2000-02-21 02:20:41 +01:00
|
|
|
The second form is the same as the first, since ALL is the default.
|
|
|
|
The third form invokes the aggregate for all distinct non-null values
|
1999-12-13 18:39:38 +01:00
|
|
|
of the expression found in the input rows. The last form invokes the
|
|
|
|
aggregate once for each input row regardless of null or non-null values;
|
|
|
|
since no particular input value is specified, it is generally only useful
|
|
|
|
for the count() aggregate.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
For example, count(*) yields the total number of input rows;
|
|
|
|
count(f1) yields the number of input rows in which f1 is non-null;
|
|
|
|
count(distinct f1) yields the number of distinct non-null values of f1.
|
1999-07-22 17:11:05 +02:00
|
|
|
</para>
|
|
|
|
</sect2>
|
|
|
|
|
|
|
|
<sect2>
|
|
|
|
<title>Target List</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
A <firstterm>target list</firstterm>
|
2000-12-16 19:22:53 +01:00
|
|
|
is a comma-separated list of one or more elements, each
|
1999-07-22 17:11:05 +02:00
|
|
|
of which must be of the form:
|
|
|
|
|
|
|
|
<synopsis>
|
2000-12-17 06:47:57 +01:00
|
|
|
<replaceable>expression</replaceable> [ AS <replaceable>result_attname</replaceable> ]
|
1999-07-22 17:11:05 +02:00
|
|
|
</synopsis>
|
|
|
|
|
|
|
|
where <replaceable>result_attname</replaceable>
|
2000-12-16 19:22:53 +01:00
|
|
|
is the name to be assigned to the created column. If
|
1999-07-22 17:11:05 +02:00
|
|
|
<replaceable>result_attname</replaceable>
|
2000-12-16 19:22:53 +01:00
|
|
|
is not present, then <productname>Postgres</productname> selects a
|
2000-12-17 06:47:57 +01:00
|
|
|
default name based on the contents of <replaceable>expression</replaceable>.
|
|
|
|
If <replaceable>expression</replaceable> is a simple attribute reference
|
2000-12-16 19:22:53 +01:00
|
|
|
then the default name will be the same as that attribute's name, but
|
|
|
|
otherwise the implementation is free to assign any default name.
|
1999-07-22 17:11:05 +02:00
|
|
|
</para>
|
|
|
|
</sect2>
|
|
|
|
|
|
|
|
<sect2>
|
|
|
|
<title>Qualification</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
A <firstterm>qualification</firstterm>
|
|
|
|
consists of any number of clauses connected by the logical operators:
|
|
|
|
|
|
|
|
<simplelist>
|
|
|
|
<member>NOT</member>
|
|
|
|
<member>AND</member>
|
|
|
|
<member>OR</member>
|
|
|
|
</simplelist>
|
|
|
|
|
2000-12-17 06:47:57 +01:00
|
|
|
A clause is an <replaceable>expression</replaceable>
|
1999-07-22 17:11:05 +02:00
|
|
|
that evaluates to a <literal>boolean</literal> over a set of instances.
|
|
|
|
</para>
|
|
|
|
</sect2>
|
|
|
|
|
|
|
|
<sect2>
|
|
|
|
<title>From List</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The <firstterm>from list</firstterm>
|
2000-12-17 06:47:57 +01:00
|
|
|
is a comma-separated list of <firstterm>from-expressions</firstterm>.
|
|
|
|
The simplest possibility for a from-expression is:
|
|
|
|
|
|
|
|
<synopsis>
|
|
|
|
<replaceable>class_reference</replaceable> [ [ AS ] <replaceable class="PARAMETER">alias</replaceable> ]
|
|
|
|
</synopsis>
|
|
|
|
|
|
|
|
where <replaceable>class_reference</replaceable> is of the form
|
1999-07-22 17:11:05 +02:00
|
|
|
|
|
|
|
<synopsis>
|
2000-12-17 06:47:57 +01:00
|
|
|
[ ONLY ] <replaceable class="PARAMETER">table_name</replaceable> [ * ]
|
1999-07-22 17:11:05 +02:00
|
|
|
</synopsis>
|
|
|
|
|
2000-12-17 06:47:57 +01:00
|
|
|
The from-expression defines an instance variable that ranges over the
|
|
|
|
rows of the specified table. The instance variable's name is either
|
|
|
|
the table name, or the <replaceable>alias</replaceable> if one is given.
|
|
|
|
Ordinarily, if the table has child tables then the instance variable
|
|
|
|
will range over all rows in the inheritance hierarchy starting with
|
|
|
|
the specified table. If <literal>ONLY</literal> is specified then
|
|
|
|
child tables are not included. A trailing asterisk <literal>*</literal>
|
|
|
|
can be written to specifically indicate that child tables are included
|
|
|
|
(<literal>ONLY</literal> and <literal>*</literal> are mutually
|
|
|
|
exclusive).
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
A from-expression can also be a sub-query:
|
|
|
|
|
|
|
|
<synopsis>
|
|
|
|
( <replaceable class="PARAMETER">select-statement</replaceable> ) [ AS ] <replaceable class="PARAMETER">alias</replaceable>
|
|
|
|
</synopsis>
|
|
|
|
|
|
|
|
Here, the effect is as though the SELECT were executed and its results
|
|
|
|
stored in a temporary table, which then becomes available as an instance
|
|
|
|
variable under the given <replaceable>alias</replaceable>.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Finally, a from-expression can be built up from simpler from-expressions
|
|
|
|
using JOIN clauses:
|
1999-07-22 17:11:05 +02:00
|
|
|
|
|
|
|
<synopsis>
|
2000-12-17 06:47:57 +01:00
|
|
|
<replaceable class="PARAMETER">from_expression</replaceable> [ NATURAL ] <replaceable class="PARAMETER">join_type</replaceable> <replaceable class="PARAMETER">from_expression</replaceable>
|
|
|
|
[ ON <replaceable class="PARAMETER">join_condition</replaceable> | USING ( <replaceable class="PARAMETER">join_column_list</replaceable> ) ]
|
1999-07-22 17:11:05 +02:00
|
|
|
</synopsis>
|
|
|
|
|
2000-12-17 06:47:57 +01:00
|
|
|
This syntax allows specification of <firstterm>outer joins</firstterm>.
|
|
|
|
For details see the reference page for SELECT.
|
1999-07-22 17:11:05 +02:00
|
|
|
</para>
|
|
|
|
</sect2>
|
2000-12-14 23:30:56 +01:00
|
|
|
|
|
|
|
|
|
|
|
<sect2 id="sql-precedence">
|
|
|
|
<title>Lexical Precedence</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The precedence and associativity of the operators is hard-wired
|
|
|
|
into the parser. Most operators have the same precedence and are
|
|
|
|
left-associative. This may lead to non-intuitive behavior; for
|
|
|
|
example the boolean operators "<" and ">" have a different
|
|
|
|
precedence than the boolean operators "<=" and ">=". Also,
|
2000-12-17 06:47:57 +01:00
|
|
|
you will sometimes need to add parentheses when using combinations
|
2000-12-14 23:30:56 +01:00
|
|
|
of binary and unary operators. For instance
|
|
|
|
<programlisting>
|
|
|
|
SELECT 5 & ~ 6;
|
|
|
|
</programlisting>
|
|
|
|
will be parsed as
|
|
|
|
<programlisting>
|
|
|
|
SELECT (5 &) ~ 6;
|
|
|
|
</programlisting>
|
|
|
|
because the parser has no idea that <token>&</token> is
|
|
|
|
defined as a binary operator. This is the price one pays for
|
|
|
|
extensibility.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<table tocentry="1">
|
|
|
|
<title>Operator Ordering (decreasing precedence)</title>
|
|
|
|
|
|
|
|
<tgroup cols="2">
|
|
|
|
<thead>
|
|
|
|
<row>
|
|
|
|
<entry>OperatorElement</entry>
|
|
|
|
<entry>Associativity</entry>
|
|
|
|
<entry>Description</entry>
|
|
|
|
</row>
|
|
|
|
</thead>
|
|
|
|
|
|
|
|
<tbody>
|
|
|
|
<row>
|
|
|
|
<entry><token>::</token></entry>
|
|
|
|
<entry>left</entry>
|
|
|
|
<entry><productname>Postgres</productname>-style typecast</entry>
|
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
|
|
|
<entry><token>[</token> <token>]</token></entry>
|
|
|
|
<entry>left</entry>
|
|
|
|
<entry>array element selection</entry>
|
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
|
|
|
<entry><token>.</token></entry>
|
|
|
|
<entry>left</entry>
|
|
|
|
<entry>table/column name separator</entry>
|
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
|
|
|
<entry><token>-</token></entry>
|
|
|
|
<entry>right</entry>
|
|
|
|
<entry>unary minus</entry>
|
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
|
|
|
<entry><token>^</token></entry>
|
|
|
|
<entry>left</entry>
|
|
|
|
<entry>exponentiation</entry>
|
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
|
|
|
<entry><token>*</token> <token>/</token> <token>%</token></entry>
|
|
|
|
<entry>left</entry>
|
|
|
|
<entry>multiplication, division, modulo</entry>
|
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
|
|
|
<entry><token>+</token> <token>-</token></entry>
|
|
|
|
<entry>left</entry>
|
|
|
|
<entry>addition, subtraction</entry>
|
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
|
|
|
<entry><token>IS</token></entry>
|
|
|
|
<entry></entry>
|
|
|
|
<entry>test for TRUE, FALSE, NULL</entry>
|
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
|
|
|
<entry><token>ISNULL</token></entry>
|
|
|
|
<entry></entry>
|
|
|
|
<entry>test for NULL</entry>
|
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
|
|
|
<entry><token>NOTNULL</token></entry>
|
|
|
|
<entry></entry>
|
|
|
|
<entry>test for NOT NULL</entry>
|
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
|
|
|
<entry>(any other)</entry>
|
|
|
|
<entry>left</entry>
|
|
|
|
<entry>all other native and user-defined operators</entry>
|
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
|
|
|
<entry><token>IN</token></entry>
|
|
|
|
<entry></entry>
|
|
|
|
<entry>set membership</entry>
|
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
|
|
|
<entry><token>BETWEEN</token></entry>
|
|
|
|
<entry></entry>
|
|
|
|
<entry>containment</entry>
|
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
|
|
|
<entry><token>OVERLAPS</token></entry>
|
|
|
|
<entry></entry>
|
|
|
|
<entry>time interval overlap</entry>
|
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
|
|
|
<entry><token>LIKE</token> <token>ILIKE</token></entry>
|
|
|
|
<entry></entry>
|
|
|
|
<entry>string pattern matching</entry>
|
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
|
|
|
<entry><token><</token> <token>></token></entry>
|
|
|
|
<entry></entry>
|
|
|
|
<entry>less than, greater than</entry>
|
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
|
|
|
<entry><token>=</token></entry>
|
|
|
|
<entry>right</entry>
|
|
|
|
<entry>equality, assignment</entry>
|
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
|
|
|
<entry><token>NOT</token></entry>
|
|
|
|
<entry>right</entry>
|
|
|
|
<entry>logical negation</entry>
|
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
|
|
|
<entry><token>AND</token></entry>
|
|
|
|
<entry>left</entry>
|
|
|
|
<entry>logical conjunction</entry>
|
|
|
|
</row>
|
|
|
|
|
|
|
|
<row>
|
|
|
|
<entry><token>OR</token></entry>
|
|
|
|
<entry>left</entry>
|
|
|
|
<entry>logical disjunction</entry>
|
|
|
|
</row>
|
|
|
|
</tbody>
|
|
|
|
</tgroup>
|
|
|
|
</table>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Note that the operator precedence rules also apply to user-defined
|
2000-12-17 06:47:57 +01:00
|
|
|
operators that have the same names as the built-in operators
|
|
|
|
mentioned above. For example, if you define a
|
2000-12-14 23:30:56 +01:00
|
|
|
<quote>+</quote> operator for some custom data type it will have
|
|
|
|
the same precedence as the built-in <quote>+</quote> operator, no
|
|
|
|
matter what yours does.
|
|
|
|
</para>
|
|
|
|
</sect2>
|
|
|
|
|
1999-05-12 09:32:47 +02:00
|
|
|
</sect1>
|
|
|
|
</chapter>
|
1999-04-06 17:41:20 +02:00
|
|
|
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
|
|
Local variables:
|
2000-03-31 05:27:42 +02:00
|
|
|
mode:sgml
|
1999-04-06 17:41:20 +02:00
|
|
|
sgml-omittag:nil
|
|
|
|
sgml-shorttag:t
|
|
|
|
sgml-minimize-attributes:nil
|
|
|
|
sgml-always-quote-attributes:t
|
|
|
|
sgml-indent-step:1
|
|
|
|
sgml-indent-data:t
|
|
|
|
sgml-parent-document:nil
|
|
|
|
sgml-default-dtd-file:"./reference.ced"
|
|
|
|
sgml-exposed-tags:nil
|
2000-03-31 05:27:42 +02:00
|
|
|
sgml-local-catalogs:("/usr/lib/sgml/catalog")
|
1999-04-06 17:41:20 +02:00
|
|
|
sgml-local-ecat-files:nil
|
|
|
|
End:
|
|
|
|
-->
|