1. 程式人生 > 其它 >SQL Server-刪除表中重複資料

SQL Server-刪除表中重複資料

在執行語句時難免會輸入相同的資料,這些重複記錄不但沒有用,還可能導致資料庫佔用大量資料或導致資料庫查詢索引無效。

如果在建表的時候沒有設定主鍵或unique索引,那麼插入完全重複的資料是不會報錯的,結果就是無法修改記錄行或者刪除記錄行。通過SQL語句可以實現刪除表中重複資料。

1.所有欄位均重複的記錄。

create table Student (Name char(1),
                      Memo char(2))
go
insert into Student
values(N'A', N'A1'),
      (N'A', N'A1'),
      (N'A', N'A2'),
      (N'B', N'B1'),
      (N'B', N'B1')
-- N'xxx' 標識將單引號中的xxx強制轉換為Unicode編碼的字元,在中文情況下不會出現亂碼
go

  

方法一:

select distinct * into #tmpt from Student
-- 通過distinct取唯一資料存到一個本地臨時表中
drop table Student
select * into Student from #tmpt
-- 把臨時表複製到新的表
drop table #tmpt

  方法二:

alter table Student
  add Id int identity(1,1)  -- 給表新增標識列
delete from Student
  where Id not in (select min(Id) from Student group by Name)
alter table Student
  drop column Id
select * from Student

  

2.部分關鍵欄位重複的記錄。

比如學號欄位重複,而其他欄位不一定重複。這種屬於隱含錯誤,需要結合實際情況具體分析。

create table t (ID int,
                Name char(1),
                Memo char(2))
go
insert into t
values(1, N'A', N'A1'),
      (2, N'A', N'A2'),
      (3, N'A', N'A3'),
      (4, N'B', N'B1'),
      (5, N'B', N'B2')
go

方法一:

delete t from t a
where ID not in (select min(ID) from t where Name = a.Name)
select * from t
go

  

delete from t
where ID not in (select min(ID) from t group by Name)
select * from t
go

  

delete t from t a
where ID <> (select top 1 ID from t where Name = a.Name order by ID)
select * from t
go

  

delete t from t a
where ID > any (select ID from t where Name = a.Name)
select * from t
go

  

方法二:

delete t from t a
where exists (select 1 from t where Name = a.Name and ID < a.ID)
select * from t
go

  

delete t from t a
where (select count(*) from t where Name = a.Name and ID < a.ID) > 0
select * from t
go

  

方法三:

delete t from t a left join
(select min(ID) ID, Name from t group by Name) b
on a.Name = b.Name and a.ID = b.ID
where b.ID is null
select * from t
go

  

3.設立主鍵的表中誤輸入重複記錄。

create table Employee (ID int primary key,
                       Name char(20),
                       Age int,
                       Sex bit default 1)
go
insert into Employee
values(1, 'James', 25, default),
      (2, 'James', 25, default),
      (3, 'James', 25, default),
      (4, 'Lisa', 24, 0),
      (5, 'Lisa', 24, 0),
      (6, 'Lisa', 24, 0),
      (7, 'Mirsa', 23, 0),
      (8, 'Mirsa', 23, 0),
      (9, 'Mirsa', 23, 0),
      (10, 'John', 26, default),
      (11, 'Abraham', 28, default),
      (12, 'Lincoln', 30, default)
go

  

delete T from (select row_number() over (partition by Name order by (select 0)) as RowNumber, * from Employee) T
where T.RowNumber > 1
select * from Employee
go