7

I set default of datetime in mariadb 5.5 such as

CREATE TABLE IF NOT EXISTS test (
    `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

But it displays this error

Error 1067: Invalid default value for 'create_time'

How can I set default current time value in mariadb but don't use TIMESTAMP type ?

Toan Nguyen
  • 1,043
  • 3
  • 13
  • 24

2 Answers2

3

In 5.5 you cannot use CURRENT_TIMESTAMP as the default value for a DATETIME column. This was later introduced in 10.0.1 to make its use possible.

Your options are to either upgrade to a newer version or to emulate it using a trigger (as was explained by inanutshellus).

markusjm
  • 2,358
  • 1
  • 11
  • 23
1

Specify it using a trigger:

create trigger bi_mytable
before insert on mytable 
for each row 
begin
    NEW.create_time = NOW();
end;

And, since it's a "create" value, make sure nobody updates it:

create trigger bu_mytable
before update on mytable 
for each row 
begin
    NEW.create_time = OLD.create_time;
end;
inanutshellus
  • 9,683
  • 9
  • 53
  • 71