skip to Main Content

I am trying to set a column on Phpmyadmin to just “TIME”. How can I set the default value to the current time? Only time, no date included.

CURRENT_TIMESTAMP does not work, I also have tried now() but no success. I have looked on the documentation but haven’t found anything specific about it.

3

Answers


  1. It’s not possible to set a default value for a TIME or DATE column. Switch to a TIMESTAMP column and use date(t_stamp) to just get the date. Or time(t_stamp) to just get the time.

    Login or Signup to reply.
  2. Excerpts from the documentation link in the question:

    Handling of Explicit Defaults as of MySQL 8.0.13

    The default value specified in a DEFAULT clause can be a literal
    constant or an expression. With one exception, enclose expression
    default values within parentheses to distinguish them from literal
    constant default values.

    […]

    Handling of Explicit Defaults Prior to MySQL 8.0.13

    With one exception, the default value specified in a DEFAULT clause
    must be a literal constant; it cannot be a function or an expression.
    This means, for example, that you cannot set the default for a date
    column to be the value of a function such as NOW() or CURRENT_DATE.
    The exception is that, for TIMESTAMP and DATETIME columns, you can
    specify CURRENT_TIMESTAMP as the default.

    So, when supported, use parenthesis:

    CREATE TABLE foo (
        what_time TIME NOT NULL DEFAULT (CURTIME())
    );
    

    Demo

    Login or Signup to reply.
  3. create table test1234235 (foo int, ts timestamp default now());
    insert into test1234235 (foo) values(1);
    select * from test1234235;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search