Home >Database >Mysql Tutorial >How to modify the value of primary key in mysql
Method: 1. Use alter table to delete the primary key, the syntax is "alter table table name drop primary key;"; 2. Use alter table to re-add the primary key, the syntax is "alter table table name add primary key;" ;".
The operating environment of this tutorial: windows10 system, mysql8.0.22 version, Dell G3 computer.
The first step: First of all, you need to know that to modify the primary key in mysql, you must first delete the original primary key constraints, and then add a new primary key to achieve the effect of modifying the primary key. Modify the primary key of coal_3 table here.
Enter "alter table coal_3 drop primary key;" to delete the original primary key constraints of the coal_3 table, as shown in the following figure:
See that the sql has been executed The statement is successful and the original primary key is deleted. It should be noted that if the primary key is set to automatically increment, you need to remove the automatic increment first and then delete the primary key, as shown in the following figure:
Step 2: Execute the "alter table coal_3 add primary key(id);" statement and add the id as the primary key of the coal_3 table, as shown in the following figure:
In five steps, mysql successfully modified the primary key, as shown in the figure below:
Recommended learning: mysql video tutorial
The above is the detailed content of How to modify the value of primary key in mysql. For more information, please follow other related articles on the PHP Chinese website!