简体   繁体   English

更改表添加主键无法识别 MySQL

[英]Alter table add primary key not recognised MySQL

I am trying to run the statement ALTER TABLE invoice_partitioned DROP PRIMARY KEY, ADD PRIMARY KEY(invoice_partitioned.id, invoice_partitioned.department_code);我正在尝试运行语句ALTER TABLE invoice_partitioned DROP PRIMARY KEY, ADD PRIMARY KEY(invoice_partitioned.id, invoice_partitioned.department_code);

This produces a syntax error (Error Code 1064) when run.运行时会产生语法错误(错误代码 1064)。 MySQL Workbench is highlighting the bracket after ADD PRIMARY KEY, with the message '"(" is not valid at this position for this server version'. MySQL Workbench 突出显示了 ADD PRIMARY KEY 之后的括号,并显示消息 '"(" is not valid at this position for this server version'。

Is there any way to resolve this issue?有没有办法解决这个问题? I am on MySQL Community Server 8.0.19我在 MySQL 社区服务器 8.0.19

For example if we had a table:例如,如果我们有一张桌子:

create table invoice_partitioned (id int
                                  , department_code INT
                                  , primary key(ID));

Then this is the way:那么方法是这样的:

ALTER TABLE invoice_partitioned DROP PRIMARY KEY;

ALTER TABLE invoice_partitioned ADD PRIMARY KEY(id, department_code);

Here is a demo 这是一个演示

This will also work:这也将起作用:

ALTER TABLE invoice_partitioned
DROP PRIMARY KEY,
ADD PRIMARY KEY(id, department_code);

Demo 演示

you can do it in single statement你可以用一个语句来完成

just don't add table name before column name as shown below只是不要在列名之前添加表名,如下所示

replace代替

ALTER TABLE invoice_partitioned DROP PRIMARY KEY, ADD PRIMARY KEY(invoice_partitioned.id, invoice_partitioned.department_code);

to this对此

ALTER TABLE invoice_partitioned DROP PRIMARY KEY, ADD PRIMARY KEY (id, department_code);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM