|
導讀數據庫,簡而言之可視為電子化的文件柜——存儲電子文件的處所,用戶可以對文件中的數據進行新增、截取、更新、刪除等操作。所謂“數據庫”是以一定方式儲存在一起、能與多個用戶共享、具有盡可能小的冗余度、與應... 數據庫,簡而言之可視為電子化的文件柜——存儲電子文件的處所,用戶可以對文件中的數據進行新增、截取、更新、刪除等操作。所謂“數據庫”是以一定方式儲存在一起、能與多個用戶共享、具有盡可能小的冗余度、與應用程序彼此獨立的數據集合。 學習sql有一段時間了,發現在我建了一個用來測試的表(沒有建索引)中出現了許多的重復記錄。后來總結了一些刪除重復記錄的方法,在Oracle中,可以通過唯一rowid實現刪除重復記錄;還可以建臨時表來實現...這個只提到其中的幾種簡單實用的方法,希望可以和大家分享(以表employee為例)。 SQL> desc employee Name Null? Type emp_id NUMBER(10) 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
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
where rowid in (select max(rowid) from employe e2 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 ( 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 <( e1.salary=e2.salary); (3)也是通過rowid,但效率更高。 SQL>delete from employee where rowid not in ( 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) 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
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
where rowid in (select max(rowid) from employe e2 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 [page_break] 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 ( 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 <( e1.salary=e2.salary); (3)也是通過rowid,但效率更高。 SQL>delete from employee where rowid not in ( t1.emp_id,t1.emp_name,t1.salary);--這里用min(rowid)也可以。 EMP_ID EMP_NAME SALARY ---------- ---------------------------------------- ---------- 1 sunshine 10000 3 xyz 30000 2 semon 20000 全新的路由器不僅讓你更穩定快速地連接無線網絡,更可以讓家中的智能設備連接在一起。 |
溫馨提示:喜歡本站的話,請收藏一下本站!