mysql delete and change the primary key of a table

        When we use MySQL, sometimes we need to change or delete the primary key of MySQL, we can simply use alter table table_name drop primary key; to complete.

        Below I use the data table table_test to make an example.

1. First create a data table table_test:

create table table_test(
`id` varchar(100) NOT NULL,
`name` varchar(100) NOT NULL,
PRIMARY KEY (`name`)
)ENGINE=MyISAM DEFAULT CHARSET=gb2312;

 

2. Suppose you find that the primary key is set incorrectly, it should be id is the primary key, but now there is a lot of data in the table, you cannot delete the table and rebuild it, you can only modify the table structure on this basis.

        delete the primary key first

alter table table_test drop primary key;

        Then add the primary key

alter table table_test add primary key(id);

        Note: Before adding the primary key, the duplicate id must be deleted.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326406328&siteId=291194637