有关数据库的 基本操作



----学习 数据库


-- 左右连接
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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值