Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a table like..... i want to delete data from table.

id	name	salary	department	departmentName
--------------------------------------------------------------------
1017	mukesh	12400	10	It
1017	mukesh	12400	10	It
1001	mukesh kumar bhagat	10000	10	IT
1002	Devendra	15000	20	MANAGEMENT
1003	manikant	15000	20	MANAGEMENT
1005	sony jha	15000	30	sales
1007	murli manohar	150000	10	IT
1008	rakesh	12400	20	MANAGEMENT
1009	deepak jha	121000	30	sales
1010	soni kumari	10000	30	sales
1016	mukesh	120000	10	IT


1.i want to keep only unique value in table..
2. i want to keep one duplicate row in the table.

please help any one..
Posted
Updated 1-Apr-13 0:59am
v2

If your table contains duplicate values that are actually identical, then it is pretty much impossible to delete just one, and you have made a fundamental design flaw in your DB - rows should have at least one unique value to allow you to identify them - normally the ID field.

Consider a redesign: make the ID an identity field and let the db assign the values instead of doing it yourself.
Then, copy all the records from your old table (or old DB) into your new ones, handling the id changes as they happen.

Otherwise, I suspect you will be doing this again, and again, and again...
 
Share this answer
 
1) You can enforce unique rows, by creating a UNIQUE CONSTRAINT[^] to the whole row - but be aware of the followings:
a) it might be resource expensive
b) it is cheaper make a stored procedure (or client side code) for the insert, that checks for duplicate before inserting
c) it won't prevent user input mismatches. To avoid that, use normalization and a dropdown on client side

2) You can use SELECT DISTINCT[^] clause to select unique rows. You can use a temporary table to select the distinct rows into it, and than delete the original table and insert the temporary table data back. But if you have some referential constraints pointing to the table, you will have to disable them temporary (especially the ON DELETE CASCADE ones).

My advice:
A) normalize your tables (on several points)
B) use some logic to avoid duplication before inserting
 
Share this answer
 
v2
Comments
vinodkumarnie 1-Apr-13 6:17am    
nice
Dear @OriginalGriff , @Zoltán

I have altered My table. add a identity column. like..

name salary department departmentName eid
mukesh 12400 10 It 1
mukesh 12400 10 It 2
mukesh 10000 10 IT 3
Deven 15000 20 MGT 4
manik 15000 20 MGT 5

so, pls help to solve this.
 
Share this answer
 
Comments
Zoltán Zörgő 1-Apr-13 7:43am    
This is not an answer, it is a comment to ours. I will have to delete it. Please put it where it belongs. Please, use the page widgets as they are meant to be used!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900