gpt4 book ai didi

mysql - 从 MySQL 日期字段中阻止 '0000-00-00'

转载 作者:IT王子 更新时间:2023-10-28 23:49:05 25 4
gpt4 key购买 nike

我有一个数据库,其中旧代码喜欢在 Date 和 DateTime 列中插入“0000-00-00”而不是实际日期。所以我有以下两个问题:

  1. 我可以在数据库级别做些什么来阻止这个吗?我知道我可以将一列设置为非空,但这似乎并没有阻止这些零值。
  2. 检测日期字段中现有零值的最佳方法是什么?我有大约一百个表,每个表有 2-3 个日期列,我不想单独查询它们。

跟进:

默认值已设置为 null。很久以前,默认值为“0000-00-00”。一些代码仍然明确放置“0000-00-00”。我宁愿强制该代码抛出错误,以便隔离并删除它。

最佳答案

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

是的,启用 NO_ZERO_DATE 模式:

SET sql_mode = 'NO_ZERO_DATE';

The behaviour is documented .此外,您可能还希望将模式设置为包括 NO_ZERO_IN_DATE...

还要确保 sql_mode 包含 STRICT_ALL_TABLES 或 STRICT_TRANS_TABLES;没有这些 NO_ZERO_IN_DATE 只会发出警告,但插入仍然成功。

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.

单独的列意味着必须单独检查它们——对此您无能为力。

关于mysql - 从 MySQL 日期字段中阻止 '0000-00-00',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3891896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com