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

How Do I Make a MySQL Column AUTO_INCREMENT?

Susan Sarandon
Susan SarandonOriginal
2025-01-08 20:47:54622browse

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>

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!

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