日期:2014-05-16  浏览次数:20414 次

oracle常识-常用的知识
用户SYS AS SYSDBA登陆
  1、系统用户:
查询用户
 SELECT USERNAME FROM DBA_USERS;
创建用户
CREATE USER SCOTT IDENTIFIED BY TIGER
删除用户
DROP USER USER_NAME CASCADE;
  改口令
   ALTER USER SCOTT IDENTIFIED BY TIGER;
解锁
  ALTER USER SCOTT SCCOUNT UNLOCK。
授权
GRANT CREATE USER,DROP USER,ALTER USER ,CREATE ANY VIEW ,
   DROP ANY VIEW,EXP_FULL_DATABASE,IMP_FULL_DATABASE,
      DBA,CONNECT,RESOURCE,CREATE SESSION  TO  SCOTT
  2、表空间:
查询表空间   
      SELECT * FROM DBA_DATA_FILES;
   SELECT * FROM DBA_TABLESPACES;//表空间

   SELECT TABLESPACE_NAME,SUM(BYTES), SUM(BLOCKS)
    FROM DBA_FREE_SPACE GROUP BY TABLESPACE_NAME;//空闲表空间

   SELECT * FROM DBA_DATA_FILES
    WHERE TABLESPACE_NAME='RBS';//表空间对应的数据文件

   SELECT * FROM DBA_SEGMENTS
    WHERE TABLESPACE_NAME='INDEXS';

删除表空间

DROP TABLESPACE DATA01 INCLUDING CONTENTS AND DATAFILES;

建立表空间

1.建立表空间

CREATE TABLESPACE DATA01
DATAFILE '/ORACLE/ORADATA/DB/DATA01.DBF' SIZE 500M
UNIFORM. SIZE 128K; #指定区尺寸为128K,如不指定,区尺寸默认为64K

2.建立UNDO表空间

CREATE UNDO TABLESPACE UNDOTBS02
DATAFILE '/ORACLE/ORADATA/DB/UNDOTBS02.DBF' SIZE 50M

#注意:在OPEN状态下某些时刻只能用一个UNDO表空间,如果要用新建的表空间,必须切换到

该表空间:

ALTER SYSTEM SET UNDO_TABLESPACE=UNDOTBS02;

3.建立临时表空间

CREATE TEMPORARY TABLESPACE TEMP_DATA
TEMPFILE '/ORACLE/ORADATA/DB/TEMP_DATA.DBF' SIZE 50M

改变表空间状态

1.使表空间脱机

ALTER TABLESPACE GAME OFFLINE;

如果是意外删除了数据文件,则必须带有RECOVER选项

ALTER TABLESPACE GAME OFFLINE FOR RECOVER;

2.使表空间联机

ALTER TABLESPACE GAME ONLINE;


3.使数据文件脱机

ALTER DATABASE DATAFILE 3 OFFLINE;

4.使数据文件联机

ALTER DATABASE DATAFILE 3 ONLINE;

5.使表空间只读

ALTER TABLESPACE GAME READ ONLY;

6.使表空间可读写

ALTER TABLESPACE GAME READ WRITE;

扩展表空间

首先查看表空间的名字和所属文件

SELECT TABLESPACE_NAME, FILE_ID, FILE_NAME,
ROUND(BYTES/(1024*1024),0) TOTAL_SPACE
FROM DBA_DATA_FILES
ORDER BY TABLESPACE_NAME;

1.增加数据文件
ALTER TABLESPACE GAME
ADD DATAFILE '/ORACLE/ORADATA/DB/GAME02.DBF' SIZE 1000M;

2.手动增加数据文件尺寸
ALTER DATABASE DATAFILE '/ORACLE/ORADATA/DB/GAME.DBF'
RESIZE 4000M;

3.设定数据文件自动扩展
ALTER DATABASE DATAFILE '/ORACLE/ORADATA/DB/GAME.DBF
AUTOEXTEND ON NEXT 100M
MAXSIZE 10000M;

设定后查看表空间信息

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
  3、数据库对象:
   SELECT * FROM DBA_OBJECTS;
   CLUSTER、DATABASE LINK、FUNCTION、INDEX、LIBRARY、PACKAGE、PACKAGE BODY、
   PROCEDURE、SEQUENCE、SYNONYM、TABLE、TRIGGER、TYPE、UNDEFINED、VIEW。
  4、表:
   SELECT * FROM DBA_TABLES;
   ANALYZE MY_TABLE COMPUTE STATISTICS;->DBA_TABLES后6列
   SELECT EXTENT_ID,BYTES FROM DBA_EXTENTS
   WHERE SEGMENT_NAME='CUSTOMERS' AND SEGMENT_TYPE='TABLE'
   ORDER BY EXTENT_ID;//表使用的EXTENT的信息。SEGMENT_TYPE='ROLLBACK'查看回滚段的空间分配信息
   列信息:
    SELECT DISTINCT TABLE_NAME
    FROM USER_TAB_COLUMNS
    WHERE COLUMN_NAME='SO_TYPE_ID';
  5、索引: 
   SELECT * FROM DBA_INDEXES;//索引,包括主键索引
   SELECT * FROM DBA_IND_COLUMNS;//索引列
   SELECT I.INDEX_NAME,I.UNIQUENESS,C.COLUMN_NAME
    FROM USER_INDEXES I,USER_IND_COLUMNS C
     WHERE I.INDEX_NAME=C.INDEX_NAME
     AND I.TABLE_NAME ='ACC_NBR';//联接使用
  6、序列:
   SELECT * FROM DBA_SEQUENCES;
  7、视图:
   SELECT * FROM DBA_VIEWS;