SQL Oracle 将日期列添加到现有表(默认为仅 sysdate 新行)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30334933/
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-09-01 03:38:49  来源:igfitidea点击:

Oracle Add Date Column to Existing Table (default to sysdate new rows only)

sqloracleoracle10g

提问by NealWalters

Is there a way to add column DATE_CREATED such as only new rows will pickup the default sysdate? When I ran the ALTER below, all priors rows got the DATE_CREATED set to the run-time of the ALTER script; I would prefer them to remain null.

有没有办法添加列 DATE_CREATED 例如只有新行会选择默认的系统日期?当我运行下面的 ALTER 时,所有先验行都将 DATE_CREATED 设置为 ALTER 脚本的运行时;我希望它们保持为空。

alter table abc.mytable
  add 
    (DATE_CREATED        date   default sysdate null
    ); 

回答by a_horse_with_no_name

You need to first add the column without a default:

您需要先添加没有默认值的列:

alter table mytable add date_created date default null;

and then add define the default value:

然后添加定义默认值:

alter table mytable modify date_created default sysdate;