zoukankan      html  css  js  c++  java
  • 【oracle】oracle经典sql,exception,database link纠错

    1111-01 oracle经典sql,exception,database link纠错


    1.给表table_a扩展2个字段:studsex varchar2(4) 和 studage integer。
    alter table table_a
    add ( studsex varchar2(4),studage integer );

    2.建立一个视图v_table,要求v_table是table_a和table_b的并集,当字段studid,studname,depart一致时,而字段studscore不一致时,以table_a的学分字段值为准。
    create or replace view v_table as
    select a.studid,a.studname,a.depart,a.studscore from table_a a
    union all
    select b.studid,b.studname,b.depart,b.studscore from table_b b
    where not exists (select 1 from table_a aa where aa.studid=b.studid,aa.studname=b.studname,aa.depart=b.depart);

    3.根据studid找出table_b中不存在table_a的数据。
    select b.studid,b.studname,b.depart,b.studscore from table_b b where not exists (select 1 from table_a a where a.studid = b.studid);

    4.几种sql的效率
    set autotrace on;

    --写法2 Plan Hash Value:1630949702
    select a.studid from tanle_a a
    minus
    select b.studid from table_b b;

    --写法2 Plan Hash Value:4195831724
    select a.studid from tanle_a a
    where not exists (
    select 1 from table_b b where b.studid = a.studid);

    --写法3 Plan Hash Value:
    select a.studid from tanle_a a
    where a.studif not in (
    select b.studid from table_b b);


    select least( 1630949702, 4195831724 , 4195831724 ) from dual;


    ——字符集
    SIMPLEFIED CHINESE_CHINA
    AMERICAN_AMERICA


    AL32UTF8
    ZHS16GBK


    ——CAP Theorem
    一致性 Consistency
    可用性 Availability
    分区容忍性Partition Tolerance
    CAP原理的意思是,一个分布式系统不能同时满足一致性,可用性和分区容错性这三个需要,最多只能同时满足两个。

    事务的ACID特性:
    Atomic 原子性
    Consistent 一致性
    Isolation 隔离性
    Durable 持久性

    ——set autotrace 查看autotrace的选项值有哪些
    set auto[trace] {off | on |trace[only]} [exp[plain]] [stat[istics]]

    ——oracle修改表数据
    select t*,t.rowid from <table> t;
    select t* from <table> t for update;

    ——绑定变量 variable 与 define
    ORACLR绑定变量用法总结
    http://wenku.baidu.com/view/4ca502ea6f1aff00bed51eed.html?from=search

    ——oracle 数据类型 —— 3种集合类型
    index-by表:不存储在数据库中,下标连续
    varray:存储在数据库中,下标连续
    嵌套表:存储在数据库中

    ——Oracle 异常 :exception
    1.预定义内部异常:ORA错误码已经与异常名称绑定
    no_data_found
    too_many_rows
    dup_val_on_index
    zero_divide
    case_not_found
    storage_error
    value_error
    cursor_already_open
    timeout_on_resource
    等 共21个
    【例子1:自动抛出】
    set serveroutput on;
    declare
    v_name t_stu.name%type;
    begin
    select name into v_name from t_stu where serial_no > 5;
    dbms_output.put_line('name='||v_name);
    exception
    when too_many_rows then
    dbms_output.put_line('查询返回了过多的行');
    end;
    /

    【例子1:手动抛出】
    set serveroutput on;
    declare
    --v_name varchar2(30);
    begin
    --select name into v_name from t_stu where id < 1;
    --dbms_output.put_line('v_name='||v_name);
    raise no_data_found; --手动抛出预定义的内部异常
    exception
    when no_data_found then
    dbms_output.put_out('no_data_found');
    when others then
    dbms_output.put_out('other');
    end;
    /


    2.未预定义内部异常:没有异常名称的ORA错误码
    --将ORA错误码与异常名称绑定 语法
    pragma exception_init(<exception>,<sqlcode>);
    【例子】ORA-00001 表示 违反唯一约束
    set serveroutput on;
    declare
    uq_exception exception;
    pragma exception_init(uq_exception,-1);
    begin
    insert into t_stu(id,name) values(1,'scott');
    insert into t_stu(id,name) values(1,'scott');
    commit;
    exception
    when uq_exception then
    rollback;
    dbms_output.put_line('违反唯一约束条件:sqlcode='||sqlcode||',sqlerrm='||substr(sqlerrm,1,200));
    when others then
    rollback;
    dbms_output.put_line('sqlcode='||sqlcode||',sqlerrm='||substr(sqlerrm,1,200));
    end;
    /

    3.自定义异常:需要编程者自己定义ORA错误码(即异常发生的条件),并与异常名称绑定。
    【例子】
    绑定 自定义ORA错误码 与 自定义异常
    抛出异常:oracle存储过程 raise_application_error
    语法:
    raise_application_error(error_number,message[,true|false])
    参数 error_message:Oracle错误码范围[-20000,-20999]
    参数 message:错误信息是文本字符串,最多为2048字节(即2kb)
    参数 true|false:true表示 添加错误堆(error_stack);false表示 覆盖错误堆。默认为false。

    declare
    my_exception exception;
    pragma exception_init(my_exception,-20001);
    message varchar2(4000);
    errmsg varchar2(200);
    begin
    message := 'my_exception occured!';
    raise_application_error(-20001,message,true); --手动抛出自定义错误码
    exception
    when my_exception then
    errmsg := substr(sqlerrm,1,200);
    dbms_output.put_line(sqlcode||',||errmsg);
    when others then
    dbms_output.put_line('other exception occured!');
    end;
    /

    说明:
    异常发生后,跳转到异常处理部分;异常处理后,程序不会返回到异常发生处的代码,即异常发生处以下的代码不会被执行。


    调试存储过程
    1.PL/SQL Developer
    用于 创建,编译 的调试
    2.sql语句插入日志表
    用于 过程的逻辑调试
    3.命令 show errors
    用于 创建,编译,调用 的调试
    /* plsql编译警告*/
    类型(3种)
    severe:检查 可能出现的不可预料错误结果
    performance:检查 可能引起的 性能问题
    informational:检查 子程序中死代码
    all:检查 所有警告

    参数 plsql_warnings
    1)作用:使得数据库在编译子程序时发出警告信息
    2)语法:
    alter system|session|procedure <prc_name> set|compile plsql_warnings = 'enable|disable:severe|performance|informational|all';

    使用 命名 show errors 查看具体的警告。


    ——oracle 的update语句
    Oracle用一个表的列更新另一个表对应的记录
    http://wenku.baidu.com/view/fb67b28f680203d8ce2f2483.html?from=search

    ——oracle partition补充
    需求:
    c1 number(12) 表示分种
    c2 integer 数值总是[1,288]
    在c1建立适当 分区,在 c2 建立适当 子分区。
    --range-list
    create table t_part_tab(
    c1 number(12), --分种
    c2 integer, --[1,288]
    c3 varchar2(100)
    ) partition by range(c1) subpartition by list(c2)
    subpartition template(
    subpartition sp001 values(1),
    subpartition sp002 values(2),
    ……
    )
    (
    partition p20150101 values less than (201501012460),
    partition p20150102 values less than (201501022460),
    ……
    );

    user_part_tables
    user_tab_partitions
    user_tab_subpartitions

    select * from t_part_tab partition(p20150101);
    select * from t_part_tab subpartition(p20150101_sp001);


    ——database link
    grant create [public] database link to <user>;
    grant drop database link to <user>; --否则只有dba用户(system, sys)才能删除 全局dblink

    创建方式1:指定ip,port,SID
    create [public] database link <dblink_name> connect to <user> identified by <pwd>
    using '
    (DESCRIPTION=
    (ADDRESS_LIST =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 10.137.150.243)(PORT = 1521)
    )
    (CONNECT_DATA = (SERVER_NAME = inomc)
    )
    )
    )
    ';

    创建方式2:指定TNS name
    create [public] database link <dblink_name> connect to <user> identified by <pwd>
    using '<tnsname>'; --<tnsname> 实在服务器端 $ORACLE_HOME/network/admin/tnsnames.ora文件中配置了

    select * from <table>@<dblink>;
    create synonym <synonym> for <table>@<dblink>;

    1.创建
    public和非public,普通用户都需要授权;DBA用户(system,sys)都无需授权。
    2.删除
    非public:无需授权,拥有者用户,dba用户 都可以删除。
    public:默认只有dba用户才有权限删除,普通用户需要授权。
    3.使用
    非public:仅 拥有者用户,dba用户 可以访问。
    public:同实例的所有用户都可以访问。

  • 相关阅读:
    datanode报错Problem connecting to server
    使用命令查看hdfs的状态
    Access denied for user root. Superuser privilege is requ
    ElasticSearch默认的分页参数 size
    SparkStreaming Kafka 维护offset
    【容错篇】Spark Streaming的还原药水——Checkpoint
    251 Android 线性与相对布局简介
    250 Android Studio使用指南 总结
    249 如何解决项目导入产生的中文乱码问题
    248 gradle更新问题
  • 原文地址:https://www.cnblogs.com/greenZ/p/8721822.html
Copyright © 2011-2022 走看看