Alter table add primary key not recognised MySQL

Cirrus86 :

I am trying to run the statement 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. MySQL Workbench is highlighting the bracket after ADD PRIMARY KEY, with the message '"(" 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

VBoka :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=10106&siteId=1