The right SQL answer would be to add a check
constraint on the column:
create table foo
(
...
bar datetime not null check ( bar != '0000-00-00 00:00:00' ) ,
...
)
However, mySql, while perfectly happy to parse and nominally 'accept' check
constraint definitions, it silently ignores them, per a sotto vocce comment in the create table
documentation:
The CHECK
clause is parsed but ignored by all storage engines.
To that end, you probably want to add a before insert
trigger to enforce the constraint. You should probably also enforce that the date is actually a valid date, since apparently mySql doesn't even enforce that minimal constraint in date
and datetime
columns:
MySQL enables you to store certain incorrect date values into DATE
and DATETIME
columns (such as '2000-02-31' or '2000-02-00'). The idea is that it is not the job
of the SQL server to validate dates. If MySQL can store a date value and retrieve
exactly the same value, MySQL stores it as given.
If the date is totally wrong (outside the server's ability to store it), the special
“zero” date value '0000-00-00' is stored in the column instead.
See this question for details on how to create such a trigger: Mysql CHECK Constraint