數(shù)據(jù)庫刪除完全重復(fù)和部分關(guān)鍵字段重復(fù)的記錄
更新時(shí)間: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è)計(jì)不周而產(chǎn)生的,增加唯一索引列就可以解決此問題。
2、此類重復(fù)問題通常要求保留重復(fù)記錄中的第一條記錄,操作方法如下。 假設(shè)有重復(fù)的字段為Name,Address,要求得到這兩個(gè)字段唯一的結(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中最后一個(gè)select得到了Name,Address不重復(fù)的結(jié)果集(多了一個(gè)autoID字段,在大家實(shí)際寫時(shí)可以寫在select子句中省去此列)
2. 因?yàn)镸ySQL和SQL Server沒有提供rowid機(jī)制,所以需要通過一個(gè)autoID列來實(shí)現(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è)計(jì)不周而產(chǎn)生的,增加唯一索引列就可以解決此問題。
2、此類重復(fù)問題通常要求保留重復(fù)記錄中的第一條記錄,操作方法如下。 假設(shè)有重復(fù)的字段為Name,Address,要求得到這兩個(gè)字段唯一的結(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中最后一個(gè)select得到了Name,Address不重復(fù)的結(jié)果集(多了一個(gè)autoID字段,在大家實(shí)際寫時(shí)可以寫在select子句中省去此列)
2. 因?yàn)镸ySQL和SQL Server沒有提供rowid機(jī)制,所以需要通過一個(gè)autoID列來實(shí)現(xiàn)行的唯一性,而利用Oracle的rowid處理就方便多了。而且使用ROWID是最高效的刪除重復(fù)記錄方法。
您可能感興趣的文章:
- 查找oracle數(shù)據(jù)庫表中是否存在系統(tǒng)關(guān)鍵字的方法
- Access數(shù)據(jù)庫中“所有記錄中均未找到搜索關(guān)鍵字”的解決方法
- Linux 自動(dòng)備份oracle數(shù)據(jù)庫詳解
- 利用SQL Server數(shù)據(jù)庫郵件服務(wù)實(shí)現(xiàn)監(jiān)控和預(yù)警
- myeclipse中連接mysql數(shù)據(jù)庫示例代碼
- Myeclipse連接mysql數(shù)據(jù)庫心得體會(huì)
- MyEclipse連接MySQL數(shù)據(jù)庫圖文教程
- python爬取NUS-WIDE數(shù)據(jù)庫圖片
- 記一次mariadb數(shù)據(jù)庫無法連接
- 數(shù)據(jù)庫 關(guān)鍵字一覽表
相關(guān)文章
顏值與實(shí)用性并存的數(shù)據(jù)庫建模工具Chiner教程
這篇文章主要為大家介紹了一款顏值與實(shí)用性并存的數(shù)據(jù)庫建模工具Chiner,推薦大家使用,有需要的朋友可以共同學(xué)習(xí)參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03建立在Tablestore的Wifi設(shè)備監(jiān)管系統(tǒng)架構(gòu)實(shí)現(xiàn)
一般大公司會(huì)有許多園區(qū),園區(qū)內(nèi)會(huì)有不同部門的同事在一起辦公。每個(gè)園區(qū)內(nèi)都要配備大量的Wifi設(shè)備從而為園區(qū)同事提供方便的上網(wǎng)服務(wù)。因此,集團(tuán)需要一套完善的監(jiān)管系統(tǒng)維護(hù)所有的Wifi設(shè)備。需要的朋友來一起學(xué)習(xí)下吧2019-05-05數(shù)據(jù)庫設(shè)計(jì)的完整性約束表現(xiàn)在哪些方面
數(shù)據(jù)完整性是指數(shù)據(jù)的正確性、完備性和一致性,是衡量數(shù)據(jù)庫質(zhì)量好壞的規(guī)范。數(shù)據(jù)庫完整性由各式各樣的完整性約束來確保,因而可以說數(shù)據(jù)庫完整性規(guī)劃即是數(shù)據(jù)庫完整性約束的規(guī)劃。那么,數(shù)據(jù)庫設(shè)計(jì)的完整性約束表現(xiàn)哪些方面?2015-10-10sql注入報(bào)錯(cuò)之注入原理實(shí)例解析
所謂報(bào)錯(cuò)注入,就是通過運(yùn)行SQL查詢語句回顯查詢結(jié)果,下面這篇文章主要給大家介紹了關(guān)于sql注入報(bào)錯(cuò)之注入原理的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06