Oracle备份/还原

本文介绍了如何在Oracle数据库中创建表空间,为用户分配权限,以及执行数据的备份和恢复操作。详细步骤包括创建表空间、增加数据文件、创建用户并授予不同级别的权限,以及使用EXP和IMP命令进行完整或部分数据库的导出与导入。此外,还提到了处理空表的方法以及在操作过程中需要注意的权限问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用户表空间及权限

-- 创建表空间(根据不同环境配置表空间的数据文件路径)
Create Tablespace TBS_XX Datafile 'D:\APP\ADMINISTRATOR\ORADATA\ORCL\TBS_XX_01.dbf' Size 100M Autoextend On Next 100M  Maxsize Unlimited;
-- 增加数据文件
Alter Tablespace TBS_XX Add Datafile 'D:\APP\ADMINISTRATOR\ORADATA\ORCL\TBS_XX_02.dbf' Size 100M Autoextend On Next 100M Maxsize Unlimited;

-- 创建用户及授权
-- Create the user 
create user test
  identified by 123456
  default tablespace TBS_XX;
-- Grant/Revoke role privileges 
grant connect to test;
grant dba to test;
grant resource to test;
-- Grant/Revoke system privileges 
grant alter system to test;
grant execute any procedure to test;
grant select any dictionary to test;
grant unlimited tablespace to test;

备份/导出

注:下面@orcl需要配置tnsnames.ora

1.导出全部
exp test/123456@orcl file=D:\backup.dmp full=y

2.导出指定用户
exp test/123456@orcl file=D:\backup.dmp owner=test

3.导出指定表(rows=n|y是否导出数据)
exp test/123456@orcl file=D:\backup.dmp tables=(table1,table2) rows=n

举个栗子:
exp test/123456@192.168.1.201:1521/orcl file=D:\backup.dmp log=D:\backup.log owner=test

还原/导入

注:下面@orcl需要配置tnsnames.ora

1.导入当前用户(异常依旧提交,忽略创建错误)
imp test2/123456@orcl file=D:\backup.dmp log=D:\backup.log ignore=y full=y commit=yes

2.导入指定用户
imp test2/123456@orcl file=D:\backup.dmp fromuser=test2 touser=test ignore=y commit=yes

3.导入指定表
imp test2/123456@orcl file=D:\backup.dmp tables=(table1,table2)  ignore=y commit=yes

举个栗子:
imp test2/123456@192.168.1.201:1521/orcl file=D:\backup.dmp log=D:\backup.log fromuser=test touser=test2 ignore=y commit=yes

注:操作用户需要拥有对应的权限

 

导出空表(对空表分配存储空间即可exp导出)

--导出dmp文件时,只有执行以下语句,才能导出0行数据的表结构.否则,dmp中不包含这些表.
--Alter Table Allocate Extent (Szie 500K); 对空表分配存储空间
--Alter Table Deallcoate Unused; 对表回收存储空间
Declare
  Cursor rows Is select table_name from sys.USER_TABLES where table_name not in (select table_name from sys.USER_PART_TABLES);
  sName   Varchar2(1024);
  temp    Number(15);
  tempSql Varchar2(1024);
Begin
  Open rows;
  Loop
    Fetch rows
      Into sName;
    Exit When rows%NotFound;
    tempSql := 'Select Count(*) From ' || sName;
  
    Execute Immediate tempSql
      Into temp;
    if temp = 0 then
      Execute Immediate 'alter table ' || sName || ' allocate extent (size 100k)';
    end if;
  End Loop;
  Close rows;
End;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值