我想在表中创建一个列,该列具有与 oracle sysdate 变量相同的功能作为默认值。因此,当插入一行时(没有日期,则使用当前的系统日期)。创建此表的查询是什么? 另外,我是否应该允许该列为空,这将如何工作?
此外,我希望能够在此表中插入一行,以便实现该功能。请提供使用此功能的示例插入查询。
此外,我希望能够对 DATETIME 列类型执行相同的操作。
提前致谢。
最佳答案
这在 documentation 中有很好的解释。 :
TIMESTAMP and DATETIME columns can be automatically initializated and updated to the current date and time (that is, the current timestamp).
语法如下:
create table t (
. . .,
createdAt datetime not null default current_timestamp
);
如果您为该列指定默认值,那么您不妨将其声明为 NOT NULL
。
您也可以对更新执行相同的操作:
updatedAt datetime not null default current_timestamp on update current_timestamp
关于mysql - mariadb/mysql - 如何使用 sysdate/curdate 创建日期列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50912140/