Oracle常用脚本

2021/5/10 19:27:42

本文主要是介绍Oracle常用脚本,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

--1、在plsql里执行,创建表空间:
CREATE TABLESPACE NNC_DATA01 DATAFILE 'D:\app\Administrator\oradata\orcl\nnc_data01.dbf' SIZE 500M AUTOEXTEND ON NEXT 50M EXTENT MANAGEMENT LOCAL UNIFORM SIZE 256K ;
CREATE TABLESPACE NNC_DATA02 DATAFILE 'D:\app\Administrator\oradata\orcl\nnc_data02.dbf' SIZE 300M AUTOEXTEND ON NEXT 50M EXTENT MANAGEMENT LOCAL UNIFORM SIZE 256K ;
CREATE TABLESPACE NNC_DATA03 DATAFILE 'D:\app\Administrator\oradata\orcl\nnc_data03.dbf' SIZE 500M AUTOEXTEND ON NEXT 100M EXTENT MANAGEMENT LOCAL UNIFORM SIZE 512K ;
CREATE TABLESPACE NNC_INDEX01 DATAFILE 'D:\app\Administrator\oradata\orcl\nnc_index01.dbf' SIZE 500M AUTOEXTEND ON NEXT 50M EXTENT MANAGEMENT LOCAL UNIFORM SIZE 128K ;
CREATE TABLESPACE NNC_INDEX02 DATAFILE 'D:\app\Administrator\oradata\orcl\nnc_index02.dbf' SIZE 300M AUTOEXTEND ON NEXT 50M EXTENT MANAGEMENT LOCAL UNIFORM SIZE 128K ;
CREATE TABLESPACE NNC_INDEX03 DATAFILE 'D:\app\Administrator\oradata\orcl\nnc_index03.dbf' SIZE 500M AUTOEXTEND ON NEXT 100M EXTENT MANAGEMENT LOCAL UNIFORM SIZE 256K ;

--查看表空间使用情况
SELECT a.tablespace_name "表空间名",total/1024/1024 "表空间大小单位M",free/1024/1024 "表空间剩余大小单位M",(total - free)/1024/1024 "表空间使用大小单位M",
Round((total - free) / total, 4) * 100 "使用率 [[%]]"FROM (SELECT tablespace_name,Sum(bytes) free FROM DBA_FREE_SPACE GROUP BY tablespace_name) a,
(SELECT tablespace_name,Sum(bytes) total FROM DBA_DATA_FILES GROUP BY tablespace_name) b WHERE a.tablespace_name = b.tablespace_name;
----查询表空间使用情况---

SELECT UPPER(F.TABLESPACE_NAME) "表空间名",
D.TOT_GROOTTE_MB "表空间大小(M)",
D.TOT_GROOTTE_MB - F.TOTAL_BYTES "已使用空间(M)",
TO_CHAR(ROUND((D.TOT_GROOTTE_MB - F.TOTAL_BYTES) / D.TOT_GROOTTE_MB * 100,
2),
'990.99') "使用比",
F.TOTAL_BYTES "空闲空间(M)",
F.MAX_BYTES "最大块(M)"
FROM (SELECT TABLESPACE_NAME,
ROUND(SUM(BYTES) / (1024 * 1024), 2) TOTAL_BYTES,
ROUND(MAX(BYTES) / (1024 * 1024), 2) MAX_BYTES
FROM SYS.DBA_FREE_SPACE
GROUP BY TABLESPACE_NAME) F,
(SELECT DD.TABLESPACE_NAME,
ROUND(SUM(DD.BYTES) / (1024 * 1024), 2) TOT_GROOTTE_MB
FROM SYS.DBA_DATA_FILES DD
GROUP BY DD.TABLESPACE_NAME) D
WHERE D.TABLESPACE_NAME = F.TABLESPACE_NAME
ORDER BY 4 DESC;

--查看表空间是否具有自动扩展的能力
SELECT T.TABLESPACE_NAME,
D.FILE_NAME,
D.AUTOEXTENSIBLE,
D.BYTES,
D.MAXBYTES,
D.STATUS
FROM DBA_TABLESPACES T, DBA_DATA_FILES D
WHERE T.TABLESPACE_NAME = D.TABLESPACE_NAME
ORDER BY TABLESPACE_NAME, FILE_NAME;

--增加表空间大小的四种方法
--Meathod1:给表空间增加数据文件

ALTER TABLESPACE app_data ADD DATAFILE
'D:\ORACLE\PRODUCT\10.2.0\ORADATA\EDWTEST\APP03.DBF' SIZE 50M;
--Meathod2:新增数据文件,并且允许数据文件自动增长

ALTER TABLESPACE app_data ADD DATAFILE
'D:\ORACLE\PRODUCT\10.2.0\ORADATA\EDWTEST\APP04.DBF' SIZE 50M
AUTOEXTEND ON NEXT 5M MAXSIZE 100M;
--Meathod3:允许已存在的数据文件自动增长

ALTER DATABASE DATAFILE 'D:\ORACLE\PRODUCT\10.2.0\ORADATA\EDWTEST\APP03.DBF'
AUTOEXTEND ON NEXT 5M MAXSIZE 100M;
--Meathod4:手工改变已存在数据文件的大小

