How to delete duplicate records in a database (1)

Source: Internet
Author: User
Today, I went to a company in Xi'an for an interview. One of the interview questions was as follows: "How to delete identical records in the data table? If two identical records have different primary keys, then how should we delete it? "At that time, because it was a very simple question, it was directly completed using SQL statements. The interviewer said that there was a problem. I was wrong when I came back to study it. Now

Today, I went to a company in Xi'an for an interview. One of the interview questions was as follows: "How to delete identical records in the data table? If two identical records have different primary keys, then how should we delete it? "At that time, because it was a very simple question, it was directly completed using SQL statements. The interviewer said that there was a problem. I was wrong when I came back to study it. Now

Today, I went to a company in Xi'an for an interview. One of the interview questions was as follows: "How to delete identical records in the data table? If two identical records have different primary keys, then how should we delete it? "At that time, because it was a very simple question, it was directly completed using SQL statements. The interviewer said that there was a problem. I was wrong when I came back to study it. Now I will share the steps of the study.

Study conditions:

Window7 + no-install mysql

1. How to delete records with the same attributes except the primary key

Main Idea: first, identify non-duplicate data records from the data table, save these records to the temporary table, and then delete the original table records to migrate the records in the temporary table.

Procedure:

1. Check that the following table contains two identical records in student, with different records searched by distinct (such as the picture on the right ).

2. Create a temporary table and migrate the data in the figure above to student_temp.

SQL statement: create temporary table if not exists student_temp as (select distinct (name), sex from student );

3. Delete the content of the original table or recreate a new table. In this example, modify the content of student_temp on the basis of the original table and migrate the content to student. The final result is as follows:

SQL used: insert into student (id, name, sex) select null, name, sex from student_temp;

Finally, we can see that the student table has only two different data records.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.