sys 超级管理员system 权限仅次于超级管理员(没有create权限)scott 普通用户oracledbconsole web控制台的服务(一般不启动)alter user scott account unlock;alter user scott identified by tiger;conn sys/sys as sysdba;disc;show user;password; //修改自己的密码password system; //修改别人的密码create user kk identified by kk;oracle系统权限(系统权限) create user create view create table create sequence create trigger create connect create sessionoracle对象权限(表数据的管理) select update delete insert 角色: connect resource(包含所有的create权限)给用户分配角色: grant connect,resource to test;回收角色: revoke connect,resource from test;删除用户: drop user xiaohong [cascade] 删除用户[删除用户建的对象]一个用户对象一套方案跨用户方案查询grant select on emp to xiaohong;conn xiaohong/xiaohongselect * from scott.emp; //带方案名oracle认证机制 conn sys/sys as sysdba; //会查看当前操作系统用户是否属于ORA_DBA组,如果是组中用户则不验证,直接采用sys用户登陆 可以通过更改network/sqlnet文件的SQLNET.AUTHENTICATION_SERVICES = (NONE)验证特权用户普通用户验证账号密码oracle找回管理员密码 D:\oracle\app\oracle\product\11.2.0\server\database\PWDXE.ora //保存密码文件cmd: orapwd file=D:\oracle\app\oracle\product\11.2.0\server\database\PWDXE.ora password=sys entries=10select t.owner, count(*) from all_tables t group by t.owner; //查询每个用户建了几张表select empno,dump(empno) from emp; //显示完整值,包括空格填充oracle数据类型 char 定长,最长2000个字符,中文占两个字符 varchar2 变长,最长4000个字符,中文占两个字符 nchar unicode存储,定长,最长2000个字符,无论汉字还是字符都占一个字符 clob 变长,最大4G字符 blob 变长,二进制存储,最大4G字符 number(p,s) 变长,p有效位,s保留位。number(6,-1) 123.4=120; number(6,2) 123.4=123.4 如果是整数位溢出则报错,小数位溢出不报错 date 格式dd/mm月/yy timestamp 时间戳<> 、!= //都表示不等于alter table t add (id number); //添加id列alter table t modify(id number); //修改id列类型alter table t drop(id); //删除id列rename class_ to class; //修改表名drop table student; //删除表analyze table table_name estimate statistics; //对表进行分析估算统计select table_name,num_rows from user_tables; //查询表的总记录数,需要先用上面命令统计select distinct id,name from students; //过滤相同行select ename , sal*13+nvl(comm*13,0) from emp; //nvl函数,如果结果是null则取0select ename||(sal*13+nvl(comm*13,0)) sal from emp; //合并列显示结果select ename||'的年工资'||(sal*13+nvl(comm*13,0)) sal from emp; //合并列显示结果,并增加自定义字符,使用单引号select * from emp where to_char(hiredate,'yyyy-mm-dd')>'1982-01-01';select * from emp where sal between 2000 and 2500; //闭区间,包含本身select job,count(job) from emp where comm is null group by job; //使用group by 必须返回至少一个group by的条件select ename , sal*13+nvl(comm*13,0) 年资 from emp order by 年资 desc; //oracle支持对别名的排序,不支持使用别名作为group by 和having的条件select sum(comm) from emp; //计算总和时,如果有值为null,则会忽略select avg(comm),count(*) from emp;//计算平均值是,如果有值为null,则会忽略,这样统计的值不准确,改为:select sum(nvl(comm,0))/count(*) from emp;select count(comm) from emp; //如果comm为null,会被忽略select avg(sal),min(sal),job,deptno from emp group by deptno,job; //每个部门每种岗位的平均工资和最低工资having //对group by 出的结果进行筛选,having的条件不能使用别名group by having order by //正确顺序select * from emp,dept; //总记录数等于emp*dept的记录数select ename,sal,grade from emp,salgrade where emp.sal between salgrade.losal and salgrade.hisal; //查询员工工资的等级select t1.ename,t2.ename from emp t1 left join emp t2 on t1.mgr=t2.empno; //左外连接,无论条件真假,至少t1.count(*)条记录,至多t1.count(*) * t2.count(*)条记录select * from emp t1 where (t1.job,t1.deptno)=(select job,deptno from emp t2 where t2.ename='ALLEN'); //多列子查询,仅oracle支持该语法; 查询本部门的同行select ename ,sal from emp order by 2; //2表示根据sal排序, mysql、oracle都支持查询高于本部门平均工资的员工 解法①:select t2.ename,sal,t1.avg,t1.deptno from emp t2 join (select deptno,avg(sal) avg from emp group by deptno)t1 on t2.sal>t1.avg and t2.deptno=t1.deptno; 解法②:select t1.* from emp t1 where t1.sal>(select avg(sal) from emp t2 where t1.deptno=t2.deptno); ps:解法1方便显示部门平均工资,解法2不能,扩展性差查询各部门工资最高的人 select ename,sal from emp t1 join(select max(sal) mymax,deptno from emp group by deptno)t2 on t1.sal=t2.mymax and t1.deptno=t2.deptno; select ename,sal from emp t1 where sal=(select max(sal) from emp t2 where t1.deptno=t2.deptno);查询各部门的编号、名称、人数 select t1.deptno,t1.dname,nvl(t2.c,0) count from dept t1 left join (select count(deptno) c ,deptno from emp group by deptno)t2 on t1.deptno=t2.deptno;分页查询 select t2.* from (select t1.* ,rownum rn from (select * from emp)t1 where rownum<=10)t2 where rn>=5; //先rownum<=10,再rn>=5拷贝表 create table mytest as select empno,ename,deptno,sal,comm from emp;自我复制 insert into mytest(empno,ename,deptno,sal,comm) select empno,ename,deptno,sal,comm from mytest;查询每个部门每种岗位的平均工资、每个部门的平均工资、每个岗位的平均工资 select deptno,avg(sal) from emp group by cube(deptno,job); //立方体函数外连 select * from emp,dept where dept.deptno=emp.deptno(+); //右外连; (+)写在左边表的条件字段后显示管理者的总人数 select count(distinct mgr) from emp;使用子查询完成更新 update emp set (sal,comm,job)=(select sal,comm,job from emp where ename='SMITH') where ename='ALLEN';脏读:一个事务读取到另一个事务尚未提交的数据。oracle中没有脏读不可重复读: 在事务1的二次相同查询中,如果事务2影响进行修改或删除,那么事务1的2次查询结果将不一致(记录数减少或内容改变)。幻读:在事务1的二次相同查询中,如果事务2影响进行增加,那么事务1的2次查询结果将不一致(记录数增加)。oracle事务隔离级别: read commited: 默认级别,不会出现脏读,会出现不可重复读和幻读 serializable:不会出现脏读、不可重复读、幻读 read only:只能执行查询,即不会出现脏读、不可重复读、幻读设置事务隔离级别: set transaction isolation level read committed;(默认) 事务不提交,不会被读到。 set transaction isolation level serializable;(串化) set transaction read only;(只读)sequence: 创建序列 create sequence myseq start with 0 minvalue 0 maxvalue 9999999 increment by 1 cache 10 --1次产生10个号,优点提高效率,缺点可能跳号 创建表 create table mytable( id_ number primary key not null, name_ varchar2(20) ); 插入数据 insert into mytable(id_,name_)values(myseq.nextval,'kk'); select myseq.currval from dual; 查看当前序列值索引: create index myindex on emp(ename); 建索引前提 1.数据量大 2.在经常用的where条件或连接条件上建索引 3.索引的层次不超过4层 聚集索引和非聚集索引: 聚集索引一个表只能有一个,而非聚集索引一个表可以存在多个。 聚集索引存储记录是物理上连续存在,而非聚集索引是逻辑上的连续,物理存储并不连续。 聚集索引的缺点是对表进行修改速度较慢,这是为了保持表中的记录的物理顺序与索引的顺序一致,而把记录插入到数据页的相应位置,必须在数据页中进行数据重排,降低了执行速度 主键默认有索引查询所有对象权限: select distinct privilege from dba_tab_privs; //需要dba角色查询查询所有系统权限: select distinct privilege from dba_sys_privs; //需要dba角色查询查询角色所拥有的权限: select * from dba_sys_privs t where t.grantee='RESOURCE';查询所有角色: select * from dba_roles;查询某个用户的所拥有的角色: select * from dba_role_privs t where t.grantee='SCOTT';查询某个用户所拥有的权限: select PRIVILEGE from (select * from dba_role_privs where grantee='SCOTT')t left join dba_sys_privs t2 on t2.grantee=t.granted_role;oracle对象权限属于级联回收: create user blake identified by jkl; create user jones identified by jkl; grant connect to blake; grant connect to jones; conn scott/tiger; grant select on emp to blake with grant option; conn blake/jkl; grant select on scott.emp to jones; conn scott/tiger; revoke select on emp from blake; conn jones/jkl; select * from scott.emp;创建自定义用户: create role myRole not identified; grant select on scott.emp to myRole; grant connect,resource to myRole;