首页 > 代码库 > 5个SQL核心

5个SQL核心

5个核心的SQL语句

1.SELECT

-查询语句的逻辑处理顺序
5     SELECT <columnlist>
1     FROM <source objectlist>
1.1   FROM <left source object><join type>
    JOIN <right source object>ON <on predicates(谓词,判断)>
2     WHERE <wherepredicates>
3    GROUP BY <group byexpression(s)>
4    HAVING <having predicates>
6    ORDER BY <order by list>
-FROM子句:
    处理联结语句的顺序
1、交叉联结,也称笛卡尔积;
2、内联结;
3、外联结。
-GROUP BY子句
将执行FROM和WHERE后得到的经过筛选后的结果集进行聚合。结果集按照GROUPBY子句列出的表达式进行分组,来为每一个分组得出一行汇总结果。
可按照FROM子句中所列出对象的任意字段进行分组,即使并不在结果中显示该列。但SELECT子句列表中的任何非聚合字段都必须包含在GROUPBY表达式中。 
GROUPBY还可包含两个附加运算:ROLLUP,CUBE。ROLLUP产生部分求和值,CUBE求得交互分类值。
-HAVING子句
   HAVING子句将分组汇总后的查询结果限定为只有该子句中的条件为真的数据行。它和GROUP BY位置可互换,但逻辑上是GROUPBY子句先执行。本质上看,HAVING子句是在GROUPBY子句执行后用来筛选汇总值的第二个WHERE语句。对结果集过滤。
    *HAVING子句后只能接分组运算限定条件
-SELECT列表
   列出查询的返回最终结果集中需要显示哪些列。可以是数据表中的一个实际列、一个表达式,甚至是一个SELECT语句的结果。
    eg:selectc.customer_id,c.customer_first_name||‘‘||c.customer_last_name,(select e.last_name from hr.employees ewhere e.employee_id = c.account_mgr_id)
-ORDER BY子句
    用来对最终结果集排序。
   需要估计需要排序的数据量大小。太大时会使用临时磁盘空间来完成排序。

2.INSERT语句
-单表插入
    insert intohr.jobs(job_id,job_title,min_salary,max_salary)
    values(‘IT_PM‘,‘ProjectManager‘,5000,11000);
    --增加10%的分红
    insert into scott.bonus(ename,job,sal)
    select ename,job,sal * .10
    from scott.emp;
-多表插入
    下面例子阐明了一个子程序返回的数据行是如何被用来插入多个表中的。从3个表开始:small_customers、medium_customers、large_customers。按照每位消费者所下订单的总金额(order_total)来将数据分别插入这些表。
INSERT ALL
WHEN sum_orders < 10000 THEN
INTO small_customers
WHEN sum_orders >=   10000 AND sum_orders <100000THEN
INTO medium_customers
ELSE
INTO large_customers
SELECT customer_id,sum(order_total) sum_orders
FROM orders
GROUP BY customer_id;

3.UPDATE语句

UPDATE tablename SET columnname=value WHEREpredicates
-使用表达式更新一个单列的值
    UPDATE employee
    SET sal = sal * .10 --increase salary by10%
   WHEREdepartment_id=90;
-通过子查询更新一个单列的值
    UPDATE employee
    SET sal=(SELECT employee2.salary
                   FROM employee2
                   WHERE employee2.employee_id=employee.employee_id
                   AND employee.salary != employee2.salary)
    WHERE department_id=90;
-通过在WHERE子句中使用子查询确定要更新的数据行来更新单列的值。
    UPDATE employee
    SET salary=salary* .10
    WHERE department_id IN (SELECTdepartment_id
                                               FROM departments
                                               WHERE department_name=‘Excutive‘);
-通过使用SELECT 语句定义及列的值来更新表
UPDATE (SELECT e1.salary,e2.salary new_sal
FROM employees e1,employees2 e2
WHERE e1.employee_id=e2.employee_id
AND e1.department_id=90)
SET salary = new_sal;
-通过子查询更新多列。
UPDATE employees
SET (salary,commission_pct) = (SELECT employees2.salary, .10comm_pct
  FROM employees2
   WHEREemployee2.employee_id=employees.employee_id
    AND employees.salary != employees2.salary)
WHERE department_id=90;

4.DELETE语句


DELETE FROM tablename WHERE predicates
-1.使用WHERE子句中的筛选条件来指定表中的删除行。
DELETE FROM employees2
WHERE department_id = 90;
-2.使用FROM子句中的子查询来删除行。
DELETE FROM (SELECT * FROM employees2 WHERE department_id =90);
-3.使用WHERE子句中的子查询来指定从表中删除的行。
DELETE FROM employees2
WHERE department_id IN (SELECT department_id
     FROM departments
      WHERE department_name =‘Excutive‘);

5.MERGE语句

    该语句具有按条件获取要更新或插入到表中的数据行,然后从1个或多个源头对表进行更新或向表中插入行两方面的能力。常被用在数据仓库中来移动大量的数据。这个操作一个很大的附加值在于你可以很方便地把多个操作结合成一个。
-基本语法
MERGE <hint>
INTO <tablename>
USING <table_view or query>
ON (<condition>)
WHEN MATCHED THEN <update_clause>
DELETE <where_clause>
WHEN NOT MATCHED THEN <insert_clause>

[LOG ERRORS <long_errors_clause> <reject limit <integer | unlimited>];
-例子
MERGE
INTO dept60_bonus bUSING (
SELECT employee_id,salary,department_id
FROM employees
WHERE department_id = 60) e
ON (b.employee_id = e.employee_id)
WHEN MATCHED THEN
UPDATE SET b.bonus_amt = e.salary * 0.2 WHERE b.bonus_amt = 0
DELETE WHERE (e.salary > 7500)
WHEN NOT MATCHED THEN
INSERT (b.employee_id,b.bonus_amt)
VALUES(e.employee_id,e.salary*0.1)
WHERE (e.salary < 7500)



来自为知笔记(Wiz)