I thought that curtime (), now () and current_timestamp are valid default datetime values ​​in MySql?

I am using the latest version of MySql and I tried changing the table to use curtime () or now () or current_timestamp as the default value for the datetime column (created_at, updated_at). For all three changes failed, stating that these are invalid default values. Any ideas what the appropriate default value is generating the current time in the datetime field?

ALTER TABLE `music_library_development`.`albums` 
MODIFY COLUMN `created_at` DATETIME NOT NULL DEFAULT current_timestamp,
MODIFY COLUMN `updated_at` DATETIME NOT NULL DEFAULT current_timestamp;

ALTER TABLE `music_library_development`.`albums` 
MODIFY COLUMN `created_at` DATETIME NOT NULL DEFAULT now(),
MODIFY COLUMN `updated_at` DATETIME NOT NULL DEFAULT now();

ALTER TABLE `music_library_development`.`albums` 
MODIFY COLUMN `created_at` DATETIME NOT NULL DEFAULT curtime(),
MODIFY COLUMN `updated_at` DATETIME NOT NULL DEFAULT curtime();
+3
source share
2 answers

Try using TIMESTAMP column type:

MODIFY COLUMN `updated_at` TIMESTAMP NOT NULL DEFAULT current_timestamp;
+1
source

alter table music_library_development change created_at created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;

alter table music_library_development change updated_at updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

0

Source: https://habr.com/ru/post/1707991/


All Articles