>데이터 베이스 >MySQL 튜토리얼 >MySQL数据库在指定位置增加字段_MySQL

MySQL数据库在指定位置增加字段_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB원래의
2016-06-01 13:54:33880검색

这个本身属于MySQL的基本语法,我这里给出一个例子而已。

view plaincopy to clipboardprint?
mysql> -- 测试数据库在指定位置增加字段  
mysql> drop table if exists t;  
Query OK, 0 rows affected (0.06 sec)  
mysql> create table t(age int,address varchar(50));  
Query OK, 0 rows affected (0.06 sec)  
mysql> desc t;  
+---------+-------------+------+-----+---------+-------+  
| Field   | Type        | Null | Key | Default | Extra |  
+---------+-------------+------+-----+---------+-------+  
| age     | int(11)     | YES  |     | NULL    |       |  
| address | varchar(50) | YES  |     | NULL    |       |  
+---------+-------------+------+-----+---------+-------+  
2 rows in set (0.02 sec)  
mysql> alter table t add column name varchar(20) after age;  
Query OK, 0 rows affected (0.16 sec)  
Records: 0  Duplicates: 0  Warnings: 0 
mysql> desc t;  
+---------+-------------+------+-----+---------+-------+  
| Field   | Type        | Null | Key | Default | Extra |  
+---------+-------------+------+-----+---------+-------+  
| age     | int(11)     | YES  |     | NULL    |       |  
| name    | varchar(20) | YES  |     | NULL    |       |  
| address | varchar(50) | YES  |     | NULL    |       |  
+---------+-------------+------+-----+---------+-------+  
3 rows in set (0.00 sec)  
mysql> alter table t add column id int FIRST;  
Query OK, 0 rows affected (0.13 sec)  
Records: 0  Duplicates: 0  Warnings: 0 
mysql> desc t;  
+---------+-------------+------+-----+---------+-------+  
| Field   | Type        | Null | Key | Default | Extra |  
+---------+-------------+------+-----+---------+-------+  
| id      | int(11)     | YES  |     | NULL    |       |  
| age     | int(11)     | YES  |     | NULL    |       |  
| name    | varchar(20) | YES  |     | NULL    |       |  
| address | varchar(50) | YES  |     | NULL    |       |  
+---------+-------------+------+-----+---------+-------+  
4 rows in set (0.00 sec)  
mysql> 
mysql> -- 测试数据库在指定位置增加字段
mysql> drop table if exists t;
Query OK, 0 rows affected (0.06 sec)
mysql> create table t(age int,address varchar(50));
Query OK, 0 rows affected (0.06 sec)
mysql> desc t;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| age     | int(11)     | YES  |     | NULL    |       |
| address | varchar(50) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
2 rows in set (0.02 sec)
mysql> alter table t add column name varchar(20) after age;
Query OK, 0 rows affected (0.16 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc t;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| age     | int(11)     | YES  |     | NULL    |       |
| name    | varchar(20) | YES  |     | NULL    |       |
| address | varchar(50) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> alter table t add column id int FIRST;
Query OK, 0 rows affected (0.13 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc t;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int(11)     | YES  |     | NULL    |       |
| age     | int(11)     | YES  |     | NULL    |       |
| name    | varchar(20) | YES  |     | NULL    |       |
| address | varchar(50) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql>  

相关的语法描述为

ALTER [ONLINE | OFFLINE] [IGNORE] TABLE tbl_name    alter_specification [, alter_specification] ...alter_specification:    table_option ...  | ADD [COLUMN] col_name column_definition        [FIRST | AFTER col_name ]

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.