如何学习PostgreSQL SQL语法
我们可以利用psql命令来查询sql语法。切换到PostgreSQL用户下,
[doctor@localhost ~]$ su - postgres
密码:
su: 鉴定故障
[doctor@localhost ~]$ su - postgres
密码:
-bash-4.3$ bash
bash-4.3$ psql
psql (9.4.2)
输入 "help" 来获取帮助信息.
postgres=#
postgres=# help
您正在使用psql, 这是一种用于访问PostgreSQL的命令行界面
键入: \copyright 显示发行条款
\h 显示 SQL 命令的说明
\? 显示 pgsql 命令的说明
\g 或者以分号(;)结尾以执行查询
\q 退出
postgres=#
\h 命令就是我们用来学习sql语法的命令。
postgres=# \h
可用的说明:
ABORT DEALLOCATE
ALTER AGGREGATE DECLARE
ALTER COLLATION DELETE
ALTER CONVERSION DISCARD
ALTER DATABASE DO
ALTER DEFAULT PRIVILEGES DROP AGGREGATE
ALTER DOMAIN DROP CAST
ALTER EVENT TRIGGER DROP COLLATION
ALTER EXTENSION DROP CONVERSION
ALTER FOREIGN DATA WRAPPER DROP DATABASE
ALTER FOREIGN TABLE DROP DOMAIN
ALTER FUNCTION DROP EVENT TRIGGER
ALTER GROUP DROP EXTENSION
ALTER INDEX DROP FOREIGN DATA WRAPPER
ALTER LANGUAGE DROP FOREIGN TABLE
ALTER LARGE OBJECT DROP FUNCTION
ALTER MATERIALIZED VIEW DROP GROUP
ALTER OPERATOR DROP INDEX
ALTER OPERATOR CLASS DROP LANGUAGE
ALTER OPERATOR FAMILY DROP MATERIALIZED VIEW
ALTER ROLE DROP OPERATOR
ALTER RULE DROP OPERATOR CLASS
postgres=# \h select
命令: SELECT
描述: 从资料表或视观表读取资料
语法:
[ WITH [ RECURSIVE ] with查询语句(with_query) [, ...] ]
SELECT [ ALL | DISTINCT [ ON ( 表达式 [, ...] ) ] ]
[ * | 表达式 [ [ AS ] 输出名称 ] [, ...] ]
[ FROM from列表中项 [, ...] ]
[ WHERE 条件 ]
[ GROUP BY 表达式 [, ...] ]
[ HAVING 条件 [, ...] ]
[ WINDOW 窗口名称 AS ( 窗口定义 ) [, ...] ]
[ { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] 查询 ]
[ ORDER BY 表达式 [ ASC | DESC | USING 运算子 ] [ NULLS { FIRST | LAST } ] [
, ...] ]
[ LIMIT { 查询所用返回记录的最大数量 | ALL } ]
[ OFFSET 起始值 [ ROW | ROWS ] ]
[ FETCH { FIRST | NEXT } [ 查询所用返回记录的最大数量 ] { ROW | ROWS } ONLY
]
[ FOR { UPDATE | NO KEY UPDATE | SHARE | KEY SHARE } [ OF 表名 [, ...] ] [ N
OWAIT ] [...] ]
from 列表中的项可以是下列内容之一
[ ONLY ] 表名 [ * ] [ [ AS ] 化名 [ ( 列的化名 [, ...] ) ] ]
[ LATERAL ] ( 查询 ) [ AS ] 化名 [ ( 列的化名 [, ...] ) ]
WITH查询语句名称(with_query_name) [ [ AS ] 化名 [ ( 列的化名 [, ...] ) ] ]
[ LATERAL ] 函数名称 ( [ 参数 [, ...] ] )
[ WITH ORDINALITY ] [ [ AS ] 化名 [ ( 列的化名 [, ...] ) ] ]
[ LATERAL ] 函数名称 ( [ 参数 [, ...] ] ) [ AS ] 化名 ( 列定义 [, ...] )
[ LATERAL ] 函数名称 ( [ 参数 [, ...] ] ) AS ( 列定义 [, ...] )
[ LATERAL ] ROWS FROM( 函数名称 ( [ 参数 [, ...] ] ) [ AS ( 列定义 [, ...] )
] [, ...] )
[ WITH ORDINALITY ] [ [ AS ] 化名 [ ( 列的化名 [, ...] ) ] ]
from列表中项 [ NATURAL ] 连接操作的类型 from列表中项 [ ON 用连接操作的条件 |
USING ( 用于连接操作的列 [, ...] ) ]
with查询语句是:
WITH查询语句名称(with_query_name) [ ( 列名称 [, ...] ) ] AS ( 查询 | 值 | in
sert | update | delete )
TABLE [ ONLY ] 表名 [ * ]
postgres=#
看这命令对于学习sql语法很有用。
------------------------------------华丽丽的分割线------------------------------------
------------------------------------华丽丽的分割线------------------------------------
PostgreSQL 的详细介绍:请点这里
PostgreSQL 的下载地址:请点这里