java 将 h2 数据库上列的默认值更改为当前时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14216704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:29:56  来源:igfitidea点击:

Change default value for column on h2 database to current timestamp

javasqlh2

提问by Andreas Dolk

I have a h2 database with a schema that is auto generated via JPA/hibernate. Now I want to alter a not-null timestamptype column on the database to default to the current time.

我有一个 h2 数据库,其架构是通过 JPA/hibernate 自动生成的。现在我想timestamp将数据库上的非空类型列更改为默认为当前时间。

I actually use a trigger, and that works, but I'd like to know if there's a more elegant way to achieve this, something like (the following snippet doesn'twork)

我实际上使用了 trigger,并且有效,但是我想知道是否有更优雅的方法来实现这一点,例如(以下代码段不起作用

ALTER TABLE my_table ALTER COLUMN my_column SET DEFAULT CURRENT_TIMESTAMP;

I looked at the documentationand tried some variants of the above "pseudo statement" but with now success.

我查看了文档并尝试了上述“伪语句”的一些变体,但现在成功了。

回答by sgeddes

Looks close, have you tried:

看起来很接近,您是否尝试过:

ALTER TABLE <table name> ALTER COLUMN <column name> SET DEFAULT CURRENT_TIMESTAMP