Delete Duplicate Records in SQL Table Without Primary Key
In the absence of a primary key, identifying duplicate records in a database can be challenging. However, utilizing a combination of window functions and logical operators allows for efficient deletion of duplicates.
In the provided example, the goal is to remove records from the Employee table that have the same EmpId and EmpSSN values. To achieve this:
ROW_NUMBER() OVER (PARTITION BY EmpId, EmpName, EmpSSN ORDER BY EmpId) cnt
SELECT SUB FROM (SELECT ROW_NUMBER() OVER (PARTITION BY EmpId, EmpName, EmpSSN ORDER BY EmpId) cnt FROM Employee) SUB WHERE SUB.cnt > 1
DELETE SUB FROM (SELECT ROW_NUMBER() OVER (PARTITION BY EmpId, EmpName, EmpSSN ORDER BY EmpId) cnt FROM Employee) SUB WHERE SUB.cnt > 1
By following these steps, the duplicate records will be effectively deleted from the Employee table, ensuring the integrity of the data without requiring a primary key.
The above is the detailed content of How to Delete Duplicate Records in a SQL Table Without a Primary Key?. For more information, please follow other related articles on the PHP Chinese website!