Saturday, September 19, 2015

Remove duplicate record in table

In this post, we are going to see how to remove duplicate records from table.

Create a test table
mysql> CREATE TABLE test_table (`id` int(11) DEFAULT NULL,`name` varchar(10) DEFAULT NULL);
Insert duplicate values in test table.
mysql> insert into test_table values (1,'aaa'),(1,'aaa'),(1,'aaa'),(1,'bbb'),(1,'bbb'),(1,'bbb');
mysql> select * from test_table;
+------+------+
| id | name |
+------+------+
| 1 | aaa |
| 1 | aaa |
| 1 | aaa |
| 1 | bbb |
| 1 | bbb |
| 1 | bbb |
+------+------+
Remove duplicate rows from test table
mysql> alter IGNORE table test_table add UNIQUE key (id,name);
Records: 6 Duplicates: 4 Warnings: 0
mysql> select * from test_table ;
+------+------+
| id | name |
+------+------+
| 1 | aaa |
| 1 | bbb |
+------+------+

Related Posts:

  • Write a Program to find the longest palindrome in a given StringWrite a Program to find the longest palindrome in a given StringProgram: Output:String: HYTBCABADEFGHABCDEDCBAGHTFYW12345678987654321ZWETYGDELongest Palindrome: 12345678987654321… Read More
  • Singleton Design Pattern Example Program in JavaDefinition: The singleton pattern is one of the simplest design patterns: it involves only one class which is responsible to instantiate itself, to make sure it creates not more than one instance; in the same time it provides… Read More
  • Spring MVC Hello World ExampleIn this post, we are going to see how to implement Spring MVC HelloWorld example.Step 1:The initial step is to create Dynamic Web Project in eclipse. To create a new Dynamic project in Eclipse IDE select File -> Dynamic We… Read More
  • AJAX Drop down Example with DatabaseThe following are the steps for creating a dropdown list by using ajax with database.1. Create Dynamic Web Project : Open File -> New -> Other... -> Web -> Dynamic Web Project to create a dynamic Web pro… Read More
  • Important C written Test Programs... P { margin-bottom: 0.08in; direction: ltr; color: rgb(0, 0, 0); line-height: 115%; widows: 2; orphans: 2; }P.western { font-family: "Calibri",sans-serif; font-size: 11pt; }P.cjk { font-family: "Calibri",sans-serif; f… Read More

0 comments: