Rename the primary key constraint to MySQL

I think this might be a bug with MySQL, but I'm not sure. Can anyone tell me how can I create a primary key for a table and then rename the main constraint? If possible, already create the primary key when creating the table with the desired name.

All primary keys I create end with the name "Primary". Already tried to create index with desired name before adding PK and rename PK using MySQL Workbench. None of them worked.

Does anyone know what's wrong and why I can't rename the PK name?

Thank!

+3


source to share


1 answer


I'm not sure if MySQL allows you to name primary keys in the first place. Although there is a syntax for it:

CREATE TABLE test (
    test_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    CONSTRAINT my_test_pk PRIMARY KEY (test_id)
)
ENGINE=InnoDB;

      

... it doesn't show up in information_schema.TABLE_CONSTRAINTS

or anywhere else I might find, so I get the impression that it is just silently discarded.



The name you see is probably the hard-coded name that your GUI client exposes to all primary keys.

Edit: here's a quote from the manual :

The name is PRIMARY KEY

alwaysPRIMARY

, which therefore cannot be used as a name for any other type of index.

+6


source







All Articles