PostgreSQL命令语法大全

可以使用help语句查看所有postgreSQL语句的语法。 按照以下步骤查看PostgreSQL中所有语句的语法。

  • 安装postgreSQL后,打开psql为:程序文件 -> PostgreSQL 9.2 -> SQL Shell(psql)
  • 使用以下语句查看特定语句的语法。 postgres-# help&

所有PostgreSQL语句

在这里,我们提供了所有postgreSQL语句及其语法的列表:

ABORT语句:

语法:

ABORT [ WORK | TRANSACTION ]
SQL

ALTER AGGREGATE语句:

语法:

ALTER AGGREGATE name ( type ) RENAME TO new_name  
ALTER AGGREGATE name ( type ) OWNER TO new_owner
SQL

ALTER CONVERSION语句:

语法:

ALTER CONVERSION name RENAME TO new_name  
ALTER CONVERSION name OWNER TO new_owner
SQL

ALTER DATABASE语句:

语法:

ALTER DATABASE name SET parameter { TO | = } { value | DEFAULT }  
ALTER DATABASE name RESET parameter  
ALTER DATABASE name RENAME TO new_name  
ALTER DATABASE name OWNER TO new_owner
SQL

ALTER DOMAIN语句:

语法:

ALTER DOMAIN name { SET DEFAULT expression | DROP DEFAULT }  
ALTER DOMAIN name { SET | DROP } NOT NULL  
ALTER DOMAIN name ADD domain_constraint  
ALTER DOMAIN name DROP CONSTRAINT constraint_name [ RESTRICT | CASCADE ]  
ALTER DOMAIN name OWNER TO new_owner
SQL

ALTER FUNCTION语句:

语法:

ALTER FUNCTION name ( [ type [, ...] ] ) RENAME TO new_name  
ALTER FUNCTION name ( [ type [, ...] ] ) OWNER TO new_owner
SQL

ALTER GROUP语句:

语法:

ALTER GROUP groupname ADD USER username [, ... ]  
ALTER GROUP groupname DROP USER username [, ... ]  
ALTER GROUP groupname RENAME TO new_name
SQL

ALTER INDEX语句:

语法:

ALTER INDEX name OWNER TO new_owner  
ALTER INDEX name SET TABLESPACE indexspace_name  
ALTER INDEX name RENAME TO new_name
SQL

ALTER LANGUAGE语句:

语法:

ALTER LANGUAGE name RENAME TO new_name
SQL

ALTER OPERATOR语句:

语法:

ALTER OPERATOR name ( { lefttype | NONE } , { righttype | NONE } )  
OWNER TO new_owner
SQL

ALTER OPERATOR CLASS语句:

语法:

ALTER OPERATOR CLASS name USING index_method RENAME TO new_name  
ALTER OPERATOR CLASS name USING index_method OWNER TO new_owner
SQL

ALTER SCHEMA语句:

语法:

ALTER SCHEMA name RENAME TO new_name  
ALTER SCHEMA name OWNER TO new_owner
SQL

ALTER SEQUENCE语句:

语法:

ALTER SEQUENCE name [ INCREMENT [ BY ] increment ]  
[ MINVALUE minvalue | NO MINVALUE ]  
[ MAXVALUE maxvalue | NO MAXVALUE ]  
[ RESTART [ WITH ] start ] [ CACHE cache ] [ [ NO ] CYCLE ]
SQL

ALTER TABLE语句:

语法:

ALTER TABLE [ ONLY ] name [ * ]  
action [, ... ]  
ALTER TABLE [ ONLY ] name [ * ]  
RENAME [ COLUMN ] column TO new_column  
ALTER TABLE name  
RENAME TO new_name
SQL

ALTER TABLESPACE语句:

语法:

ALTER TABLESPACE name RENAME TO new_name  
ALTER TABLESPACE name OWNER TO new_owner
SQL

ALTER TRIGGER语句:

语法:

ALTER TRIGGER name ON table RENAME TO new_name
SQL

