Add columns to MySQL table at the specified location

Source: Internet
Author: User

ALTER TABLE test ADD COLUMN ID INT UNSIGNED not NULL auto_increment PRIMARY KEY First


Adding columns to a table is a common operation, and when MySQL adds columns, you can specify the location of the column
Adding a column to a specified location requires two keywords:
FirstAnd After
FirstIndicates that the column is incremented first
After indicates an increase after a column
Note that MySQL does not use before when adding columns to the specified position, and the first column can be used First, non-first column usesAfter .
Syntax:
ALTER TABLE table_name ADD [COLUMN] col_name column_definition  [First | After Col_name]

Instance:
DROP TABLE IF EXISTS ' test '; CREATE TABLE ' test ' (  ' a ' int (one) ' is not null,  ' B ' varchar ($) NOT null) Engine=innodb DEFAULT Charset=utf8;

Add a column C after the test table a column:
ALTER TABLE Test ADD COLUMN C INT not NULL after a

Increase the field ID in the first column of the test table:
ALTER TABLE test ADD COLUMN ID INT UNSIGNED not NULL auto_increment PRIMARY KEY First

Complete the full text.



Add columns to MySQL table at the specified location

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.