Blocking '0000-00-00' from MySQL Date Fields

前端 未结 7 1267
猫巷女王i
猫巷女王i 2020-12-10 05:35

I have a database where old code likes to insert \'0000-00-00\' in Date and DateTime columns instead of a real date. So I have the following two questions:

  1. Is
7条回答
  •  离开以前
    2020-12-10 05:41

    This is the trigger I use:

    delimiter //
    CREATE TRIGGER bad_date BEFORE INSERT ON some_table
        FOR EACH ROW
            BEGIN
                IF NEW.the_date='0000-00-00' THEN 
                    SET NEW.the_date= CURDATE();
                END IF;
            END;//
    

    If updates are a concern, add a separate trigger (BEFORE UPDATE) to do the same thing.

提交回复
热议问题