SQL Server tables should never contain duplicate rows, if there is a key
constraints. Table with no key constraints allow the duplicates. It happens in
many ways such as import data from other resources.
Nothing to worry, write a query and make your database without duplicates.
First we need a table and some data to explain the process. Use the following
script to create a table and insert a record.
CREATE TABLE Dup_Students (ID INT, FirstName varchar(25), Department Char(2))
CREATE
TABLE Dup_Students (ID
INT, FirstName
varchar(25),
Department Char(2))
INSERT
INTO Dup_Students
VALUES(1,
'Jack',
'IT')
INSERT
INTO Dup_Students
VALUES(2,
'Alice',
'ME')
INSERT
INTO Dup_Students
VALUES(3,
'James',
'EE')
INSERT
INTO Dup_Students
VALUES(4,
'Nickle',
'CE')
INSERT
INTO Dup_Students
VALUES(5,
'George',
'IT')
--
INSERT
INTO Dup_Students
VALUES(1,
'Jack',
'IT')
INSERT
INTO Dup_Students
VALUES(2,
'Alice',
'ME')
INSERT
INTO Dup_Students
VALUES(3,
'James',
'EE')
INSERT
INTO Dup_Students
VALUES(4,
'Nickle',
'CE')
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.
Shilpa kamthanPosted Aug 18, 2010, 7:01 AM
I dont know y to use "partition" .m not much aware about the sql server fundamentals.
giamminPosted Jul 1, 2010, 4:25 AM
you are taking the long, dangerous way: that is easiest: DELETE FROM Dup_Students WHERE ID NOT IN (SELECT min(ID) FROM Dup_Students GROUP BY FirstName, Department )