數(shù)據(jù)庫刪除完全重復(fù)和部分關(guān)鍵字段重復(fù)的記錄
更新時間:2008年05月08日 22:19:14 作者:
重復(fù)記錄分為兩種,第一種是完全重復(fù)的記錄,也就是所有字段均重復(fù)的記錄,第二種是部分關(guān)鍵字段重復(fù)的記錄,例如Name字段重復(fù),而其它字段不一定重復(fù)或都重復(fù)。
1、第一種重復(fù)很容易解決,不同數(shù)據(jù)庫環(huán)境下方法相似:
以下為引用的內(nèi)容:
Mysql
create table tmp select distinct * from tableName;
drop table tableName;
create table tableName select * from tmp;
drop table tmp;
SQL Server
select distinct * into #Tmp from tableName;
drop table tableName;
select * into tableName from #Tmp;
drop table #Tmp;
Oracle
create table tmp as select distinct * from tableName;
drop table tableName;
create table tableName as select * from tmp;
drop table tmp;
發(fā)生這種重復(fù)的原因是由于表設(shè)計不周而產(chǎn)生的,增加唯一索引列就可以解決此問題。
2、此類重復(fù)問題通常要求保留重復(fù)記錄中的第一條記錄,操作方法如下。 假設(shè)有重復(fù)的字段為Name,Address,要求得到這兩個字段唯一的結(jié)果集
Mysql
以下為引用的內(nèi)容:
alter table tableName add autoID int auto_increment not null;
create table tmp select min(autoID) as autoID from tableName group by Name,Address;
create table tmp2 select tableName.* from tableName,tmp where tableName.autoID = tmp.autoID;
drop table tableName;
rename table tmp2 to tableName;
SQL Server
select identity(int,1,1) as autoID, * into #Tmp from tableName;
select min(autoID) as autoID into #Tmp2 from #Tmp group by Name,Address;
drop table tableName;
select * into tableName from #Tmp where autoID in(select autoID from #Tmp2);
drop table #Tmp;
drop table #Tmp2;
Oracle
DELETE FROM tableName t1 WHERE t1.ROWID > (SELECT MIN(t2.ROWID) FROM tableName t2 WHERE t2.Name = t1.Name and t2.Address = t1.Address);
說明:
1. MySQL和SQL Server中最后一個select得到了Name,Address不重復(fù)的結(jié)果集(多了一個autoID字段,在大家實際寫時可以寫在select子句中省去此列)
2. 因為MySQL和SQL Server沒有提供rowid機制,所以需要通過一個autoID列來實現(xiàn)行的唯一性,而利用Oracle的rowid處理就方便多了。而且使用ROWID是最高效的刪除重復(fù)記錄方法。
以下為引用的內(nèi)容:
Mysql
create table tmp select distinct * from tableName;
drop table tableName;
create table tableName select * from tmp;
drop table tmp;
SQL Server
select distinct * into #Tmp from tableName;
drop table tableName;
select * into tableName from #Tmp;
drop table #Tmp;
Oracle
create table tmp as select distinct * from tableName;
drop table tableName;
create table tableName as select * from tmp;
drop table tmp;
發(fā)生這種重復(fù)的原因是由于表設(shè)計不周而產(chǎn)生的,增加唯一索引列就可以解決此問題。
2、此類重復(fù)問題通常要求保留重復(fù)記錄中的第一條記錄,操作方法如下。 假設(shè)有重復(fù)的字段為Name,Address,要求得到這兩個字段唯一的結(jié)果集
Mysql
以下為引用的內(nèi)容:
alter table tableName add autoID int auto_increment not null;
create table tmp select min(autoID) as autoID from tableName group by Name,Address;
create table tmp2 select tableName.* from tableName,tmp where tableName.autoID = tmp.autoID;
drop table tableName;
rename table tmp2 to tableName;
SQL Server
select identity(int,1,1) as autoID, * into #Tmp from tableName;
select min(autoID) as autoID into #Tmp2 from #Tmp group by Name,Address;
drop table tableName;
select * into tableName from #Tmp where autoID in(select autoID from #Tmp2);
drop table #Tmp;
drop table #Tmp2;
Oracle
DELETE FROM tableName t1 WHERE t1.ROWID > (SELECT MIN(t2.ROWID) FROM tableName t2 WHERE t2.Name = t1.Name and t2.Address = t1.Address);
說明:
1. MySQL和SQL Server中最后一個select得到了Name,Address不重復(fù)的結(jié)果集(多了一個autoID字段,在大家實際寫時可以寫在select子句中省去此列)
2. 因為MySQL和SQL Server沒有提供rowid機制,所以需要通過一個autoID列來實現(xiàn)行的唯一性,而利用Oracle的rowid處理就方便多了。而且使用ROWID是最高效的刪除重復(fù)記錄方法。
您可能感興趣的文章:
- 查找oracle數(shù)據(jù)庫表中是否存在系統(tǒng)關(guān)鍵字的方法
- Access數(shù)據(jù)庫中“所有記錄中均未找到搜索關(guān)鍵字”的解決方法
- Linux 自動備份oracle數(shù)據(jù)庫詳解
- 利用SQL Server數(shù)據(jù)庫郵件服務(wù)實現(xiàn)監(jiān)控和預(yù)警
- myeclipse中連接mysql數(shù)據(jù)庫示例代碼
- Myeclipse連接mysql數(shù)據(jù)庫心得體會
- MyEclipse連接MySQL數(shù)據(jù)庫圖文教程
- python爬取NUS-WIDE數(shù)據(jù)庫圖片
- 記一次mariadb數(shù)據(jù)庫無法連接
- 數(shù)據(jù)庫 關(guān)鍵字一覽表
相關(guān)文章
大數(shù)據(jù)時代的數(shù)據(jù)庫選擇:SQL還是NoSQL?
執(zhí)行大數(shù)據(jù)項目的企業(yè)面對的關(guān)鍵決策之一是使用哪個數(shù)據(jù)庫,SQL還是NoSQL?SQL有著驕人的業(yè)績,龐大的安裝基礎(chǔ);而NoSQL正在獲得可觀的收益,且有很多支持者。我們來看看兩位專家對這個問題的看法2014-03-03
GaussDB數(shù)據(jù)庫使用COPY命令導(dǎo)入導(dǎo)出數(shù)據(jù)的場景分析
使用COPY命令可以方便地導(dǎo)入數(shù)據(jù)到GaussDB,GaussDB還提供了其他數(shù)據(jù)導(dǎo)入工具和功能,如使用GDS導(dǎo)入數(shù)據(jù)、使用INSERT多行插入、使用gsql元命令導(dǎo)入數(shù)據(jù)、ETL工具集成等,以滿足不同場景下的數(shù)據(jù)導(dǎo)入需求,對GaussDB COPY命令相關(guān)知識感興趣的朋友一起看看吧2024-01-01
最近關(guān)于Navicat到期的完美解決辦法(親測有效)
這篇文章主要介紹了最近關(guān)于Navicat到期的完美解決辦法(親測有效),本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-02-02

