Home >Database >Mysql Tutorial >mysql修改自增长主键int类型为char类型示例_MySQL

mysql修改自增长主键int类型为char类型示例_MySQL

WBOY
WBOYOriginal
2016-06-01 13:04:361304browse

原来有一个表中的主键是int自增长类型,
因为业务变化需要把int改成char类型的主键。同时因为原来的表中已经存在了数据,不能删除表重建,只能修改表结构。
首先去掉自增长属性:
alter table table_name  change indexid indexid int;
然后去掉主键:
ALTER TABLE table_name   DROP   primary key;
修改表结构为char类型:
alter table table_name change indexid indexid char(18);
最后重新添加主键:
alter table table_name add primary key(indexid);

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn