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

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

提交回复
热议问题