您的位置:首页 > 数据库 > Oracle

【Oracle篇】管理常用对象

2014-09-30 14:25 183 查看
转载请注明出处:http://blog.csdn.net/feng1790291543

-- 将scott.emp 中20和30部门员工信息建立到 test用户中emp表

--(注:sysdba 下操作)

create table chenfeng.emp as select * from scott.emp where deptno!=10;

-- 将scott.dept中多有数据建立到test用户中dept表

create table dept(

d_no number(2) not null ,

d_name varchar2(14),

d_loc varchar2(13)

);

--- (注:sysdba 下操作)

insert into chenfeng.dept select deptno,dname,loc from scott.dept where deptno!=10;

insert into chenfeng.dept select deptno,dname,loc from scott.dept where deptno=20;

--在test用户下做如下操作

--使用命令,给emp表添加一个列:emp_address varchar2(20) 默认值 'YIN CHUAN'

alter table emp add emp_address varchar2(20) default 'YIN JING';

--使用命令,修改这个列默认值: 'BEI JING'

alter table emp modify emp_address default 'BEI CHUAN';



--使用命令,修改列名 emp_address 为 emp_addre

alter table emp rename column emp_address to emp_addre;



--使用命令,删除这个列

alter table emp drop column emp_addre;



--使用命令,删除emp表

drop table emp;



--使用命令,闪回emp表

flashback table emp to before drop;



--使用命令,将emp表名修改为 test_emp;

rename emp to chenfeng_emp;

rename chenfeng_emp to emp;



--在tet用户下做如下查询操作:



--查询信息如下:

--部门编号 部门名称 部门人数

select count(*) dept_count,d_no,d_name

from dept group by d_no ,d_name ;



---查询信息如下:并按照部门名称升序排序;工资降序排序;

--员工编号 员工名称 员工工资 部门名称 员工总收入(sal+comm) 占本部门总收入百分比

select e.empno,e.ename,e.sal,d.d_name,round((sal+nvl(comm,0))/(select sum(sal+nvl(comm,0)) from emp)*100,2)||'%' percent_salcom

from dept d,emp e order by d.d_name asc,e.sal desc;





--查询信息如下:

--输出每个部门工资最低的人员所有信息。



select e.*

from emp e

where (e.sal,e.deptno)in (select min(sal),deptno from emp group by deptno);



--查询信息如下:

--输出工资 高于其工作平均工资(对job分组查询的平均工资)的人缘信息

select e.*

from emp e,(select avg(sal) avg_sal,job from emp group by job) avg_emp

where e.sal>avg_emp.avg_sal and e.job=avg_emp.job;



--查询信息如下:

--对上题,使用相关子查询来输出;



select e.*

from emp e

where e.sal>(select avg(sal) from emp group by e.job);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: