可以计算SQL默认值吗?

时间:2016-05-15 16:29:27

标签: sql-server-2014 default-value liquibase calculated-columns calculated-field

如果我的表 do{ myfunc(); glClear(GL_COLOR_BUFFER_BIT); glDisable(GL_DEPTH_TEST); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, fb_width, fb_height, 0, 0, 1); glMatrixMode(GL_MODELVIEW); drawSomething(); glfwSwapBuffers(w); glfwPollEvents(); } while (something); 的列号为NUMBER - FOO,则VARCHAR - FOO.ID有一种方法可以让SQL为{{{{{}}设置计算FOO.LABEL 1}}就像“DFLT_LBL_”+ DEFAULT VALUE

IE FOO.LABEL = 1的记录将FOO.ID设置为“DFLT_LBL_1”,而ID 2的记录将LABEL设置为“DFLT_LBL_2”等等上。

1 个答案:

答案 0 :(得分:0)

一种适用于所有数据库的方法是使用视图:

create view v_table as 
    select t.*, concat(label, id) as label
    from foo t;

这不允许你改变它。你可以在表格中有一个名为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)));