ALTER DATABASE DATAFILE 'D:\ORACLE\PRODUCT\10.2.0\ORADATA\EDWTEST\APP02.DBF'

--允许导出空表
Alter system set deferred_segment_creation=false scope=both;

--删除表空间:drop tablespace NNC_DATA01 including contents and datafiles;

--查看表空间: select tablespace_name from dba_tablespaces;
CREATE TABLESPACE NNC_INDEX01 DATAFILE 'E:\app\wangh\oradata\orcl\nnc_index01.dbf' SIZE 500M AUTOEXTEND ON NEXT 50M EXTENT MANAGEMENT LOCAL UNIFORM SIZE 128K ;

--oracle查看被锁的表或者数据以及解锁方法
--查看
select a.object_name,b.session_id,c.serial#,c.program,c.username,c.command,c.machine,c.lockwait
from all_objects a,v\(locked_object b,v\)session c where a.object_id=b.object_id and c.sid=b.session_id;
--解锁
alter system kill session'session_id,serial#';
--例句
alter system kill session'74,2433';

--2、创建用户:
CREATE USER nc631_ft0221 IDENTIFIED BY 1 DEFAULT TABLESPACE NNC_DATA01 TEMPORARY TABLESPACE temp;
GRANT connect,dba to nc631_ft0221;

--查看所有用户
select * from dba_users;
--删除用户:drop user aqsc2015 cascade;
--修改用户密码:ALTER USER NCTEST IDENTIFIED BY NCTEST

--在终端导入.dmp文件命令
imp btpg1/1@orcl file=文件路径 log=123.txt full=y;
pause

用数据泵导入数据库:
SELECT * FROM Dba_Directories a WHERE a.directory_name = 'DATA_PUMP_DIR';
expdp nc63/orcl@127.0.0.1:1521/oadbtest dumpfile=javaboy.dmp directory= DATA_PUMP_DIR logfile=1.LOG

远程数据泵导出语句:
expdp btpg/1@orcl11g DUMPFILE=BTPG20140223.DMP DIRECTORY=DATA_PUMP_DIR logfile=BTPG20140223.log
expdp nc63/orcl@127.0.0.1:1521/oadbtest dumpfile=javaboy.dmp directory= DATA_PUMP_DIR logfile=1.LOG
把数据库文件拷在改路径下

impdp btpg/1@orcl DUMPFILE=BTPG_EXPDP_20140219.DMP DIRECTORY=DATA_PUMP_DIR logfile=BTPG_EXPDP_20140219.log remap_schema=gnnczs:aqsc20151116

本机导出数据库
exp nc631_ft0221_04/1@orcl file=C:\Users\javaboy\Desktop\nc631_ft0221_04.dmp owner = 'nc631_ft0221_04'
pause
本机导入数据库
imp test1/test1@orcl file= full=y owner = 'nc631_ft0221_04' log=d:\gzgl570118.log
远程导入数据库
imp test1/test1@//ip:端口号/orcl file= full=y log=d:\gzgl570118.log
远程导出数据库
exp test1/test1@//ip:端口号/orcl file=d:\xmt_0414.dmp owner=xmt_041;(exp btpg/1@//20.1.78.45:1521/orcl11g file=F:\btpg0226V2.0.dmp owner= btpg log=G:\ btpg0226 .log)

exp nc631_ft0103_test/1@//127.0.0.1:1521/ft file =C:\Users\javaboy\Desktop\nc631_ft0103_test.dmp owne=xmt_0


select * from dba_tablespaces--查看表空间
--查看表空间文件路径
select tablespace_name,file_id,bytes/1024/1024,file_name
   from dba_data_files order by file_id;
--查看用户和默认表空间的关系
select username,default_tablespace from dba_users;


--删除用户
drop user XX cascade;
--创建用户
CREATE USER hq201805111 IDENTIFIED BY 1 DEFAULT TABLESPACE NNC_DATA01 TEMPORARY TABLESPACE temp;
--用户授权
GRANT connect,dba to hq201805111;
--创建备份文件夹
create directory exp_dir as 'D:\dmp';
--授权文件夹操作给用户
grant read,write on directory exp_dir to username;
数据泵导入语句
--impdp hq201805111/1 dumpfile=NC65.dmp logfile=NC65.log directory=exp_dir job_name=imp remap_schema=nc65:nc65

impdp NC65/NC65 dumpfile=20170817.DMP logfile=20170817.log directory=exp_dir full=y;
impdp erp/1@tiger schemas=estate directory=dmp_814 dumpfile=814erp.DMP logfile=dmp_814.log table_exists_action=replace remap_schame=NC63Z:erp814

/完整倒库*****************/

CREATE USER yasha IDENTIFIED BY 1 DEFAULT TABLESPACE NNC_DATA01 TEMPORARY TABLESPACE TEMP PROFILE DEFAULT;

GRANT CONNECT TO yasha
GRANT RESOURCE TO yasha
GRANT UNLIMITED TABLESPACE TO yasha
GRANT DBA to yasha
grant read,write on directory exp_dir to yasha

I:\2018年12月3日备份\数据库备份\yasha.dmp
imp yasha/1@orcl file=I:\2018年12月3日备份\数据库备份\yasha.dmp full=y log=D:\dmp\exp_2017121310.log



这篇关于Oracle常用脚本的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程