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.