Search code examples
mysqlsqlif-statementload-data-infile

conditionally ignore rows load data mysql


I am loading a large data set (60 gigs) into a database. There are some records that can be skipped because they contain values that are missing. How do I tell MySQL to skip rows that contain certain (missing in this case) values? For example, my file looks like this

Value1, Value2
1,2
3,4
,5
9,10

The third row can be skipped and not loaded. I know that I can load everything and then just delete it but processing a 60 gig file takes a long time so I want to save on the computing power.

Thanks


Solution

  • Create a trigger on the before insert on the table which checks if the particular field is null

    CREATE TRIGGER before_insert_test
    BEFORE INSERT ON test FOR EACH ROW
    BEGIN
        -- condition to check
        IF NEW.Value1 is null THEN
           set msg = concat('MyTriggerError: Trying to insert a null value
                      in trigger_test: ', cast(new.Value1 as char));
       signal sqlstate '45000' set message_text = msg;
        END IF;
    END$$
    

    then run your Load data command