user2513149
user2513149

Reputation: 880

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

Upvotes: 0

Views: 513

Answers (2)

user2513149
user2513149

Reputation: 880

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.

Upvotes: 0

orique
orique

Reputation: 1303

According to MySQL CREATE TABLE syntax, you need parentheses before and after a column list for a constraint:

[CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...) [index_option] ...

Upvotes: 1

Related Questions