Home > Database > Mysql Tutorial > How Do I Make a MySQL Column AUTO_INCREMENT?

How Do I Make a MySQL Column AUTO_INCREMENT?

Susan Sarandon
Release: 2025-01-08 20:47:54
Original
609 people have browsed it

How Do I Make a MySQL Column AUTO_INCREMENT?

Transforming a MySQL Column into AUTO_INCREMENT

Modifying an existing MySQL column to become AUTO_INCREMENT is done using the ALTER TABLE command. The example SQL provided earlier has a syntax error. Here's the correct approach:

<code class="language-sql">ALTER TABLE document MODIFY COLUMN document_id INT AUTO_INCREMENT;</code>
Copy after login

Let's dissect this command:

  • ALTER TABLE document: This targets the table named "document" for modification.
  • MODIFY COLUMN document_id: This specifies the column, "document_id", to be changed.
  • INT AUTO_INCREMENT: This sets the data type to integer (INT) and enables automatic incrementing for new rows.

After executing this statement, the document_id column will be an AUTO_INCREMENT column, typically also functioning as the primary key.

The above is the detailed content of How Do I Make a MySQL Column AUTO_INCREMENT?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template