6

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
  • 2
    TIMESTAMP and DATETIME columns can default to CURRENT_TIMESTAMP, but not to a value calculated by a function. Best bet is a trigger as in Ike's example. Commented Mar 15, 2012 at 14:34

1 Answer 1

8

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 ;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.