1. How to find duplicates records in table
select first_name, last_name , email, phone_number ,count(*) from employees
group by first_name, last_name , email, phone_number
having count(*)>1;
2. How to delete duplicates by keeping one :
DELETE FROM EMPLOYEES
WHERE ROWID NOT IN (
SELECT MIN(ROWID)
FROM EMPLOYEES
GROUP BY first_name, last_name , email, phone_number
);
3. How to restrict data getting duplicates in EMPLOYEES table ?
Answer :
Option 1
- Create Composite unique key or USER KEY -(Best Practice)
Create user key with above 4 columns, so that no one can insert record with this combination again in the database.
ALTER TABLE employees
ADD CONSTRAINT emp_unique_key UNIQUE (first_name, last_name, email, phone_number);
5. Ways to identify duplicates -
Example Student table with below duplicates -
5. User Intersect
No comments:
Post a Comment