oracle 更改表添加列作为选择语句

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

Alter table add column as select statement

sqldatabaseoracleoracle9i

提问by Kimaya

How to do something like this...

如何做这样的事情...

alter table customer_schedule add (week_number as (TO_CHAR((SCHEDULE_DATE),'iw')) 

Wherein SCHEDULE_DATE is one of the existing columns in table

其中 SCHEDULE_DATE 是表中现有的列之一

回答by Lalit Kumar B

This is where you need VIRTUAL COLUMN. If you are on 11g and up, you could certainly do -

这是您需要VIRTUAL COLUMN 的地方。如果您使用 11g 或更高版本,您当然可以这样做 -

alter table table_name
add (column_name [data_type] [generated always] as (column_expression) [virtual]);

In your case, it will be something like -

在您的情况下,它将类似于 -

alter table customer_schedule add (week_number data_type generated always as (TO_CHAR((SCHEDULE_DATE),'iw') VIRTUAL) 

回答by René Nyffenegger

On 9i, you cannot use virtual columns, so I'd probably go with a view:

在 9i 上,您不能使用virtual columns,所以我可能会采用以下观点:

create view customer_schedule_view as
    select 
      c.*,
      to_char(c.schedule_date, 'iw')) week_number
    from
      customer_schedule c;

Of course, in your forms you need then to select from the view rather from the table.

当然,在您的表单中,您需要从视图而不是从表中进行选择。

回答by Sathyajith Bhat

The default keyword is supposed to let you store values when there is some value missing, but due to restrictions, you cannot use a column name

default 关键字应该让您在缺少某些值时存储值,但由于限制,您不能使用列名

A DEFAULT expression cannot contain references to PL/SQL functions or to other columns, the pseudocolumns LEVEL, PRIOR, and ROWNUM, or date constants that are not fully specified.

DEFAULT 表达式不能包含对 PL/SQL 函数或其他列、伪LEVEL、PRIOR 和 ROWNUM 或未完全指定的日期常量的引用。

I think a row-level BEFORE INSERT/UPDATE trigger should do want you want to do.

我认为行级 BEFORE INSERT/UPDATE 触发器应该是你想做的。