PostgreSQL语法

开发 PostgreSQL
本章提供了PostgreSQL的SQL命令,这些命令的准确语法规则。这组命令是从psql的命令行工具。上一节我们已经安装Postgres,直接打开psql:

本章提供了PostgreSQL的SQL命令,这些命令的准确语法规则。这组命令是从psql的命令行工具。上一节我们已经安装Postgres,直接打开psql:

Program Files > PostgreSQL 9.2 > SQL Shell(psql).

使用psql的时候,可以生成完整的命令列表使用的\ help命令。对于一个特定的命令的语法使用下面的命令:

  1. postgres-# \help <command_name> 

SQL语句

一个SQL语句,包括令牌,每个令牌可以代表一个关键字,标识符,带引号的标识符,常量,或特殊的字符符号。以下表格使用简单的SELECT语句来说明一个基本的,完整的SQL语句及其组件。

  SELECT id, name FROM states
Token Type Keyword Identifiers Keyword Identifier
Description Command Id and name columns Clause Table name

PostgreSQL的SQL命令

ABORT

中止当前事务。

  1. ABORT [ WORK | TRANSACTION ] 

ALTER AGGREGATE

更改聚合函数的定义。

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

ALTER CONVERSION

更改一个转换的定义。

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

ALTER DATABASE

更改数据库的具体参数。

  1. ALTER DATABASE name SET parameter { TO | = }
  2.  { value | DEFAULT } ALTER DATABASE name RESET
  3.  parameter ALTER DATABASE name RENAME TO new_name ALTER DATABASE name OWNER TO new_owner 

ALTER DOMAIN

变更的定义域的具体参数。

  1. 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

改变它的定义了一个函数。

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

ALTER GROUP

更改用户组

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

ALTER INDEX

更改索引的定义

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

ALTER LANGUAGE

更改程序语言的定义.

  1. ALTER LANGUAGE name RENAME TO new_name

ALTER OPERATOR

更改操作员的定义.

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

ALTER OPERATOR CLASS

改变一个操作符的定义.

  1. ALTER OPERATOR CLASS name USING index_method RENAME TO new_name ALTER OPERATOR CLASS name USING index_method OWNER TO new_owner

ALTER SCHEMA

更改一个模式的定义.

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

ALTER SEQUENCE

更改序列发生器的定义.

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

ALTER TABLE

更改的表的定义.

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

动作是以下行之一:

  1. ADD [ COLUMN ] column_type [ column_constraint [ ... ] ]
  2.  DROP [ COLUMN ] column [ RESTRICT | CASCADE ] ALTER [ COLUMN ]
  3.  column TYPE type [ USING expression ] ALTER [ COLUMN ] column SET DEFAULT expression ALTER 
  4. COLUMN ] column DROP DEFAULT ALTER [ COLUMN ] column { SET | DROP } NOT NULL ALTER [ COLUMN ] column SET STATISTICS integer ALTER [ COLUMN ] column SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN } ADD table_constraint DROP CONSTRAINT constraint_name [ RESTRICT | CASCADE ] CLUSTER ON index_name SET WITHOUT CLUSTER SET WITHOUT OIDS OWNER TO new_owner SET TABLESPACE tablespace_name 

ALTER TABLESPACE

更改一个表空间的定义.

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

ALTER TRIGGER

改变一个触发器的定义.

  1. ALTER TRIGGER name ON table RENAME TO new_name

ALTER TYPE

Change the definition of a type.

  1. ALTER TYPE name OWNER TO new_owner

ALTER USER

更改数据库用户帐户

  1. 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 

选项​​可以是:

  1. [ ENCRYPTED | UNENCRYPTED ] PASSWORD 'password' | CREATEDB | NOCREATEDB | CREATEUSER | NOCREATEUSER | VALID UNTIL 'abstime' 

ANALYZE

收集数据库的统计信息.

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

BEGIN

Start a transaction block.

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

transaction_mode是其中一个:

  1. ISOLATION LEVEL { SERIALIZABLE | REPEATABLE READ | READ COMMITTED | READ UNCOMMITTED } READ WRITE | READ ONLY 

CHECKPOINT

强制事务处理日志检查点.

  1. CHECKPOINT

CLOSE

关闭游标.

  1. CLOSE name

CLUSTER

根据索引集群表.

CLUSTER index_name ON table_name CLUSTER table_name CLUSTER

COMMENT

定义或改变一个对象的注释.

