oracle sql语句 创建表空间、数据库

 1、查看表空间的名称及大小

  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; 
  

  2、查看表空间物理文件的名称及大小

  select tablespace_name, file_id, file_name, round(bytes/(1024*1024),0) total_space from dba_data_files order by tablespace_name;

  

  3、查看回滚段名称及大小

  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 ;

  

  4、查看控制文件

  select name from v$controlfile;

  

  5、查看日志文件

  select member from v$logfile;

  

  6、查看表空间的使用情况

  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;

  

  7、查看数据库库对象

  select owner, object_type, status, count(*) count# from all_objects group by owner, object_type, status;

  

  8、查看数据库的版本 

  Select version FROM Product_component_version Where SUBSTR(PRODUCT,1,6)='Oracle';


9.查看某个表空间内所占空间大于某个值的段(表或索引): 
Select segment_name,bytes FROM dba_segments Where bytes>10000000 AND tablespace_name='tablespace_name';

10.查看所有表空间的碎片程度(值在30以下表示碎片很多)
select tablespace_name,sum(bytes),sum(free),sum(free)*100/sum(bytes) from   (select       b.file_id file_ID,      b.tablespace_name tablespace_name,      b.bytes Bytes,      (b.bytes-sum(nvl(a.bytes,0))) used,      sum(nvl(a.bytes,0)) free,      sum(nvl(a.bytes,0))/(b.bytes)*100         Percent      from dba_free_space a,dba_data_files b       where a.file_id=b.file_id       group by b.tablespace_name,b.file_id,b.bytes       order by b.file_id) group by tablespace_name order by sum(free)*100/sum(bytes);

11.查看自上次数据库启动以来所有数据文件的读写次数   
select       substr(DF.NAME,1,5) Drive,      DF.NAME file_name,      (fs.phyblkrd+fs.phyblkwrt)     from v$filestat fs,v$datafile df        where df.file#=fs.file#;


创建临时表空间:
//创建临时表空间

create temporary tablespace mytest tempfile 'E:/Program/oracle/product/10.1.0/oradata/orcl/mytest.dbf' size 32m autoextend on next 32m maxsize 1024m extent management local;

//创建数据表空间
create tablespace mytest_data logging datafile 'E:/Program/oracle/product/10.1.0/oradata/orcl/mytest_data.dbf' size 32m autoextend on next 32m maxsize 1024m extent management local;

//创建用户并指定表空间
create user testaccount identified by testpwd default tablespace mytest_data temporary tablespace mytest;

//给用户授予权限

grant connect,resource to testaccount; (db2:指定所有权限)


12,创建一个用户:
CREATE USER username IDENTIFIED BY password OR IDENTIFIED EXETERNALLYOR IDENTIFIED GLOBALLY AS ‘CN=user’[DEFAULT TABLESPACE tablespace][TEMPORARY TABLESPACE temptablespace][QUOTA [integer K[M] ] [UNLIMITED] ] ON tablespace[,QUOTA [integer K[M] ] [UNLIMITED] ] ON tablespace[PROFILES profile_name][PASSWORD EXPIRE][ACCOUNT LOCK or ACCOUNT UNLOCK];

//查看数据库中的所有用户:
select * from all_users;
//or
select * from dba_users

你可能感兴趣的:(oracle)