Oracle Database :玩转Oracle学习笔记之(23):Oracle数据库管理--导入及导出

1、Oracle数据库管理员的职责:

1)、安装和升级Oracle数据库;
2)、创建表,表空间,数据库,视图,索引等;
3)、指定并实施备份与恢复计划;
4)、数据库权限管理,调优,故障排除;
5)、对于高级DBA(database administratro),要求能够参与项目开发,会编写sql语句、存储过程、触发器、规则、约束、包;

2、管理数据库的用户主要是SYS和SYSTEM;
1)、主要区别是:存储数据的重要性不同;
sys:所有oracle的数据字典的基表和视图都存放在sys中,这些基表和视图对于Oracle的运行时至关重要的,由数据库自己维护,任何用户都不能手动更改,sys用户拥有dba,sysdba,sysoper角色或权限,是oracle权限最高的用户;
SYS TEM:用于存放次一级的内部数据,如:Oracle的一些特性或工具的管理信息,SYSTEM用户拥有dab,sysdba角色或系统权限,没有sysoper系统权限;

2)、其次的区别是权限不同;
sys用户必须以as sysdba或者as sysoper形式登陆,不能以normal方式登陆数据库;
system如果正常登陆,他其实就是一个普通的dba用户,但是如果以as sysdba登陆,其结果 实际上是作为sys用户登录的,从登录信 息里面我们可以看出来的;

SQL> conn system/S123
Connected.
SQL> conn system/S123 as sysdba;
Connected.
SQL> show user;
USER is "SYS"

sysdba 和sysoper的区别:

sysdba:
启动数据库;
关闭数据库;
alter database open/mount/backup;
改变字符集;
create database;
delete database;
create profile;
alter database archivelog(归档日志);
alter database recover(恢复数据库);
拥有restricted session(会话限制)权限;
可以让用户作为sys用户连接;
登陆之后是sys;


sysoper:
启动数据库;
关闭数据库;
alter database open/mount/backup;
没有改变字符集权限;
不能创建数据库;
不能删除数据库;
create profile;
alter database archivelog(归档日志);
只能完全恢复数据库,不能执行不完全恢复;
拥有restricted session(会话限制)权限;
可以进行一些基本的操作,但不能查看用户数据;
登陆之后用户是public;

而DBA连启动和关闭数据库的权限都没有,dba只能在启动数据库后才能进行操作;


SQL> show user;
USER is "SYS"
SQL> shutdown;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup;
ORACLE instance started.

Total System Global Area  535662592 bytes
Fixed Size                  1375792 bytes
Variable Size             306184656 bytes
Database Buffers          222298112 bytes
Redo Buffers                5804032 bytes
Database mounted.
Database opened.


2、管理初始化参数;
显示参数命令:show parameter;


3、数据库表的备份与恢复;
逻辑备份是指使用工具export将数据库对象的结构和数据导出到文件的过程,逻辑恢复是指当数据库对象被误操作后而损坏后使用工具import利用备份的文件吧数据对象导入到数据库的过程,物理备份即可在数据库open的状态下进行也可以在数据库关闭后进行,三十逻辑备份和恢复只能在open状态下进行;


1)、导出:导出具体分为导出表,导出方案,导出数据库三种方式;使用exp命令来完成,该命令常用的参数选项有:
userid:用于指定执行导出操作的用户名,口令,连接字符串;
tables:用于指定执行导出操作的表;
owner:用于指定执行导出操作的方案;
full=y:用于指定执行导出操作的数据库;
inctype:用于指定执行导出操作的增量类型;(增量备份:可以设置为complete,这样在第二次备份的时候就会检查有没有新的数据写入到了数据库中,这样的备份要快点)rows:用于指定导出操作是否要导出表中的数据;
file:用于指定导出文件名;

注意:在导入和导出的时候,要到oracle主目录下面的bin目录下面去执行导入导出操作;

1、导出表:
1)、导出自己的表;
C:\OthrPromFiles\OraclePrdc\OrclDBDir\app\zhaohaim\product\11.2.0\dbhome_1\BIN>exp userid=scott/S123@orcl tables=(emp) file=c:\emp.dmp

Export: Release 11.2.0.1.0 - Production on Thu Sep 2 09:51:44 2010

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.


Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in WE8MSWIN1252 character set and AL16UTF16 NCHAR character set
server uses AL32UTF8 character set (possible charset conversion)

