Search code examples
mysqlprimary-keymysql-error-1064

MySQL primary key must be written in parentheses


I have tried the following query (it was not written by myself, but was included in a source code from the Internet)

CREATE TABLE `city_list_count` (
  `city_created` date NOT NULL,
  `count_created` int(8) unsigned NOT NULL,
  PRIMARY KEY `city_created` 
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

It gave me the following error:

1064 - 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 ') ENGINE=InnoDB DEFAULT CHARSET=utf8' at line 5


Solution

  • Trying different things I've pointed out that the reason of the error is that primary key field name should be wrapped in parentheses:

    CREATE TABLE `city_list_count` (
      `city_created` date NOT NULL,
      `count_created` int(8) unsigned NOT NULL,
      PRIMARY KEY (`city_created`) 
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    

    I couldn't find the explanation for this anywhere. However, it worked for me. Just wanted to share this with others if someone came across the same trouble.