MySQL Generated Column

Virtual and stored generated columns are both available in MySQL. Every time data is read, the virtual columns are calculated instantly, but when data is updated, the stored columns are calculated and physically saved. This is equivalent to a calculated column in SQL Server that is either virtual or physically stored.

An expression that is part of the column definition is used to calculate the values of a created column.

Below is an example to create a table with generated columns.

CREATE TABLE test1 (
  col1 INT,
  col2 INT,
  calcol1 DOUBLE AS (col1*col2),
calcol2 DOUBLE AS (col1/col2),
calcol3 DOUBLE AS (col1-col2)
);
INSERT INTO test1 (col1, col2) VALUES(23,2),(21,4),(20,8);
SELECT * FROM test1;

mysql_generated_column

Hope this helps.

Leave a Reply