Home > Database > Mysql Tutorial > 给MySQL表增加指定位置的列_MySQL

给MySQL表增加指定位置的列_MySQL

WBOY
Release: 2016-06-01 13:02:21
Original
1462 people have browsed it

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

给表添加列是一个常用的操作,MySQL增加列的时候可以指定此列的位置
给指定位置加列需要两个关键字:
FIRST和AFTER
FIRST表示增加此列为第一个列
AFTER表示增加在某个列之后
注意MySQL增加列指定位置时没有BEFORE的用法,第一列可以使用FIRST,非第一列使用AFTER。
语法:
Copy after login
ALTER TABLE table_name ADD [COLUMN] col_name column_definition  [ FIRST | AFTER col_name]
Copy after login
实例:
DROP TABLE IF EXISTS `test`;

CREATE TABLE `test` (
  `a` int(11) NOT NULL,
  `b` varchar(200) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Copy after login
在test表a列后面增加一列c:
ALTER TABLE test ADD COLUMN c INT NOT NULL AFTER a
Copy after login
在test表的第一列增加字段id:
ALTER TABLE test ADD COLUMN id INT UNSIGNED NOT NULL auto_increment PRIMARY KEY FIRST
Copy after login
全文完。

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template