zoukankan      html  css  js  c++  java
  • oralce 菜鸟总结

    1、Oracle 中默认的日期格式:DD-Mon-RR

    2、SELECT  (SYSDATE-to_date('2010-01-01','yyyy-mm-dd'))/7 AS WEEKS from dual;

    3、如果只是计算两个日期的月份的话为double行结果:

    select months_between(sysdate,to_date('2010-01-01','yyyy-mm-dd')) from dual; ----9.27152964456392

    如果只要整数部分而不要小数部分:

    select trunc(months_between(sysdate,to_date('2010-01-01','yyyy-mm-dd'))) from dual; -----9

    如果想四舍五入求日期则:

    select ceil(months_between(sysdate,to_date('2010-01-01','yyyy-mm-dd'))) from dual; ------10

    4、add_months 函数

    select add_months(sysdate,2) from dual;

    5、next_day 函数 :返回的是下一个星期一对应的日期

    select next_day(sysdate,'星期一') from dual;

    select next_day(date'2003-12-2',1) from dual;

    select date'2003-12-2'  from dual;

     6、NULL指的是空值,或者非法值,

    NVL (expr1, expr2)->expr1为NULL,返回expr2;不为NULL,返回expr1。注意两者的类型要一致,

    NVL2 (expr1, expr2, expr3) ->expr1不为NULL,返回expr2;为NULL,返回expr3。expr2和expr3类型不同的话,expr3会转换为expr2的类型,
    NULLIF (expr1, expr2) ->相等返回NULL,不等返回expr1

    7、case when then 作为一个整体包含在括号中,同时要有end作为结尾语句,否则报错

    select (case frist_name when 'zhang' then 1.2*salary
     when 'li' then 1.5*salary end ) hh  from employees   

     8、 复制表结构及其数据:

    create table table_name_new as select * from table_name_old

    只复制表结构:

    create table table_name_new as select * from table_name_old where 1=2;

    如果两个表结构一样:

    insert into table_name_new select * from table_name_old

    如果两个表结构不一样:

    insert into table_name_new(column1,column2...) select column1,column2... from table_name_old

    将多个表数据插入一个表中
    insert into 目标表test(字段1。。。字段n) (select 字段1.。。。。字段n) from 表 union all select 字段1.....字段n from 表

    nsert into twotable select * from employees union all select * from newtable;

    本文来自CSDN博客,转载请标明出处http://blog.csdn.net/yzj_000/archive/2008/05/04/2378432.aspx

    网友的关于Oracle 表或数据复制的文章

    http://hi.baidu.com/wpwxf/blog/item/f1f03c61c102af4aeaf8f871.html

    9、oracle误删除表数据后的的快速回复功能方法:

    insert into newTable
     select * from newTable as of timestamp to_timestamp('2010-10-09 14:00:00', 'yyyy-mm-dd hh24:mi:ss');

    或者

    flashback table newTable to timestamp to_timestamp('2010-10-09 14:00:00','yyyy-mm-dd hh24:mi:ss');
    这种方法简单,容易掌握,功能和上面的一样时间为你误操作之前的时间,最好是离误操作比较近的,因为oracle保存在回滚保持段里的数据时间有一定的时间限制由undo_retention 这个参数值决定。

    网友的oracle误删除表数据后的的快速回复功能文章

    http://hi.baidu.com/xoy2129/blog/item/82abb88f0a4b0eecf01f36f3.html

    http://hi.baidu.com/whzkinger/blog/item/4b1a8f167cb42e5df3de3238.html

    快速的恢复已经删除的表(newtable)

    drop table newtable;
    flashback table newtable to before drop; 

    newtable已经删除,select * from tab; 可以看到newtable并不是真正意义上的删除掉了,而是多了一个"BIN$dFFlS6jJQLShRwRQ+kLJMg==$0"的表,这个表是原来的newtable表,这是oracle的recyclebin,一个类似垃圾回收站的机制。当用户删除表以后把表放到recyclebin中,而不是删除掉!

    flashback:即回闪,是从oracle9i就开始提出的一种操作恢复的功能,在oracle10g中进行了增强和修改,通过回闪,用户可以完成许多不可能恢复的工作,目前oracle10g的回闪包括以下特性;

    1〉oracle falshback Database. 特性允许oracle通过Flashback database语句,将数据库会滚到前一个时间点或者scn上,而不需要作时间点的恢复工作!
    2〉oracle falshback table. 特性允许oracle通过flashback table语句,将表会滚到前一个时间点或者scn上。
    3〉oracle falshback drop. 特性允许oracle把恢复drop掉的table或者索引。
    4〉oracle falshback version query. 特性可以得到特定的表在某一个时间段内的任何修改记录!
    5〉oracle falshback transaction query 特性可以限制用户在某一个事务级别上检查数据库的修改操作,适用于诊断问题、分析性能、审计事务。

    Oracle falshback 网上的一些总结:

    http://myfriend2010.itpub.net/post/29012/283941

    http://www.sosdb.com/jdul/dispbbs.asp?boardID=1&ID=268

    10、ORACLE已经在DATE数据类型上扩展出来了TIMESTAMP数据类型,它包括了所有DATE数据类型的年月日时分秒的信息,而且包括了小数秒的信息。如果你想把DATE类型转换成TIMESTAMP类型,就使用CAST函数。

    select cast(sysdate as timestamp) dated from dual;----------09-10月-10 02.38.46.000000 下午

    select sysdate from dual;--------2010-10-9 14:39:27

    11、Merge用法:Oracle 10g中对Merge语句的增强(或者是 exits 存在或者不存在)

    if exists(select 1 from T where T.a='1001' ) update T set T.b=2 Where T.a='1001' else insert into T(a,b) values('1001',2);

    在Oracle 10g之前,merge语句支持匹配更新和不匹配插入2种简单的用法,在10g中Oracle对merge语句做了增强,增加了条件选项和DELETE操作

    merge 普通操作

     merge into acct a using subs b on (a.msid=b.msid)
     when matched then
     update set a.areacode=b.areacode
     when not matched then
     insert(msid,bill_month,areacode) values(b.msid,201009,b.areacode);

    merge 条件操作

    merge into acct a using subs b on (a.msid=b.msid)    
    when MATCHED then
    update set a.areacode=b.areacode
    where b.ms_type=0
    when NOT MATCHED then
    insert(msid,bill_month,areacode)
    values(b.msid,'200702',b.areacode)
    where b.ms_type=0;
    merge 删除操作
    merge into acct a 
    using subs b on (a.msid=b.msid)
     when MATCHED then
    update set a.areacode=b.areacode       
    delete where (b.ms_type!=0);       

    Merge用法:Oracle 10g中对Merge语句的增强     

    http://www.eygle.com/digest/2009/02/mergeoracle_10gmerge.html

     http://www.eygle.com/digest/2009/01/merge_into_insertupdate.html

    如果不懂Merge语句的原理,Merge语句是一条比较危险的语句,特别是在您只想更新一条记录的时候,因为不经意间,你可能就把整表的数据都Update了一遍.....汗!!!

     在一个同时存在Insert和Update语法的Merge语句中,总共Insert/Update的记录数,就是Using语句中alias2的记录数

    如下:

    有一个表T,有两个字段a,b,我们想在表T中做Insert/Update,如果存在,则更新T中b的值,如果不存在,则插入一条记录。在Microsoft的SQL语法中,很简单的一句判断就可以了,SQL Server中的语法如下:

    if exists(select 1 from T where T.a='1001' ) update T set T.b=2 Where T.a='1001' else insert into T(a,b) values('1001',2);

    以上语句表明当T表中如果存在a='1001' 的记录的话,就把b的值设为2,否则就Insert一条a='100',b=2的记录到T中。

    但是接下来在Oracle中就遇到麻烦了,记得在Oracle 9i之后就有一条Merge into 的语句可以同时进行Insert 和Update的

     select * from t; 如下图

    执行merge 语句:

    merge into t t1  using (select a,b from t where t.a='1001') t2 on (t1.a=t2.a)
    when matched then update set t1.b='haha'
    when not matched then insert(a,b) values('1001','2')

    以上的语句貌似很对是吧,实际上,该语句只能进行更新,而无法进行Insert,错误在哪里呢?

    在t2中Select出来的数据,每一条都跟t1进行 ON (join condition)的比较,如果匹配,就进行更新的操作(Update),如果不匹配,就进行插入操作(Insert)。

    因此,严格意义上讲,"在一个同时存在Insert和Update语法的Merge语句中,总共Insert/Update的记录数,就是Using语句中t2的记录数。"

    以上这句话也就很好的解释了在上面写的语句为何只能进行Update,而不能进行Insert了,因为都Select不到数据,如何能进行Insert呢:)

    MERGE INTO T T1  USING (SELECT '1001' AS a,2 AS b FROM dual) T2 ON ( T1.a=T2.a)
    WHEN MATCHED THEN UPDATE SET T1.b = T2.b
    WHEN NOT MATCHED THEN  INSERT (a,b) VALUES(T2.a,T2.b);

    具体详细内容在如下:

    http://www.cnblogs.com/6303c/admin/EditPosts.aspx?postid=1846473

    12、with语句:其实就是把一大堆重复用到的SQL语句放在with as 里面,取一个别名,后面的查询就可以用它

    这样对于大批量的SQL语句起到一个优化的作用,而且清楚明了

    with a as(select * from t ) select * from a;

     13 Oracle dblink

    insert into odrm023_autoset select * from odrm023_autoset@dblink_db51;

  • 相关阅读:
    解决Xcode8打印了nw_socket_handle_socket_event Event mask
    调用系统框架使用设备系统语言的设置,相册相机设置为中文
    ios开发 之 设置多种文字颜色/背景色/文字下划线/行间距 NSString
    IOS 开发中 Whose view is not in the window hierarchy 错误的解决办法
    UITableView设置cell的separator 分割线
    iOS用户点击推送消息进入应用后自动跳转到对应的ViewController
    随感
    JS获取当前网页大小以及屏幕分辨率等
    js将秒转换为 分:秒 函数
    css实现强制不换行/自动换行/强制换行
  • 原文地址:https://www.cnblogs.com/6303c/p/1846473.html
Copyright © 2011-2022 走看看