Open-Source World

          let's learn and study.
          posts - 28, comments - 23, trackbacks - 0, articles - 1

          導航

          <2007年9月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          常用鏈接

          留言簿(1)

          隨筆檔案

          文章檔案

          Friends

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          Oracle中常用SQL操作

          Posted on 2007-09-15 18:17 tearofmoscow 閱讀(257) 評論(0)  編輯  收藏
          Oracle中常用SQL操作
          jelver 轉貼   更新:2007-09-10 16:56:02  版本: 1.0   

          學習sql有一段時間了,發現在我建了一個用來測試的表(沒有建索引)中出現了許多的重復記錄。后來總結了一些刪除重復記錄的方法,在Oracle中,可以通過唯一rowid實現刪除重復記錄;還可以建臨時表來實現...這個只提到其中的幾種簡單實用的方法,希望可以和大家分享(以表employee為例)。 


          SQL> desc employee 

           Name                                      Null?    Type
           ----------------------------------------- -------- ------------------ 

          emp_id                                                NUMBER(10)
          emp_name                                           VARCHAR2(20) 
          salary                                                  NUMBER(10,2) 


          可以通過下面的語句查詢重復的記錄: 

          SQL> select * from employee; 


              EMP_ID EMP_NAME                                  SALARY 

          ---------- ---------------------------------------- ---------- 

                   1 sunshine                                      10000 

                   1 sunshine                                      10000

                   2 semon                                         20000 

                   2 semon                                         20000 



                   3 xyz                                           30000 



                   2 semon                                         20000 


          SQL> select distinct * from employee; 

              EMP_ID EMP_NAME                                     SALARY 

          ---------- ---------------------------------------- ---------- 

                   1 sunshine                                      10000 

                   2 semon                                         20000 

                   3 xyz                                             30000 



          SQL>  select * from employee group by emp_id,emp_name,salary having count (*)>1 

              EMP_ID EMP_NAME                                     SALARY 

          ---------- ---------------------------------------- ---------- 

                   1 sunshine                                      10000 

                   2 semon                                          20000 


          SQL> select * from employee e1 
          where rowid in (select max(rowid) from employe e2 
           where e1.emp_id=e2.emp_id and 
            e1.emp_name=e2.emp_name and e1.salary=e2.salary); 



              EMP_ID EMP_NAME                                     SALARY 

          ---------- ---------------------------------------- ---------- 

                   1 sunshine                                      10000 

                   3 xyz                                             30000 

                   2 semon                                         20000 

          2. 刪除的幾種方法: 

          (1)通過建立臨時表來實現 

          SQL>create table temp_emp as (select distinct * from employee)  


          SQL> truncate table employee; (清空employee表的數據) 

          SQL> insert into employee select * from temp_emp;  (再將臨時表里的內容插回來) 


          ( 2)通過唯一rowid實現刪除重復記錄.在Oracle中,每一條記錄都有一個rowid,rowid在整個數據庫中是唯一的,rowid確定了每條記錄是在Oracle中的哪一個數據文件、塊、行上。在重復的記錄中,可能所有列的內容都相同,但rowid不會相同,所以只要確定出重復記錄中那些具有最大或最小rowid的就可以了,其余全部刪除。 

          SQL>delete from employee e2 where rowid not in (
                  select max(e1.rowid) from employee e1 where 



                  e1.emp_id=e2.emp_id and e1.emp_name=e2.emp_name and e1.salary=e2.salary);--這里用min(rowid)也可以。 




          SQL>delete from employee e2 where rowid <(
                  select max(e1.rowid) from employee e1 where 
                  e1.emp_id=e2.emp_id and e1.emp_name=e2.emp_name and 
                            e1.salary=e2.salary); 

          (3)也是通過rowid,但效率更高。 



          SQL>delete from employee where rowid not in (
                  select max(t1.rowid) from employee t1 group by 

                   t1.emp_id,t1.emp_name,t1.salary);--這里用min(rowid)也可以。 


              EMP_ID EMP_NAME                                     SALARY 

          ---------- ---------------------------------------- ---------- 

                   1 sunshine                                      10000 


                   3 xyz                                             30000 


                   2 semon                                         20000 


          SQL> desc employee 

           Name                                      Null?    Type
           ----------------------------------------- -------- ------------------ 


          emp_id                                                NUMBER(10)
          emp_name                                           VARCHAR2(20) 

          salary                                                  NUMBER(10,2) 


          可以通過下面的語句查詢重復的記錄: 

          SQL> select * from employee; 

              EMP_ID EMP_NAME                                  SALARY 

          ---------- ---------------------------------------- ---------- 

                   1 sunshine                                      10000 



                   1 sunshine                                      10000 



                   2 semon                                         20000 



                   2 semon                                         20000 

                   3 xyz                                           30000 

                   2 semon                                         20000 

          SQL> select distinct * from employee; 

              EMP_ID EMP_NAME                                     SALARY 
          ---------- ---------------------------------------- ---------- 

                   1 sunshine                                      10000 

                   2 semon                                         20000 

                   3 xyz                                             30000 



          SQL>  select * from employee group by emp_id,emp_name,salary having count (*)>1 

              EMP_ID EMP_NAME                                     SALARY 

          ---------- ---------------------------------------- ---------- 

                   1 sunshine                                      10000 

                   2 semon                                          20000 


          SQL> select * from employee e1 
          where rowid in (select max(rowid) from employe e2 
           where e1.emp_id=e2.emp_id and 
            e1.emp_name=e2.emp_name and e1.salary=e2.salary); 

              EMP_ID EMP_NAME                                     SALARY 

          ---------- ---------------------------------------- ---------- 

                   1 sunshine                                      10000 

                   3 xyz                                             30000 

                   2 semon                                         20000 

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 九江市| 方城县| 奉新县| 鲁甸县| 博湖县| 云南省| 平邑县| 绥德县| 文化| 福鼎市| 廉江市| 甘谷县| 库尔勒市| 漳平市| 名山县| 吴川市| 灵宝市| 蒙阴县| 盘山县| 宜宾市| 罗山县| 海林市| 三河市| 沙田区| 衡阳县| 乌拉特中旗| 西畴县| 禹城市| 贵南县| 汶上县| 馆陶县| 合山市| 武安市| 五华县| 紫云| 凉城县| 赣榆县| 若尔盖县| 五家渠市| 武强县| 无极县|