oracle常用高級sql操作


一、運算符
算術運算符:+ - * / 可以在select 語句中使用
連接運算符:||    select deptno|| dname from dept; 
比較運算符:> >= = != < <= like between is null in
邏輯運算符:not and or 
集合運算符: intersect ,union, union all, minus 
要求:對應集合的列數和數據類型相同
     查詢中不能包含long 列
     列的標簽是第一個集合的標簽
     使用order by時,必須使用位置序號,不能使用列名
 
 
1. 復制表結構及其數據:
 
create table table_name_new as select * from table_name_old
 
2. 只復制表結構:
 
create table table_name_new as select * from table_name_old where 1=2;
 
或者:
 
create table table_name_new like table_name_old
 
3. 只復制表數據:
 
如果兩個表結構一樣:
 
insert into table_name_new select * from table_name_old
 
如果兩個表結構不一樣:
 
insert into table_name_new(column1,column2...) select column1,column2... from table_name_old
 
pasting
----------------------------------------------遞歸查詢
--子查父(通過子節點向根節點追朔.) 
查詢〔特下邊〕的父節點
select *
  from tb_class t
 start with t.class_id = '1030107742'
connect by prior t.super_class_id = t.class_id
order by t.tree_level desc 
 
--父查子(通過根節點遍歷子節點.) 
查詢〔特下邊〕的子節點:結果
select *
  from tb_class t
 start with t.class_id = '1030107742'
connect by prior t.class_id = t.super_class_id
order by t.tree_level desc
 
1.NVL函數
NVL函數的格式如下:NVL(expr1,expr2)
含義是:如果oracle第一個參數為空那么顯示第二個參數的值,如果第一個參數的值不為空,則顯示第一個參數本來的值。
 
oralce 坐連接右連接  
有+號的表不全部顯示,對面的表全部顯示。
 
----------------------------------------------2016-2-24 10:06:49
2、DECODE函數 ( )
語法:decode(expr,search1,result1,
search2,result2,
 ……
search n,result n, default)
解釋:decode函數將expr值與各search值一個一個比對,若expr值等於search值oracle數據庫返回其對應的result值;若沒有匹配的search值,則返回default值;若函數中default值缺省則返回null。
/*select decode(status,0,'yes',1,'no') as end  from sys_user  where id = 158 */
 
oracle高級update語句  (批量update)
update t_source_phase p set p.lineno = 
 (select num from t_source_line l where p.lineid = l.id) where p.lineid is not null;
 select的同時就update
 oracle高級查詢語句  (分組后按組排序)
  select t.*,row_number() over(partition by planid order by sort asc ) row_number
  from t_temp_pathinfo_log t 
 

oracle增加字段語法:
@添加字段的語法:alter table tablename add (column datatype [default value][null/not null],….);
 
 
@修改字段的語法:alter table tablename modify (column datatype [default value][null/not null],….);
 
@刪除字段的語法:alter table tablename drop (column);
//2016年1月21日09:56:53
@創建序列
create sequence SEQ_DEPT
minvalue 1
maxvalue 99999999
start with 241
increment by 1
cache 20;
 
@高級語句
    1.INSERT INTO SELECT --語句
 
      語句形式為:Insert into Table2(field1,field2,...) select value1,value2,... from Table1
 
      要求目標表Table2必須存在,由於目標表Table2已經存在,所以我們除了插入源表Table1的字段外,還可以插入常量。
      
 2.SELECT INTO FROM --語句
 
      語句形式為:SELECT vale1, value2 into Table2 from Table1
 
      要求目標表Table2不存在,因為在插入時會自動創建表Table2,並將Table1中指定字段數據復制到Table2中。示例如下:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM