0

Can we create a table in Mysql server with a column as the difference between two other columns. So when those two columns are populated the third column will be computed from them.

CREATE TABLE LOAD(
START_TIME DATETIME,
END_TIME DATETIME,
ELAPSED_TIME DATETIME AS START_TIME-END_TIME
)

Something like this

sticky bit
  • 36,626
  • 12
  • 31
  • 42

1 Answers1

0

MySQL 8 does indeed support generated columns. So to have a generated column elapsed_time which has the seconds passed between a column start_time and end_time, you could use timestampdiff() like:

CREATE TABLE `load`
             (start_time datetime,
              end_time datetime,
              elapsed_time integer AS (timestampdiff(SECOND, start_time, end_time)));

(Note that load is a reserved word and therefore needs to be enclosed in backticks when used as an identifier.)
db<>fiddle

sticky bit
  • 36,626
  • 12
  • 31
  • 42