COMMENT ON { TABLE object_name | COLUMN table_name.column_name | AGGREGATE agg_name (agg_type) | CAST (source_type AS target_type) | CONSTRAINT constraint_name ON table_name | CONVERSION object_name | DATABASE object_name | DOMAIN object_name | FUNCTION func_name (arg1_type, arg2_type, ...) | INDEX object_name | LARGE OBJECT large_object_oid | OPERATOR op (left_operand_type, right_operand_type) | OPERATOR CLASS object_name USING index_method | [ PROCEDURAL ] LANGUAGE object_name | RULE rule_name ON table_name | SCHEMA object_name | SEQUENCE object_name | TRIGGER trigger_name ON table_name | TYPE object_name | VIEW object_name } IS 'text'

COMMIT

提交当前事务.

COMMIT [ WORK | TRANSACTION ]

COPY

Copy data between a file and a table.

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

Define a new aggregate function.

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

CREATE CAST

Define a new 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

定义一个新的约束触发器

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

CREATE CONVERSION

定义一个新的转换

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

CREATE DATABASE

创建一个新的数据库.

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

CREATE DOMAIN

定义一个新的域.

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

Where constraint is:

[ CONSTRAINT constraint_name ] { NOT NULL | NULL | CHECK (expression) }

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

CREATE GROUP

定义一个新的用户组.

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

CREATE INDEX

定义一个新的索引.

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

CREATE LANGUAGE

定义一个新的程序语言.

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

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

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

CREATE RULE

定义一个新的重写规则.

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

CREATE SCHEMA

定义一个新的模式.

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

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 ]

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 ]

Where column_constraint is:

