mysql 表的增删改

语法:

alter table 表名 执行动作 字段名;


1. 添加到末尾

alter table 表名 add 字段名 数据类型;


例如:

mysql> desc t1;

+-------+----------+------+-----+---------+-------+

| Field | Type     | Null | Key | Default | Extra |

+-------+----------+------+-----+---------+-------+

| name  | char(20) | YES  |     | NULL    |       |

+-------+----------+------+-----+---------+-------+

1 row in set (0.00 sec)


mysql> alter table t1 add age tinyint unsigned;

Query OK, 0 rows affected (0.02 sec)

Records: 0  Duplicates: 0  Warnings: 0


mysql> desc t1;

+-------+---------------------+------+-----+---------+-------+

| Field | Type                | Null | Key | Default | Extra |

+-------+---------------------+------+-----+---------+-------+

| name  | char(20)            | YES  |     | NULL    |       |

| age   | tinyint(3) unsigned | YES  |     | NULL    |       |

+-------+---------------------+------+-----+---------+-------+

2 rows in set (0.00 sec)


2. 添加到开始

alter table 表名 add 字段名 数据类型 first;


例如:

mysql> alter table t1 add id int first;

Query OK, 0 rows affected (0.13 sec)

Records: 0  Duplicates: 0  Warnings: 0


mysql> desc t1;

+-------+---------------------+------+-----+---------+-------+

| Field | Type                | Null | Key | Default | Extra |

+-------+---------------------+------+-----+---------+-------+

| id    | int(11)             | YES  |     | NULL    |       |

| name  | char(20)            | YES  |     | NULL    |       |

| age   | tinyint(3) unsigned | YES  |     | NULL    |       |

+-------+---------------------+------+-----+---------+-------+

3 rows in set (0.00 sec)


3. 添加到指定位置

添加到指定字段名下边

alter table 表名 add 表字段 数据类型 after 指定字段名;


例如:

mysql> alter table t1 add sex enum("M","F") after name;

Query OK, 0 rows affected (0.03 sec)

Records: 0  Duplicates: 0  Warnings: 0


mysql> desc t1;

+-------+---------------------+------+-----+---------+-------+

| Field | Type                | Null | Key | Default | Extra |

+-------+---------------------+------+-----+---------+-------+

| id    | int(11)             | YES  |     | NULL    |       |

| name  | char(20)            | YES  |     | NULL    |       |

| sex   | enum('M','F')       | YES  |     | NULL    |       |

| age   | tinyint(3) unsigned | YES  |     | NULL    |       |

+-------+---------------------+------+-----+---------+-------+

4 rows in set (0.00 sec)


4. 删除字段

alter table 表名 drop 字段名;


例如:

mysql> alter table t1 drop sex;

Query OK, 0 rows affected (0.03 sec)

Records: 0  Duplicates: 0  Warnings: 0


mysql> desc t1;

+-------+---------------------+------+-----+---------+-------+

| Field | Type                | Null | Key | Default | Extra |

+-------+---------------------+------+-----+---------+-------+

| id    | int(11)             | YES  |     | NULL    |       |

| name  | char(20)            | YES  |     | NULL    |       |

| age   | tinyint(3) unsigned | YES  |     | NULL    |       |

+-------+---------------------+------+-----+---------+-------+

3 rows in set (0.00 sec)


5. 修改数据类型

alter table 表名 modify 字段名 新的数据类型;


例如:

mysql> alter table t1 modify name varchar(10);

Query OK, 0 rows affected (0.03 sec)

Records: 0  Duplicates: 0  Warnings: 0


mysql> desc t1;

+-------+---------------------+------+-----+---------+-------+

| Field | Type                | Null | Key | Default | Extra |

+-------+---------------------+------+-----+---------+-------+

| id    | int(11)             | YES  |     | NULL    |       |

| name  | varchar(10)         | YES  |     | NULL    |       |

| age   | tinyint(3) unsigned | YES  |     | NULL    |       |

+-------+---------------------+------+-----+---------+-------+

3 rows in set (0.01 sec)


6. 修改字段名

alter table 表名 change 旧名 新名 数据类型;


例如:

mysql> alter table t1 change id ID int(11);

Query OK, 0 rows affected (0.01 sec)

Records: 0  Duplicates: 0  Warnings: 0


mysql> desc t1;

+-------+---------------------+------+-----+---------+-------+

| Field | Type                | Null | Key | Default | Extra |

+-------+---------------------+------+-----+---------+-------+

| ID    | int(11)             | YES  |     | NULL    |       |

| name  | varchar(10)         | YES  |     | NULL    |       |

| age   | tinyint(3) unsigned | YES  |     | NULL    |       |

+-------+---------------------+------+-----+---------+-------+

3 rows in set (0.00 sec)


7. 修改表名

alter table 表名 rename 新表名;


例如:

mysql> alter table t1 rename new_t1;

Query OK, 0 rows affected (0.01 sec)


猜你喜欢

转载自blog.51cto.com/calabash/2139533