Table cannot be created in mysql -Error 1064

倾然丶 夕夏残阳落幕 提交于 2019-12-31 04:05:06

问题


I am trying to create a table in MySQL with the query

CREATE TABLE ofRosterGroups (
  rosterID              BIGINT          NOT NULL,
  rank                  TINYINT         NOT NULL,
  groupName             VARCHAR(255)    NOT NULL,
  PRIMARY KEY (rosterID, rank),
  INDEX ofRosterGroup_rosterid_idx (rosterID)
);

but seems like it is throwing error everytime I made updates too. I don't know what is going wrong with it.

Error coming up is

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 TINYINT NOT NULL, groupName
VARCHAR at line 3


回答1:


MySQL 8.0.2 added support for the window rank function, making it a reserverd word.

You could escape it using backticks (`):

CREATE TABLE ofRosterGroups (
  rosterID              BIGINT          NOT NULL,
  `rank`                TINYINT         NOT NULL, -- Here
  groupName             VARCHAR(255)    NOT NULL,
  PRIMARY KEY (rosterID, `rank`), -- And here
  INDEX ofRosterGroup_rosterid_idx (rosterID)
);

But it may be a better idea to just use a name that isn't a reserved word, such as rosterRank instead of rank:

CREATE TABLE ofRosterGroups (
  rosterID              BIGINT          NOT NULL,
  rosterRank            TINYINT         NOT NULL, -- Here
  groupName             VARCHAR(255)    NOT NULL,
  PRIMARY KEY (rosterID, rosterRank), -- And here
  INDEX ofRosterGroup_rosterid_idx (rosterID)
);


来源:https://stackoverflow.com/questions/53570132/table-cannot-be-created-in-mysql-error-1064

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!