zoukankan      html  css  js  c++  java
  • oracle数据库

    目录:

      1.导入导出dmp文件                   6.函数

      2.删除用户下所有的表       7.存储过程和存储函数  

      3.数据闪回                                 8.视图

      4.序列             9.事物回滚

      5.索引             10.常用符号

      11.求两个时间段相差的时间

    1.超级用户账号密码

    账号:sqlplus
    密码:as sysdba

    2.创建用户

    create user 用户名 identified by 密码

    3.赋予用户权限

    赋予用户connect连接数据库,resource创建实体但是没有创建数据库结构权限,dba赋予用户所有权限
    grant connect,resource,dba to 用户名

    其他权限

    grant execute on sys.dbms_crypto to 用户名 with grant option;
    grant execute on SYS.DBMS_AQADM to 用户名 with grant option;
    grant execute on SYS.DBMS_AQELM to 用户名 with grant option;
    grant execute on SYS.DBMS_AQ_IMPORT_INTERNAL to 用户名 with grant option;
    grant execute on SYS.DBMS_DEFER_IMPORT_INTERNAL to 用户名;
    grant execute on SYS.DBMS_REPCAT to 用户名;
    grant execute on SYS.DBMS_RULE_EXIMP to 用户名 with grant option;
    grant execute on SYS.DBMS_SQL to 用户名;
    grant execute on SYS.DBMS_SYS_ERROR to 用户名;
    grant execute on SYS.DBMS_SYS_SQL to 用户名;
    grant execute on SYS.DBMS_TRANSFORM_EXIMP to 用户名 with grant option;
    grant select, insert, update, delete, references, alter, index on SYS.INCEXP to 用户名;
    grant select, insert, update, delete, references, alter, index on SYS.INCFIL to 用户名;
    grant select, insert, update, delete, references, alter, index on SYS.INCVID to 用户名;
    grant execute on SYS.SYS_GROUP to 用户名;
    grant select on WMSYS.WM$UDTRIG_INFO to 用户名;
    grant aq_administrator_role to 用户名 with admin option;
    grant dba to NEWUSER123 with admin option;
    grant sales_history_role to 用户名 with admin option;
    grant students to 用户名 with admin option;
    grant unlimited tablespace to 用户名 with admin option;

    4.全库导入导出dmp文件,在cmd运行

    导出

    exp 账号/密码@127.0.0.1/orcl file=导出文件名.dmp full=y

    导入

    imp epm/epm@127.0.0.1/orcl file=D:doudianworklq.dmp full=y

    5.oracle11g的数据库导入oracle10g的数据库

    1.oracle11g导出

    EXPDP USERID='账号/密码@orcl as sysdba' schemas=账号 directory=DATA_PUMP_DIR dumpfile=导出文件名.dmp logfile=导出日志名.log version=10.2.0.1.0 (这是导出成数据库版本号)

    导出的文件在oracle11g安装的盘的appASUSadminorcldpdump

    2.oracle10导入

    导入的时候将要导入的.dmp文件放入oracle10g的安装盘的ppASUSadminorcldpdump文件夹下

    IMPDP USERID='账号/密码@orcl as sysdba' schemas=账号 directory=DATA_PUMP_DIR dumpfile=导入的文件名.dmp logfile=导入的日志名.log version=10.2.0.1.0 (导入的版本号)

    原文地址: https://www.cnblogs.com/huangzhiqiang/p/4725664.html

    6.删除用户下所有的表

    在oracle的窗口执行

    SELECT 'DELETE FROM '|| table_name || ';' FROM USER_TABLES ORDER BY TABLE_NAME;

    7.数据闪回(恢复删除的数据)

    1.查询最近的快照时间

    SELECT R.FIRST_LOAD_TIME,R.SQL_TEXT,R.* FROM V$SQLAREA R WHERE R.SQL_TEXT LIKE '%ABOUT YOUR SQL%' ORDER BY R.FIRST_LOAD_TIME DESC

    2.以当前时间为准查询125分钟之前的数据快照

    SELECT * FROM 表名 AS OF TIMESTAMP SYSDATE - 125 / 1440

    3.恢复数据

    1.启动表的row movement特性
    
    ALTER TABLE 表名 ENABLE ROW MOVEMENT;
    
    2.闪回指定时间的快照
    
    FLASHBACK TABLE 表名 TO TIMESTAMP 
    TO_TIMESTAMP('2018-04-23 16:06:00','yyyy-mm-dd hh24:mi:ss');
    
    3.关闭表的row movement特性
    
    ALTER TABLE 表名 DISABLE ROW MOVEMENT;

    4.恢复删除的表(通过TRUNCATE语句删除的表无法闪回)

    FLASHBACK TABLE 表名 TO BEFORE DROP;

    8.序列

    创建序列

    create sequence 序列名称
    increment by 1 每次增长多少
    start with 1 从几开始
    minvalue 1 最小值
    maxvalue 999999999 最大值
    cycle/no cycle 序列到达最大值之后怎么办,一般去cycle
    cache 20 需要不需要使用缓存,就是一次生成多少个序列的值,如果生成了多个序列值,如果缓存中有数据, 就
    不需要查询缓存了

    查询序列

    select 序列名.currval from dual  //取当前值
    
    select 序列名.nextval form dual //取下一值

    9.索引

    oracle查询有一个优化,那就是查询过一次的数据他会产生缓存

    使用索引的原则:

      1.大数据才创建索引

      2.为经常用到的列创建索引

      3.索引的层数不要超过4层,也就是 on 表名(列名1,列名2) 这里的列明不要超过4个,因为索引太多也会拖慢检索速度

      4.表的主键自带索引

      多列的索引就是复合索引

      创建索引

    create index 索引名 on 表名(列明)

    10.函数

    1.nvl 见到空值替换为后面的值

    //如果age是空值就为1
    select nvl(age,1) from table

    2.concat 拼接

    select concat('年龄',age) from table
            或
    select '年龄'||age from table

    11.存储过程

    in是输入,out是输出

    as 那是定义变量用的

    create or replace proceduer 存储过程名(参数名 in或out 参数类型)
    as 变量名 变量类型
    begin
        执行的操作
    end;

    例子

    create or replace proceduer test(ids in number)
    as lizi number
    begin
        select value into lizi from tableName where id=ids //这样写可以将查询的值赋给声明的变量
        update tableName set  value='1000' where id=ids
    end;

    12.存储函数

    in输入,out输出

    as 定义变量用的,定义的变量类型要和返回的类型一致

    create or replace function 方法名(参数 in|out 类型) return 参数类型
    as 变量名称 变量类型
    begin
        执行的操作
        return 变量名;//返回的变量类型要和return那定义的类型一样
    end;

    例子

    create or replace function sc(ids in number) return varchar
    as aa varchar
    begin
        select value into aa from emp where id=ids
        return aa;
    end;

    out的用法例子

    创建函数
        create or replace proceduer test(id out number)
        as     
        begin
            select id into id from table
        end;    
    执行函数
        declare
            id number;//声明变量获得值
        begin
            test(id);
            dbms_output.put_line(id);//这个是输出语句
        end;    

    13.视图

    删除视图中的数据会影响基本表,但是删除整个视图不会对基本表产生影响

    删除的视图不会进入回收站,删除基本表会影响视图

    闪回删除基本表,视图也会恢复

    创建视图

    create view 视图名 as 查询的sql语句

    创建只读视图

    create or replace view 视图名 as sql语句

    14.oracle事物回滚的条件

    1.显示回滚:rollback

    2.隐藏回滚:关闭sqlplus工具窗口,死机,掉电

    oracle能回滚主要是因为实例池

    oracle的事物隔离级别有两种:1.read committed 和 serializable

    设置事物隔离级别为serializable:
    
    set transaction isolation level serializable;

    15.常用符号

    原文地址: https://www.cnblogs.com/WebcrawlerBlog/p/4037162.html

    运算符:

    等于:=、<、<=、>、>=、<>
    
    包含:in、not in、 exists、not exists
    
    范围:between...and、not between....and
    
    匹配测试:like、not like
    
    Null测试:is null、is not null
    
    布尔链接:and、or、not   

    通配符

    %(百分号): 用来表示任意数量的字符,或者可能根本没有字符。
    
    _(下划线): 表示确切的未知字符。
    
    ?(问号): 用来表示确切的未知字符。
    
    #(井号): 用来表示确切的阿拉伯数字,0到9。
    
    [a-d](方括号):用来表示字符范围,在这里是从a到d    

    常用符号

    单引号:单引号将文本和字符和日期括起来
    
    双引号:双引号被用来将包含特定字符或者空格的列别名括起来。双引号还被用来将文本放入日期格式
    
    &符号:&号在oracle中常来指出一个变量,例如:&fox 这就声明fox是一个变量,当单&声明变量的时候,每次看见这个变量都要给他赋一个值,而双&号声明的时候,只需要在声明的时候赋一次值就行例如:&&fox,如果想要关闭&号特性,将&号当为普通的符号使用,需要在oracle的命令窗口运行: set define off 命令
    
    单&相当于var,双&相当于let
    
    双竖线||:表示字符串连接函数,就像java的+号
    
    星号*:表示0个或任意多个字符。
    
    := :赋值语句,如 name :='admin'
    
    : :变量绑定

    16.求两个时间段相差的分钟数

    在oracle中时间加减运算得到的结果是以天为单位的,如果想要得到小时就*24,得到分钟*24*60,得到秒*24*60*60

    得到天
    select (to_date( t.A,'yyyy-mm-dd hh24:mi:ss')-  to_date( t.B,'yyyy-mm-dd hh24:mi:ss'))  from table t 
    得到小时
    select (to_date( t.A,'yyyy-mm-dd hh24:mi:ss')-  to_date( t.B,'yyyy-mm-dd hh24:mi:ss'))*24  from table t 
    得到分
    select (to_date( t.A,'yyyy-mm-dd hh24:mi:ss')-  to_date( t.B,'yyyy-mm-dd hh24:mi:ss'))*24*60  from table t 
    得到秒
    select (to_date( t.A,'yyyy-mm-dd hh24:mi:ss')-  to_date( t.B,'yyyy-mm-dd hh24:mi:ss'))*24*60*60  from table t 
    
    原文链接:https://blog.csdn.net/u010050174/article/details/79459664

     

     

     

  • 相关阅读:
    target runtime apache v6.0 not defined解决
    java.lang.AbstractMethodError: javax.servlet.jsp.JspFactory.getJspApplicationContext(Ljavax/servlet/ServletContext;)Ljavax/servlet/jsp/JspApplicationContext;
    The valid characters are defined in RFC 7230 and RFC 3986问题
    invalid END header解决方法
    You have more than one version of ‘org.apache.commons.logging.Log’ visible, which is not allowed问题解决
    Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
    在eclipse中import java web项目时遇到的一些问题并将该项目通过tomcat发布
    java byte转string 涉及到字节流中有中文
    spring+mybatis框架搭建时遇到Mapped Statements collection does not contain value for...的错误
    试试看读一下Zepto源码
  • 原文地址:https://www.cnblogs.com/HQ0422/p/12740374.html
Copyright © 2011-2022 走看看