Linux下Oracle

发布时间 2024-01-04 00:16:44作者: KLAPT

1.导出文件

---导出到DATA_PUMP_DIR
expdp njchecker/password@192.168.0.120/orcl dumpfile=njchecker2021120701_120.dmp DIRECTORY=DATA_PUMP_DIR version=12.2.0.1.0

 解释:expdp 用户名/密码@ip/实例名 dumpfile=dmp文件 DIRECTORY=DATA_PUMP_DIR version=指定导到某个版本的数据库)

一、准备工作

1、创建directory 目录,用户导出的数据存放于此目录
SQL> create directory EXPDP_DUMP as '/test/expdp_dump';

2、查看创建的directory

SQL> select * from dba_directories;

3、授予EXPDP_DUMP目录权限给导出用户scott

SQL> grant read, write on directory EXPDP_DUMP to test;

4、删除 EXPDP_DUMP

SQL> drop directory EXPDP_DUMP;

二:EXP、EXPDP的不同

1、EXPDP必须要dierctory
2、EXPDP可以导出没有分配空间的空表
3、EXPDP导出过程中会建立一个JOB且 会产生一张表SYS_EXPORT_SCHEMA_01 , 导完后又会自动删除, 所以 EXPDP无法在read only模式下操作
4、EXP按表名的字母顺序导出表,而EXPDP随机的
5、默认同等情况下(不加压缩这样的参数),EXPDP导出快些,且生成文件占用磁盘空间也相对少些
6、 EXPDP是服务端程序,安装了oracle server才有,导出的文件存放在服务端;EXP是客户端程序,安装了oracle client就有,安装oracle server也有EXP是因为oracle server包含了oracle client,导出的文件存放在客户端。

 

三:查询dba的常用默认目录:
SQL> select * from dba_directories;
#或者 find / -name *.dmp

 

2.新建用户njchecktest2

(1)切换用户

su - oracle

(2)登录sqlplus,使用sysdba登录

sqlplus / as sysdba

(3)查询表空间存储位置(/u01/app/oracle/oradata/orcl/)

SQL> select name from v$tempfile;
NAME
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/orcl/qhchecker_temp.dbf
/u01/app/oracle/oradata/orcl/QHCHECKER2019_temp.dbf
/u01/app/oracle/oradata/orcl/njchecker2019_temp.dbf
/u01/app/oracle/oradata/orcl/njchecker2019fk_temp.dbf
/u01/app/oracle/oradata/orcl/kdchecker_temp.dbf

(4)在/u01/app/oracle/oradata/orcl/下 创建临时表空间

create temporary tablespace TMP_NJCHECKERTEST2 tempfile '/u01/app/oracle/oradata/orcl/njcheckertest2_temp.dbf' size 2G reuse autoextend on next 20m maxsize unlimited;

(5)创建数据表空间

create tablespace DATA_NJCHECKERTEST2 datafile '/u01/app/oracle/oradata/orcl/njcheckertest2_data.dbf' size 100M reuse autoextend on next 40M maxsize unlimited;

(6)创建索引表空间

create tablespace IDX_NJCHECKERTEST2 datafile '/u01/app/oracle/oradata/orcl/njcheckertest2_index.dbf' size 100m autoextend on next 32m maxsize 2048m extent management local;

(7)创建用户:njcheckertest2,密码:password 并分配 表空间:DATA_NJCHECKERTEST2

create user njcheckertest2 identified by password default tablespace DATA_NJCHECKERTEST2 temporary tablespace TMP_NJCHECKERTEST2;

(8)赋权dba给用户(重要步骤,否则该用户不能导入dmp)

grant dba to njcheckertest2;

 

导入文件:

1、创建临时存放文件的目录
create directory DATA_DIR as ‘/home/oracle/temp’;
2、给用户赋文件目录的读写权限,把导出的数据库文件njchecker20211106.dmp,暂放这个目录
grant read,write on directory DATA_DIR to system;


普通方式:同名同库同空间的导入

