sql管理命令记录
查询数据库基本属性:编号,数据库名称,全局名称,创建时间,归档方式,访问方式,版本类型
select dbid,name,db_unique_name,created,log_mode,open_mode,platform_name from v$database;
查询数据库版本号
select * from v$version;
查看当前数据库的字符集
select userenv('language') from dual; or
select userenv('lang') from dual; or
select * from sys.props$ where name='NLS_CHARACTERSET';
字符集要求一致,但是语言设置却可以不同,语言设置建议用英文。如字符集是zhs16gbk,则nls_lang可以是American_America.zhs16gbk。
修改当前数据的字符集,不建议使用修改props$的方式。
Startup nomount;
Alter database mount exclusive;
Alter system enable restricted session;
alter system set job_queue_processes = 0;
Alter database open;
Alter database character set zhs16gbk;
如果报ORA-12712: 则ALTER DATABASE character set INTERNAL_USE zhs16gbk;
查看临时文件 ,创建临时表空间。
select * from v$tempfile;
create temporary tablespace temp2 tempfile 'd:\fss.TMP' size 10M;
查看归档进程信息
select * from v$archive_processes;
查看数据字典
select * from v$dict;
查看数据库的后台进程
select * from v$bgprocess;
查取系统时间
SQL> select to_char(sysdate) from dual;
SQL> select trunc(sysdate) from dual;
SQL> select to_char(sysdate, 'DD') from dual;
SQL> select to_char(sysdate, 'YY') from dual;
SQL> select to_char(sysdate, 'SS') from dual;
查看系统当前scn
SQL> SELECT dbms_flashback.get_system_change_number FROM dual;
or
SQL> select dbms_flashback.get_system_change_number() a,current_scn from v$database;
查看数据库当前session连接数,以及曾经达到最高的连接个数
SQL> select sessions_current,sessions_highwater from v$license;
比 select count(*) from v$session; 更准的表现当前的连接个数。
查看数据库连接的客户端信息
select process,osuser,username,machine,logon_time,sql_text from v$session a,v$sqltext b where a.sql_address=b.address
查看客户端主机地址信息
select sys_context ('usernv','terminal') terminal,sys_context('userenv','host') host, sys_context('userenv','ip_address') ip_address from dual;
select sys_context('userenv','ip_address') ip from dual;
知道sql语句的address,查询具体sql语句
select * from v$sqltext where address='4A9C3868' order by piece;
查看用户默认表空间
select username,default_tablespace from user_users;
select username,default_tablespace from dba_users;
看到表的创建时间了
select created from dba_objects where object_name = 'table_name'
为表空间添加文件
alter tablespace kkk add datafile 'd:\app\administrator\oradata\orcl\datafile\kkk2.dbf' size 10M autoextend on next 12M maxsize 20M;
查看表空间基本信息
select * from dba_tablepaces;
查看数据库块文件大小
select value from v$parameter where name='db_block_size'
#查询数据库表空间使用状态
select f.tablespace_name tablespace_name,round((d.sumbytes/1024/1024/1024),2) total_g,
round(f.sumbytes/1024/1024/1024,2) free_g,
round((d.sumbytes-f.sumbytes)/1024/1024/1024,2) used_g,
round((d.sumbytes-f.sumbytes)*100/d.sumbytes,2) used_percent
from (select tablespace_name,sum(bytes) sumbytes from dba_free_space group by tablespace_name) f,
(select tablespace_name,sum(bytes) sumbytes from dba_data_files group by tablespace_name) d
where f.tablespace_name= d.tablespace_name
order by d.tablespace_name;
查询asm磁盘使用情况 asm SID下
export ORACLE_SID=+ASM1
sqlplus / as sysdba
select group_number gno,name,state,type,total_mb,free_mb,required_mirror_free_mb rmfmb,usable_file_mb ufmb from v$asm_diskgroup;
数据文件扩展
SQL> select tablespace_name,file_name,autoextensible from dba_data_files where tablespace_name ='USERS';
TABLESPACE_NAME FILE_NAME AUTOEXTENSIBLE
------------------------------ -------------------------------------------------------------------------------- --------------
USERS +DATA/oracle/users01.dbf YES
USERS +DATA/oracle/users02.dbf NO
SQL> alter database datafile '+DATA/oracle/users02.dbf' autoextend on;
Database altered
表空间自动扩展,
SQL> select file_name,autoextensible,increment_by from dba_data_files;
查询用户表所占空间容量
select owner,table_name,num_rows,blocks*8192/1024/1024 "SIZE M" ,empty_blocks,last_analyzed from dba_tables where owner='SYSTEM';
查看表空间的自由碎片索引值
SQL> select tablespace_name ,to_char(sqrt(max(blocks)/sum(blocks))*(100/sqrt(sqrt(count(blocks)))),'999.99') from dba_free_space group by tablespace_name order by 1;
当碎片值超过30时就应该碎片整理的
alter tablespace tablesapce_name coalesce;
查看数据文件状态
SQL> select d.name,d.file#,d.status,h.status from v$datafile d,v$datafile_header h where d.file#=h.file#;
查看表空间数据文件的读写性能
select name,phyrds,phywrts,avgiotim,miniotim,maxiowtm,maxiortm from v$filestat,v$datafile where v$filestat.file#=v$datafile.file#;
select fs.name nmae,f.phyrds,f.phyblkrd,f.phywrts,f.phyblkwrt,f.readtim,f.writetim from v$filestat f,v$datafile fs where f.file# = fs.file# order by fs.name;
如果phyblkrd和phyrds值很接近的话,则表示这个表空间中存在全表扫描的表。需要调整这些表的索引,或者优化sql语句。
查看表的大小
select sum(bytes)/(1024*1024) as "size (M)" from user_segments where segment_name='EMP';
查看表的存储参数
select num_rows,blocks,empty_blocks as empty ,avg_space,chain_cnt,avg_row_len from dba_tables where owner='SCOTT' and table_name='EMP';
查询表的相关参数之前要 现更新下分析信息,再查看。
analyze table scott.emp2 compute statistics;
analyze table t compute statistics = analyze table t compute statistics for table for all indexes for all columns
for table的统计信息存在于视图:user_tables 、all_tables、dba_tables
for all indexes的统计信息存在于视图: user_indexes 、all_indexes、dba_indexes
for all columns的统计信息存在于试图:user_tab_columns、all_tab_columns、dba_tab_columns
当analyze table t delete statistics 会删除所有的statistics
创建远程数据库连接
create database link myself connect to scott identified by scott using 'orcl';
查看远程数据库emp表的命令
select * from emp@myself;
orcl tnsname中的连接名;myself 链接名。
查看数据库链接
select * from dba_db_links; or all_db_links or user_db_links;
查看有primary key 和forgen key 关系的表
select
a.owner 外键拥有者,
a.table_name 外键表,
c.column_name 外键列,
b.owner 主键拥有者,
b.table_name 主键表,
d.column_name 主键列
from
user_constraints a,
user_constraints b,
user_cons_columns c,
user_cons_columns d
where
a.r_constraint_name=b.constraint_name
and a.constraint_type='R'
and b.constraint_type='P'
and a.r_owner=b.owner
and a.constraint_name=c.constraint_name
and b.constraint_name=d.constraint_name
and a.owner=c.owner
and a.table_name=c.table_name
and b.owner=d.owner
and b.table_name=d.table_name
;
存储过程
SQL> select text from user_source; 查看
创建:
create or replace procedure p
is
cursor c is
select * from emp2 for update;
begin
for v_emp in c loop
if(v_emp.sal <2000) then
update emp2 set sal =sal+1 where current of c ;
elsif(v_emp.sal>=2000) then
delete from emp2 where current of c;
end if;
end loop;
commit;
end;
--查询所有函数和储存过程:
select * from user_source where type='PROCEDURE';
select * from user_source where type='PACKAGES';
SQL> select * from user_procedures;
--查看用户角色
SELECT * FROM USER_ROLE_PRIVS;
--查看当前用户权限:
select * from session_privs;
SELECT * FROM DBA_ROLE_PRIVS;
--查看所有角色
select * from dba_roles;
本文出自 “ItBull” 博客,谢绝转载!