before insert trigger for insert duplicate rows into another table

前端 未结 1 1045
心在旅途
心在旅途 2021-01-21 21:28

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 <

相关标签:
1条回答
  • 2021-01-21 22:16

    As far as you trigger concerned there are several problems:

    1. you don't have ; after insert statement
    2. IF statement should end with END IF and a semicolon, not just END
    3. you have to change a delimiter with DELIMITER command
    4. use EXISTS() 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:

    1. create temporary staging table with no constraints and no indices
    2. use LOAD DATA INFILE to populate staging table
    3. having tblspmaster and the staging table and using INSERT ... SELECT syntax insert all duplicates in tblspduplicate in one go
    4. insert only non-existent rows from staging table into tblspmaster again in one go
    5. TRUNCATE or DROP staging table
    0 讨论(0)
提交回复
热议问题