Add a column with a specified position to the MySQL table, and specify a position in the mysql table.

Source: Internet
Author: User

Add a column with a specified position to the MySQL table, and specify a position in the mysql table.

ALTER TABLE test ADD COLUMN id INT UNSIGNED NOT NULL auto_increment PRIMARY KEY FIRST


Adding columns to a table is a common operation. When adding columns to a table in MySQL, you can specify the position of this column.
Adding a column to a specified position requires two keywords:
FIRST and AFTER
FIRST indicates adding the FIRST column
AFTER indicates that AFTER a column is added
Note that BEFORE is not used when a column is added to MySQL. You can use FIRST for the FIRST column and AFTER for non-FIRST columns.
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(11) NOT NULL,  `b` varchar(200) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Add a column c after column a of table test:
ALTER TABLE test ADD COLUMN c INT NOT NULL AFTER a

Add 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

The full text is complete.




How does MySQL add fields, that is, columns, to a table? Here is an example. Thank you.

Add Field
Alter table <table_name> add <new_field_name> <new_field_type>.
The statement is written in this way. If you use a tool, you can directly add it, which is simpler ..

How does mysql Add a primary key to the front of a field or rank it in the 1st column?

Alter table 'user'
Add column 'firstname' VARCHAR (255) null default null after 'name ';

You can use the after keyword to specify which field to add.

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.