About to export specified tables via Conventional Path ...
. . exporting table                            EMP         16 rows exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
Export terminated successfully with warnings.


2)、导出多张表;
C:\>exp userid=scott/S123@orcl tables=(emp,dept) file=c:\emptwo.dmp

Export: Release 11.2.0.1.0 - Production on Thu Sep 2 09:55:22 2010

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.


Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in WE8MSWIN1252 character set and AL16UTF16 NCHAR character set
server uses AL32UTF8 character set (possible charset conversion)

About to export specified tables via Conventional Path ...
. . exporting table                            EMP         16 rows exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
. . exporting table                           DEPT          4 rows exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
Export terminated successfully with warnings.


3)、导出其他方案的表:如果要到处其他方案的表,则需要dba的权限或是exp_full_database的权限,比如system就可以导出scott的表;
C:\>exp userid=system/S123@orcl tables=(scott.emp,scott.dept) file=c:\empThree.dmp

Export: Release 11.2.0.1.0 - Production on Thu Sep 2 09:56:59 2010

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.


Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in WE8MSWIN1252 character set and AL16UTF16 NCHAR character set
server uses AL32UTF8 character set (possible charset conversion)

About to export specified tables via Conventional Path ...
Current user changed to SCOTT
. . exporting table                            EMP         16 rows exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
. . exporting table                           DEPT          4 rows exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
Export terminated successfully with warnings.


4)、导出表的结构:(只需要表的结构而不需要表的数据);
C:\>exp userid=scott/S123@orcl tables=(emp,dept) file=c:\empfour.dmp rows=n

Export: Release 11.2.0.1.0 - Production on Thu Sep 2 10:01:59 2010

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.


Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in WE8MSWIN1252 character set and AL16UTF16 NCHAR character set
server uses AL32UTF8 character set (possible charset conversion)
Note: table data (rows) will not be exported

About to export specified tables via Conventional Path ...
. . exporting table                            EMP
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
. . exporting table                           DEPT
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
Export terminated successfully with warnings.


5)、使用直接导出方式;(这种方式比默认的常规方式速度要快,当数据量大时,可以考虑这种方法)
这时需要数据库的字符集要与客户端的字符集完全一致,否则就会报错;
C:\>exp userid=scott/S123@orcl tables=(emp,dept) file=c:\empfive.dmp direct=y

Export: Release 11.2.0.1.0 - Production on Thu Sep 2 10:04:31 2010

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.


Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in WE8MSWIN1252 character set and AL16UTF16 NCHAR character set
server uses AL32UTF8 character set (possible charset conversion)

About to export specified tables via Direct Path ...
. . exporting table                            EMP         16 rows exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
. . exporting table                           DEPT          4 rows exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
Export terminated successfully with warnings.


2、导出方案:指使用export工具导出一个方案或者是多个方案中的所有对象(表,索引,约束...)和数据,并放到文件中去;

1)、导出自己的方案:
C:\>exp scott/S123@orcl owner=scott file=c:\scott.dmp
C:\>exp userid=scott/S123@orcl owner=scott file=c:\scott.dmp

Export: Release 11.2.0.1.0 - Production on Thu Sep 2 10:10:45 2010

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.


Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in WE8MSWIN1252 character set and AL16UTF16 NCHAR character set
server uses AL32UTF8 character set (possible charset conversion)
. exporting pre-schema procedural objects and actions
. exporting foreign function library names for user SCOTT
. exporting PUBLIC type synonyms
. exporting private type synonyms
. exporting object type definitions for user SCOTT
About to export SCOTT's objects ...
. exporting database links
. exporting sequence numbers
. exporting cluster definitions
. about to export SCOTT's tables via Conventional Path ...
. . exporting table                           DEPT          4 rows exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
. . exporting table                            EMP         16 rows exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
. . exporting table                          MYEMP         14 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table                       SALGRADE          5 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table                       TABLEONE          0 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table                          USERS          0 rows exported
EXP-00091: Exporting questionable statistics.
. exporting synonyms
. exporting views
. exporting stored procedures
. exporting operators
. exporting referential integrity constraints
. exporting triggers
. exporting indextypes
. exporting bitmap, functional and extensible indexes
. exporting posttables actions
. exporting materialized views
. exporting snapshot logs
. exporting job queues
. exporting refresh groups and children
. exporting dimensions
. exporting post-schema procedural objects and actions
. exporting statistics
Export terminated successfully with warnings.


