SQL语法整理

SELECT

1SELECT attribute-expression-list
2FROM relation-list
3[ WHEREcondition ];
attribute-expression-list:
  • [relation-name.]attribute
  • [relation-name.]attribute [arithmatic computation] AS another name
    • i.e., E.Salary - 40000 AS SalaryDiff
  • CASE WHEN ... THEN ...
    ELSE ... END
    • i.e., CASE WHEN E.Salary < 40000 THEN 0
      ELSE E.Salary - 40000 END
relation-list:
  • list of table names
  • seperate by comma
condition:
  • arithmetic operation +, -, *, /
  • comparisions =, <>, <, <=, >. >=
  • logical connectives AND, OR, NOT
  • attribute IN (Q)
  • attribute NOT IN (Q)
  • attribute op SOME (Q)
  • attribute op ALL (Q)
  • EXISTS (Q)
  • NOT EXISTS (Q)
  • IS [NOT] NULL

NULL


AND Table
ANDTRUEFALSENULL
TRUETRUEFALSENULL
FALSEFALSEFALSEFALSE
NULLNULLFALSENULL

OR Table
ORTRUEFALSENULL
TRUETRUETRUETRUE
FALSETRUEFALSENULL
NULLTRUENULLNULL

NOT Table
NOTTRUEFALSENULL
 FALSETRUENULL

UNION INTERSECT EXCEPT


(Q1 and Q2 must have same attribute-list)

Q1 UNION Q2 => Together all the tuples in Q1 and Q2
Q1 INTERSECT Q2 => Only tuples in both Q1 and Q2
Q1 EXCEPT Q2 => Tuples only in Q1 but not in Q2

ALL关键字: 允许重复
UNION ALL will include twice of duplicate tuples
INTERSECT ALL will include all possible pairs of match tuples, duplication possible
EXCEPT ALL will include all "not in Q2" tuples, duplication possible

[INNER]/OUTER JOIN

  1. Inner join把两个表连接在一起, 返回两个表中相匹配的记录, 是2和3的交集
  2. Left outer join, 左侧表所有的记录都返回, 右侧匹配的记录返回, 没有匹配的返回Null
  3. Right outer join, 与Left outer join相反, 右侧的记录返回, 左侧返回匹配的记录, 没有匹配返回Null
  4. Full outer join, 2和3的并集
  5. Cross join, 两个表的笛卡儿积, 返回所有可能的值, 不允许有连接条件

ORDER BY

1SELECT ...
2...
3ORDER BY attribute [DESC/ASC], attribute [DESC/ASC], ......

Note: 如果没有指定Order, return的数据可能是任意顺序

GROUP BY, HAVING, Aggregate expressions

{count, sum, avg, min, max} => Aggregate expressions

Order: Group => Having => Aggregate
  • count(*): number of tuples
  • count(E): number of tupple for which E is non-NULL
  • count(distinct E): number of distinct non-NULL E values
  • sum(E)
  • sum(distinct E)
  • avg(E)
  • avg(distinct E)
  • min(E)
  • max(E)

Note: 没有被group by指定的attribute不能出现在SELECT的attr-list中, 除非是aggregate

INSERT INTO

1INSERT INTO relation-name
2[( attribute-list )]
3VALUE ( value-list );

DELETE

1DELETE FROM relation-name
2[ WHEREcondition ];

UPDATE

1UPDATE relation-name
2SET attribute-assignment-list
3[ WHEREcondition ];
attribute-assignment-list:
  • pairs of assignment
  • seperate by comma
  • i.e., WorkDept = 'E01', Address = 'Waterloo'

CREATE TABLE

1CREATE TABLE relation-name(
2     attribute-nameattribute-type [constraints-list], 
3     ...
4)

attribute-type: http://www.w3school.com.cn/sql/sql_datatypes.asp
constraints-list: (Constraints的格式在各种数据库中都不太一样, 就不列举了)
  • NOT NULL
  • PRIMARY KEY
  • UNIQUE
  • FOREIGN KEY
  • Column or Tuple CHECK

CREATE VIEW

1CREATE VIEW view-nameAS (
2     SELECT...
3)
从View SELECT的方法和table一样

CREATE TRIGGER

1CREATE TRIGGER trigger-name
2AFTER UPDATE OF attribute-listON relation-name
3REFERENCING OLD asinstance-name(o) NEWas instance-name(n)
4FOR EACH ROW
5...
不同database语法不一  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值