Home > database >  Mysql Error 1064 when trying to create a table
Mysql Error 1064 when trying to create a table

Time:10-02

I'm trying to create a table so that I can pass in a csv file to load it into the table but I'm getting this error and I'm not sure why. Here's what I have:

CREATE TABLE imdb(
-> rank int auto_increment PRIMARY key,
-> title varchar(255),
-> genre varchar(255),
-> director varchar(255),
-> actors varchar(255),
-> year int,
-> rating decimal(1,1),
-> votes int,
-> metascore int
-> );

Here is the error:

ERROR 1064 (42000): 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 'rank int 

auto_increment PRIMARY key, title varchar(255), genre varchar(255), dir' at line 2

CodePudding user response:

Those arrows shouldn't be there (maybe something that got added when copy/pasting?). Also, rank and year are reserved words (thanks @ Aloiso Gomes) so you have to add backticks to allow the name anytime you reference it (stored procs, selects, inserts, etc.)

This works:

CREATE TABLE imdb(
 `rank` int auto_increment PRIMARY key,
 `title` varchar(255),
 `genre` varchar(255),
 `director` varchar(255),
 `actors` varchar(255),
 `year` int,
 `rating` decimal(1,1),
 `votes` int,
 `metascore` int
);

INSERT INTO imdb (title,genre) VALUES 
('Fast Furious 1', 'racing'),
('Fast Furious 2', 'racing'),
('Fast Furious 3', 'racing?'),
('Fast Furious 4', '"racing"');

SELECT `rank`, title, genre FROM imdb;
  • Related