首页 > 代码库 > oracle笔记

oracle笔记

好久没写博客了,最近这段项目再在迁移所以有点忙.下面是这段时间学习并用到的命令.


1.导出数据库.首先赋予用户对目录的权限,然后再导出 log为日志.
  Ⅰ: SQL >> create or replace directory exportDIR as ‘/opt/oracle/oradata‘;
  Ⅱ: SQL >> grant read,write on directory exportDIR to userX;
  Ⅲ: [cmd] >> expdp userX/userX directory=exportDIR dumpfile=userX_date.dump logfile=userX_date.log;
2.导入数据库.首先赋予用户对目录的权限,然后再导入.
  Ⅰ: SQL >> create or replace directory importDIR as ‘/opt/oracle/oradata‘;
  Ⅱ: SQL >> grant read,write on directory importDIR, exp_full_database to userX;
  Ⅲ: [cmd] >> impdp userX/userX directory=importDIR dumpfile=userX_date.dump;
3.新建用户并授权.
  Ⅰ: SQL >> create tablespace userSpace datafile ‘/opt/oracle/tablespace/userSpace.dbf‘ size 50M reuse autoextend off;
  ------------用户名--------------密码---------------------表空间-------------------临时表空间--
  Ⅱ: SQL >> create user userX identified by userX default tablespace userSpace temporary tablespace TEMP;
  Ⅲ: SQL >> grant create table,drop any table,create sequence,
    alter any trigger, create trigger, drop any trigger,
    alter any type, create type, drop any type,
    create database link,
    create view, drop any view,
    create indextype,
    create procedure,
    create synonym,
    create materialized view,
    unlimited tablespace,
    create session to userX;
4.用户登录获取sysdba权限.
    linux
      Ⅰ: [cmd] >> su - oracle;
      Ⅱ: SQL >> conn / as sysdba;
    window
      Ⅰ: [cmd] >> sqlplus
      Ⅱ: 按提示输入用户名和口令 用户名为system 口令为建立数据库时设置的口令 1234
      Ⅲ: 当不记得口令是可以尝试
      [cmd] >> sqlplus /nolog;
      SQL >> conn / as sysdba;
5.常用的 SQL
  Ⅰ.查询表空间
      SQL >> SELECT A.TABLESPACE_NAME "表空间名称",
            100 - ROUND((NVL(B.BYTES_FREE, 0) / A.BYTES_ALLOC) * 100, 2) "占用率(%)",
            ROUND(A.BYTES_ALLOC / 1024 / 1024, 2) "容量(M)",
            ROUND((A.BYTES_ALLOC - NVL(B.BYTES_FREE, 0)) / 1024 / 1024, 2) "使用(M)",
            ROUND(NVL(B.BYTES_FREE, 0) / 1024 / 1024, 2) "空闲(M)",
            ROUND((NVL(B.BYTES_FREE, 0) / A.BYTES_ALLOC) * 100, 2) "空闲率(%)",
            TO_CHAR(SYSDATE, ‘yyyy-mm-dd hh24:mi:ss‘) "采样时间"
            FROM (SELECT F.TABLESPACE_NAME,
            SUM(F.BYTES) BYTES_ALLOC,
            SUM(DECODE(F.AUTOEXTENSIBLE, ‘YES‘, F.MAXBYTES, ‘NO‘, F.BYTES)) MAXBYTES
            FROM DBA_DATA_FILES F
            GROUP BY TABLESPACE_NAME) A,
            (SELECT F.TABLESPACE_NAME, SUM(F.BYTES) BYTES_FREE
           FROM DBA_FREE_SPACE F
            GROUP BY TABLESPACE_NAME) B
          WHERE A.TABLESPACE_NAME = B.TABLESPACE_NAME;
  Ⅱ.查询目录
    SQL >> select * from v$directory ;
  Ⅲ.数据库启动关闭
    =======================================================
    --查询数据库启动状态
    SQL >> select status from v$instance; || [cmd] >> ps -ef | grep oracle;
    --关闭监听
    [cmd] >> $lsnrctl stop
    --登陆sqlplus
    [cmd] >> $ sqlplus / as sysdba
    --关闭数据库
    SQL >> shutdown immediate;
    --重启数据库
    SQL >> startup;
    --退出sqlplus
    SQL >> exit
    --打开监听
    [cmd] >> lsnrctl start
    =======================================================
  Ⅳ.查看数据库一共有多少条数据
    SQL >> select sum(num_rows) from dba_tables;
  Ⅴ.查看当前用户多少表
    SQL >> select * from user_tables;
  Ⅵ.删除用户
    SQL >> drop user userX cascade;
  Ⅶ.删除表空间
    SQL> drop tablespace userSpace including contents and datafiles cascade constraints;

6.遇到的问题.
  Ⅰ:存储过程不能debug调试.用户权限不够。
    SQL>> grant debug any procedure,debug connect session to userX;
  Ⅱ:oracle环境变量设置
    export ORACLE_BASE=/opt/oracle/app
    export ORACLE_HOME=$ORACLE_BASE/oracle/product/11.1.0/db_1
    export ORA_CRS_HOME=/opt/oracrs
    export ORACLE_SID=orcl--数据库名字