How to Return the Definition of a Generated Column in MariaDB

If you have a table in MariaDB with a generated column, you can use the following methods to find out its definition.

Option 1: The COLUMNS Information Schema Table

The information_schema.columns table contains information about columns in each table on the server. This table includes a column called IS_GENERATED which indicates whether or not it’s a generated column, and another column called GENERATION_EXPRESSION that contains the expression used for computing the column value in a generated column.

Here’s an example of querying this table for the definition of a generated column:

SELECT 
  column_name, 
  is_generated,
  generation_expression
FROM information_schema.columns
WHERE table_name = 'Employees';

Result:

column_name	is_generated	generation_expression
--------------- --------------- ----------------------------------
Id	        NEVER	        NULL
FirstName	NEVER	        NULL
LastName	NEVER	        NULL
FullName	ALWAYS	        concat(`FirstName`,' ',`LastName`)

Actually, in this case I queried the whole table (I returned all rows from the table).

We can see that only the FullName column is generated. This has ALWAYS in the is_generated column, and its definition is in the generated_expression column.

The base columns (i.e. the non-generated columns) have NEVER in the is_generated column, and a NULL value in the generation_expression column.

Option 2: The SHOW CREATE TABLE Statement

The SHOW CREATE TABLE statement shows the CREATE TABLE statement that created the given table. Therefore, we can use it to show us the definition of any generated columns in a table (as well as the definition of other columns).

Example:

SHOW CREATE TABLE Employees;

Result:

Table	        Create Table
--------------- -----------------------------------------------------------------------------------
Employees	CREATE TABLE `Employees` (
                `Id` int(11) NOT NULL,
                `FirstName` varchar(50) DEFAULT NULL,
                `LastName` varchar(50) DEFAULT NULL,
                `FullName` varchar(101) GENERATED ALWAYS AS (concat(`FirstName`,' ',`LastName`)) STORED,
                PRIMARY KEY (`Id`)
                ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4