日常实用oracle SQL

来源:互联网 发布:手机朗读软件 编辑:程序博客网 时间:2024/05/16 06:23

                                                                        日常实用oracle SQL

‘参考《sql语法大全中文版》学习

数据库及用户
启动、关闭数据库:


sqlplus '/as sysdba'


shutdown abort;


startup;


启动、关闭、查看数据库监听进程:


lsnrctl status


lsnrctl start


给普通用户授权调试存储过程


GRANT debug any procedure, debug connect session TO usename;


查询数据库实例名


Select instance_name from v$instance;


查询用户密码有效期,LIMIT字段是密码有效天数


SELECT *
  FROM dba_profiles
 WHERE profile = 'DEFAULT'
   AND resource_name = 'PASSWORD_LIFE_TIME';


将密码改成永不过期


ALTER PROFILE DEFAULT LIMIT PASSWORD_LIFE_TIME UNLIMITED;


数据库备份SQL
数据库备份是对一个DBA最基本的要求,在维护工作中也是经常要用到,的常用的备份如下,这里不会把所有的参数列出来,根据实际情况需要自己可以添加一些参数:


导出表: 

  exp name/password@mycon tables=(table1,table2) file=tab1.dmp

导出用户:

  exp name/password@mycon owner=name file=name.dmp

导出数据库:

  exp system/password@mycon full=y inctype=complete file=full1.dmp

导入表:

  imp system/password@mycon file= tab1.dmp tables=(table1,table2) touser=name

导入用户:

imp name/password@mycon file=name.dmp full=yes

导入数据库:

  imp system/password@mycon file=full1.dmp full=yes

其它操作
数据文件表:dba_data_files


表空间表:dba_tablespaces

创经表空间:

create tablespace tableSpaceName datafile '< data_file_name >' size 1000M;

重新定义表空间数据文件大小:

alter database datafile '' resize 5000M;

改变表空间大小:


alter tablespace tableSpaceName add datafile '< data_file_name >' resize 1024M;


查询表空间与数据文件对应关系:


select tablespace_name, bytes, file_name from dba_data_files;


查询用户缺省表空间:


select username, default_tablespace from dba_users;


查询表与存储该表的表空间:


select table_name, tablespace_name from user_tables;


修改用户缺省表空间:


alter user username default tablespace tablespace_name;


将数据从一个表空间移动到另一个表空间:


alter table table_name move tablespace tablespace_name;


检查是否有业务用户的默认表空间设置在了system上。
select * from dba_users t where t.default_tablespace='SYSTEM';


检查是否有业务用户的表被创建在了system上:


select * from dba_tables w where w.owner not in ('SYSTEM', 'SYS', 'MGMT_VIEW','OUTLN') and w.tablespace_name='SYSTEM';


查询表空间的数据文件大小


select tablespace_name,(bytes/1024/1024) M from dba_data_files;


查询数据文件已使用空间


select file_name, sum(e.bytes)/1024/1024 as MB from dba_extents e join dba_data_files f on e.file_id=f.file_id group by file_name;


表空间使用率


select a.tablespace_name,
       total / 1024 / 1024 / 1024,
       free / 1024 / 1024 / 1024,
       (total - free) / 1024 / 1024 / 1024,
       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;


系统表空间占用TOP9


SELECT *
  FROM (SELECT BYTES, segment_name, segment_type, owner
          FROM dba_segments
         WHERE tablespace_name = 'SYSTEM'
         ORDER BY BYTES DESC)
 WHERE ROWNUM < 10;


system空间中具体是什么对象占用较高:


select sum(bytes) as bt,  s.owner, s.segment_name from dba_segments s where s.tablespace_name='SYSTEM'
group by s.owner, s.segment_name
order by sum(bytes) desc;

将审计功能关闭:


alter system set audit_trail='NONE' scope=spfile;

锁1-锁2
 

0 0