Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent Duplicate records from my table Insert ignore does not work here

Tags:

sql

mysql

insert

mysql> select * from emp;

    +-----+---------+------+------+------+
    | eno | ename   | dno  | mgr  | sal  |
    +-----+---------+------+------+------+
    |   1 | rama    |    1 | NULL | 2000 |
    |   2 | kri     |    1 |    1 | 3000 |
    |   4 | kri     |    1 |    2 | 3000 |
    |   5 | bu      |    1 |    2 | 2000 |
    |   6 | bu      |    1 |    1 | 2500 |
    |   7 | raa     |    2 | NULL | 2500 |
    |   8 | rrr     |    2 |    7 | 2500 |
    |   9 | sita    |    2 |    7 | 1500 |
    |  10 | dlksdgj |    2 |    2 | 2000 |
    |  11 | dlksdgj |    2 |    2 | 2000 |
    |  12 | dlksdgj |    2 |    2 | 2000 |
    |  13 | dlksdgj |    2 |    2 | 2000 |
    |  14 | dlksdgj |    2 |    2 | 2000 |
    +-----+---------+------+------+------+

Here is my table. I want to eliminate or prevent insertion of the duplicate records, as the eno field is auto increment total row never be duplicate, but the records are duplicates. How can I prevent inserting those duplicate records?

I tried using INSERT IGNORE AND ON DUPLICATE KEY UPDATE (I think I have not used them properly).

The way I used them is,

mysql> insert into emp(ename,dno,mgr,sal) values('dlksdgj',2,2,2000);
Query OK, 1 row affected (0.03 sec)

mysql> insert ignore into emp(ename,dno,mgr,sal) values('dlksdgj',2,2,2000);
Query OK, 1 row affected (0.03 sec)

mysql> insert into emp(ename,dno,mgr,sal) values('dlksdgj',2,2,2000) ON DUPLICATE KEY UPDATE eno=eno;
Query OK, 1 row affected (0.03 sec)
mysql> insert into emp(ename,dno,mgr,sal) values('dlksdgj',2,2,2000) ON DUPLICATE KEY UPDATE eno=eno;
Query OK, 1 row affected (0.04 sec

mysql> desc emp;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| eno   | int(11)     | NO   | PRI | NULL    | auto_increment |
| ename | varchar(50) | YES  |     | NULL    |                |
| dno   | int(11)     | YES  |     | NULL    |                |
| mgr   | int(11)     | YES  | MUL | NULL    |                |
| sal   | int(11)     | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
like image 425
Chella Avatar asked Dec 25 '12 05:12

Chella


People also ask

How do you avoid inserting duplicate records in a table?

You can prevent duplicate values in a field in an Access table by creating a unique index. A unique index is an index that requires that each value of the indexed field is unique.

How do I ignore duplicate records in SQL?

Following is the syntax: select count(distinct yourColumnName) from yourTableName; In MySQL, COUNT() will display the number of rows. DISTINCT is used to ignore duplicate rows and get the count of only unique rows.

How do you know if the record exists before insert to avoid duplicates?

You want to look at the removeAll() method for Sets. First create a set of all the possible Ids you want to insert, then query the Contact object with records that already exist. Then you can use the removeAll method to remove all instance of the Id that already exist in Salesforce.

Which command is used to ignore duplicate rows while importing data?

Settings -> Export -> SQL -> Use ignore inserts.


2 Answers

alter the table by adding UNIQUE constraint

ALTER TABLE employee ADD CONSTRAINT emp_unique UNIQUE (ename,dno,mgr,sal)

but you can do this if the table employee is empty.

or if records existed, try adding IGNORE

ALTER IGNORE TABLE employee ADD CONSTRAINT emp_unique UNIQUE (ename,dno,mgr,sal)

UPDATE 1

Something went wrong, I guess. You only need to add unique constraint on column ename since eno will always be unique due to AUTO_INCREMENT.

In order to add unique constraint, you need to do some cleanups on your table.

The queries below delete some duplicate records, and alters table by adding unique constraint on column ename.

DELETE a
FROM Employee a
     LEFT JOIN
     (
        SELECT ename, MIN(eno) minEno
        FROM Employee
        GROUP BY ename
     ) b ON a.eno = b.minEno
WHERE b.minEno IS NULL;

ALTER TABLE employee ADD CONSTRAINT emp_unique UNIQUE (ename);

Here's a full demonstration

  • SQLFiddle Demo
like image 109
John Woo Avatar answered Oct 04 '22 16:10

John Woo


Create a UNIQUE CONSTRAINT on which you think the duplicacy exist .

like

ALTER TABLE MYTABLE ADD CONSTRAINT constraint1 UNIQUE(column1, column2, column3)
like image 33
Sashi Kant Avatar answered Oct 04 '22 15:10

Sashi Kant