Postgresql流水帐(第一天): SQL Syntax

4.1. Lexical Structure SQL命令的结构or组成)

 

A token can be a key word, an identifier, a quoted identifier, a literal (or constant), or a special character symbol. 

SQL语句的基本组成

关键字:SELECT, INSERT, UPDATE, DELETE

带双引号的标识符(delimited identifier or quoted identifier):

    加了双引号后,关键字就可以作为 标识符 如表名或字段名, select * from "select" where "insert"=2

标识符:表名、字段名等

关键字和标识符都是大小写不敏感的,关键字一般大写。

UPDATE MYTABLE SET a=5 等价于 update myTable set a =5

 

常量(Constants):

字符串常量: select 'foo' from bar

C语言风格的字符串常量:转义字符…, 牵扯到数据库系统的字符集,两者要一致

Backslash Escape Sequence

Interpretation

\b

backspace

\f

form feed

\n

newline

\r

carriage return

\t

tab

\o, \oo, \ooo (o = 0 - 7)

octal byte value

\xh, \xhh (h = 0 - 9, A - F)

hexadecimal byte value

\uxxxx, \Uxxxxxxxx (x = 0 - 9, A - F)

16 or 32-bit hexadecimal Unicode character value

 

Dollar-quoted String Constants

写函数时:

$function$

BEGIN

RETURN ($1 ~ $q$[\t\r\n\v\\]$q$);

END;

$function$

Bit-string Constants

B'1001', 有点儿像python里的 bytes b'hello'

Numeric Constants

42
3.5
4.
.001
5e2
1.925e-3

 

Operators

+ - * / < > = ~ ! @ # % ^ & | ` ?

 

Special Characters

. * : ; [] () $

Comments

单行注释

-- This is a standard SQL comment

多行注释

/* multiline comment

* with nesting: /* nested block comment */

*/

 

 

 Operator Precedence(操作符优先规则)

OpSELECT 5 ! - 6;

SELECT 5 ! - 6;

SELECT 5 ! (- 6);

SELECT (5 !) - 6; 不懂这是啥意思

 

Operator/Element

Associativity

Description

.

left

table/column name separator

::

left

PostgreSQL-style typecast

[ ]

left

array element selection

+ -

right

unary plus, unary minus

^

left

exponentiation

* / %

left

multiplication, division, modulo

+ -

left

addition, subtraction

(any other operator)

left

all other native and user-defined operators

BETWEEN IN LIKE ILIKE SIMILAR

  

range containment, set membership, string matching

< > = <= >= <>

  

comparison operators

IS ISNULL NOTNULL

  

IS TRUE, IS FALSE, IS NULL, IS DISTINCT FROM, etc

NOT

right

logical negation

AND

left

logical conjunction

OR

left

logical disjunction

posted @ 2016-04-08 15:31  songlihong  阅读(239)  评论(0编辑  收藏  举报