I'd like to have a column that has a value dependant on other columns of the same table in MySQL Server 5.7 (InnoDB Cluster).
As a base for my statement, and as a test, I've used the following instruction:
ALTER TABLE contracts
ADD COLUMN curr_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
But I want my column to be a DATE
and to have a value that is always a date resulting from the formula ADDDATE(order_loaded_on, INTERVAL FLOOR(delivery_time * 1.4) DAY)
(where order_loaded_on
is a DATE
and delivery_time
is an INT
), so I wrote and ran the following:
ALTER TABLE contracts
ADD COLUMN est_delivery_on DATE
DEFAULT ADDDATE(order_loaded_on, INTERVAL FLOOR(delivery_time * 1.4) DAY)
ON UPDATE ADDDATE(order_loaded_on, INTERVAL FLOOR(delivery_time * 1.4) DAY)
But it returns a syntax error near to ADDDATE(order_loaded_on, INTERVAL FLOOR(delivery_time * 1.4) DAY)
on line 4, so I ran it without the ON UPDATE
clause and it works. The problem is that without the latter clause it doesn't update when the record changes.
Where am I doing wrong?
UPDATE:
As per suggestion, I've tried to add it as a generated column running the following:
ALTER TABLE CrmContratto ADD COLUMN PrevistaConsegna DATE AS ADDDATE(DataCaricamentoOrdine, INTERVAL FLOOR(TempiConsegna * 1.4) DAY)
But it returns another syntax error.
The correct statement is in the accepted answer. Thanks.