Oracle常用语句

--1 Oracle 版本查询
SELECT * FROM V$VERSION;
SELECT * FROM PRODUCT_COMPONENT_VERSION;
--2 Oracle字符集查看
SELECT * FROM Nls_Database_Parameters where PARAMETER in ('NLS_LANGUAGE','NLS_CHARACTERSET') ;--服务端
SELECT Userenv('language') FROM dual;--客户端
--3 oracle用户删除
--查看用户占用了多少空间
SELECT owner, tablespace_name, ROUND (SUM (BYTES) / 1024 / 1024/ 1024, 2) "USED(G)"
FROM dba_segments
GROUP BY owner, tablespace_name
ORDER BY SUM (BYTES) DESC;
--查询当前登陆用户所占空间大小:
select sum(BYTES) / 1024 / 1024/1024 as SIZE_G
from user_segments
--查询所有dba用户所占空间大小:
select sum(BYTES) / 1024 / 1024/1024 as SIZE_G
from dba_segments
--查看当前用户下所有表占用的空间情况:
select segment_name,tablespace_name,bytes,blocks from user_segments
/*先删除用户,再删除表空间,必须保证表空间没有用户使用才能删除*/
--查询用户的相关信息:
select * from DBA_USERS;
--删除用户:
drop user username cascade;
--删除表空间和它的系统文件:
drop tablespace YSSUCO including contents and datafiles;
--如果发现不能删除用户,应该是用户还有连接,查询用户的连接:
select username,sid,serial# from v$session where username='username';
--查询出他的sid,serial#,然后杀掉:
alter system kill session ‘1505,81’;
--再次查询,可以查询它的状态:
select saddr,sid,serial#,paddr,username,status from v$session where username='username';
--发现他的状态为KILLD时,已经杀掉,再次执行删除用户即可;
--4 Oracle用户创建
/*分为四步 */
 
/*第1步:创建数据表空间 */
create tablespace YSSUCO datafile '/u01/App/oracle/oradata/FA/YSSUCO01.DBF' size 1000M autoextend on next 100M maxsize 31900M;
/*第2步:Oracle表空间不足的处理方法*/
alter tablespace YSSUCO add datafile '/u01/app/oracle/oradata/FA/YSSUCO02.DBF' size 1000M autoextend on next 100M MAXSIZE 31900M;
alter tablespace YSSUCO add datafile '/u01/app/oracle/oradata/FA/YSSUCO03.DBF' size 1000M autoextend on next 100M MAXSIZE 31900M;
 
/*第3步:创建用户并指定表空间 */
CREATE USER username IDENTIFIED BY 1 DEFAULT TABLESPACE YSSUCO TEMPORARY TABLESPACE TEMP;
 
/*第4步:给用户授予权限 */
grant connect, resource, dba to username;
grant connect to username;
grant read,write on directory dp_dir to username;
grant exp_full_database,imp_full_database to username;
grant resource to username;
grant create database link to username;
grant create public synonym to username;
grant create synonym to username;
grant create view to username;
grant unlimited tablespace to username;
grant execute on dbms_crypto to username;
--5 Oracle创建数据库逻辑目录dp_dir
[root@CNZHAULAMC094 ~]# mkdir /dp_dir
[root@CNZHAULAMC094 ~]# chmod -R 777 /dp_dir
[root@CNZHAULAMC094 ~]# chmod -R 777 /dp_dir/expdp.dmp
[root@CNZHAULAMC094 ~]# su - oracle
[oracle@localhost ~]$ lsnrctl status;
[oracle@localhost ~]$ export ORACLE_SID=FA
[oracle@localhost ~]$ sqlplus / as sysdba;
SQL>select * from dba_directories; /*查询逻辑目录*/
SQL> create directory dp_dir as '/dp_dir'; /*创建数据库逻辑目录*/
--6 Oracle新建directory
/*1、查询有哪些directory*/
select * from dba_directories
/*2、把目录/dp_dir设置成dp_dir代表的directory*/
create or replace directory dp_dir as '/dp_dir';
/*3、赋权*/
grant read,write on directory dp_dir to username;
grant exp_full_database,imp_full_database to username;
/*4、删除*/
drop directory dp_dir
--7 Oracle数据备份
--表备份
/*1、备份表数据*/
create table user_info_bak as select * from user_info;
/*2、还原表数据*/
insert into user_info_bak select * from user_info;
--库备份,oracle 从一个oracle导数据到另外一个oracle
/*1、普通导库*/
exp INDBADMIN/INDBADMIN@10.1.252.38:1521/move owner=INDBADMIN file=D:INDBADMIN20190622.dmp log=D:INDBADMIN20190622.log
imp username/1@127.0.0.1:1521/orcl file=D:appyuoradatadp_dirnewgzdb.dmp log=D:appyuoradatadp_dirusername.log ignore=y FULL=y;
/*2、数据泵导库*/
1)按用户导
expdp v45test/1 schemas=v45test directory=dp_dir dumpfile=expdp.dmp ;
2)并行进程parallel
expdp v45test/1 directory=dp_dir dumpfile=expdp.dmp parallel=3 job_name=scott3
3)按表名导
expdp v45test/1 TABLES=t_s_user directory=dp_dir dumpfile=expdp.dmp;


推荐阅读