[ CONSTRAINT constraint_name ] { NOT NULL | NULL | UNIQUE [ USING INDEX TABLESPACE tablespace ] | PRIMARY KEY [ USING INDEX TABLESPACE tablespace ] | CHECK (expression) | REFERENCES ref_table [ ( ref_column ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE action ] [ ON UPDATE action ] } [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]

And table_constraint is:

[ CONSTRAINT constraint_name ] { UNIQUE ( column_name [, ... ] ) [ USING INDEX TABLESPACE tablespace ] | PRIMARY KEY ( column_name [, ... ] ) [ USING INDEX TABLESPACE tablespace ] | CHECK ( expression ) | FOREIGN KEY ( column_name [, ... ] ) REFERENCES ref_table [ ( ref_column [, ... ] ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE action ] [ ON UPDATE action ] } [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]

CREATE TABLE AS

定义一个新表的查询结果。

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

CREATE TABLESPACE

定义一个新的表空间。

CREATE TABLESPACE tablespace_name [ OWNER username ] LOCATION 'directory'

CREATE TRIGGER

定义一个新的触发器。

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

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

CREATE USER

定义一个新的数据库用户帐户。

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

选项​​可以是:

SYSID uid | [ ENCRYPTED | UNENCRYPTED ] PASSWORD 'password' | CREATEDB | NOCREATEDB | CREATEUSER | NOCREATEUSER | IN GROUP group_name [, ...] | VALID UNTIL 'abs_time'

CREATE VIEW

定义一个新的视图。

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

DEALLOCATE

释放一份事先准备好的声明。

DEALLOCATE [ PREPARE ] plan_name

DECLARE

定义一个游标。

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

DELETE

删除一个表中的行。

DELETE FROM [ ONLY ] table [ WHERE condition ]

DROP AGGREGATE

Remove an aggregate function.

DROP AGGREGATE name ( type ) [ CASCADE | RESTRICT ]

DROP CAST

移除一个转换。

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

DROP CONVERSION

取下转换。

DROP CONVERSION name [ CASCADE | RESTRICT ]

DROP DATABASE

删除一个数据库。

DROP DATABASE name

DROP DOMAIN

删除域。

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

DROP FUNCTION

删除功能(函数)。

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

DROP GROUP

删除用户组。

DROP GROUP name

DROP INDEX

Remove an index.

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

DROP LANGUAGE

Remove a procedural language.

DROP [ PROCEDURAL ] LANGUAGE name [ CASCADE | RESTRICT ]

DROP OPERATOR

Remove an operator.

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

DROP OPERATOR CLASS

Remove an operator class.

DROP OPERATOR CLASS name USING index_method [ CASCADE | RESTRICT ]

DROP RULE

Remove a rewrite rule.

DROP RULE name ON relation [ CASCADE | RESTRICT ]

DROP SCHEMA

Remove a schema.

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

DROP SEQUENCE

Remove a sequence.

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

DROP TABLE

Remove a table.

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

DROP TABLESPACE

Remove a tablespace.

DROP TABLESPACE tablespace_name

DROP TRIGGER

Remove a trigger.

DROP TRIGGER name ON table [ CASCADE | RESTRICT ]

DROP TYPE

Remove a data type.

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

DROP USER

Remove a database user account.

DROP USER name

DROP VIEW

Remove a view.

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

END

Commit the current transaction.

END [ WORK | TRANSACTION ]

EXECUTE

Execute a prepared statement.

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

EXPLAIN

Show the execution plan of a statement.

EXPLAIN [ ANALYZE ] [ VERBOSE ] statement

FETCH

Retrieve rows from a query using a cursor.

FETCH [ direction { FROM | IN } ] cursor_name

Where direction can be empty or one of:

NEXT PRIOR FIRST LAST ABSOLUTE count RELATIVE count count ALL FORWARD FORWARD count FORWARD ALL BACKWARD BACKWARD count BACKWARD ALL

GRANT

Define access privileges.

  1. GRANT { { SELECT | INSERT | UPDATE | DELETE | RULE | REFERENCES | TRIGGER } [,...]
  2.  | ALL [ PRIVILEGES ] } ON [ TABLE ] table_name [, ...] TO { username | GROUP group_name | PUBLIC } [, ...] 
  3. WITH GRANT OPTION ]  GRANT { { CREATE | TEMPORARY | TEMP } [,...] | ALL [ PRIVILEGES ] } ON DATABASE db_name [, ...] 
  4. TO { username | GROUP group_name | PUBLIC } [, ...] [ WITH GRANT OPTION ]  GRANT { CREATE | ALL [ PRIVILEGES ] }
  5.  ON TABLESPACE tablespace_name [, ...] TO { username | GROUP group_name | PUBLIC } [, ...] [ WITH GRANT OPTION ] 
  6.  GRANT { EXECUTE | ALL [ PRIVILEGES ] } ON FUNCTION func_name ([type, ...]) [, ...]
  7. TO { username | GROUP group_name | PUBLIC } [, ...] [ WITH GRANT OPTION ]  GRANT { USAGE | ALL [ PRIVILEGES ] }
  8.  ON LANGUAGE lang_name [, ...] TO { username | GROUP group_name | PUBLIC } [, ...] [ WITH GRANT OPTION ]  GRANT { { CREATE | USAGE }
  9.  [,...] | ALL [ PRIVILEGES ] } ON SCHEMA schema_name [, ...] TO { username | GROUP group_name | PUBLIC } [, ...] [ WITH GRANT OPTION ]

INSERT

Create new rows in a table.

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

LISTEN

Listen for a notification.

  1. LISTEN name

LOAD

Load or reload a shared library file.

  1. LOAD 'filename'

LOCK

Lock a table.

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

Where lock_mode is one of:

  1. ACCESS SHARE | ROW SHARE | ROW EXCLUSIVE | SHARE UPDATE EXCLUSIVE | SHARE | SHARE ROW EXCLUSIVE | EXCLUSIVE | ACCESS EXCLUSIVE 

MOVE

Position a cursor.

  1. MOVE [ direction { FROM | IN } ] cursor_name 

NOTIFY

Generate a notification.

  1. NOTIFY name

PREPARE

Prepare a statement for execution.

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

REINDEX

Rebuild indexes.

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

RELEASE SAVEPOINT

Destroy a previously defined savepoint.

  1. RELEASE [ SAVEPOINT ] savepoint_name 

RESET

Restore the value of a runtime parameter to the default value.

  1. RESET name RESET ALL

REVOKE

Remove access privileges.

  1. REVOKE [ GRANT OPTION FOR ] 
  2. { { SELECT | INSERT | UPDATE | DELETE | RULE | REFERENCES | TRIGGER } 
  3. [,...] | ALL [ PRIVILEGES ] } ON [ TABLE ] table_name [, ...] FROM { username | GROUP group_name | PUBLIC }
  4.  [, ...] [ CASCADE | RESTRICT ]  REVOKE [ GRANT OPTION FOR ] { { CREATE | TEMPORARY | TEMP } [,...] | ALL [ PRIVILEGES ] }
  5.  ON DATABASE db_name [, ...] FROM { username | GROUP group_name | PUBLIC } [, ...] [ CASCADE | RESTRICT ]  REVOKE [ GRANT OPTION FOR ] { CREATE | ALL [ PRIVILEGES ] } ON TABLESPACE tablespace_name [, ...] FROM { username | GROUP group_name | PUBLIC } [, ...] [ CASCADE | RESTRICT ]  REVOKE [ GRANT OPTION FOR ] { EXECUTE | ALL [ PRIVILEGES ] } ON FUNCTION func_name ([type, ...]) [, ...] FROM { username | GROUP group_name | PUBLIC } [, ...] [ CASCADE | RESTRICT ]  REVOKE [ GRANT OPTION FOR ] { USAGE | ALL [ PRIVILEGES ] } ON LANGUAGE lang_name [, ...] FROM { username | GROUP group_name | PUBLIC } [, ...] [ CASCADE | RESTRICT ]  REVOKE [ GRANT OPTION FOR ] { { CREATE | USAGE } [,...] | ALL [ PRIVILEGES ] } ON SCHEMA schema_name [, ...] FROM { username | GROUP group_name | PUBLIC } [, ...] [ CASCADE | RESTRICT ] 

ROLLBACK

Abort the current transaction.

  1. ROLLBACK [ WORK | TRANSACTION ] 

ROLLBACK TO SAVEPOINT

Roll back to a savepoint.

  1. ROLLBACK [ WORK | TRANSACTION ] TO [ SAVEPOINT ] savepoint_name 

SAVEPOINT

Define a new savepoint within the current transaction.

  1. SAVEPOINT savepoint_name

SELECT

Retrieve rows from a table or view.

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

Where from_item can be one of:

  1. ONLY ] table_name [ * ]
  2.  [ [ AS ] alias [ ( column_alias [, ...] ) ] ] ( select ) [ AS ] alias [ ( column_alias [, ...] ) ] 
  3. function_name ( [ argument [, ...] ] ) [ AS ] alias [ ( column_alias [, ...] | column_definition [, ...] ) ]
  4.  function_name ( [ argument [, ...] ] ) AS ( column_definition [, ...] ) from_item [ NATURAL ] join_type from_item 
  5. ON join_condition | USING ( join_column [, ...] ) ] 

