Oracle数据库笔记

一、

1.创建表空间的语法

  create tablespace 表空间名称
  ​
  datafile 文件名 size 初始大小 [reuse][autoextend[off|on[next 增长量][maxsize[unlimited|最大容量]]
  ​
  [online|offline]

1、创建表空间stuspace

  
  create tablespace stuspace
  datafile 'E:\Oracle\app\oradata\ORCL\studata01.dbf'
  size 200m autoextend on next 50m maxsize 500m;

2、创建临时表空间stutempspace

  
  create temporary tablespace stutempspace
  tempfile 'E:\Oracle\app\oradata\ORCL\stutemp01' size 50m
  extent management local uniform size 15m;

3、为表空间stuspace增加数据文件studata02alter tablespace stuspace

  
  add datafile 'E:\Oracle\app\oradata\ORCL\studata02.dbf'
  size 200m autoextend on next 50m maxsize 500m;

4、为表空间stuspace 删除studata02alter tablespace stuspace

  
  drop datafile 'E:\Oracle\app\oradata\ORCL\studata02.dbf';

5、设置空间联机状态或脱机状态

  
  alter tablespace stuspace online
  alter tablespace stuspace offline
  host copy E:\Oracle\app\oradata\ORCL\studata01.dbf E:\Oracle\app\studata01.dbf

6、删除表空间stutempspace

  
  drop tablespace stutempspace including contents and datafiles;

1.创建用户tem

  
  create user tem identified by 123 default tablespace stuspace 
  temporary tablespace stutempspace
  quota unlimited on stuspace 
  1. 修改用户锁定状态

  
  alter user tem account lock
  alter user tem account unlock

3、查询权限

  
  select * from system_privilege_map; 
  select * from dba_sys_privs; 
  select * from user_sys_privs; 

4、创建角色

  
    create role sturole
    grant connect,resource to sturole;
    grant sturole to tem;
    revoke sturole from tem;

1、用户2、授权、角色3、时间格式

  
  select sysdate from dual
  yyyy/mm/dd
  alter session set nls_date_format='yyyy/mm/dd'

1、修改student表,增加字段dorm

  
  alter table student add drom char(5) modify sname varchar(12) 

2、录入数据

  
  (1)insert into ... values
  (2)insert into 表明(字段1,...)
  select 值1,... from dual union=

三、

1、建表、录入数据

2、从已有用户下的复制整个数据表(1)赋权

  
  grant select on student to tem
  select * from staff.sdept

(2)导入数据

  
  insert into sdept select * from staff.sdept

(2)复制数据表

  
  create table student as select * from staff.student
  select * from student

(3)注意事项设置某列不可用

  
  alter table course set unused column grade;

复制数据表:

  
  create table score as select * from staff.score

4、使用SQLPLUS导入数据连接数据库c:>sqlplus/nolgsql>conn tem/123@myowner 设置日期格式:

  
  alter session set nls_date_format='yyyy/mm/dd'

执行脚本文件:@d:\student.sql

  
  drop table failgrade
  create table failgrade as select student.sno,student.sname,course.cname,score.grade from student,course,score where student.sno = score.sno and score.cno = course.cno and score.grade < 60
  select * from failgrade

视图:1、创建视图

注意权限问题,需赋予create_view系统权限

  
  create[or replace][force|noforce] view 视图名(列名1,……) as 子查询
  [with check option][with read only]

船舰每个学生的所有成绩视图

  
  create  or replace view v_allcj
  as select student.sno,sname,cname,grade from student,course,score
  where student.sno=score.sno and course.cno=score.cno

(2)查询视图

  
  select * from v_allcj
  select * from user_views

测试:

  
  update v_allcj set grade=150 where cname='C语言' and grade<60

(3)创建所有女生视图

  
  create or replace view c_stf as select sno,sname,ssex,birthdate,deptno
  from student where ssex='女' 
  with check option
  select * from v_stf

测试:

  
  update v_stf set deptno='01' where sname='张庚源'
  create or replace view c_stf as select sno,sname,ssex,birthdate,deptno
  from student where ssex='女' 
  with read only
  (只读)

创建分区

  1. sys用户创建表空间(只有sys用户可以创建)

  2. 为用户tem分配表空间配额

  3. 分别创建范围分区表、列表分区表、复合分区表

  4. 设置表空间的读写状态、可用性

  5. 测试查询数据、更新数据

创建范围分区

  
  create tabke 表名()
  ​
  partition by range(属性列1[,属性列2...])
  ​
  {
  ​
  partition p1 values less than(最大值) tablespace 表空间名
  ​
  partition p2 values less than(最大值) tablespace 表空间名
  ​
  ..............
  ​
  }

1、创建表空间

  
  create tablespace stusubspace1
  datafile 'E:\Oracle\app\oradata\ORCL\stusubdata1' size 20m
  autoextend on next 10m maxsize 50m
  create tablespace stusubspace2
  datafile 'E:\Oracle\app\oradata\ORCL\stusubdata2' size 20m
  autoextend on next 10m maxsize 50m
  create tablespace stusubspace3
  datafile 'E:\Oracle\app\oradata\ORCL\stusubdata3' size 20m
  autoextend on next 10m maxsize 50m
  create tablespace stusubspace4
  datafile 'E:\Oracle\app\oradata\ORCL\stusubdata4' size 20m
  autoextend on next 10m maxsize 50m

查询表空间:

  
  select * from dba_tablespace;

2、给tem用户分配表空间使用配额

  
  alter user tem
  quota unlimited on stusubspace1
  quota 10m on stusubspace2
  quota unlimited on stusubspace3
  quota 10m on stusubspace4

3、创建范围分区表

  
  create table teacherpart
  (tno varchar(15) primary key,
   tname varchar(10) not null,
   tsex char(4),
   hiredate date, 
   grad varchar(30),
   position varchar(10),
   deptno char(2) references sdept(deptno)
  )
  partition by range(hiredate)
  (
  partition p1 values less than(to_date('1975/01/01','yyyy/mm/dd')) tablespace stusubspace1,
  partition p2 values less than(to_date('1980/01/01','yyyy/mm/dd'))tablespace stusubspace2,
  partition p3 values less than(to_date('1990/01/01','yyyy/mm/dd')) tablespace stusubspace3
  );

4、插入数据

  
  insert into teacherpart
  select '2007053','刘贺','女','1980/01/03','师范大学','院长','01' from dual union
  select '2005126','李俊','男','1977/08/01','工业大学','教师','01' from dual union
  select '2004161','刘书通','男','1976/08/09','东北师大','院长','02' from dual union
  select '2003012','王翠茂','男','1975/06/18','林业大学','教师','02' from dual union
  select '2001102','王丽','女','1973/11/11','农业大学','院长','03' from dual union
  select '2010217','赵文艳','女','1982/07/05','工程大学','教师','03' from dual union
  select '2001161','洪哲','男','1972/10/08','工业大学','院长','04' from dual union
  select '2004078','王翠茂','男','1976/09/10','商业大学','教师','04' from dual

5.查询数据

  
  select * from teacherpart
  select * from teacherpart where tno='2001161'

6.设置某一表空间不可用(sys用户)

  
  alter tablespace stusubspace2 offline

7.设置某一表空间为只读(sys用户)

  
  alter tablespace stusubspace2 online
  alter tablespace stusubspace2 read only

8.对只读表空间的数据进行更新操作,进行测试:

  
  update teacherpart set grad='上海交大' where tno='2005126'

9.设置某一表空间为可读写(sys用户)

  
  alter tablespace stusubspace2 read  write

select * from tem.student1、创建公有同义词

  
  lcreate public synonym temstu for tem.student
  select * from temstu

2、创建私有同义词

  
  create synonym temscore for tem.score
  select * from temscore

3、访问授权

  
  grant select on temscore to system

4、删除同义词

  
  drop public synonym temstu
  查询索引列表
  select * from dba_sequences

1、create[unique] 索引名 on 表明(字段名)

  
  create index in_stusname on student(sname)

2、

  
  create unique index in_courcname on course(cname)
  select * from tem.score

1.创建序列

  
  create sequence seq_book start with 1 increment by 2 maxvalue 5 nocache nocycle

2.创建数据表book

  
  oono number primary key,bookname char(20))

3.录入数据

  
  insert into book values(seq_book.nextval,'java');
  insert into book values(seq_book.nextval,'oracle');

4.查询数据

  
  select * from book
  insert into book values(seq_book.nextval,'mysql');
  insert into book values(seq_book.nextval,'jsp');
  select seq_book.currval from dual

5.修改序列

  
  alter sequence seq_book increment by 1 maxvalue 25 

6.查询序列(sys用户)

  
  select * from dba_sequences
  ​
  insert into student(sno,sname,ssex) values('111','李军','m')
  不要违反约束,数据类型、长度、顺序要和字段匹配

2.将成绩表中不及格的C语言的成绩全部修改为60分

  
  update score set grade=60 where grade<60 and
  cno in(select cno from course where cname='C语言')

3.删除score表中所有计算机专业学生的成绩数据

  
  delete from score where sno in(select sno from student where 
  deptno in(select deptno from sdept where dname like'%计算机%')) 
  rollback

4.查询入职时间在1980/01/01和1985/01/01之间的所有教师信息

  
  select * from teacher where hiredate
  between to_date('1980/01/01','yyyy/mm/dd') and to_date('1985/01/01','yyyy/mm/dd')

5.查询课程的基本情况,学分降序排列

  
  select * from course order by ccredit desc

6.统计每门课程的选课人数,列出课程号和人数。

  
  select cno,count(sno) from score group by cno

7.查询修课门数等于或大于3门的学生的平均成绩和选课门数。

  
  select sno,count(*),avg(grade) from score group by sno having count(*)>=3 

8.笛卡尔积查询,通常没意义

  
  select * from sdept,student
  select * from sdept cross join student 

9.使用内连接和笛卡尔积两种形式查询通信学院所有学生的基本情况。

  
  select * from sdept,student where dname like'%通信%'
       and sdept.deptno=student.deptno
  select * from sdept inner join student on student.deptno=sdept.deptno
       and dname like'%通信%' 

10.使用oracle外连接out join查询每个人的选修课成绩情况左外连接:

  
  select student.sno,sname,grade from student left outer join 
  score on student.sno=score.sno

右外连接:

  
  select student.sno,sname,grade from student right outer join 
  score on student.sno=score.sno

11.自身查询,查询与高天在同一个系学习的学生的姓名

  
  select s2.sname from student s1 inner join student s2 on s1.deptno= s2.deptno and  

12.查询计算机学院院长的基本情况。

  
  select * from teacher where title='院长' and deptno=
  (select deptno from sdept where dname like'%计算机%')

13.查询学生表中01、02院系的所有学生

  
  select * from student where deptno='01'
  union 
  select * from student where deptno='02' 

重点:top-n查询语法:select 查询 offset n(查询行数) rows [fetch(定义的区间) first [n] row with ties]

14.查询学生表中第6条以后的所有学生数据

  
  select * from student  offset 5 rows

15.查询score表中成绩为1~3名的所有数据

  
  select * from score order by grade desc fetch  first 3  rows with ties

16.查询score表中成绩为6~10名的所有数据

  
  select * from score order by grade desc offset 5 rows fetch  first 3  rows with ties

查询版本

  
  select * from v$version

一、选择结构1.if ... then ... else ... end if例1:查询选修学分高于10分的学生人数情况2.if...then...elsif...else...end if例2:查询某人的成绩是哪个成绩段?优秀、良好、中等、及格、不及格

  
  declare
    n score.grade%type;
  begin
    select grade into n from score where sno='0601020212' and cno='B001';
    if n>=90 then
       dbms_output.put_line('优秀');
    elsif n>=80 then
       dbms_output.put_line('良好');
    elsif n>=70 then
       dbms_output.put_line('中等');
    elsif n>=60 then
       dbms_output.put_line('及格');
    else
       dbms_output.put_line('不及格');
    end if;
  end;
  set serveroutput on

二、多分支结构语法:

  
  case  
    when...then... ;
    when...then... ;
    else...
  end case;

例3:使用case语句查询某人的成绩是哪个成绩段?优秀、良好、中等、及格、不及格

  
  declare
    n score.grade%type;
  begin
    select grade into n from score where sno='0601020212' and cno='B001';
    case
      when n>=90 then dbms_output.put_line('优秀');
      when n>=80 and n<90 then dbms_output.put_line('良好');
      when n>=70 then dbms_output.put_line('中等');
      when n>=60 then dbms_output.put_line('及格');
      else dbms_output.put_line('不及格');
    end case;
  end;  

三、循环结构1.简单循环语法:loop ...if...then exit end if... end loop;select * from score例4:遍历score表中所有数据

  
  declare
    i int default 1;
    n score%rowtype;
    k int;
  begin
    select count(*) into k from score;
    loop
      select a.sno,a.cno,a.grade into n from 
          (select rownum id,score.* from score) a where a.id=i;
      dbms_output.put_line('学号:'||n.sno||'  课程号:'||n.cno||'  成绩:'||n.grade);
      i:=i+1;
      if i>k then
        exit;
      end if;
    end loop;
  end;

(1)rownum的用法

  
  select rownum,score.* from score where rownum=2

(2)查找任意指定数据

  
  select * from (select rownum id,score.* from score) where id=6
  select a.sno,a.cno,a.grade from (select rownum id,score.* from score) a 
         where a.id=6

2.while ... loop ... end loop;例5:使用while循环遍历score表中所有数据

  
  declare
    i int default 1;
    n score%rowtype;
    k int;
  begin
    select count(*) into k from score;
    while i<=k
    loop
      select a.sno,a.cno,a.grade into n from 
          (select rownum id,score.* from score) a where a.id=i;
      dbms_output.put_line('学号:'||n.sno||'  课程号:'||n.cno||'  成绩:'||n.grade);
      i:=i+1;
    end loop;
  end;

3.for i in 1..n loop ...end loop;例5:使用for循环遍历score表中所有数据

  
  declare 
    n score%rowtype;
    k int;
  begin
    select count(*) into k from score;
    for i in 1..k
    loop
      select a.sno,a.cno,a.grade into n from
        (select rownum id,score.* from score) a where a.id=i;
      dbms_output.put_line(n.sno||n.cno||n.grade);  
    end loop;  
  end;

一、预定义异常1.no_data_found异常例1:查不到数据

  
  declare 
   n score.grade%type;
  begin
    select grade into n from score where sno='06010202' and cno='B001';
    if n>=60 then
      dbms_output.put_line('及格');
    else
       dbms_output.put_line('不及格');
    end if;
    exception 
      when no_data_found then dbms_output.put_line('no data');
  end;

2.too_many_rows异常例2:根据姓名查询学生学号

  
  insert into student
  values('22222','高天','女',to_date('2003/07/21','yyyy/mm/dd'),'01');
  
  select * from student where sname='高天';

(3)编写代码

  
  declare
    n student.sno%type;
  begin
    select sno into n from student where sname=&sname;
    dbms_output.put_line(n);
    exception      
      when too_many_rows then dbms_output.put_line('many values');
  end;

二、非预定义异常1.删除数据

  
  delete from course where cno='A001'
  ​
  ​
  declare
    err_references exception;
    pragma exception_init(err_references,-02292);
  begin
    delete from course where cno=&cno;
    exception
      when err_references then dbms_output.put_line('数据被引用'); 
  end;

2.子查询返回多个值

  
  declare
    n score.grade%type;
    err_subselect exception;
    pragma  exception_init(err_subselect,-01427);
  begin
    select grade into n from score 
      where sno=(select sno from student where sname=&sname);
    if n>=60 then
      dbms_output.put_line('及格');
    else
      dbms_output.put_line('不及格');
    end if;
    exception
      when err_subselect  then dbms_output.put_line('子查询返回多个值'); 
  end;

3.向score表录入数据时,需要考虑三种情况:外键问题、数据类型错误、录入数据与字段个数不匹配

​三、用户自定义异常1.工资表,现对所有员工涨工资,工资总额要低于20000,超过取消调整,未超确认调整。创建数据employee

  
  create table employee
  (empid char(5) primary key,
   ename char(8),
   sal number(10,2))
  set serveroutput on

获取学生的姓名和所属院系名称

  
  declare
  type recstu is record(
  vsname student.sname%type,
  vdname sdept.dname%type
  );
  n recstu;
  begin
  select sname,dname into n from sdept,student
  where sdept.deptno=student.deptno and sno = &sno;
  dbms_output.put_line('姓名:'||n.vsname||',院系:'||n.vdname);
  exception 
  when no_data_found then dbms_output.put_line('no data');
  end;

游标

一、分类

  • 显式游标

  • 隐式游标

二、显示游标

  1. 定义游标 carsor .. is select

  2. 打开游标 open

  3. 提取数据 fetch ... into ...

  4. 关闭游标 close

四、游标属性

  • %isopen 表示的是此刻否打开游标,是布尔类型,当打开时为true,一般用的比较少;

  • %found 最近一次fetch后是否取到结果,布尔类型,如果没有,则为false;

  • %notfound 最近一次fetch后是否取到结果,布尔类型,如果有结果就返回true

  • %rowcount 返回的是游标取到的行数 为number类型

例1:获取学生的姓名获取学号,院系编号

  
  declare
    cursor cur_stu is select sno,deptno from student where sname=&sname;
    n student.sno%type;
    m student.deptno%type;
  begin
    open cur_stu;
    loop
      fetch cur_stu into n,m;
      exit when cur_stu%notfound;
      dbms_output.put_line(n||m);
    end loop;
    close cur_stu;
  end;

例2:获取学生的学号获取学生选修的所有课程名称,成绩

  
  declare
    cursor cur_stu is select cname,grade from course,score
       where sno=&sno and course.cno=score.cno;
    n cur_stu%rowtype; 
  begin
    open cur_stu;
    loop
      fetch cur_stu into n;
      exit when cur_stu%notfound;
      dbms_output.put_line(n.cname||n.grade);
    end loop;
    close cur_stu;
  end;

二、for循环结合

例3:改写获取学生的学号获取学生选修的所有课程名称,成绩

  
  declare
    cursor cur_stu is select cname,grade from course,score
       where sno=&sno and course.cno=score.cno;
  begin
    for rec in cur_stu 
    loop
      dbms_output.put_line(rec.cname||rec.grade);  
    end loop;
  end;

例4:改写例3获取学生的学号获取学生选修的所有课程名称,成绩

  
  begin
    for rec in (select cname,grade from course,score
       where sno='0601020212' and course.cno=score.cno) 
    loop
      dbms_output.put_line(rec.cname||rec.grade);  
    end loop;
  end;
  ​
  alter table employee add job VARCHAR(10)
  ​
  select * from employee
  insert into employee values('010','xiaoming','6000','president');
  insert into employee values('012','xiaoli','5000','manger');
  insert into employee values('013','xiaoxu','4000','sale');
  update employee set job='giao' where empid='012'

(3)使用游标

  
  declare 
  cursor cur_empup is select job from employee for update of sal;
  n employee.job%type;
  m employee.sal%type;
  begin
  open cur_empup;
  loop
      fetch cur_empup into n;
      exit when cur_empup%notfound;
      if n='president' then
          update employee set sal=sal*1.5 where current of cur_empup;
      elsif n='manager' then
          update employee set sal=sal*1.3 where current of cur_empup;
      else
          update employee set sal=sal*1.1 where current of cur_empup;
      end if;
  end loop;
  select sum(sal) into m from employee;
  if m>30000 then
      dbms_output.put_line('cancle');
      rollback;
  else
      dbms_output.put_line('sure');
      commit; 
  end if;

二、函数1、系统函数数学、字符串、日期、转换函数

  
  select power(3,2) ,round(3.678,2),mod(10,4) from dual;
  select chr('70') ,length('hello world') from dual;
  select avg(grade) from score where cno=upper('a001');
  select sno,replace(substr(sno,3,2),'0','c'),sname from student;

3.根据学号获得该生的选修总学分,使用异常处理。(1)创建函数

  
   create or replace function f_getcredit
     (n score.sno%type)
     return char
  is
     err_nodata exception;
     m course.ccredit%type;
  begin
     select sum(ccredit) into m from course,score
     where course.cno=score.cno  and sno=n;
     if m is null then
   raise err_nodata;
      else
       return to_char(m);
          end if;
     exception
      when err_nodata then return 'no data';
      end;

(2)调用函数

  
  select f_getcredit('0601020212') from dual

二、存储过程:1、特点执行速度快,确保数据安全,自动完成预先执行的任务。2、语法

  
  create or replace procedure 存储过程的名称
  (n  in 数据类型,m out 数据类型 ,k in out 数据类型)
  is|as
  ​
  begin
  ​
  end;

执行:exec存储过程

例一:创建一个能够统计某医学院学生人数的存储过程

  
  create or replace procedure p_countstu
  (n in sdept.dname%type)
  is
  m int;
  begin
  select count(*) into m from student where
      deptno=(select deptno from sdept where dname = n);
  dbms_output.put_line(n||'院系人数:'||m||'人')
  end;

执行:

  
  exec p_countstu('计算机学院');
  set serveroutput on;

例4:创建存储过程,查找某一门课程的所有不及格学生的学号,课程号、成绩将其存储在不及格表中

  
  drop table failex

(1)建表

  
  create table failex
  (sno char(15),
  cno char(15),
  grade number(3,2))

(2)创建存储过程思路:根据课程名称查找,游标处理多条数据

  
  create or replace procedure p_failex
    (n course.cname%type)
  is
    cursor cur_scfail is select * from score where grade<60 
      and cno=(select cno from course where cname=n);
    m cur_scfail%rowtype;
  begin
    open cur_scfail;
    loop
       fetch cur_scfail into m;
       if cur_scfail%rowcount=0 then
          dbms_output.put_line('nodata');
          exit;
       else
          exit when cur_scfail%notfound;
          insert into failex values(m.sno,m.cno,m.grade);
          commit;
       end;
    end loop;
    dbms_output.put_line('已处理'||cur_failex.rowcount||'数据');
    close cur_failex;
  end;

(1)创建存储过程

  
  create or replace procedure p_failex
    (n course.cname%type)
  is
    cursor cur_scfail is select * from score where grade<60 
      and cno=(select cno from course where cname=n);
    m cur_scfail%rowtype;
  begin
    open cur_scfail;
    loop
       fetch cur_scfail into m;
       if cur_scfail%rowcount=0 then
          dbms_output.put_line('nodata');
          exit;
       else
          exit when cur_scfail%notfound;
          insert into failex values(m.sno,m.cno,m.grade);
          dbms_output.put_line('已处理'||cur_scfail%rowcount||'数据');
          commit;
       end if;
    end loop;
    dbms_output.put_line('已处理'||cur_scfail%rowcount||'数据');
    close cur_scfail;
  end;

(2)执行:exec p_failex('计算机导论');(3)验证:select * from failex

创建存储过程,能够根据学号删除student表中数据,考虑异常情况:外键引用问题和没有找到数据问题。

  
  create or replace procedure p_delstudent
    (n in student.sno%type)
  is
    err_nodata exception;
    err_refer exception;
    pragma exception_init(err_refer,-02292);
  begin
    delete from student where sno=n;
    if sql%notfound then
      raise err_nodata;
    else
      dbms_output.put_line('已删除'||sql%rowcount||'行数据');
      commit;
    end if;
    exception 
      when err_nodata then dbms_output.put_line('no data');
      when err_refer then dbms_output.put_line('有外键引用,无法删除');
  end;

执行:

  
  exec p_delstudent('0702010205');

二、触发器

  
  delete from student where  sno='0702010205'

1.触发器类型DML触发器、替代触发器、系统触发器2.定义触发器

  
  create trigger 触发器名
    before|after|instead of
    delete|insert|update of  列名 on 表名
    [for each row][when 触发条件]
  begin
    触发器执行体;
  end;

例1:创建触发器,实现更新student表中sno的值时,可以级联更新其他表中引用该列的sno值。

  
  create or replace trigger tri_upd_stu
    before update of sno on student
    for each row
  begin
    update score set sno=:new.sno where sno=:old.sno;
  end;

测试:

  
  update student set sno='99999' where sno='0702010205';
  select * from score

例2:创建触发器,实现删除course表中数据时,可以级联删除引用该列其他表中的数据。

  
  create or replace trigger tri_del_cour
    before delete on course
    for each row
  begin
    delete from score where cno=:old.cno;
  end;

测试:

  
  delete from course where cno='A001';

安全管理一、用户

  
  create user 用户名 identified by 密码 default tablespace 表空间
  quota unlimited on 表空间
  profile 概要文件

二、权限grant 权限 to 用户

三、角色role:connect、resource

四、概要文件1.查询概要文件相关信息

  
  select * from dba_profiles;
  select username,profile from dba_users;

2.创建概要文件

  
  create profile stuprofile limit
    资源配置参数
    sessions_per_user 3
    connect_time 30
    idle_time 1
    cpu_per_call 1000
    口令设置
    failed_login_attempts 3
    password_lock_time 1

3.为用户指定概要文件

  
  alter user tem profile stuprofile

一、闪回技术的作用1.作用:SQL语句恢复数据2.完成内容:

  1. 查询数据库过去某一时刻的状态。

  2. 查询反映过去一段时间内数据变化情况的元数据。

  3. 将表中数据或删除了的数据表恢复到过去某一时刻的状态。

  4. 自动跟踪、存档数据变化信息。

  5. 回滚事务及其依赖事务的操作。

3.闪回技术分类:

  • 闪回查询

  • 闪回表

  • 闪回删除二、闪回查询1.闪回查询原理:利用数据库撤销表空间的回退信息。2.实现功能

  • 返回丢失的快照

  • 当前数据与数据快照之间的比较

  • 可以检查过去某一时刻事务操作的结果

  • 简化应用设计

3.闪回查询的语法Select …from … as of scn|timestamp where…4.基于as of timestamp的闪回查询五个步骤

三、闪回表1.作用:将表及附属对象一起恢复到以前的某个时刻2.满足以下条件:

  • flashback any table 的系统权限和对象权限。

  • 具有select 、update、delete、insert对象权限。

  • undo_retention设置合理。

  • 行移动特征。

3.闪回表操作

四、闪回删除1.作用:通过数据库回收站技术恢复使用的drop 命令删除的表。2.管理回收站

  1. 查询回收站状态

  2. 修改回收站状态

  3. 查看回收站内容

  4. 清除回收站

  5. 删除数据表

  6. 闪回删除操作

一、expdp1.创建文件夹2.创建对象目录

  
  create directory studir as 'd:\backup';
  select * from  dba_directories;

3.授权:目录读写;导入\导出

  
  grant read,write on directory studir to tem;
  grant exp_full_database,imp_full_database to tem;

4.导出问题:ora-03237原因:表空间的本地管理模式参数"minimum extent"=4*db_block_size

  
  show parameter db_block_size;
  select * from dba_tablespaces;

(1)导出表

  
  expdp tem/123@myowner directory=studir dumpfile=temsc tables=tem.score

(2)导出用户

  
  expdp tem/123@myowner directory=studir dumpfile=temall schemas=tem

(3)导出表空间

  
  expdp tem/123@myowner directory=studir dumpfile=stuspace tablespaces=studentspace

5.导入(1)恢复数据表数据删除操作:

  
  delete from tem.score;
  commit;
  select * from tem.score

导入数据表数据:

  
  impdp tem/123@myowner directory=studir dumpfile=temsc tables=tem.score content=data_only

(2)恢复数据表删除操作:

  
  drop table tem.score;
  commit;
  select * from tem.score

导入数据表:

  
  impdp tem/123@myowner directory=studir dumpfile=temsc tables=tem.score nologfile=y

(3)恢复模式删除操作:

  
  drop table tem.score;
  drop table tem.student;
  commit;
  select * from tem.scor

恢复模式:

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值