Tag Archives: Generated Column

Add Generated column to the MySQL Table

This code can help you add a column in your table that can be generated from the other columns


ALTER TABLE `tablename`
CHANGE COLUMN `col_generated` `col_generated` VARCHAR(45) GENERATED ALWAYS AS (timediff(col_start_time, '%Y-%m-%d %h:%i:%s'), STR_TO_DATE(col_end_time,'%Y-%m-%d %h:%i:%s'))) STORED ;

The above code above will generate a column with the time that gets calculated as a result of difference in the time in the other two columns.