http://blog.csdn.net/wuyinxian/article/details/7393398 2012
----学习 数据库 -- 左右连接 select * from emp ,dept where emp.deptno (+)= dept.deptno and dept.deptno = 40 -- 1999 笛卡尔集 自然连接 select * from emp ,dept select * from emp natural join dept ---指定消除笛卡尔集 select * from emp join dept using (deptno) --- 分组查询之后,只能出现改字段和统计函数, nal( , 0) ,函数是用来设定default的 select dept.dname ,count(dept.deptno) ,nvl(sum(emp.sal),0) from emp, dept where emp.deptno (+)= dept.deptno --and avg(emp.sal) >2000 group by dept.dname having avg(emp.sal) >2000 --- 测试 select (select dept.deptno from dept) dno ,* from emp where dno = emp.deptno where job <> 'SALESMAN' group by job order by sums ASC select * from emp order by sal --- /////////////验证 语句 start --- 验证子查询 在where 中主要返回 单行单列 ,多行单列,单行多列 select * from emp where sal > (select avg (sal) from emp ) --- 验证子查询 在where 中主要返回 单行多列 select * from emp where (job ,sal) =(select job ,sal from emp where emp.ename = 'SMITH') --- 验证 子查询总 在where 中返回 多行单列 --- ;有关 in 的操作 ---注意如果 not in 中有null 那么不会有任何结果返回 select * from emp where sal in (select sal from emp where emp.ename <> 'SMITH') -- 有关 any 操作 = 任何一个 > 比最小的要大, < 比最大的要小 select * from emp where sal >any (select sal from emp where emp.sal > 2000) -- 有关 all 操作 = (如果是多行,则不会有结果) > 比最大的要大, < 比最小的要小 select * from emp where sal =all (select sal from emp where emp.sal > 2000 ) --- 验证子查询 在where 中主要返回 单行单列 ,多行单列,单行多列 select * from emp natural join (select sal,job from emp where emp.ename <> 'SMITH') tmp ------ /////////////验证 语句 end ---下面考虑的是 在什么时候用子查询,这里说的是有负责的统计一般是会放在子查询中 select dept.deptno, dept.dname, dept.loc, tmp.sals from dept, (select deptno, sum(sal) sals from emp where emp.ename <> 'SMITH' group by deptno) tmp where tmp.deptno = dept.deptno ---数据迁移 create table tmpemp as select * from emp where emp.ename = 'SMITH' --数据跟新 insert into tmpemp select 2999 + 1, ename, job, mgr, sysdate, 3000, comm, deptno from emp where emp.sal < 2000 select * from tmpemp commit -- 删除数据 truncate drop delete delete from tmpemp where tmpemp.empno = 2999 commit -- TRUNCATE TABLE name [DROP/REUSE STORAGE] -- DROP STORAGE:显式指明释放数据表和索引的空间 -- REUSE STORAGE:显式指明不释放数据表和索引的空间 truncate table tmpemp drop storage ---truncate 不能加where条件 如: where tmpemp.empno = 3000 |
|