+ linkend="sql-keywords-appendix">.
- Information on
SQL92 and
SQL3 key words
- is derived from .
+ SQL identifiers and key words must begin with a letter
+ (a-z) or underscore
+ (_). Subsequent characters in an identifier or
+ key word can be letters, digits
+ (0-9), or underscores,
+ although the SQL standard will not define a key word that contains
+ digits or start or ends with an underscore.
-
-
Reserved Key Words
-
- reserved key words which are not allowed
- as identifiers and not allowed in any usage other than as fundamental
- tokens in
SQL statements.
-
Postgres 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).
-
-
-
- Any string can be used as an identifier if surrounded by
- double quotes (like this!
). Some care is required since
- such an identifier will be case sensitive
- and will retain embedded whitespace and most other special characters.
-
-
-
- The following are
Postgres
- reserved words that are neither
SQL92
- nor
SQL3 reserved words. These are allowed
- to be present as column labels, but not as identifiers:
-
-ABORT ANALYZE
-BINARY
-CLUSTER CONSTRAINT COPY
-DO
-EXPLAIN EXTEND
-LISTEN LOAD LOCK
-MOVE
-NEW NONE NOTIFY
-OFFSET
-RESET
-SETOF SHOW
-UNLISTEN UNTIL
-VACUUM VERBOSE
-
-
-
- The following are
Postgres
- reserved words that are also
SQL92
- or
SQL3 reserved words, and that
- are allowed to be present as column labels, but not as identifiers:
-
-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
-GLOBAL GROUP
-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
-
-
- The following are
Postgres
- reserved words that are also
SQL92
- or
SQL3 reserved words:
-
-ADD ALTER AND AS
-BEGIN BY
-CASCADE CLOSE COMMIT CREATE CURSOR
-DECLARE DEFAULT DELETE DESC DISTINCT DROP
-EXECUTE EXISTS EXTRACT
-FETCH FLOAT FOR FROM FULL
-GRANT
-HAVING
-IN INNER INSERT INTERVAL INTO INOUT IS
-JOIN
-LEADING LEFT LIKE LOCAL
-NAMES NATIONAL NATURAL NCHAR NO NOT NULL
-ON OR OUT OUTER
-PARTIAL PRIMARY PRIVILEGES PROCEDURE PUBLIC
-REFERENCES REVOKE RIGHT ROLLBACK
-SELECT SESSION SET SUBSTRING
-TO TRAILING TRIM
-UNION UNIQUE UPDATE USING
-VALUES VARCHAR VARYING VIEW
-WHERE WITH WITHOUT WORK
-
-
-
- The following are
SQL92 reserved key words that
- are not
Postgres reserved key words, but that
- if used as function names are always translated into the function
- CHAR_LENGTH:
-
-CHARACTER_LENGTH
-
-
-
- The following are
SQL92 or
SQL3
- reserved key words that
- are not
Postgres reserved key words, but
- if used as type names are always translated into an alternate, native type:
-
-BOOLEAN DOUBLE FLOAT INT INTEGER INTERVAL REAL SMALLINT
-
-
-
- The following are not keywords of any kind, but when used in the
- context of a type name are translated into a native
-
Postgres type, and when used in the
- context of a function name are translated into a native function:
-
-DATETIME TIMESPAN
-
-
- (translated to TIMESTAMP and INTERVAL,
- respectively). This feature is intended to help with
- transitioning to version 7.0, and will be removed in a future release.
-
-
- The following are either
SQL92
- or
SQL3 reserved key words
- that are not key words in
Postgres.
- These have no proscribed usage in
Postgres
- at the time of writing (version 7.0) but may become reserved key words in the
- future:
-
-
- Some of these key words represent functions in
SQL92.
- These functions are defined in
Postgres,
- but the parser does not consider the names to be key words and they are allowed
- in other contexts.
-
-
-
-ALLOCATE ARE ASSERTION AT AUTHORIZATION AVG
-BIT_LENGTH
-CASCADED CATALOG CHAR_LENGTH CHARACTER_LENGTH COLLATION
- CONNECT CONNECTION CONTINUE CONVERT CORRESPONDING COUNT
- CURRENT_SESSION
-DATE DEALLOCATE DEC DESCRIBE DESCRIPTOR
- DIAGNOSTICS DISCONNECT DOMAIN
-ESCAPE EXCEPT EXCEPTION EXEC EXTERNAL
-FIRST FOUND
-GET GO GOTO
-IDENTITY INDICATOR INPUT INTERSECT
-LAST LOWER
-MAX MIN MODULE
-OCTET_LENGTH OPEN OUTPUT OVERLAPS
-PREPARE PRESERVE
-ROWS
-SCHEMA SECTION SESSION SIZE SOME
- SQL SQLCODE SQLERROR SQLSTATE SUM SYSTEM_USER
-TEMPORARY TRANSLATE TRANSLATION
-UNKNOWN UPPER USAGE
-VALUE
-WHENEVER WRITE
-
-
-
-
-
-
Non-reserved Keywords
-
- non-reserved keywords which have
- a prescribed meaning in the language but which are also allowed
- as identifiers.
-
Postgres has additional keywords
- which allow similar unrestricted usage.
- In particular, these keywords
- are allowed as column or table names.
-
-
- The following are
Postgres
- non-reserved key words that are neither
SQL92
- nor
SQL3 non-reserved key words:
-
-ACCESS AFTER AGGREGATE
-BACKWARD BEFORE
-CACHE COMMENT CREATEDB CREATEUSER CYCLE
-DATABASE DELIMITERS
-EACH ENCODING EXCLUSIVE
-FORCE FORWARD FUNCTION
-HANDLER
-INCREMENT INDEX INHERITS INSENSITIVE INSTEAD ISNULL
-LANCOMPILER LOCATION
-MAXVALUE MINVALUE MODE
-NOCREATEDB NOCREATEUSER NOTHING NOTIFY NOTNULL
-OIDS OPERATOR
-PASSWORD PROCEDURAL
-RECIPE REINDEX RENAME RETURNS ROW RULE
-SEQUENCE SERIAL SHARE START STATEMENT STDIN STDOUT
-TEMP TRUSTED
-UNLISTEN UNTIL
-VALID VERSION
-
-
-
- The following are
Postgres
- non-reserved key words that are
SQL92
- or
SQL3 reserved key words:
-
-ABSOLUTE ACTION
-CHARACTERISTICS CONSTRAINTS
-DAY DEFERRABLE DEFERRED
-HOUR
-IMMEDIATE INITIALLY INSENSITIVE ISOLATION
-KEY
-LANGUAGE LEVEL
-MATCH MINUTE MONTH
-NEXT
-OF ONLY OPTION
-PATH PENDANT PRIOR PRIVILEGES
-READ RELATIVE RESTRICT
-SCHEMA SCROLL SECOND
-TIME TIMESTAMP TIMEZONE_HOUR TIMEZONE_MINUTE TRIGGER
-YEAR
-ZONE
-
-
-
- The following are
Postgres
- non-reserved key words that are also either
SQL92
- or
SQL3 non-reserved key words:
-
-COMMITTED SERIALIZABLE TYPE
-
-
-
- The following are either
SQL92
- or
SQL3 non-reserved key words that are not
- key words of any kind in
Postgres:
-
-ADA
-C CATALOG_NAME CHARACTER_SET_CATALOG CHARACTER_SET_NAME
- 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
-DATA DATE_TIME_INTERVAL_CODE DATE_TIME_INTERVAL_PRECISION
- DYNAMIC_FUNCTION
-FORTRAN
-LENGTH
-MESSAGE_LENGTH MESSAGE_OCTET_LENGTH MORE MUMPS
-NAME NULLABLE NUMBER
-PAD PASCAL PLI
-REPEATABLE RETURNED_LENGTH RETURNED_OCTET_LENGTH
- RETURNED_SQLSTATE ROW_COUNT
-SCALE SCHEMA_NAME SERVER_NAME SPACE SUBCLASS_ORIGIN
-TABLE_NAME
-UNCOMMITTED UNNAMED
-
-
-
-
-
-
-
Comments
-
- A comment
- is an arbitrary sequence of characters beginning with double dashes
- and extending to the end of the line, e.g.:
-
--- This is a standard SQL comment
- </programlisting>
+ The system uses no more than NAMEDATALEN-1
+ characters of an identifier; longer names can be written in
+ commands, but they will be truncated. By default,
+ NAMEDATALEN is 32 so the maximum identifier length
+ is 31 (but at the time the system is built,
+ NAMEDATALEN can be changed in
+ <filename>src/include/postgres_ext.h).
- We also support C-style block comments, e.g.:
-
-/* multi-line comment
- * with nesting: /* nested block comment */
- */
-
-
- where the comment begins with "/*" and extends
- to the matching occurrence of "*/". These block
- comments nest, as specified in SQL99, so that one can comment out
- larger blocks of code that may contain existing block comments.
+ Identifier and key word names are case insensitive. Therefore
+UPDATE MY_TABLE SET A = 5;
+
+ can equivalently be written as
+uPDaTE my_TabLE SeT a = 5;
+
+ A good convention to adopt is perhaps to write key words in upper
+ case and names in lower case, e.g.,
+UPDATE my_table SET a = 5;
+
-
-
-
-
Names
- Names in SQL must begin with a letter
- (a-z) or underscore
- (_).
- Subsequent characters in a name can be letters, digits
- (0-9),
- 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
- src/include/postgres_ext.h).
+ There is a second kind of identifier: the delimited
+ identifier or quoted
+ identifier. It is formed by enclosing an arbitrary
+ sequence of characters in double-quotes
+ ("). A delimited
+ identifier is always an identifier, never a key word. So
+ "select" could be used to refer to a column or
+ table named select
, whereas an unquoted
+ select would be taken as part of a command 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 so:
+UPDATE "my_table" SET "a" = 5;
+
- Names containing other characters may be formed by surrounding them
- with double quotes ("). For example, table or column
- names may contain
- 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 FOO, foo
- and "foo" are
- considered the same by
Postgres, but
- "Foo" is a different name.
+ Quoted identifiers can contain any character other than a double
+ quote itself. 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.
- Double quotes can also be used to protect a name that would otherwise
- be taken to be an SQL keyword. For example, IN
- is a keyword but "IN" is a name.
+ Quoting an identifier also makes it case-sensitive, whereas
+ unquoted names are always folded to lower case. For example, the
+ identifiers FOO, foo and
+ "foo" are considered the same by
+ and "FOO" are different from these three and
+ each other.
+
+ This is incompatible with SQL, where unquoted names are folded to
+ upper case. Thus, foo is equivalent to
+ "FOO". If you want to write portable
+ applications you are advised to always quote a particular name or
+ never quote it.
+
+
-
+
+
- 1 id="sql-constants">
+ 2 id="sql-syntax-constants">
Constants
- There are three kinds of implicitly typed constants
- in
Postgres: strings, integers,
- 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
+ There are four kinds of implicitly typed
+ strings, bit strings, integers, and floating point numbers.
+ Constants can also be specified with explicit types, which can
+ enable more accurate representation and more efficient handling by
+ the system. The implicit constants are described below; explicit
constants are discussed afterwards.
- 2>
+ 3>
String Constants
- Strings
- in SQL are arbitrary sequences of ASCII characters bounded by single
- quotes ("'", e.g. 'This is a string').
- SQL92 allows single quotes to be embedded in strings by typing two
- adjacent single quotes (e.g. 'Dianne''s horse').
- In
Postgres single quotes may alternatively
- be escaped with a backslash ("\", e.g.
- 'Dianne\'s horse'). To include a
+ A string constant in SQL is an arbitrary sequence of characters
+ bounded by single quotes ('
), e.g., 'This
+ is a string'. SQL allows single quotes to be embedded
+ in strings by typing two adjacent single quotes (e.g.,
+ 'Dianne''s horse'). In
+
Postgres single quotes may
+ alternatively be escaped with a backslash (\
,
+ e.g., 'Dianne\'s horse').
+
+
+ C-style backslash escapes are also available:
+ \b is a backspace, \f is a
+ form feed, \n is a newline,
+ \r is a carriage return, \t
+ is a tab, and \xxx,
+ where xxx is an octal number, is the
+ character with the corresponding ASCII code. Any other character
+ following a backslash is taken literally. Thus, 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. '\tab').
-
+ The character with the code zero cannot be in a string constant.
+
-
-
Integer Constants
+ Two string constants that are only separated by whitespace
+ with at least one newline are concatenated
+ and effectively treated as if the string had been written in one
+ constant. For example:
+SELECT 'foo'
+'bar';
+
+ is equivalent to
+SELECT 'foobar';
+
+ but
+SELECT 'foo' 'bar';
+
+ is not valid syntax.
+
+
+
+
+
Bit String Constants
- Integer constants
- 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 integer type accepts values
- ranging from -2147483648 to +2147483647.
+ Bit string constants look like string constants with a
+ B (upper or lower case) immediately before the
+ opening quote (no intervening whitespace), e.g.,
+ B'1001'. The only characters allowed within
+ bit string constants are 0 and
+ 1. Bit strings constants can be continued
+ across lines in the same way as regular string constants.
- 2>
+ 3>
- 2>
-
Floating Point Constants
+ 3>
+
Integer Constants
- Floating point constants
- consist of an integer part, a decimal point, and a fraction part or
- scientific notation of the following format:
+ Integer constants in SQL are sequences of decimal digits (0
+ though 9) with no decimal point. The range of legal values
+ depends on which integer data type is used, but the plain
+ integer type accepts values ranging from -2147483648
+ to +2147483647. (The optional plus or minus sign is actually a
+ separate unary operator and not part of the integer constant.)
+
+
-
-{dig}.{dig} [e [+-] {dig}]
-
+
+
Floating Point Constants
- where dig is one or more digits.
- You must include at least one dig 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
- extra characters embedded in the string.
+ Floating point constants are accepted in these general forms:
+
+digits.digitse+-digits
+digits.digitse+-digits
+digitse+-digits
+
+ where digits is one or more decimal
+ digits. At least one digit must be before or after the decimal
+ point and after the e if you use that option.
+ Thus, a floating point constant is distinguished from an integer
+ constant by the presence of either the decimal point or the
+ exponent clause (or both). There must not be a space or other
+ characters embedded in the constant.
+
+ These are some examples of valid floating point constants:
+
+3.5
+4.
+.001
+5e2
+1.925e-3
+
+
+
+
- Floating point constaints are of type
- float8. float4 can be specified
-
explicitly by using SQL92 string notation or
+ Floating point constants are of type DOUBLE
+ PRECISION. REAL can be specified explicitly
+
by using SQL string notation or
-float4 '1.23' -- string style
-'1.23'::float4 -- Postgres (historical) style
+REAL '1.23' -- string style
+'1.23'::REAL -- Postgres (historical) style
- 2>
+ 3>
- 2>
-
Constants of Postgres User-Defined Types
+ 3>
+
Constants of Other Types
- A constant of an
- arbitrary
- type can be entered using any one of the following notations:
-
-
+ A constant of an arbitrary type can be
+ entered using any one of the following notations:
+
type 'string'
'string'::type
CAST ( 'string' AS type )
-
-
- The value inside the string is passed to the input
- conversion routine for the type called
- type. The result is a
- 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.
+
+ The value inside the string is passed to the input conversion
+ routine for the type called type. The
+ result is a constant of the indicated type. The explicit type
+ cast may be omitted if there is no ambiguity as to the type the
+ constant must be (for example, when it is passed as an argument
+ to a non-overloaded function), in which case it is automatically
+ coerced.
It is also possible to specify a type coercion using a function-like
syntax:
-
-
+
typename ( value )
-
-
+
although this only works for types whose names are also valid as
function names. (For example, double precision
can't be used this way --- but the equivalent float8
- The ::, CAST(), and function-call
- syntaxes can also be used to specify run-time type conversions. But
- the form type
- 'string' can only be used to specify the
- type of a literal constant.
+ The ::, CAST(), and
+ function-call syntaxes can also be used to specify the type of
+ arbitrary expressions, but the form
+ type
+ 'string' can only be used to specify
+ the type of a literal constant.
- 2>
+ 3>
- 2>
+ 3>
Array constants
- Array constants
- are n-dimensional arrays of any Postgres datatype.
The general format of an array constant is the following:
-
-
-{ val1 delim val2 delim ... }
-
-
- where delim
- is the delimiter character for the type, as recorded in its
- pg_type class entry.
- (For all built-in types, this is the comma character ",".)
- Each val is either a constant
- of the array element type, or a sub-array.
- An example of an array constant is
-
-{{1,2,3},{4,5,6},{7,8,9}}
-
-
+
+'{ val1 delim val2 delim ... }'
+
+ where delim is the delimiter character
+ for the type, as recorded in its pg_type
+ entry. (For all built-in types, this is the comma character
+ ",".) Each val is either a constant
+ of the array element type, or a sub-array. An example of an
+ array constant is
+'{{1,2,3},{4,5,6},{7,8,9}}'
+
This constant is a two-dimensional, 3 by 3 array consisting of three
sub-arrays of integers.
Individual array elements can be placed between double-quote
- marks (") to avoid ambiguity problems with respect to
- white space.
- Without quote marks, the array-value parser will skip leading white space.
+ marks (") to avoid ambiguity
+ problems with respect to white space. Without quote marks, the
+ array-value parser will skip leading white space.
-
-
-
+ (Array constants are actually only a special case of the generic
+ type constants discussed in the previous section. The constant
+ is initially treated as a string and passed to the array input
+ conversion routine. An explicit type specification might be
+ necessary.)
+
+
+
+
+
+
+
Operators
+
+ An operator is a sequence of up to NAMEDATALEN-1
+ (31 by default) characters from the following list:
+
++ - * / < > = ~ ! @ # % ^ & | ` ? $
+
+
+ There are a few restrictions on operator names, however:
+
+
+ "$" (dollar) cannot be a single-character operator, although it
+ can be part of a multi-character operator name.
+
+
+
+
+ -- and /* cannot appear
+ anywhere in an operator name, since they will be taken as the
+ start of a comment.
+
+
+
+
+ A multi-character operator name cannot end in "+" or "-",
+ unless the name also contains at least one of these characters:
+
+~ ! @ # % ^ & | ` ? $
+
+ For example, @- is an allowed operator name,
+ but *- is not. This restriction allows
+
Postgres to parse SQL-compliant
+ queries without requiring spaces between tokens.
+
+
+
+
+
+ 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 "@",
+ you cannot write X*@Y; you must write
+ X* @Y to ensure that
+
Postgres reads it as two operator names
+ not one.
+
+
+
+
+
Special Characters
+
+ 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.
+
+
+
+ A dollar sign ($) followed by digits is used
+ to represent the positional parameters in the body of a function
+ definition. In other contexts the dollar sign may be part of an
+ operator name.
+
+
+
+
+ Parentheses (()) 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.
+
+
+
+
+ Brackets ([]) are used to select the elements
+ of an array. See for more information
+ on arrays.
+
+
+
+
+ Commas (,) are used in some syntactical
+ constructs to separate the elements of a list.
+
+
+
+
+ The semicolon (;) terminates an SQL command.
+ It cannot appear anywhere within a command, except when quoted
+ as a string constant or identifier.
+
+
+
+
+ The colon (:) is used to select
+
slices
from arrays. (See
+ linkend="arrays">.) In certain SQL dialects (such as Embedded
+ SQL), the colon is used to prefix variable names.
+
+
+
+
+ The asterisk (*) has a special meaning when
+ used in the SELECT command or with the
+ COUNT aggregate function.
+
+
+
+
+ The period (.) is used in floating point
+ constants, and to separate table and column names.
+
+
+
+
+
+
+
+
+
Comments
+
+ A comment is an arbitrary sequence of characters beginning with
+ double dashes and extending to the end of the line, e.g.:
+-- This is a standard SQL92 comment
+
+
+
+ Alternatively, C-style block comments can be used:
+/* multi-line comment
+ * with nesting: /* nested block comment */
+ */
+
+ where the comment begins with /* and extends to
+ the matching occurrence of */. These block
+ comments nest, as specified in SQL99 but unlike C, so that one can
+ comment out larger blocks of code that may contain existing block
+ comments.
+
+
+ A comment is removed from the input stream before further syntax
+ analysis and is effectively replaced by whitespace.
+
+
+
+
+
+
Fields and Columns
-
-
Operators
-
- Any built-in or user-defined operator may be used in SQL.
- For the list of built-in operators consult .
- For a list of user-defined operators consult your system administrator
- or run a query on the pg_operator class.
- Parentheses may be used for arbitrary grouping of operators in expressions.
-
-
-
Expressions
&datetime;
+ &keywords;
&biblio;