ALTER TYPE语句:

语法:

ALTER TYPE name OWNER TO new_owner
SQL

ALTER USER语句:

语法:

ALTER USER name [ [ WITH ] option [ ... ] ]  
ALTER USER name RENAME TO new_name  
ALTER USER name SET parameter { TO | = } { value | DEFAULT }  
ALTER USER name RESET parameter
SQL

ANALYSE语句:

语法:

ANALYZE [ VERBOSE ] [ table [ (column [, ...] ) ] ]
SQL

BEGIN语句:

语法:

BEGIN [ WORK | TRANSACTION ] [ transaction_mode [, ...] ]
SQL

CHECKPOINT语句:

语法:

CHECKPOINT
SQL

CLOSE语句:

语法:

CLOSE name
SQL

CLUSTER语句:

语法:

CLUSTER index_name ON table_name  
CLUSTER table_name  
CLUSTER
SQL

COMMIT语句:

语法:

COMMIT [ WORK | TRANSACTION ]
SQL

COPY语句:

语法:

COPY table_name [ ( column [, ...] ) ]  
FROM { 'filename' | STDIN }  
[ [ WITH ]  
[ BINARY ]  
[ OIDS ]  
[ DELIMITER [ AS ] 'delimiter' ]  
[ NULL [ AS ] 'null string' ]  
[ CSV [ QUOTE [ AS ] 'quote' ]  
[ ESCAPE [ AS ] 'escape' ]  
[ FORCE NOT NULL column [, ...] ]  
COPY table_name [ ( column [, ...] ) ]  
TO { 'filename' | STDOUT }  
[ [ WITH ]  
[ BINARY ]  
[ OIDS ]  
[ DELIMITER [ AS ] 'delimiter' ]  
[ NULL [ AS ] 'null string' ]  
[ CSV [ QUOTE [ AS ] 'quote' ]  
[ ESCAPE [ AS ] 'escape' ]  
[ FORCE QUOTE column [, ...] ]
SQL

CREATE AGGREGATE语句:

语法:

CREATE AGGREGATE name (  
BASETYPE = input_data_type,  
SFUNC = sfunc,  
STYPE = state_data_type  
[ , FINALFUNC = ffunc ]  
[ , INITCOND = initial_condition ]  
)
SQL

CREATE CAST语句:

语法:

CREATE CAST (source_type AS target_type)  
WITH FUNCTION func_name (arg_types)  
[ AS ASSIGNMENT | AS IMPLICIT ]  
CREATE CAST (source_type AS target_type)  
WITHOUT FUNCTION  
[ AS ASSIGNMENT | AS IMPLICIT ]
SQL

CREATE CONSTRAINT TRIGGER语句:

语法:

CREATE CONSTRAINT TRIGGER name  
AFTER events ON  
table_name constraint attributes  
FOR EACH ROW EXECUTE PROCEDURE func_name ( args )
SQL

CREATE CONVERSION语句:

语法:

CREATE [DEFAULT] CONVERSION name  
FOR source_encoding TO dest_encoding FROM func_name
SQL

CREATE DATABASE语句:

语法:

CREATE DATABASE name  
[ [ WITH ] [ OWNER [=] db_owner ]  
[ TEMPLATE [=] template ]  
[ ENCODING [=] encoding ]  
[ TABLESPACE [=] tablespace ] ]
SQL

CREATE DOMAIN语句:

语法:

CREATE DOMAIN name [AS] data_type  
[ DEFAULT expression ]  
[ constraint [ ... ] ]
SQL

CREATE FUNCTION语句:

语法:

CREATE [ OR REPLACE ] FUNCTION name ( [ [ arg_name ] arg_type [, ...] ] )  
RETURNS ret_type  
{ LANGUAGE lang_name  
| IMMUTABLE | STABLE | VOLATILE  
| CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT  
| [ EXTERNAL ] SECURITY INVOKER | [ EXTERNAL ] SECURITY DEFINER  
| AS 'definition'  
| AS 'obj_file', 'link_symbol'  
} ...  
[ WITH ( attribute [, ...] ) ]
SQL

CREATE GROUP语句:

语法:

CREATE GROUP name [ [ WITH ] option [ ... ] ]  
Where option can be:  
SYSID gid  
| USER username [, ...]
SQL

CREATE INDEX语句:

语法:

CREATE [ UNIQUE ] INDEX name ON table [ USING method ]  
( { column | ( expression ) } [ opclass ] [, ...] )  
[ TABLESPACE tablespace ]  
[ WHERE predicate ]
SQL

CREATE LANGUAGE语句:

语法:

CREATE [ TRUSTED ] [ PROCEDURAL ] LANGUAGE name  
HANDLER call_handler [ VALIDATOR val_function ]
SQL

CREATE OPERATOR语句:

语法:

CREATE OPERATOR name (  
PROCEDURE = func_name  
[, LEFTARG = left_type ] [, RIGHTARG = right_type ]  
[, COMMUTATOR = com_op ] [, NEGATOR = neg_op ]  
[, RESTRICT = res_proc ] [, JOIN = join_proc ]  
[, HASHES ] [, MERGES ]  
[, SORT1 = left_sort_op ] [, SORT2 = right_sort_op ]  
[, LTCMP = less_than_op ] [, GTCMP = greater_than_op ]  
)
SQL

CREATE OPERATOR CLASS语句:

语法:

CREATE OPERATOR CLASS name [ DEFAULT ] FOR TYPE data_type  
USING index_method AS  
{ OPERATOR strategy_number operator_name [ ( op_type, op_type ) ] [ RECHECK ]  
| FUNCTION support_number func_name ( argument_type [, ...] )  
| STORAGE storage_type  
} [, ... ]
SQL

CREATE RULE语句:

语法:

CREATE [ OR REPLACE ] RULE name AS ON event  
TO table [ WHERE condition ]  
DO [ ALSO | INSTEAD ] { NOTHING | command | ( command ; command ... ) }
SQL

CREATE SCHEMA语句:

语法:

CREATE SCHEMA schema_name  
[ AUTHORIZATION username ] [ schema_element [ ... ] ]  
CREATE SCHEMA AUTHORIZATION username  
[ schema_element [ ... ] ]
SQL

CREATE SEQUENCE语句:

语法:

CREATE [ TEMPORARY | TEMP ] SEQUENCE name  
[ INCREMENT [ BY ] increment ]  
[ MINVALUE minvalue | NO MINVALUE ]  
[ MAXVALUE maxvalue | NO MAXVALUE ]  
[ START [ WITH ] start ] [ CACHE cache ] [ [ NO ] CYCLE ]
SQL

CREATE TABLE语句:

语法:

CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } ] TABLE table_name (  
{ column_name data_type [ DEFAULT default_expr ] [ column_constraint [ ... ] ]  
| table_constraint  
| LIKE parent_table [ { INCLUDING | EXCLUDING } DEFAULTS ] } [, ... ]  
)  
[ INHERITS ( parent_table [, ... ] ) ]  
[ WITH OIDS | WITHOUT OIDS ]  
[ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]  
[ TABLESPACE tablespace ]
SQL

CREATE TABLE AS语句:

语法:

CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } ] TABLE table_name  
[ (column_name [, ...] ) ] [ [ WITH | WITHOUT ] OIDS ]  
AS query
SQL

