Oracle 常用操作实例汇总
查看表空间大小
SELECT t.tablespace_name, round(SUM(bytes / (1024 * 1024)), 0) ts_size
FROM dba_tablespaces t, dba_data_files d
WHERE t.tablespace_name = d.tablespace_name
GROUP BY t.tablespace_name;
查看表空间物理文件的名称及大小
SELECT tablespace_name,
file_id,
file_name,
round(bytes / (1024 * 1024), 0) total_space
FROM dba_data_files
ORDER BY tablespace_name;
查看回滚段名称及大小
SELECT segment_name,
tablespace_name,
r.status,
(initial_extent / 1024) initialextent,
(next_extent / 1024) nextextent,
max_extents,
v.curext curextent
FROM dba_rollback_segs r, v$rollstat v
WHERE r.segment_id = v.usn(+)
ORDER BY segment_name;
查看控制文件
SELECT NAME FROM v$controlfile;
查看日志文件
SELECT MEMBER FROM v$logfile;
查看表空间使用情况
SELECT SUM(bytes) / (1024 * 1024) AS free_space, tablespace_name
FROM dba_free_space
GROUP BY tablespace_name;
SELECT a.tablespace_name,
a.bytes total,
b.bytes used,
c.bytes free,
(b.bytes * 100) / a.bytes "% USED ",
(c.bytes * 100) / a.bytes "% FREE "
FROM sys.sm$ts_avail a, sys.sm$ts_used b, sys.sm$ts_free c
WHERE a.tablespace_name = b.tablespace_name
AND a.tablespace_name = c.tablespace_name;
select a.tablespace_name,
a.total_G,
b.fee_G,
(a.total_G - b.fee_G) / a.total_G as "used%"
from (select a.tablespace_name,
sum(a.bytes) / 1024 / 1024 / 1024 As total_G
from dba_data_files a
group by a.tablespace_name) a
left join (select a.tablespace_name,
sum(a.bytes) / 1024 / 1024 / 1024 as fee_G
from dba_free_space a
group by a.tablespace_name) b
on a.tablespace_name = b.tablespace_name
order by 4 desc;
select a.tablespace_name,
a.bytes / 1024 / 1024 "Sum MB",
(a.bytes - b.bytes) / 1024 / 1024 "used MB",
b.bytes / 1024 / 1024 "free MB",
round(((a.bytes - b.bytes) / a.bytes) * 100, 2) "percent_used %"
from (select tablespace_name, sum(bytes) bytes
from dba_data_files
group by tablespace_name) a,
(select tablespace_name, sum(bytes) bytes, max(bytes) largest
from dba_free_space
group by tablespace_name) b
where a.tablespace_name = b.tablespace_name
order by ((a.bytes - b.bytes) / a.bytes) desc;
查看数据库对象
SELECT owner, object_type, status, COUNT(*) count#
FROM all_objects
GROUP BY owner, object_type, status;
查看数据库版本
SELECT version
FROM product_component_version
WHERE substr(product, 1, 6) = 'Oracle';
查看数据库的创建日期和归档方式
SELECT created, log_mode, log_mode FROM v$database;
临时表空间的使用情况
Select f.tablespace_name,
sum(f.bytes_free + f.bytes_used) / 1024 / 1024 / 1024 "total GB",
sum((f.bytes_free + f.bytes_used) - nvl(p.bytes_used, 0)) / 1024 / 1024 / 1024 "Free GB",
sum(nvl(p.bytes_used, 0)) / 1024 / 1024 / 1024 "Used GB"
from sys.v_$temp_space_header f,
dba_temp_files d,
sys.v_$temp_extent_pool p
where f.tablespace_name(+) = d.tablespace_name
and f.file_id(+) = d.file_id
and p.file_id(+) = d.file_id
group by f.tablespace_name;
具体到某个SID临时表空间使用情况
select b.tablespace,
b.segfile#,
b.segblk#,
b.blocks,
b.blocks * 32 / 1024 / 1024,
a.sid,
a.serial#,
a.username,
a.osuser,
a.status,
c.sql_text,
b.contents
from v$session a, v$sort_usage b, v$sql c
where a.saddr = b.session_addr
and a.sql_address = c.address(+)
order by b.blocks desc;
查看表空间名称、id,文件存储位置,初始大小
select tablespace_name, file_id, file_name, bytes/1024/1024/1024
from dba_data_files
order by file_id;
修改表空间大小
alter database datafile '/u01/app/oracle/oradata/prod/useSpacer.dbf' resize 1000M;
修改表空间大小:增加数据文件
alter tablespace USERSPACE add datafile '/u01/app/oracle/oradata/prod/USERSPACE2.dbf' size 500M autoextend on next 10M maxsize 20G;;
创建用户
create user smart identified by smart default tablespace MAODOUDATA temporary tablespace MAODOUTEMP;
创建数据文件
create tablespace MAODOUDATA datafile '/u01/app/oracle/oradata/prod/data_01.dbf'
size 32g autoextend on next 10M maxsize 2048m;
创建临时表空间
create temporary tablespace MAODOUTEMP tempfile '/u01/app/oracle/oradata/prod/temp_01.dbf'
size 20m
autoextend on next 10M maxsize 50m;
创建索引表空间
create tablespace MAODOUINDEX datafile '/u01/app/oracle/oradata/prod/index.dbf'
size 20g
autoextend on next 10M maxsize 1024m;
修改用户数据表空间
select 'alter table '|| table_name ||' move tablespace MAODOUDATA;' from user_tables;
修改用户索引表空间
select 'alter index '|| index_name ||' rebuild tablespace tablespacename;' from user_indexes;
删除用户-及级联关系也删除
drop user 用户名称 cascade;
删除表空间
drop tablespace USERSPACE;
删除表空间-及对应的表空间文件也删除掉
drop tablespace 表空间名称 including contents and datafiles cascade constraint;
查看当前归档日志目录
SQL> show parameter recovery
更改归档日志目录
SQL> alter system set db_recovery_file_dest='/u01/app/oracle/archivelog' scope=spfile;
更改归档日志目录大小
SQL> alter system set db_recovery_file_dest_size=4096m scope=spfile;
检查Oracle示例状态
select instance_name,host_name,startup_time,status,database_status from v$instance;
检查Oracle在线日志状态
select group#,status,type,member from v$logfile;
检查Oracle表空间的状态
select tablespace_name,status from dba_tablespaces;
检查Oracle所有数据文件状态
select name,status from v$datafile;
解锁用户-管理员权限
alter user scott account unlock;
锁定用户-管理员权限
alter user scott account lock;
监听
启动监听
SQL>lsnrctl start
停止监听
SQL>lsnrctl stop
查看监听状态
SQL>lsnrctl status
启动数据库
SQL>startup
停止数据库
SQL>shutdown immediate
远程连接数据库
SQL>sqlplus /nolog
SQL>conn system/oracle@ip:port/sid as sysdba
直接运行
启动数据库脚本
dbstart
修改数据库连接数
连接数
修改连接数
alter system set processes=1000 scope=spfile;
shutdown immediate;
startup;
查看用户当前连接数
select count(*) from sys.v_$session;
使用pfile启动Oracle实例
使用pfile启动Oracle例程
lsnrctl start
sqlplus /nolog
conn user@instance as sysdba password
create spfile from pfile=’/Oracle/instance/pfile/init.ora.789456123’;
startup
查看Oracle锁表语句
select object_name, machine, s.sid, s.serial#
from v$locked_object l, dba_objects o, v$session s
where l.object_id = o.object_id
and l.session_id = s.sid;
Oracle解锁表方法
ALTER system kill session 'sid, serial#';
或
ALTER system kill session 'sid, serial#' immediate;
如:ALTER system kill session '324, 23212';
or
ALTER system kill session '324, 23212' immediate;