Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Find Generated Column Definition in MySQL

I have added a virtual generated column to the salaries table in the test employees database as follows:

ALTER TABLE salaries
    ADD COLUMN salary_k int AS (salary / 1000);

Now when I query INFORMATION_SCHEMA.COLUMNS the EXTRA column shows VIRTUAL GENERATED as expected, but how can I get the details of the generated column, i.e. in this case (salary / 1000)? COLUMN_DEFAULT shows NULL.

SHOW CREATE TABLE salaries shows the details in the result, but I want the result as part of a larger query of the INFORMATION_SCHEMA, so that doesn't work for me.

like image 885
isapir Avatar asked Sep 05 '25 13:09

isapir


2 Answers

It's in the GENERATION_EXPRESSION column of INFORMATION_SCHEMA.COLUMNS.

From the documentation:

GENERATION_EXPRESSION
For generated columns, displays the expression used to compute column values.

like image 104
Barmar Avatar answered Sep 08 '25 03:09

Barmar


Here is a more practical answer:

SELECT column_name, generation_expression
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'salaries'
like image 34
caram Avatar answered Sep 08 '25 03:09

caram