CREATE TABLESPACE语句:

语法:

CREATE TABLESPACE tablespace_name [ OWNER username ] LOCATION 'directory'
SQL

CRFEATE TRIGGER语句:

语法:

CREATE TRIGGER name { BEFORE | AFTER } { event [ OR ... ] }  
ON table [ FOR [ EACH ] { ROW | STATEMENT } ]  
EXECUTE PROCEDURE func_name ( arguments )
SQL

CREATE TYPE语句:

语法:

CREATE TYPE name AS  
( attribute_name data_type [, ... ] )  
CREATE TYPE name (  
INPUT = input_function,  
OUTPUT = output_function  
[ , RECEIVE = receive_function ]  
[ , SEND = send_function ]  
[ , ANALYZE = analyze_function ]  
[ , INTERNALLENGTH = { internal_length | VARIABLE } ]  
[ , PASSEDBYVALUE ]  
[ , ALIGNMENT = alignment ]  
[ , STORAGE = storage ]  
[ , DEFAULT = default ]  
[ , ELEMENT = element ]  
[ , DELIMITER = delimiter ]  
)
SQL

CREATE USER语句:

语法:

CREATE USER name [ [ WITH ] option [ ... ] ]
SQL

CREATE VIEW语句:

语法:

CREATE [ OR REPLACE ] VIEW name [ ( column_name [, ...] ) ] AS query
SQL

