我可以在sqlite3中创建一个默认值的datetime列吗?

时间:2022-08-07 00:16:26

Is there a way to create a table in sqlite3 that has a datetime column that defaults to 'now'?

有没有办法在sqlite3中创建一个具有默认为“now”的日期时间列的表?

The following statement returns a syntax error:

以下语句返回语法错误:

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

Update: Here's the correct ddl courtesy of Sky Sanders:

更新:以下是Sky Sanders提供的正确ddl:

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

4 个解决方案

#1


71  

Try this:

尝试这个:

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

Background:

背景:

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".

DEFAULT约束指定执行INSERT时要使用的默认值。值可以是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 HH:MM:SS”。

From http://www.sqlite.org/lang_createtable.html

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

#2


13  

... default (datetime(current_timestamp))

The expression following default must be in parentheses. This form is useful if you want to perform date arithmetic using SQLite date and time functions or modifiers.

默认情况下的表达式必须在括号中。如果要使用SQLite日期和时间函数或修饰符执行日期算术,则此表单很有用。

#3


6  

CURRENT_TIMESTAMP is a literal-value just like 'mystring'

CURRENT_TIMESTAMP是一个文字值,就像'mystring'一样

column-constraint:

列约束:

我可以在sqlite3中创建一个默认值的datetime列吗?

literal-value:

字面值:

我可以在sqlite3中创建一个默认值的datetime列吗?

#4


-2  

you can use the following query for using current date value in your table

您可以使用以下查询在表中使用当前日期值

create table tablename (date_field_name Created_on default CURRENT_DATE);

#1


71  

Try this:

尝试这个:

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

Background:

背景:

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".

DEFAULT约束指定执行INSERT时要使用的默认值。值可以是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 HH:MM:SS”。

From http://www.sqlite.org/lang_createtable.html

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

#2


13  

... default (datetime(current_timestamp))

The expression following default must be in parentheses. This form is useful if you want to perform date arithmetic using SQLite date and time functions or modifiers.

默认情况下的表达式必须在括号中。如果要使用SQLite日期和时间函数或修饰符执行日期算术,则此表单很有用。

#3


6  

CURRENT_TIMESTAMP is a literal-value just like 'mystring'

CURRENT_TIMESTAMP是一个文字值,就像'mystring'一样

column-constraint:

列约束:

我可以在sqlite3中创建一个默认值的datetime列吗?

literal-value:

字面值:

我可以在sqlite3中创建一个默认值的datetime列吗?

#4


-2  

you can use the following query for using current date value in your table

您可以使用以下查询在表中使用当前日期值

create table tablename (date_field_name Created_on default CURRENT_DATE);