SQL FAQ 个人总结(结合oracle FAQ)

·                     The method that remove the duplicate rows from a table
1、delete from table_name
   where row_id>(
                 select min(row_id) from table_name B
                 where a.key_value=b.key_value);
2、create table table_name2
          as select distinct * from table_name1
   drop table_name1
   rename table_name2 to table_name1;
 
Note:Beacuse the table has been dropped from the table,the indexs、triggers、constrants etc also has been removed,we must establish them again.
3、delete table table_name1 tbl1
   where exists (select 'X' from table_name2 tbl2
                where tbl2.key_value1=tbl1.key_value2
                    tbl2.key_value2=tbl1.key_value1
                    tbl2.row_id>tbl1.row_id);
 
·                     How to use SYSDATE?
1、add "day/hour/minute/second" format value to a column
   select sysdate,sysdate+1 tomorow,sysdate+1/24 H_before,sysdate+1/1440 min_before,sysdate+30/86400 from table1;
 
2、Using "sysdate+7" To descripe one week before;
3、The midnight using "trunc(sysdate+1)";
4、Using "trunc(sysdate+1)+8/24" to descripe 8:00 AM the next day;
 
·                     Get the Nth column value only.
1、select * from table t1
 where N=(
          select count(row_id) from table t2
          where t1.key_value=t2.key_value
          t1.row_id>t2.row_id
          );
2、select * from (
    select rowMN rn from table1 where rn<101
    )
   where rn=100;
 
Note:The second SQL statement is better than the first,also far better than using the MINUS operation,the first must scan all the records,but the second not,only 101 records.
if the table consists of many records,the first SQL statement will expend large memory.
 
 
·                     Get rows N to M from a table.(N<101,M<101)
 
1、select * from (
         select col1,rownum RN from table1
         where RN>101)
   where rownum between N to M;
 
2、select rownum,col2 from table
   group by rownum,having rownum between N to M;
 
3、select rownum RN,col2 from (
                    select rownum,col2 from
                    (select * from table1 order by col3) a
                   where rownum<=M )
   where rownum>=N;
 
·                     Get the every Nth rows from a table
 
select * from table
group by rownum having mod(rownum,N)=0;
 
·                     Get the topN of the rows from a table
 
select * from
      (select * from table1 order by col1 desc) A
where A.rownum>N;
 
Note:The SQL statement can be used to oracles from oracle8i;
 
·                     Rename a column of the table
 
1、alter table table1 rename oldcolumnname to new columnname;
2、create table table2(list of the new columns) as (
                     select * from table1)
drop table table1;
rename table table2 to table1;
 
3、alter table table1 add(newcolumnname datatype);
update table1 set newcolumnname=oldcolumnname;
alter table table1 drop column oldcolumnname;
 
 
·                     Change the password of your Oracle.
 
alter user username identified by newpassword.
 
·                     The function of DECODE in Oracle SQL
 
decode (expression, search_1, result_1)
decode (expression, search_1, result_1, search_2, result_2)
decode (expression, search_1, result_1, search_2, result_2, ...., search_n, result_n)
 
decode (expression, search_1, result_1, default)
decode (expression, search_1, result_1, search_2, result_2, default)
decode (expression, search_1, result_1, search_2, result_2, ...., search_n, result_n, default)
 
Note:DECODE's function is same of comparetion expression,
    it search the result_X which match the expression,if YES,returns result_X,if NOT, returns default,if default left out,returns nothing.
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值