2)、导出其他方案;如果是导出其他方案,则需要dba权限或者拥有exp_full_database权限,经过测试,system不能够导出sys的方案,但是能偶导出scott的方案;如果用system去导出sys的方案的话,最后会提出:EXP-00000: Export terminated unsuccessfully信息,指示导出不成功;
C:\>exp system/S123@orcl owner=(system,scott) file=c:\system_scott.dmp
C:\>exp system/S123@orcl owner=(scott) file=c:\scott.dmp


3、导出数据库:指利用export工具导出所有数据库中的对象以及数据,要求该用户具有dba或者具有exp_full_database权限;
C:\>exp userid=system/S123@orcl full=y inctype=complete file=c:\database.dmp

2)、导入:就是利用import工具将文件中的对象和数据导入到数据库中,但是idaoru要使用的文件必须为export所导出的文件,与导出相似,导入也分为导入表,导入方案,导入数据库三种方式,
imp常用的选项有:
userid:用于指定执行导入操作的用户名,口令,连接字符串;
tables:用于指定执行导入操作的表;
fromuser:用于指定源用户;
touser:用于指定目标用户;
file:用于指定导入文件名;
full=y:用于指定执行导入操作的数据库;
inctype:用于指定执行导入操作的增量类型;rows:用于指定导人操作是否要导入表行(表中的数据);
ignore:如果表存在,则只导入数据;

1、导入表;
1)、导入自己的表:
imp userid=scott/S123@orcl tables=(emp) file=c:\emp.dmp
C:\>imp userid=scott/S123@orcl tables=(emp) file=c:/empData.dmp

Import: Release 11.2.0.1.0 - Production on Thu Sep 2 11:23:16 2010

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.


Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

Export file created by EXPORT:V11.02.00 via conventional path
import done in WE8MSWIN1252 character set and AL16UTF16 NCHAR character set
import server uses AL32UTF8 character set (possible charset conversion)
. importing SCOTT's objects into SCOTT
. importing SCOTT's objects into SCOTT
. . importing table                          "EMP"         16 rows imported
About to enable constraints...
Import terminated successfully without warnings.


注意:这里我们如果这样写:
imp userid=system/Spongcer123@orcl tables=(emp) file=c:/empData.dmp
它的意思是将emp表导入到system用户中去,但是由于emp表总存在一个外间字段deptno,在导入的过程当中,数据库在system中寻找deptno这张表,并且看看是否符合emp表中要求的外键表的样式,但是system不存在这样一张表,因此就会出现导入失败,所以,可以得出结论,如果导入一张本来不属于本方案中的表,要想使得导入成功,那么被导入的表最好不要存在主外键关系;否则会出现导入失败;
比如:
C:\>imp userid=system/S123@orcl tables=(dept) file=c:/dept.dmp

Import: Release 11.2.0.1.0 - Production on Thu Sep 2 11:36:34 2010

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.


Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

Export file created by EXPORT:V11.02.00 via conventional path

Warning: the objects were exported by SCOTT, not by you

import done in WE8MSWIN1252 character set and AL16UTF16 NCHAR character set
import server uses AL32UTF8 character set (possible charset conversion)
. importing SCOTT's objects into SYSTEM
. importing SCOTT's objects into SYSTEM
. . importing table                         "DEPT"          4 rows imported
Import terminated successfully without warnings.



2)、导入表到其他用户:要求改用户具有dba权限,或是具有imp_full_database;
imp userid=system/S123@orcl tables=(emp) file=c:\emp.dmp touser=scott

3)、导入表的结构:只导入结构而不导入数据;
imp userid=scott/S123@orcl tables=(emp) file=c:\emp.dmp rows=n

4)、导入数据;如果对象(比如表)已经存在,可以只导入表的数据;
imp userid=scott/S123@orcl tables=(emp) file=c:\emp.dmp ignore=y

2、导入方案:指用import工具将文件中的对象和数据导入到一个或者是多个方案中,如果要导入其他方案,要求该用户具有dba的权限,或者是有imp_full_database;

1)、导入自身方案:
C:\>imp userid=scott/S123 file=scottSln.dmp

2)、导入其他方案;要求该用户具有dba权限;
imp userid=system/S123 file=c:/scott.dmp fromuser=system touser=scott

3)、导入数据库:在默认情况下,当导入数据库时候,会导入所有对象结构和数据;
imp userid=system/S123 full=y file=c:/database.dmp


这里甚至连数据库实例都不用写了,因为它会自动创建一个数据库实例;





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值