Expire date as default value for TIMESTAMP column

2019-04-29 09:54发布

Is there any way to set the default value for a column as an expire date (some hours from CURRENT_TIMESTAMP)?

I have already tried:

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

But didn't work..

1条回答
Rolldiameter
2楼-- · 2019-04-29 10:00

You can't implement a complex default value like that in the table definition.

You can do it 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 ;
查看更多
登录 后发表回答