Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How can mysql add id as a self-increasing primary key to a table that has already been built?

Shulou Source: shulou.com Published: 2022-06-02 05:14:37 10月28日 Update

This article introduces the relevant knowledge of "mysql how to add id to the established table as self-increasing primary key". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

Add id to self-increasing primary key in the created table without primary key:

alter table TABLE_NAME add id int not null primary key Auto_increment

There is no primary key in the original table, and the modified table already has a primary key:

alter table TABLE_NAME add primary key(COL_NAME);

There is a primary key in the original table. If you want to modify other columns as primary keys, you need to delete the original primary key first, and then modify other columns as primary keys:

alter table TABLE_NAME drop primary key;

alter table TABLE_NAME add primary key(COL_NAME);

The content of "mysql how to add id to the already built table is self-increasing primary key" is introduced here. Thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Tags: Forms built content more knowledge practical successful learning next dilemma reality situation articles cases editing websites industries processes high quality learning related Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Xiaomi Linux Microsoft Huawei