PostgreSQL 语法


PostgreSQL 语法

我们可以使用 help 命令查看所有 postgreSQL 命令的语法。按照以下步骤查看 postgreSQL 中所有命令的语法。

  • 安装 postgreSQL 后,打开 psql 为: Program Files > PostgreSQL 9.2 > SQL Shell(psql)
  • 使用以下命令查看特定命令的语法。 postgres-# \help &

所有 PostgreSQL 命令

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

中止命令:

句法:

ABORT [ WORK | TRANSACTION ]

ALTER AGGREGATE 命令:

句法:

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

更改转换命令:

句法:

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

更改数据库命令:

句法:

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

更改域命令:

句法:

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

更改功能命令:

句法:

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

更改组命令:

句法:

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

更改索引命令:

句法:

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

更改语言命令:

句法:

ALTER LANGUAGE name RENAME TO new_name

更改操作员命令:

句法:

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

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

ALTER SCHEMA 命令:

句法:

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

更改序列命令:

句法:

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

更改表命令:

句法:

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

更改表空间命令:

句法:

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

ALTER TRIGGER 命令:

句法:

ALTER TRIGGER name ON table RENAME TO new_name

更改类型命令:

句法:

ALTER TYPE name OWNER TO new_owner

更改用户命令:

句法:

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

分析命令:

句法:

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

开始命令:

句法:

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

检查点命令:

句法:

CHECKPOINT

关闭命令:

句法:

CLOSE name

集群命令:

句法:

CLUSTER index_name ON table_name  
CLUSTER table_name  
CLUSTER

提交命令:

句法:

COMMIT [ WORK | TRANSACTION ]

复制命令:

句法:

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 [, ...] ]

创建聚合命令:

句法:

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

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 ]

创建约束触发命令:

句法:

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

创建转换命令:

句法:

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

创建数据库命令:

句法:

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

创建域命令:

句法:

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

创建函数命令:

句法:

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 [, ...] ) ]

创建组命令:

句法:

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

创建索引命令:

句法:

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

创建语言命令:

句法:

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

创建操作员命令:

句法:

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 ]  
)

创建操作员类命令:

句法:

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  
} [, ... ]

创建规则命令:

句法:

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

创建架构命令:

句法:

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

创建序列命令:

句法:

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

创建表:

句法:

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 ]

CREATE TABLE AS 命令:

句法:

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

创建表空间命令:

句法:

CREATE TABLESPACE tablespace_name [ OWNER username ] LOCATION 'directory'

CRFEATE TRIGGER 命令:

句法:

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

创建类型命令:

句法:

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 ]  
)

创建用户命令:

句法:

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

创建视图命令:

句法:

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

解除分配命令:

句法:

DEALLOCATE [ PREPARE ] plan_name

声明命令:

句法:

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

删除命令:

句法:

DELETE FROM [ ONLY ] table [ WHERE condition ]

删除聚合命令:

句法:

DROP AGGREGATE name ( type ) [ CASCADE | RESTRICT ]

DROP CAST 命令:

句法:

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

删除转换命令:

句法:

DROP CONVERSION name [ CASCADE | RESTRICT ]

删除数据库命令:

句法:

DROP DATABASE name

删除域命令:

句法:

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

删除功能命令:

句法:

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

删除组命令:

句法:

DROP GROUP name

删除索引命令:

句法:

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

删除语言命令:

句法:

DROP [ PROCEDURAL ] LANGUAGE name [ CASCADE | RESTRICT ]

删除操作员命令:

句法:

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

删除操作员类命令:

句法:

DROP OPERATOR CLASS name USING index_method [ CASCADE | RESTRICT ]

删除规则命令:

句法:

DROP RULE name ON relation [CASCADE | RESTRICT ]

DROP SCHEMA 命令:

句法:

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

删除序列命令:

句法:

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

删除表命令:

句法:

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

删除表空间命令:

句法:

DROP TABLESPACE tablespace_name

删除触发器命令:

句法:

DROP TRIGGER name ON table [ CASCADE | RESTRICT ]

删除类型命令:

句法:

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

删除用户命令:

句法:

DROP USER name

删除视图命令:

句法:

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

结束命令:

句法:

END [ WORK | TRANSACTION ]

执行命令:

句法:

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

解释命令:

句法:

EXPLAIN [ ANALYZE ] [ VERBOSE ] statement

取回命令:

句法:

FETCH [ direction { FROM | IN } ] cursor_name

插入命令:

句法:

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

监听命令:

句法:

LISTEN name

加载命令:

句法:

LOAD 'filename'

锁定命令

句法:

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

移动命令:

句法:

MOVE [ direction { FROM | IN } ] cursor_name

通知命令:

句法:

NOTIFY name

准备命令:

句法:

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

REINDEX 命令:

句法:

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

复位命令:

句法:

RESET name  
RESET ALL

回滚命令:

句法:

ROLLBACK [ WORK | TRANSACTION ]

回滚到保存点命令:

句法:

ROLLBACK [ WORK | TRANSACTION ] TO [ SAVEPOINT ] savepoint_name

保存点命令:

句法:

SAVEPOINT savepoint_name

选择命令:

句法:

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 [, ...] ] ]

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 [, ...] ] ]

设置命令:

句法:

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

设置约束命令:

句法:

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

设置事务命令:

句法:

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

显示命令:

句法:

SHOW name  
SHOW ALL

开始交易:

句法:

START TRANSACTION [ transaction_mode [, ...] ]

截断表命令:

句法:

TRUNCATE [ TABLE ] name

更新命令:

句法:

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