【问题标题】:Mysql - add current timestamp field without affecting the existing recordsMysql - 添加当前时间戳字段而不影响现有记录
【发布时间】:2020-04-08 18:31:15
【问题描述】:

假设有一个名为 users 的 mysql 表,其中有 100 个条目。

如果我添加了一个 created_on 字段,例如

alter table users add created_on TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP;

created_on 的那 100 条记录将被当前时间戳填充。

我希望它只用于新的插入。并希望为现有记录保留 null。

这可能吗?

【问题讨论】:

  • 创建列后运行这个? UPDATE users SET created_at = NULL

标签: mysql timestamp


【解决方案1】:

添加它然后设置默认值:

alter table users add created_on timestamp null;
alter table users change created_on created_on timestamp null default current_timestamp;

这会将所有现有记录设置为 null,然后为新记录提供默认值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-05
    • 2014-11-22
    • 1970-01-01
    • 2013-05-10
    相关资源
    最近更新 更多