zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

MySQL简单操作之alter table改变表的结构

2023-03-31 11:06:52 时间

我们知道,MySQL数据库常常与PHP结合来开发出功能强大的应用程序。那么对于我们初学PHP编程的人来说,就很有必要了解一些MySQL数据库的简单操作了。本文我们对MySQL数据库使用alter table语句来修改表结构的知识进行了介绍,接下来就让我们来一起了解一下这部分内容。

1. 增加列

alter table tbl_name add col_name type

例如,  给pet的表增加一列 weight,

mysql>alter table pet add weight int;

2. 删除列

alter table tbl_name drop col_name

例如, 删除pet表中的weight这一列

mysql>alter table pet drop weight;

3. 改变列

分为改变列的属性和改变列的名字

改变列的属性——方法1:

alter table tbl_name modify col_name type

例如,改变weight的类型

mysql>alter table pet modify weight varchar(30);

改变列的属性——方法2:

alter table tbl_name change old_col_name col_name type

例如,改变weight的类型

alter table pet change weight weight varchar(30);

改变列的名字:

alter table tbl_name change old_col_name col_name

例如改变pet表中weight的名字:

mysql>alter table pet change weight wei;

4. 改变表的名字

alter table tbl_name rename new_tbl

例如, 把pet表更名为animal

mysql>alter table pet rename animal;

关于MySQL数据库alter table改变表结构的知识就介绍到这里了,接下来我们还会介绍一些MySQL数据库的简单操作方法,希望本次的介绍能够对您有所收获吧!

【编辑推荐】

  1. MySQL数据库的查询缓冲机制
  2. MySQL数据库查询步骤和缓存原理
  3. MySQL数据库负荷较高时的原因排查思路
  4. MySQL数据库中关于ENUM类型的详细解释
  5. MySQL数据库主从服务器文档的部署与切换详解