【发布时间】:2020-03-02 20:31:34
【问题描述】:
我已经创建了表格
create table testdates2(dates date primary key,ate text);
insert into testdates2(2018-04,'nov');
我收到了这个错误
SyntaxException:第 1:23 行在输入“2018”处没有可行的替代方案 (插入 testdates2([2018]...)
【问题讨论】:
我已经创建了表格
create table testdates2(dates date primary key,ate text);
insert into testdates2(2018-04,'nov');
我收到了这个错误
SyntaxException:第 1:23 行在输入“2018”处没有可行的替代方案 (插入 testdates2([2018]...)
【问题讨论】:
格式为yyyy-mm-dd,这意味着您也必须始终坚持一天。这是一个使用您的架构的示例,它将为 2018 年 4 月 1 日插入一行。我为 ate 列选择了一个随机值。
cqlsh> insert into testdates2 (dates, ate) VALUES ('2018-04-01', 'Soup');
cqlsh> select * from testdates2;
dates | ate
------------+------
2018-04-01 | Soup
(1 rows)
您可以在 DataStax's documentation 中找到有关在 Cassandra 中使用日期值的更多详细文档。
编辑基于关于时间戳与日期的评论。
timestamp 具有更高的分辨率,它可以指向事件发生的确切秒数,而date 可以指向事件发生的日期。
假设我想确切地知道汤是在几秒钟内被吃掉的,我可以使用时间戳类型而不是日期类型。
ALTER TABLE testdates2 ADD time_eaten timestamp;
insert into testdates2 (dates, ate, time_eaten) VALUES ('2018-04-01', 'Soup', 1522576800);
1522576800 和 2019-04-01 都代表 2018 年 4 月 1 日,但时间戳还指定事件发生在上午 10 点。
【讨论】:
timestamp 具有更高的分辨率,它可以指向事件发生的确切秒,而date 可以指向事件发生的日期。您要使用哪一个取决于您希望的准确度。