DEALLOCATE语句:

语法:

DEALLOCATE [ PREPARE ] plan_name
SQL

DECLARE语句:

语法:

DECLARE name [ BINARY ] [ INSENSITIVE ] [ [ NO ] SCROLL ]  
CURSOR [ { WITH | WITHOUT } HOLD ] FOR query  
[ FOR { READ ONLY | UPDATE [ OF column [, ...] ] } ]
SQL

DELETE语句:

语法:

DELETE FROM [ ONLY ] table [ WHERE condition ]
SQL

DROP AGGREGATE语句:

语法:

DROP AGGREGATE name ( type ) [ CASCADE | RESTRICT ]
SQL

DROP CAST语句:

语法:

DROP CAST (source_type AS target_type) [ CASCADE | RESTRICT ]
SQL

DROP CONVERSION语句:

语法:

DROP CONVERSION name [ CASCADE | RESTRICT ]
SQL

DROP DATABASE语句:

语法:

DROP DATABASE name
SQL

DROP DOMAIN语句:

语法:

DROP DOMAIN name [, ...] [ CASCADE | RESTRICT ]
SQL

DROP FUNCTION语句:

语法:

DROP FUNCTION name ( [ type [, ...] ] ) [ CASCADE | RESTRICT ]
SQL

DROP GROUP语句:

语法:

DROP GROUP name
SQL

DROP INDEX语句:

语法:

DROP INDEX name [, ...] [ CASCADE | RESTRICT ]
SQL

DROP LANGUAGE语句:

语法:

DROP [ PROCEDURAL ] LANGUAGE name [ CASCADE | RESTRICT ]
SQL

DROP OPERATOR语句:

语法:

DROP OPERATOR name ( { left_type | NONE } , { right_type | NONE } )  
[ CASCADE | RESTRICT ]
SQL

DROP OPERATOR CLASS语句:

语法:

DROP OPERATOR CLASS name USING index_method [ CASCADE | RESTRICT ]
SQL

DROP RULE语句:

语法:

DROP RULE name ON relation [CASCADE | RESTRICT ]
SQL

DROP SCHEMA语句:

语法:

DROP SCHEMA name [, ...] [ CASCADE | RESTRICT ]
SQL

DROP SEQUENCE语句:

语法:

DROP SEQUENCE name [, ...] [ CASCADE | RESTRICT ]
SQL

DROP TABLE语句:

语法:

DROP TABLE name [, ...] [ CASCADE | RESTRICT ]
SQL

DROP TABLESPACE语句:

语法:

DROP TABLESPACE tablespace_name
SQL

DROP TRIGGER语句:

语法:

DROP TRIGGER name ON table [ CASCADE | RESTRICT ]
SQL

DROP TYPE语句:

语法:

DROP TYPE name [, ...] [ CASCADE | RESTRICT ]
SQL

DROP USER语句:

语法:

DROP USER name
SQL

DROP VIEW语句:

语法:

DROP VIEW name [, ...] [ CASCADE | RESTRICT ]
SQL

END语句:

语法:

END [ WORK | TRANSACTION ]
SQL

EXECUTE语句:

语法:

