Oracle相关知识点(续)

查看当前登录的用户的表:
select *from tab;
select table_name from user_tables;
一.子查询
可以在主查询的where select having from后面使用子查询
select empno,ename,sal,(select job from emp where empno=7839) 第四列 from emp

select * from (select empno,ename,sal from emp );

  1. 不可以在group by后面使用子查询
  2. 主查询和子查询可以不是同一张表;只要子查询返回的结果 主查询可以使用即可
    select * from emp where deptno=(select deptno from dept where dname=’SALES’);
  3. 一般不在子查询排序;但top-n分析问题中,必须对子查询排序
  4. 一般先执行子查询,再执行主查询;但相关子查询例外
  5. 单行子查询只能使用单行操作符;多行子查询只能使用多行操作符
    多行操作符:
    in 在集合中 查询部门名称是SALES和ACCOUNTING的员工
    select * from emp where deptno in (select deptno from dept where dname=’SALES’ or dname=’ACCOUNTING’);

any:和集合中任意一个值比较 查询工资比30号部门任意一个员工高的员工信息
select * from emp where sal > any (select sal from emp where deptno=30);

all: 和集合中的所有值比较 查询工资比30号部门所有员工高的员工信息
select * from emp where sal > all (select sal from emp where deptno=30);

  1. 子查询中的null 查询不是老板的员工
    select * from emp where empno not in (select mgr from emp where mgr is not null);

二.集合查询 union/union all(并集) 、intersect(交集) 、minus(差集)
关于集合运算
1. 参与运算的各个集合必须列数相同 且类型一致
2. 采用第一个集合作为最后的表头
3. order by永远在最后
4. 括号,括号决定执行顺序,有括号的先执行。

select deptno,job,sum(sal) from emp group by deptno,job
 union
 select deptno,to_char(null),sum(sal) from emp group by deptno
 union
 select to_number(null),to_char(null),sum(sal) from emp;
 等价于
select deptno,job,sum(sal) from emp group by rollup(deptno,job);

三.SQL执行时间的开关
set timing on/off

四.处理数据
0.SQL的类型
0.1 DML(data manipulation language 数据操作语言):insert update delete select
0.2 DDL(data definition language 数据定义语言): create table,alter table,drop table,truncate table, create/drop view,sequence,index,synonym(同义词)
0.3 DCL(data control language 数据控制语言):grant(授权) revoke(撤销权限)

1.插入数据
insert into emp(empno,ename,sal,deptno) values(&empno,&ename,&sal,&deptno);

**一次性将emp中所有10号部门的员工插入到emp10中
insert into emp10 select * from emp where deptno=10;

2.oracle创建一张与其他表相同表结构的空表
–只是建立ta表,与emp表结构相同,并不添加数据
–这种构造与现存表相同结构的表,是不会将comment带过来的
create table t1 as select * from scott.emp where 1=0;

–建立tb表,结构与dept结构相同,将dept表中的数据导入其中
–这种构造与现存表相同结构的表,是不会将comment带过来的:
create table t2 as select * from scott.dept where 1=1;

2.1、where 1=1

恒为true, 用于动态构造不确定条件数的查询语句,动态构造效果如下:

select  *  from  emp  where  1=1  [and 条件n];

2.2、where 1=0

恒为false, 无数据返回,常用于快速建表,eg:

create  table  emp_copy  as  (select  *  from  emp  where  1=0).

3.海量插入数据
3.1. 数据泵(PLSQL程序:dbms_datapump)
3.2. SQL*Loader工具
3.3. 外部表

4.delete和truncate的区别:
4.1. delete逐条删除;truncate先摧毁表 再重建
4.2. (*)delete是DML(可以回滚) truncate是DDL(不可以回滚)
4.3. delete不会释放空间 truncate会
4.4. delete可以闪回(flashback) truncate不可以
4.5. delete会产生碎片 truncate不会
4.6 关闭反馈语句 set feedback off;

5.去掉碎片
5.1 alter table 表名 move;
5.2 数据的导出和导入
导出:exp/ expdp
导入:imp/ impdp

