sql - 如何创建默认值为 'now' 的时间戳列?

标签 sql sqlite

如何创建一个时间戳列默认为 DATETIME('now') 的表?

像这样:

CREATE TABLE test (
    id INTEGER PRIMARY KEY AUTOINCREMENT, 
    t TIMESTAMP DEFAULT DATETIME('now')
);

这会出错。

最佳答案

截至version 3.1.0您可以将 CURRENT_TIMESTAMPDEFAULT 一起使用子句:

If the default value of a column is CURRENT_TIME, CURRENT_DATE or CURRENT_TIMESTAMP, then the value used in the new row is a text representation of the current UTC date and/or time. For CURRENT_TIME, the format of the value is "HH:MM:SS". For CURRENT_DATE, "YYYY-MM-DD". The format for CURRENT_TIMESTAMP is "YYYY-MM-DD HH:MM:SS".

CREATE TABLE test (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    t TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

关于sql - 如何创建默认值为 'now' 的时间戳列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/200309/

相关文章:

SQL Server CTE 选择单树分支结构直至根

iphone - 如何为包含 18000 个条目的 Sqlite 数据库表建立索引

python - Django-Celery 数据库访问

ruby-on-rails - 将 .find 与正则表达式结合使用

sql - 错误: an expression of non-boolean type specified in a context where condition is expected

SQL查询一对多关系

sql - 需要根据单词列表在 Teradata SQL 中搜索文本字段并返回该单词

sql - 如何使用搜索和替换查询更新 SQLite 数据库?

php - 网站到IOS App

c# - 在 View 后面获取sql语句