且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在sqlite3中创建具有默认值的日期时间列?

更新时间:2023-01-08 09:08:12

试试这个:

create table tbl1(id int primary key, dt datetime default current_timestamp);

背景:

DEFAULT 约束指定了一个执行操作时使用的默认值插入.该值可能是 NULL,一个字符串常量、数字或包含在其中的常量表达式括号.默认值可能也是特别的之一大小写无关的关键字CURRENT_TIME、CURRENT_DATE 或CURRENT_TIMESTAMP.如果值为NULL,字符串常量或数字,它每当插入到列中不包含的 INSERT 语句为列指定一个值是执行.如果值为CURRENT_TIME、CURRENT_DATE 或CURRENT_TIMESTAMP,则当前UTC 日期和/或时间被插入到列.对于 CURRENT_TIME,格式为 HH:MM:SS.对于 CURRENT_DATE,YYYY-MM-DD.格式为CURRENT_TIMESTAMP 是YYYY-MM-DD时:分:秒".

The DEFAULT constraint specifies a default value to use when doing an INSERT. The value may be NULL, a string constant, a number, or a constant expression enclosed in parentheses. The default value may also be one of the special case-independant keywords CURRENT_TIME, CURRENT_DATE or CURRENT_TIMESTAMP. If the value is NULL, a string constant or number, it is inserted into the column whenever an INSERT statement that does not specify a value for the column is executed. If the value is CURRENT_TIME, CURRENT_DATE or CURRENT_TIMESTAMP, then the current UTC date and/or time is inserted into the columns. For CURRENT_TIME, the format is HH:MM:SS. For CURRENT_DATE, YYYY-MM-DD. The format for CURRENT_TIMESTAMP is "YYYY-MM-DD HH:MM:SS".

来自http://www.sqlite.org/lang_createtable.html