6.事务
Oracle中事务的标志
6.1起始标志:事务中第一条DML语句
6.2. 结束标志:提交 显式 commit
隐式 正常退出exit,DDL,DCL
回滚 显式 rollback
隐式 非正常退出,掉电,宕机
7.保存点
savepoint a;
rollback to a;
8.delete和truncate
在oracle中delete比truncate的执行效率要高,原因是oracle中有undo数据(还原数据)

9.事务的隔离级别
9.1 oracle支持3种事务隔离级别:read commited,serializable.read only
oracle默认的事务隔离级别为:read commited
9.2 mysql支持4种事务隔离级别:read uncommited,read commited,repeatable read ,serializable(串行化).
mysql默认的事务隔离级别为:repeatable read

五.关于rownum
1. rownum永远按照默认的顺序生成
2. rownum只能使用< <=;不能使用> >=
–oracle分页(Pageing Query)

select *
 from   (select rownum r,e1.*
     from (select * from emp order by sal) e1
     where rownum <=8
    )
 where r >=5;

3 .oracle分为三种表:标准表,索引表,临时表
rownum始终按照产生的标准表来生成顺序,而order by生成的是临时表
临时表:
3.1. 手动: create global temporary table *
3.2. 自动:排序
特点:当事务或者会话结束的时候,表中自动删除
**基于事务的临时表,当commit时表中的数据会被删除。

create global temporary table temptest1
(tid number,tname varchar2(20))
 on commit delete rows;

**基于会话的临时表,当exit退出会话时,数据会被删除
create global temporary table temptest1
(tid number,tname varchar2(20))
on commit preserve rows;

六.相关子查询:将主查询中的值 作为参数传递给子查询
select empno,ename,sal,(select avg(sal) from emp where deptno=e.deptno) avgsal from emp e
where sal > (select avg(sal) from emp where deptno=e.deptno);

七.创建和管理表
1.使用子查询创建表举例
创建表:保存20号部门的员工
create table emp20
as
select * from emp where deptno=20;

2.修改表:追加列,修改列,删除列,重命名列,重命名表
–增加photo保存照片
alter table test1 add photo blob;
–修改列
alter table test1 modify tname varchar2(40);
–删除列
alter table test1 drop column photo;
–重命名列
alter table test1 rename column tname to username;
–重命名表
rename test1 to test2;

3.删除表
drop table test2;
–Oracle的回收站
–查看回收站
show recyclebin;

–清空回收站
purge recyclebin;

-oracle 10g:6种不同闪回 oracle11g:7种
–闪回删除
—-flashback table “BIN f s u i V f 1 U T O a 5 q v l z p 6 i n R g == 0” to before drop;
flashback table TESTSAVEPOINT to before drop;
–回收站中有两个重名的表
flashback table TESTSAVEPOINT to before drop rename to TESTSAVEPOINT_OLD;

————–注意:管理员没有回收站——–
4.用户认证方式
4.1 密码认证
sqlplus sys/password as sysdba

4.2 主机认证(外部认证)
sqlplus sys/wqwqwq(任意) as sysdba
sqlplus opopop(任意)/wqwqwq(任意) as sysdba

主机认证优先于密码认证,当oracle安装成功时,计算机管理下回自动创建一个名为ora_dba的组,组下的用户均为数据库的管理员,只有管理员用户才可以通过主机认证,否则不可。

4.3全局认证

八,约束
1.check 检查约束

create table test3
 (
    tid number,
    tname varchar2(20),
    gender varchar2(2) check (gender in ('男','女')),
    sal number check (sal > 0)
 );

2.references:指定表和父表中的列
on delete cascade:当删除父表时,级联删除子表记录
on delete set null:将子表的相关依赖记录的外键置为Null

3.关键字constaint 给约束起别名

create table student
  (
    sid number constraint student_pk primary key,
    sname varchar2(20) constraint student_name_notnull not null,
    gender varchar2(2) constraint student_gender check (gender in ('男','女')),
    email varchar2(40) constraint student_email_unique unique
                        constraint student_email_notnull not null,
     deptno number constraint student_fk references dept(deptno) on delete set null
     );

猜你喜欢

转载自blog.csdn.net/lee_charles/article/details/81866397