SQL语法整理
2012-02-06 10:01
253 查看
SELECT
1 | SELECT attribute-expression-list |
2 | FROM relation-list |
3 | [ WHERE condition ]; |
[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 TableAND | TRUE | FALSE | NULL |
TRUE | TRUE | FALSE | NULL |
FALSE | FALSE | FALSE | FALSE |
NULL | NULL | FALSE | NULL |
OR | TRUE | FALSE | NULL |
TRUE | TRUE | TRUE | TRUE |
FALSE | TRUE | FALSE | NULL |
NULL | TRUE | NULL | NULL |
NOT | TRUE | FALSE | NULL |
FALSE | TRUE | NULL |
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
Inner join把两个表连接在一起, 返回两个表中相匹配的记录, 是2和3的交集Left outer join, 左侧表所有的记录都返回, 右侧匹配的记录返回, 没有匹配的返回Null
Right outer join, 与Left outer join相反, 右侧的记录返回, 左侧返回匹配的记录, 没有匹配返回Null
Full outer join, 2和3的并集
Cross join, 两个表的笛卡儿积, 返回所有可能的值, 不允许有连接条件
ORDER BY
1 | SELECT ... |
2 | ... |
3 | ORDER BY attribute [ DESC / ASC ], attribute [ DESC / ASC ], ...... |
GROUP BY, H***ING, Aggregate expressions
{count, sum, avg, min, max} => Aggregate expressionsOrder: 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
1 | INSERT INTO relation- name |
2 | [(attribute-list )] |
3 | VALUE (value-list ); |
DELETE
1 | DELETE FROM relation- name |
2 | [ WHERE condition ]; |
UPDATE
1 | UPDATE relation- name |
2 | SET attribute-assignment-list |
3 | [ WHERE condition ]; |
pairs of assignment
seperate by comma
i.e., WorkDept = 'E01', Address = 'Waterloo'
CREATE TABLE
1 | CREATE TABLE relation- name ( |
2 | attribute- name attribute-type [constraints-list], |
3 | ... |
4 | ) |
constraints-list: (Constraints的格式在各种数据库中都不太一样, 就不列举了)
NOT NULL
PRIMARY KEY
UNIQUE
FOREIGN KEY
Column or Tuple CHECK
CREATE VIEW
1 | CREATE VIEW view - name AS ( |
2 | SELECT ... |
3 | ) |
CREATE TRIGGER
1 | CREATE TRIGGER trigger - name |
2 | AFTER UPDATE OF attribute-list ON relation- name |
3 | REFERENCING OLD as instance- name (o) NEW as instance- name (n) |
4 | FOR EACH ROW |
5 | ... |
相关文章推荐
- SQL语法整理[6]——函数
- SQL语法整理[5]——存储过程
- SQL 基础语法整理
- SQL语法整理[4]——视图
- sql基本语法复习和整理
- SQL基础语法笔记教程整理
- SQL语法整理[8]——SQL语言编程
- SQL语句 - 1 - 语法整理
- [导入]全面接触SQL语法(网络收集,整理转载)
- SQL语法整理[7]——触发器
- SQL常用语法网络整理版本
- SQL语法整理[10]——事务与锁
- SQL 语法整理
- SQL语法整理[9]——索引
- SQL_Server 常用语句以及语法整理
- SQL基础语法笔记教程整理
- SQL常用语法网络整理版本
- SQL语法粗整理
- 数据库语言(一):SQL语法实例整理
- sql 基本语法整理