Validity as default value for TIMESTAMP column

Is it possible to set the default value for a column as an expiration date (several hours from CURRENT_TIMESTAMP )?

I already tried:

 ALTER TABLE `table` ADD COLUMN `expire` TIMESTAMP NOT NULL DEFAULT TIMESTAMPADD(HOUR, 5, CURRENT_TIMESTAMP); 

But it didn’t work out.

+4
source share
1 answer

You cannot implement a complex default value like this in a table definition.

You can do this with a trigger if you want:

 DELIMITER $$ DROP TRIGGER IF EXISTS tr_b_ins_table $$ CREATE TRIGGER tr_b_ins_table BEFORE INSERT ON table FOR EACH ROW BEGIN SET NEW.expire = NOW() + INTERVAL 5 HOUR; END $$ DELIMITER ; 
+6
source

Source: https://habr.com/ru/post/1401618/


All Articles