Home >Database >Mysql Tutorial >How to modify the value of primary key in mysql

How to modify the value of primary key in mysql

WBOY
WBOYOriginal
2022-05-16 16:45:4412053browse

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;" ;".

How to modify the value of primary key in mysql

The operating environment of this tutorial: windows10 system, mysql8.0.22 version, Dell G3 computer.

How to modify the value of the primary key in mysql

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:

How to modify the value of primary key in mysql

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:

How to modify the value of primary key in mysql

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:

How to modify the value of primary key in mysql

In five steps, mysql successfully modified the primary key, as shown in the figure below:

How to modify the value of primary key in mysql

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn