可以计算出SQL个默认值吗?
Can a SQL Default Value be calculated?
如果我有 table FOO
列 NUMBER - FOO.ID
,VARCHAR - FOO.LABEL
有没有办法设置 SQL FOO.LABEL
的计算 DEFAULT VALUE
类似于 "DFLT_LBL_" + FOO.ID
I.E ID
= 1 的记录会将 LABEL
设置为 "DFLT_LBL_1" 而 ID
2 的记录会将 LABEL
设置为 "DFLT_LBL_2"等等。
一种适用于所有数据库的方法是使用视图:
create view v_table as
select t.*, concat(label, id) as label
from foo t;
这不允许您更改它。您可以在 table 中有一个名为 override_label
的列,它具有 NULL
默认值,然后视图是:
create view v_table as
select t.*, coalesce(override_label, concat(label, id)) as label
from foo t;
在某些数据库中,您可以对计算列执行类似的操作:
alter table foo add label as (coalesce(override_label, concat(label, id)));
如果我有 table FOO
列 NUMBER - FOO.ID
,VARCHAR - FOO.LABEL
有没有办法设置 SQL FOO.LABEL
的计算 DEFAULT VALUE
类似于 "DFLT_LBL_" + FOO.ID
I.E ID
= 1 的记录会将 LABEL
设置为 "DFLT_LBL_1" 而 ID
2 的记录会将 LABEL
设置为 "DFLT_LBL_2"等等。
一种适用于所有数据库的方法是使用视图:
create view v_table as
select t.*, concat(label, id) as label
from foo t;
这不允许您更改它。您可以在 table 中有一个名为 override_label
的列,它具有 NULL
默认值,然后视图是:
create view v_table as
select t.*, coalesce(override_label, concat(label, id)) as label
from foo t;
在某些数据库中,您可以对计算列执行类似的操作:
alter table foo add label as (coalesce(override_label, concat(label, id)));