Add an auto-increment primary key column to the MySQL table

Source: Internet
Author: User
Tags table definition

When I added a primary key to a Mysql table, I used the following SQL statement:

  1. Alter Table'Table name'Add Column'Column name'Int Not NullAuto_increment comment'Primary key'Before 'a column name ';

The following two errors are returned:

  1. Error Code: 1064
  2. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'before column name' at line 1
  3. (0 MS taken)

This error indicates a syntax error near before.

  1. Error Code: 1075
  2. Incorrect table definition; there can be only one auto column and it must be defined as a key
  3. (0 MS taken)

This error indicates that only the primary key can specify auto-increment columns.

I found the reason on the internet, saying that I want to add a column before modifying the column. I think this is quite troublesome. I tried it myself and I can do it in one sentence:

  1. Alter Table'Table name'Add Column'Column name'Int Not NullAuto_incrementPrimary KeyComment'Primary key' First;

The preceding statement adds an Automatically increasing primary key field to a table and places the field in the first column.

If you want to put it behind other columns, you canFirstChangeAfter 'existing column name'

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.