impdp njchecker/password@orcl directory=DATA_DIR dumpfile=njchecker20211106.dmp
#directory 不写默认:DATA_PUMP_DIR

特殊情况①:不同名,不同表空间,不同用户

impdp njcheckertest2/password@192.168.0.120/orcl directory=DATA_DIR dumpfile=njchecker20211106.dmp remap_tablespace=njchecker:njcheckertest2 remap_schema=njchecker:njcheckertest2 logfile=exdp_njcheckertest2.log table_exists_action=REPLACE;

(impdp 用户名/密码@ip/实例名 directory=DATA_PUMP_DIR dumpfile=导出的那个dmp文件 table_exists_action=REPLACE )

(
注释: remap_tablespace=source_tablespace:target_tablespace remap_schema=source_schema:target_schema table_exists_action=REPLACE
table_exists_action选项:{skip 是如果已存在表,则跳过并处理下一个对象;append是为表增加数据;truncate是截断表,然后为其增加新数据;replace是删除已存在表,重新建表并追加数据;
辅助记忆: 从source>>导入>>target

特殊情况② 跨数据库版本导数据 (从高到低版本需要指定版本;低版本向高版本迁移无需指定。)
如: 12C==>11g
在12C中备份出来时,应指定版本信息,例如我要导进11g的库(具体版本为11.2.0.1.0,打开sqlplus就能看到),则version=11.2.0.1.0
导出:expdp njcheckert/password@192.168.0.120/orcl dumpfile=njchecker20211106.dmp remap_tablespace=njchecker:DATA_NJCHECKERTEST2 remap_schema=njchecker:njcheckertest2 DIRECTORY=DATA_PUMP_DIR version=11.2.0.1.0
导入:impdp njcheckertest2/password@192.168.0.120/orcl directory=DATA_PUMP_DIR dumpfile=njchecker20211106.dpdmp full=y table_exists_action=REPLACE version=11.2.0.1.0

常见问题:
1. oracle14460,oracle impdp导入数据库报ora-14460错误解决方法:
在impdp命令加transform=segment_attributes:n参数即可

impdp username/password directory=impdp_dir dumpfile=*.dmp transform=segment_attributes:n

以上语句表示在导入时不导入任何表的已有存储属性

说明:transform_name有四个选项:segment attributes,storage,oid,pctspace

--segment attributes:段属性包括物理属性、存储属性、表空间和日志。

--storage:可以使用storage=y(默认)说明只取对象的存储属性作为导入作业的一部分。

--oid:如果指定oid=Y(默认),则在导入过程中将分配一个新的oid给对象表。

--pctspace:通过提供一个正数作为该转换的值,可以增加对象的分配尺寸,并且数据文件尺寸等于pctspace的值(按百分比)

value:transform_name的值,transform_name为segment attributes,storage,oid时值可为Y/N,pctspace为数值

object_type的值可以表、索引、表空间、类型、集群、约束等对象。


附:

切换数据库
su - oracle
登录数据库
sqlplus / as sysdba
切换PDB1
show pdbs;
设置会话
alter session set container=PDB1;
查看数据文件
select file_name from dba_data_files;
创建数据库空间
create tablespace orms_TBS datafile '/dddddd' size 1G autoextend on next 100M maxsize unlimited extent management local AUTOALLOCATE SEGMENT space MANAGEMENT AUTO;
创建索引表空间
create tablespace orms_TBS_INDEX datafile '/dddddd' size 1G autoextend on next 100M maxsize unlimited extent management local AUTOALLOCATE SEGMENT space MANAGEMENT AUTO;

创建临时表空间
create temporary tablespace tmp_data tempfile '/dddddd' size 1G reuse autoextend on next 100M maxsize unlimited ;

创建用户
create user orms identified by orms123 account unlock; /// create user hke identified by hke defaulte tablespace HKE_TBS temporary tablespace temp;
赋予角色
grant connect,resource to orms

分配权限
grant create procedure to hke with admin option;
grant create sequence to hke;
grant create session to hke;
grant create synonym to hke;
grant create table to hke;
grant create trigger to hke;
grant create type to hke;
grant create view to hke;
grant unlimited tablespace to hke;
grant create job to hke;

grant select on V_$SQLAREA to hke;
grant select on V_$SESSION to hke;
grant select on V_$LOCKED_OBJECT to hke;
grant select on V_$PARAMETER to hke;
grant select on V_$PROCESS to hke;
grant select on V_$RECOVERY_FILE_DEST to hke;
grant select on DBA_FREE_SPACE to hke;
grant select on DBA_DATA_FILES to hkeON_NAME for a30

创建逻辑目录:
create or replace directory dp_dir as '/oradata'
赋予权限:
grant read,write on directory dp_dir to ORMS;


修改库名
ALTER DATABASE RENAME GLOBAL_NAME TO new_db_name;
/*修改原字段名*/
ALTER TABLE 表名 RENAME COLUMN 字段名 TO 字段名1;
/*添加一个字段同名的字段*/
ALTER TABLE 表名 ADD 字段名 VARCHAR2(30);
查询当前登录用户下的所有表的名称
SELECT TABLE_NAME FROM USER_TABLES;

步骤一: 删除user

drop user ×× cascade

说明: 删除了user,只是删除了该user下的schema objects,是不会删除相应的tablespace的。

步骤二: 删除tablespace

DROP TABLESPACE tablespace_name INCLUDING CONTENTS AND DATAFILES

查询所有用户:select * from all_users;select * from dba_users;

导出:expdp orms_uat/orms_uat_123@192.168.10.138:1521/pdb1 directory=DP_DIR dumpfile=orms_uat20231111.dmp logfile=orms_uat20231111.log SCHEMAS=orms_uat parallel=4 version=19.3.0.0;


同名同库同空间导入:impdp njchecker/password@orcl directory=DATA DIRdumpfile=njchecker20211106.dmp

不同名,不同表空间,不同用户:impdp njcheckertest2/password@192.168.0.120/orcl directory=DATA DIR dumpfile=njchecker20211106.dmpremap tablespace=nichecker:nicheckertest2 remap schema=nichecker:nicheckertest2 logfile=exdp nicheckertest2..octable exists action=REPL ACE
(impd 用户名密码@i/实名direcorv=DATA PUMP DIR dumpfle=导出的那dmp文件 table exists action=REPLACE
注释: remap tablespace=source tablespace:target tablespace remap schema=source schema:target schematable exists action=REPLACE
table exists action选项:skip 是如果已存在表,则跳过并处理下一个对象;append是为表增加数据: tuncate是截断表,然后为其增加新数据;replace是删除已存在表,重新建表并追加数据;


查看字符集 : select userenv('language') from dual
查看锁表 :select * from V$locked_object ;
杀对应进程:alter system kill session '111'
查看表数据类型:select * from all_tab_columns where table_name=upper('表名') and owner=upper('数据库登录用户')

导入.sql 使用@命令,后面跟上.sql文件的路径和文件名,按回车键执行导入操作

查询导入的表:select table_name from user tables;


创建临时表:CREATE GLOBAL TEMPORARY TABLE table_name (指定表空间只能指定临时表空间)
查询结果导入的新表:(1)INSERT INTO table_name(column1,column2,…) SELECT column1,column2… FROM existing_table_name;
(2)SELECT column1, column2,… INTO new_table_name[IN externaldb] FROM existing_table_name WHERE [condition];
(3)CREATE TABLE new_table_name AS SELECT column1,column2,… FROM existing_table_name WHERE [condition];
(4) create table ... ON COMMIT子句用于指定在事务提交时临时表中的数据的行为。
你可以选择 PRESERVE ROMS 保留数(默认选项),这意味着在事务提交后临时表中的数据将保留,
或者选择 DELETE ROMS 删除数据,这意味着在事务提后临时表中的数据将被删除。


修改表空间 :ALTER TABLE table_name MOVE TABLESPACE new_tablespace;
修改索引表空间:ALTER INDEX indexname REBUILD TABLESPACE newtablespacename;