What are "columns generated" in MySQL and what would your applications be?

6

I downloaded the new version of the MySQL workbench and when I was creating a table, I noticed a new field property called Generated Column .

On the MySQL site, this is a new implementation of MySQL 5.7.5 , but it has not been very clear to me how to use it and where it can be useful. Could anyone explain?

    
asked by anonymous 28.05.2016 / 17:18

1 answer

5

Are calculated columns. These columns should not be updated by you, they will always have the value set based on the data from the other columns. It is a facilitator to access certain information that is used frequently and need to always have an expression to get it.

It is possible that it is virtual and the value is calculated every time that access it. Or it may be stored , where the calculation is performed whenever one of the columns used in its formula is updated. Obviously it is faster to access, but it takes up more disk space and memory, since the calculated value is stored. Each case has better use.

It is very easy to abuse this. It is useful, but you can live without, always gave.

CREATE TABLE venda ( 
    nome VARCHAR(30),  
    preco DECIMAL(10, 2),
    qtde INT,
    total DECIMAL(10, 2) AS (preco * qtde)); //tem seu valor gerado por esta fórmula

It can be very useful for use in indexes that needs to be an expression. Documentation ( secondary ).

    
28.05.2016 / 17:34