I get MySQL ERROR 1064 (42000) when I try to run this code :
create table articles (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(30) NOT NULL,
`title` VARCHAR(30) NOT NULL,
`description` VARCHAR(255),
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIME, `users_id` INT(11) NOT NULL,
PRIMARY KEY(`id`),
FOREIGN KEY (`users_id`) REFERENCES `users` (`id`) ON DELETE
CASCADE);
Then I get this error : "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘CURRENT_TIME, users_id
INT(11) NOT NULL,PRIMARY KEY(id
), FOREIGN KEY (`users’ at line 1".
I already tried with and without backticks everywhere but it still gave me the same error… I think it probably has to do with the foreign key because I created a user table before without a foreign key and it worked.
2
Answers
I do not think CURRENT_TIME is valid for MySQL – it should be CURRENT_TIMESTAMP so try below which works for me (bar the foreign key as I do not have the users table):
‘use the DEFAULT CURRENT_TIMESTAMP’ https://dev.mysql.com/doc/refman/8.0/en/timestamp-initialization.html
https://dbfiddle.uk/K_cGe49R
Voting to close as typo..