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

前端 未结 7 1265
猫巷女王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.

    0 讨论(0)
  • 2020-12-10 05:44

    You could make the columns nullable and have NULL as the default value, but it sounds like you already have that and it's not working. ALTHOUGH... it could be the tool you're using to display the data doesn't like displaying NULL dates... what tool are you using? Or is the '0000-00-00' showing up in data retreived by code?

    You could set a default value that is non-null and also easily recognizable as a default such as 1900-01-01 (assuming you don't normally deal with dates that are close to this date).

    0 讨论(0)
  • 2020-12-10 05:44

    Assuming you can't easily fix the data and "SET sql_mode = 'NO_ZERO_DATE';", you could create a view on the table...

    CREATE VIEW filter AS
    SELECT other_column, 
    CASE 
      WHEN realtable.dodgy_date = 0 THEN NULL 
      ELSE realtable.dodgy_date
    END AS dodgy_date
    FROM realtable;
    
    0 讨论(0)
  • 2020-12-10 05:47

    set the timestamp by default is maybe an option for you, use table change statement for that:

    ALTER TABLE mytable CHANGE date_update timestamp NOT NULL DEFAULT CURRENT_DATE()
    

    MySQL CURRENT_DATE() documentation at w3c resource

    To remove Zero Dates and replace them by e.g. the current date do this:

    UPDATE mytable SET date_update = CURRENT_DATE() where date_update = "0000-00-00"
    
    0 讨论(0)
  • 2020-12-10 05:58

    If it doesn't matter what date goes in there (ie as long as it's non-zero) you can change the column definition to use NOW() as the default. Probably not an ideal solution, but it does satisfy the criteria : 1) Not-null 2) Non-zero I'm actually really not proud of that suggestion

    0 讨论(0)
  • 2020-12-10 05:59

    Is there anything that I could do on the db level to block this?

    Yes, enable the NO_ZERO_DATE mode:

    SET sql_mode = 'NO_ZERO_DATE';
    

    The behaviour is documented. Additionally, you might want to also set the mode to include NO_ZERO_IN_DATE...

    Also make sure the sql_mode includes either STRICT_ALL_TABLES or STRICT_TRANS_TABLES; without these NO_ZERO_IN_DATE only give a warning, but insert still succeeds.

    What is the best way to detect the existing zero values in date fields? I have about a hundred tables with 2-3 date columns each and I don't want to query them individually.

    Separate columns means they have to be checked individually--nothing you can do about that.

    0 讨论(0)
提交回复
热议问题