oracle笔记

 

select e.ename,s.grade from emp e join salgrade s on e.sal between s.losal and s.hisal; --不等连接[between...and...包括两个端点值]
select e.ename,s.grade from emp e,salgrade s where e.sal between s.losal and s.hisal;--不等连接
select ename,dname from emp left outer join dept on dept.deptno=emp.deptno;--左外连接
select ename,dname from emp right join dept on dept.deptno=emp.deptno;--右外连接
select ename,dname from emp inner join dept on dept.deptno=emp.deptno;--内连接
select ename,dname from emp natural join dept;--自然[内]连接
select ename,dname from emp full join dept on dept.deptno=emp.deptno;--全连接
select ename,dname from emp  join dept on dept.deptno=emp.deptno;--默认内连接 
select manager.ename as manager,worker.ename as worker from emp manager,emp worker where manager.empno=worker.mgr;--自连接
select manager.ename as manager , worker.ename as worker from emp manager full join emp worker on manager.empno=worker.mgr;--自连接

--更新数据
原数据:update emp set sal=2450,comm=null where ename='CLARK';
新数据:update emp set sal=1500,comm=30 where ename='CLARK'; 
select * from emp where ename='CLARK';

select * from emp where deptno=(select deptno from emp where ename='SCOTT');--单行子查询 
select * from emp where job in(select distinct job from emp where deptno=10);--多行子查询用in
select * from emp where sal > all(select sal from emp where deptno=30);--多行子查询用all 可转换为单行子查询
select * from emp where sal > any(select sal from emp where deptno=30);--多行子查询用any 可转换为单行子查询
select * from emp where (deptno,job)=(select deptno,job from emp where ename='SMITH');--多列单行子查询
select * from emp where ((sal,nvl(comm,-1)) in (select sal,nvl(comm,-1) from emp where deptno=30));--多列 成对比较
select * from emp where (sal in (select sal from emp where deptno=30) and nvl(comm,-1) in (select nvl(comm,-1) from emp where deptno=30));--多列非成对比较 
select ename,job,sal,deptno from emp where exists(select * from dept where emp.deptno=dept.deptno and dept.loc='NEW YORK');--相关子查询
select ename,job,sal,deptno from emp natural join dept where loc='NEW YORK';--上面语句的等效转换
select ename,job,sal,dep.deptno,avg_sal from emp,( select deptno,avg(sal) as avg_sal from emp group by deptno) dep where emp.deptno=dep.deptno and sal>dep.avg_sal;--from子句子查询

select ename||' is a '||job detail from emp ;--连接
select * from emp where sal>2500 union select * from emp where job='MANAGER';--union查询
select * from emp where sal>2500 or job='MANAGER';--or代替union查询
select * from emp order by comm;--order排序 null值在后面
select deptno,sal from emp order by deptno desc,2;--多列排序 
select count(comm) from emp;--统计列值个数时,空值不被统计
select max(sal),min(sal) from emp ;--取得最大、最小值
select avg(sal),sum(sal) from emp;--平均、总和
select count(distinct deptno) dept_count from emp;--统计部门总数
select deptno,avg(sal),max(sal) from emp group by deptno;--单列分组(部门)
select deptno,job,avg(sal),max(sal) from emp group by deptno ,job;--多列分组(部门,岗位)
select deptno,avg(sal) as avg_sal,max(sal) as max_sal from emp group by deptno having avg(sal)<2500;--having限制结果
select ename,dep.deptno,sal ,avg_sal from emp,( select deptno,avg(sal) avg_sal from emp group by deptno)dep where emp.deptno=dep.deptno and emp.sal<dep.avg_sal order by deptno;--显示低于部门平均工资的雇员


 CREATE TABLE EMPloyee (------------单表插入
 "EMPNO" NUMBER(4) NOT NULL,
 "ENAME" VARCHAR2(10 byte), 
  "SAL" NUMBER(7, 2),
 "DEPTNO" NUMBER(2));
 select * from employee;
 insert into employee (empno,ename,sal,deptno) select empno,ename,sal,deptno from emp where deptno=20;--将部门是20的数据插入EMPloyee表
 
  CREATE TABLE dept10 (-------------多表插入
 "EMPNO" NUMBER(4) NOT NULL,
 "ENAME" VARCHAR2(10 byte), 
  "SAL" NUMBER(7, 2),
 "DEPTNO" NUMBER(2));
 
CREATE TABLE dept20 (
 "EMPNO" NUMBER(4) NOT NULL,
 "ENAME" VARCHAR2(10 byte), 
  "SAL" NUMBER(7, 2),
 "DEPTNO" NUMBER(2));
 
 CREATE TABLE other (
 "EMPNO" NUMBER(4) NOT NULL,
 "ENAME" VARCHAR2(10 byte), 
  "SAL" NUMBER(7, 2),
 "DEPTNO" NUMBER(2));  
 select * from dept10;
 select * from dept20;
 select * from other;
 insert all when deptno=10 then into dept10 when deptno=20 then into dept20 else into other 
 select empno,ename,sal,deptno from emp;--部门10的插在子表dept10...以此类推
 
 insert first when deptno=10 then into dept10 when deptno=20 then into dept20 else into other
 select empno,ename,sal,deptno from emp;--"当使用first操作符执行多表插入时,如果数据已经满足了先前条件,
 --并且已经被插入到某表,那么该行数据在后续的插入中将不会再次使用"  不能理解
 
 update dept10 set(ename,sal)= (select ename,sal from dept10 where empno=7839) where empno=7782 ;--查询更新数据
 delete emp where deptno=(select deptno from dept where dname='SALES');--查询删除数据(删除销售部的所有雇员)
alter user scott account unlock;--账号解锁
grant connect to scott;--赋予权限
grant create view to scott;
grant create table to scott;
grant create session to scott;
grant create role to scott;
grant create user to scott;
grant drop user to scott;
revoke connect from scott;-- 移除权限

create user tian identified by dddd[ default tablespace users quota 10M on users];--创建用户
create table d(t number);
insert into d values(1234);
alter table d drop column t;--删除列
alter table d add tt varchar2(12);
select * from d;

create table classes(
       c_id number,
       cname varchar2(20),
       constraint pk_classes primary key (c_id) --主键
);
create table student(
       s_id number,
       sname varchar2(20),
       cid number,
       constraint pk_student primary key (s_id),--主键
       constraint fk_classes foreign key (cid) references classes(c_id)--外键
);
insert into classes values (1,'java培训班');
insert into classes values (2,'c++培训班');
insert into student values(1,'张三',1);
insert into student values (2,'李四',1);
delete from classes cascade where c_id=1;

select * from dba_role_privs; 
select * from dba_sys_privs; 
select * from role_sys_privs;
select * from user_sys_privs;--查询用户权限
select * from user_role_privs;--查询用户角色
select dep.* from (select rownum r,t.* from scott.emp t where rownum<10) dep where dep.r>=5;--分页查询

你可能感兴趣的:(笔记,仅此而已)