skip to Main Content

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


  1. 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):

    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_TIMESTAMP, `users_id` INT(11) NOT NULL,
    PRIMARY KEY(`id`),
    FOREIGN KEY (`users_id`) REFERENCES `users` (`id`) ON DELETE
    CASCADE);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search