MySQL 数据表中添加字段

目录

添加字段

将添加的字段放在表中的第一行

将新增的字段放在指定字段之后


 

在一个创建好的表中添加字段。

 

添加字段

alter table  add column   ; 
  
table name 要添加字段的表名称
column name 要添加的字段名称
column type 要添加字段的类型

例如对user表添加一个字段name用来记录用户的名称:

mysql> desc user;
+-------+------------------------------+------+-----+---------+-------+
| Field | Type                         | Null | Key | Default | Extra |
+-------+------------------------------+------+-----+---------+-------+
| phone | char(11)                     | NO   | PRI | NULL    |       |
| email | varchar(255)                 | YES  |     | NULL    |       |
| age   | tinyint(3) unsigned          | YES  |     | NULL    |       |
| level | set('0','1','2','3','4','5') | YES  |     | NULL    |       |
+-------+------------------------------+------+-----+---------+-------+
4 rows in set (0.02 sec)

mysql> alter table user add column name varchar(255) not null;
Query OK, 0 rows affected (0.09 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc user;
+-------+------------------------------+------+-----+---------+-------+
| Field | Type                         | Null | Key | Default | Extra |
+-------+------------------------------+------+-----+---------+-------+
| phone | char(11)                     | NO   | PRI | NULL    |       |
| email | varchar(255)                 | YES  |     | NULL    |       |
| age   | tinyint(3) unsigned          | YES  |     | NULL    |       |
| level | set('0','1','2','3','4','5') | YES  |     | NULL    |       |
| name  | varchar(255)                 | NO   |     | NULL    |       |
+-------+------------------------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

提示:默认新增字段命令将新增的字段放在表的最后一行。

 

将添加的字段放在表中的第一行

alter table  add column   first; 
  
table name 要添加字段的表名称
column name 要添加的字段名称
column type 要添加字段的类型

例如要对user表添加一个字段 username用来记录用户的昵称:

mysql> desc user;
+-------+------------------------------+------+-----+---------+-------+
| Field | Type                         | Null | Key | Default | Extra |
+-------+------------------------------+------+-----+---------+-------+
| phone | char(11)                     | NO   | PRI | NULL    |       |
| email | varchar(255)                 | YES  |     | NULL    |       |
| age   | tinyint(3) unsigned          | YES  |     | NULL    |       |
| level | set('0','1','2','3','4','5') | YES  |     | NULL    |       |
| name  | varchar(255)                 | NO   |     | NULL    |       |
+-------+------------------------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> alter table user add column username varchar(200) not null first;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc user;
+----------+------------------------------+------+-----+---------+-------+
| Field    | Type                         | Null | Key | Default | Extra |
+----------+------------------------------+------+-----+---------+-------+
| username | varchar(200)                 | NO   |     | NULL    |       |
| phone    | char(11)                     | NO   | PRI | NULL    |       |
| email    | varchar(255)                 | YES  |     | NULL    |       |
| age      | tinyint(3) unsigned          | YES  |     | NULL    |       |
| level    | set('0','1','2','3','4','5') | YES  |     | NULL    |       |
| name     | varchar(255)                 | NO   |     | NULL    |       |
+----------+------------------------------+------+-----+---------+-------+
6 rows in set (0.01 sec)

 

将新增的字段放在指定字段之后

alter table  add column   after ; 
  
table name 要添加字段的表名称
column name 要添加的字段名称
column type 要添加字段的类型
old_param 要将新字段放入某个旧字段之后

例如要对user表添加一个字段 sex用来记录用户的性别,并将sex放入level之后:

mysql> desc user;
+-------+------------------------------+------+-----+---------+-------+
| Field | Type                         | Null | Key | Default | Extra |
+-------+------------------------------+------+-----+---------+-------+
| uuid  | char(36)                     | NO   | PRI | NULL    |       |
| phone | char(11)                     | NO   | UNI | NULL    |       |
| email | varchar(255)                 | YES  | MUL | NULL    |       |
| age   | tinyint(3) unsigned          | YES  |     | NULL    |       |
| level | set('0','1','2','3','4','5') | YES  |     | NULL    |       |
+-------+------------------------------+------+-----+---------+-------+
5 rows in set (0.01 sec)

mysql> alter table user add column sex enum('boy', 'girl') after level;
Query OK, 0 rows affected (0.10 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc user;
+-------+------------------------------+------+-----+---------+-------+
| Field | Type                         | Null | Key | Default | Extra |
+-------+------------------------------+------+-----+---------+-------+
| uuid  | char(36)                     | NO   | PRI | NULL    |       |
| phone | char(11)                     | NO   | UNI | NULL    |       |
| email | varchar(255)                 | YES  | MUL | NULL    |       |
| age   | tinyint(3) unsigned          | YES  |     | NULL    |       |
| level | set('0','1','2','3','4','5') | YES  |     | NULL    |       |
| sex   | enum('boy','girl')           | YES  |     | NULL    |       |
+-------+------------------------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

 

你可能感兴趣的:(MySQL,mysql)