skip to Main Content

I have MyISAM engine running in my cPanel server ,the server does not support InnoDB Engine, i only have MyISAM engine.

  When i try to create a table column of type timestamp with a default value of (current time + 5 minutes) it gives an error.
This worked in my machine with InnoDB but in the server with MyISAM it gives an error You have an error in your SQL syntax; check the manual that... near DEFAULT (NOW() + 300)

CREATE TABLE test(
    token_death_time TIMESTAMP DEFAULT (NOW() + 300)
   );

I also tried

CREATE TABLE test(
    token_death_time TIMESTAMP DEFAULT DATE_ADD(NOW(), INTERVAL 5 MINUTE)
   );

Can I achieve what I want with MyISAM or I have to do the ‘adding’ in my PHP script?

3

Answers


  1. You can use CURRENT_TIMESTAMP

    CREATE TABLE test(
       token_death_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );
    

    To set desired timestamp as default, you can set it in variable and can use prepare & execute statement, use below code:

    SET @def_timestamp = DATE_ADD(NOW(), INTERVAL 1 DAY);
    SET @query = CONCAT("CREATE TABLE test(token_death_time TIMESTAMP DEFAULT '", @def_timestamp , "')");
    PREPARE stmt from @query;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    
    Login or Signup to reply.
  2. The DEFAULT value expression which you need is available for MySQL version 8.0.13 or later (fiddle – both queries are successfully executed on 8.0.19 version).

    See Data Type Default Values.

    And the issue is not relative to the table’s engine.

    Login or Signup to reply.
  3. you cant use function for default value (before version 8)

    but you can use trigger

    CREATE TRIGGER before_insert_test
    BEFORE INSERT ON test 
    FOR EACH ROW
    SET new.token_death_time = DATE_ADD(NOW(), INTERVAL 5 MINUTE);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search