DB2 ADD a new COLUMN to a table with DEFAULT value from an EXPRESSION using existing data in another column

huangapple go评论87阅读模式
英文:

DB2 ADD a new COLUMN to a table with DEFAULT value from an EXPRESSION using existing data in another column

问题

ALTER TABLE my_table ADD COLUMN new_column_name data_type DEFAULT expression;

在DB2中,是否可以使用现有列的数据作为新列的默认值?

ALTER TABLE employees ADD COLUMN work_hours INT DEFAULT (man_days);

或者像这样使用默认值的表达式?

ALTER TABLE employees ADD COLUMN work_hours INT DEFAULT ( CASE WHEN salary < 50000 THEN (man_days + 10) * 24 WHEN salary >= 50000 THEN (man_days) * 24 END);

英文:
ALTER TABLE my_table ADD COLUMN new_column_name data_type DEFAULT expression;

Is it possible to use an existing column data as default value for a new column in DB2?

ALTER TABLE employees ADD COLUMN work_hours INT DEFAULT (man_days);

Or an expression like this for the default value?

ALTER TABLE employees ADD COLUMN work_hours INT DEFAULT ( CASE WHEN salary &lt; 50000 THEN (man_days + 10) * 24
   WHEN salary &gt;= 50000 THEN (man_days) * 24 END);

答案1

得分: 2

目前可用的IBM Db2的版本,无论是在哪个平台上,都不能使用ALTER TABLE语句来指定基于表达式的默认值。

对于default-clause的语法,Db2平台(LUW、Z/OS、i)之间存在轻微的差异,但都限制选项基于空值、特殊寄存器或常量。

您可以通过DDL和随后的SQL语句的组合来实现您的需求。

Db2-LUW alter table

DB2-for-Z/OS alter table

Db2-for-i alter table

英文:

With currently shipping versions of IBM Db2, regardless of the platform, you cannot use an ALTER TABLE statement to specify an expression-based default value.

There are minor differences between the Db2-platforms (LUW, Z/OS, i) for the syntax of the default-clause, but all limit the options to be based on null or special registers or constants.

You can achieve what you need by a combination of DDL and subsequent SQL statements.

Db2-LUW alter table

DB2-for-Z/OS alter table

Db2-for-i alter table

huangapple
  • 本文由 发表于 2023年6月9日 11:06:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436949.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定