欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySQL修改表结构操作命令总结

程序员文章站 2024-03-01 23:13:22
表的结构如下: 复制代码 代码如下: mysql> show create table person; | person | create table `per...

表的结构如下:

复制代码 代码如下:

mysql> show create table person;
| person | create table `person` (
  `number` int(11) default null,
  `name` varchar(255) default null,
  `birthday` date default null
) engine=myisam default charset=utf8 |

删除列:

复制代码 代码如下:

alter table person drop column birthday;

添加列:

复制代码 代码如下:

alter table person add column birthday datetime;

修改列,把number修改为bigint:

复制代码 代码如下:

alter table person modify number bigint not null;

或者是把number修改为id,类型为bigint:

复制代码 代码如下:

alter table person change number id bigint;

添加主键:

复制代码 代码如下:

alter table person add primary key (id);

删除主键:

复制代码 代码如下:

alter table person drop primary key;

添加唯一索引:

复制代码 代码如下:

alter table person add unique name_unique_index (`name`);

为name这一列创建了唯一索引,索引的名字是name_unique_index.

添加普通索引:

复制代码 代码如下:

alter table person add index birthday_index (`birthday`);

删除索引:

复制代码 代码如下:

alter table person drop index birthday_index;
alter table person drop index name_unique_index;

禁用非唯一索引

复制代码 代码如下:

alter table person disable keys;

alter table...disable keys让mysql停止更新myisam表中的非唯一索引。

激活非唯一索引

复制代码 代码如下:

alter table person enable keys;

alter table ... enable keys重新创建丢失的索引。

把表默认的字符集和所有字符列(char, varchar, text)改为新的字符集:

复制代码 代码如下:

alter table person convert to character set utf8;

修改表某一列的编码

复制代码 代码如下:

alter table person change name name varchar(255) character set utf8;

仅仅改变一个表的默认字符集

复制代码 代码如下:

alter table person default character set utf8;

修改表名

复制代码 代码如下:

rename table person to person_other;

移动表到其他数据库

复制代码 代码如下:

rename table current_db.tbl_name to other_db.tbl_name;