[英]Add Column with Default value from existing JSON Column
I have a table with name GDN_AUDIT_TRAIL having JSON column with name我有一个名为GDN_AUDIT_TRAIL的表,其中包含带有名称的 JSON 列
DETAILS .详情。
I want to add new column SOLUTION_ID in this table, so that the default value of this column should be one of the field from DETAILS JSON Column.我想在此表中添加新列 SOLUTION_ID,以便此列的默认值应该是 DETAILS JSON 列中的字段之一。 I have tried below query which is giving error -
我试过下面的查询,它给出了错误 -
ALTER TABLE GDN_AUDIT_TRAIL
ADD COLUMN SOLUTION_ID VARCHAR(50) DEFAULT JSON_UNQUOTE(JSON_EXTRACT(DETAILS,'$.SolutionID')) AFTER ACTION_TYPE;
Please let me know if this is possible.请让我知道这是否可行。
You would need to surround the expression given as a default with parentheses for MySQL to properly understand it:您需要将作为默认值给出的表达式用括号括起来,以便 MySQL 正确理解它:
alter table gdn_audit_trail
add column solution_id varchar(50)
default (json_unquote(json_extract(details,'$.SolutionID'))) after action_type;
-- ^ -- -- ^ --
Note that using expresions in the DEFAULT
clause requires MySQL 8.0.13.请注意,在
DEFAULT
子句中使用表达式需要 MySQL 8.0.13。 In earlier versions, only literal constants were allowed (with current_timestamp
being the only exceptions).在早期版本中,只允许使用文字常量(
current_timestamp
是唯一的例外)。 This iswell explained in the documentation :这在文档中有很好的解释:
Handling of Explicit Defaults as of MySQL 8.0.13
从 MySQL 8.0.13 开始处理显式默认值
The default value specified in a
DEFAULT
clause can be a literal constant or an expression.DEFAULT
子句中指定的默认值可以是文字常量或表达式。 With one exception, enclose expression default values within parentheses to distinguish them from literal constant default values.除了一个例外,将表达式默认值括在括号内以将它们与文字常量默认值区分开来。
[...]
[...]
Handling of Explicit Defaults Prior to MySQL 8.0.13
MySQL 8.0.13 之前的显式默认值的处理
With one exception, the default value specified in a
DEFAULT
clause must be a literal constant;除了一个例外,
DEFAULT
子句中指定的默认值必须是文字常量; it cannot be a function or an expression.它不能是函数或表达式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.