SELECT INTO

Define a new table from the results of a query.

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

SET

Change a runtime parameter.

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

SET CONSTRAINTS

Set constraint checking modes for the current transaction.

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

SET SESSION AUTHORIZATION

Set the session user identifier and the current user identifier of the current session.

  1. SET [ SESSION | LOCAL ] SESSION AUTHORIZATION username SET [ SESSION | LOCAL ] SESSION AUTHORIZATION DEFAULT RESET SESSION AUTHORIZATION 

SET TRANSACTION

Set the characteristics of the current transaction.

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

Where transaction_mode is one of:

  1. ISOLATION LEVEL { SERIALIZABLE | REPEATABLE READ | READ COMMITTED | READ UNCOMMITTED } READ WRITE | READ ONLY 

SHOW

显示运行参数的值.

  1. SHOW name SHOW ALL 

START TRANSACTION

开始一个事务块.

  1. START TRANSACTION [ transaction_mode [, ...] ] 

transaction_mode是其中一个:

  1. ISOLATION LEVEL { SERIALIZABLE | REPEATABLE READ | READ COMMITTED | READ UNCOMMITTED } READ WRITE | READ ONLY 

TRUNCATE

清空一个表.

  1. TRUNCATE [ TABLE ] name 

UNLISTEN

停止监听通知.

  1. UNLISTEN { name | * } 

UPDATE

更新表行.

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

VACUUM

垃圾收集和分析数据库(可选).

  1. VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ table ] VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] ANALYZE [ table [ (column [, ...] ) ] ] 

 

责任编辑:陈四芳 来源: yiibai.com
相关推荐

2024-04-03 09:25:53

数据库OraclePostgreSQL

2014-01-02 13:22:01

PythonPostgreSQL

2014-01-02 15:41:24

PostgreSQLPHP

2014-01-02 14:04:39

PostgreSQLPerl

2014-01-02 16:06:03

PostgreSQL实用函数

2022-07-05 10:06:55

PostgreSQLMySQL数据库

2014-01-03 09:30:44

PostgreSQL数字函数

2013-12-26 13:19:26

PostgreSQL优化

2012-08-23 09:33:25

数据库数组PostgreSQL

2014-01-03 13:27:33

PostgreSQL

2022-04-26 07:45:52

PostgreSQL容器

2017-07-24 17:00:49

2023-06-21 10:05:32

MySQLPostgreSQL数据库

2014-01-05 17:08:09

PostgreSQL数据类型

2014-01-02 10:19:54

PostgreSQL安装

2024-07-08 10:48:51

2023-02-07 08:15:45

PostgreSQLIO技巧

2015-03-16 11:09:28

MongoDBPostgreSQL数据迁移

2022-02-22 10:52:21

PostgreSQLZstd压缩能力

2013-12-24 10:04:01

PostgreSQL
点赞
收藏

51CTO技术栈公众号