I have a table called tblspmaster
in which sp column i have unique index so there will be no duplicates will be inserted, but i want to insert duplicate rows into <
As far as you trigger concerned there are several problems:
;
after insert statementIF
statement should end with END IF
and a semicolon, not just END
DELIMITER
commandEXISTS()
rather then COUNT()
That being said your trigger might look like
DELIMITER $$
CREATE TRIGGER tblspmaster_noduplicate
BEFORE INSERT ON tblspmaster
FOR EACH ROW
BEGIN
IF (EXISTS(SELECT * FROM tblspmaster WHERE sp = NEW.sp)) THEN
INSERT INTO tblspduplicate (sp,FileImported,AMZFileName)
VALUES (NEW.sp, NEW.FileImported, NEW.AMZFileName);
END IF;
END$$
DELIMITER ;
Here is SQLFiddle demo
Use IGNORE
clause in your LOAD DATA INFILE
statement. MySql will treat errors (violating unique constraint) as warnings effectively discarding duplicates.
LOAD DATA INFILE
If you specify IGNORE, input rows that duplicate an existing row on a unique key value are skipped.
LOAD DATA LOCAL INFILE 'E://31october//SP//sp_files_sample1//400k sp00 6-19 E.csv'
IGNORE
INTO TABLE tblspmaster
FIELDS TERMINATED BY ',' ENCLOSED BY '"' ESCAPED BY '\\'
LINES TERMINATED BY '\n'
-- IGNORE 1 LINES
Note: FYI failed inserts for duplicate rows will leave gaps in values of auto_increment SCN
column.
You may consider another approach which might be more preferable performance wise:
LOAD DATA INFILE
to populate staging tabletblspmaster
and the staging table and using INSERT ... SELECT
syntax insert all duplicates in tblspduplicate
in one gotblspmaster
again in one goTRUNCATE
or DROP
staging table