oracle oracle表中自动填充日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2033063/
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
Automatically populate date in oracle table
提问by Adnan
I have created a table in oracle XE, and I have a field with type date. I would like if possible when I insert a row, that it automatically fills that field with the current date from the system.
我在 oracle XE 中创建了一个表,并且我有一个日期类型的字段。如果可能,我希望在插入一行时,它会自动使用系统中的当前日期填充该字段。
I am inserting the rows from the SQL prompt.
我正在从 SQL 提示插入行。
Thanks
谢谢
回答by ant
Here is how, you need to format your table properly:
以下是您需要正确格式化表格的方法:
create table test (first number
, second timestamp default systimestamp
, third varchar2(12));
And your default value is always current system time formatted as timestamp.
并且您的默认值始终是格式化为时间戳的当前系统时间。
回答by Жасулан Бердибеков
change the field after creating the table
创建表后更改字段
ALTER TABLE table MODIFY time_collumn TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
回答by Vivek R
Or you could also use a trigger:
或者你也可以使用触发器:
CREATE OR REPLACE TRIGGER date_trigger
BEFORE INSERT
ON table_name
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
SELECT sysdate INTO :NEW.column_name FROM dual;
END;
回答by Sagnik
The below snippet might be helpful if we forget to add the constraint while creating the table:
如果我们在创建表时忘记添加约束,以下代码段可能会有所帮助:
ALTER TABLE TABLE_NAME
ADD CONSTRAINT CONSTRAINT_NAME
COLUMN_NAME DATA_TYPE DEFAULT CURRENT_DATE;