MySQL Generated column from another table

No, if this is what you are trying to achieve, then it isn’t supported in any SQL product.

Sometimes we may require a generated/computed column in MySQL that references other tables. However, this isn’t supported and not a practical solution to have a reference of another column.

The following code will return a syntax error.

ALTER TABLE tableName1
MODIFY COLUMN columnName1 DECIMAL(20,4)
GENERATED ALWAYS AS
(SELECT columnName2 FROM tableName2);

Subqueries, parameters, variables, stored functions, and user-defined functions are not allowed, according to the MySQL documentation.

Additionally, only columns from the same row may be referenced in an expression for the newly generated column. The generated column is not permitted to reference functions that are non-deterministic, other tables, or subqueries.

Then, what is the solution?

An UPDATE statement. Or a View.

Hope you find this helpful.

One comment

Leave a Reply