EXECUTE plan_name [ (parameter [, ...] ) ]
SQL

EXPLAIN语句:

语法:

EXPLAIN [ ANALYZE ] [ VERBOSE ] statement
SQL

FETCH语句:

语法:

FETCH [ direction { FROM | IN } ] cursor_name
SQL

INSERT语句:

语法:

INSERT INTO table [ ( column [, ...] ) ]  
{ DEFAULT VALUES | VALUES ( { expression | DEFAULT } [, ...] ) | query }
SQL

LISTEN语句:

语法:

LISTEN name
SQL

LOAD语句:

语法:

LOAD 'filename'
SQL

LOCK语句:

语法:

LOCK [ TABLE ] name [, ...] [ IN lock_mode MODE ] [ NOWAIT ]
SQL

MOVE语句:

语法:

MOVE [ direction { FROM | IN } ] cursor_name
SQL

NOTIFY语句:

语法:

NOTIFY name
SQL

PREPARE语句:

语法:

PREPARE plan_name [ (data_type [, ...] ) ] AS statement
SQL

REINDEX语句:

语法:

REINDEX { DATABASE | TABLE | INDEX } name [ FORCE ]
SQL

RESET语句:

语法:

RESET name  
RESET ALL
SQL

ROLLBACK语句:

语法:

ROLLBACK [ WORK | TRANSACTION ]
SQL

ROLLBACK TO SAVEPOINT语句:

语法:

ROLLBACK [ WORK | TRANSACTION ] TO [ SAVEPOINT ] savepoint_name
SQL

SAVEPOINT语句:

语法:

SAVEPOINT savepoint_name
SQL

SELECT语句:

语法:

SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]  
* | expression [ AS output_name ] [, ...]  
[ FROM from_item [, ...] ]  
[ WHERE condition ]  
[ GROUP BY expression [, ...] ]  
[ HAVING condition [, ...] ]  
[ { UNION | INTERSECT | EXCEPT } [ ALL ] select ]  
[ ORDER BY expression [ ASC | DESC | USING operator ] [, ...] ]  
[ LIMIT { count | ALL } ]  
[ OFFSET start ]  
[ FOR UPDATE [ OF table_name [, ...] ] ]
SQL

SELECT INTO语句:

语法:

SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]  
* | expression [ AS output_name ] [, ...]  
INTO [ TEMPORARY | TEMP ] [ TABLE ] new_table  
[ FROM from_item [, ...] ]  
[ WHERE condition ]  
[ GROUP BY expression [, ...] ]  
[ HAVING condition [, ...] ]  
[ { UNION | INTERSECT | EXCEPT } [ ALL ] select ]  
[ ORDER BY expression [ ASC | DESC | USING operator ] [, ...] ]  
[ LIMIT { count | ALL } ]  
[ OFFSET start ]  
[ FOR UPDATE [ OF table_name [, ...] ] ]
SQL

SET语句:

语法:

SET [ SESSION | LOCAL ] name { TO | = } { value | 'value' | DEFAULT }  
SET [ SESSION | LOCAL ] TIME ZONE { time_zone | LOCAL | DEFAULT }
SQL

SET CONSTRAINTS语句:

语法:

SET CONSTRAINTS { ALL | name [, ...] } { DEFERRED | IMMEDIATE }
SQL

SET TRANSACTION语句:

语法:

SET TRANSACTION transaction_mode [, ...]  
SET SESSION CHARACTERISTICS AS TRANSACTION transaction_mode [, ...]
SQL

SHOW语句:

语法:

SHOW name  
SHOW ALL
SQL

START TRANSACTION语句:

语法:

START TRANSACTION [ transaction_mode [, ...] ]
SQL

TRUNCATE TABLE语句:

语法:

TRUNCATE [ TABLE ] name
SQL

UPDATE语句:

语法:

UPDATE [ ONLY ] table SET column = { expression | DEFAULT } [, ...]  
[ FROM from_list ]  
[ WHERE condition ]