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

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

提交回复
热议问题