mysql表中某一列修改为自动增长的方法
如果表中没有数据可以使用 drop column然后再add column,如果存在一部分数据可以使用本文提供的第二种解决方法 /**************** 准备环境********************/ --判断是否存在test表 if object_id(N 'test' ,N 'U' ) is not null drop table test --创建test表 create table test ( id int not null , name varchar (20) not null ) --插入临时数据 insert into test values (1, '成龙' ) 总结:在表设计界面修改最为简单。如果该列已有的数据中存,修改可能会引发异常,可以使用数据导入导出的方式解决。 insert into test values (3, '章子怡' ) insert into test values (4, '刘若英' ) insert into test values (8, '王菲' ) select * from test /**************** 实现更改自动增长列********************/ begin transaction create table test_tmp ( id int not null identity(1,1), name varchar (20) not null ) go set identity_insert test_tmp on go if exists( select * from test) exec ( ' insert into test_tmp(id, name ) select id, name from test with(holdlock tablockx)' ) go set identity_insert test_tmp off go drop table test go exec sp_rename N 'test_tmp' ,N 'test' , 'OBJECT' go commit GO /****************验证结果*****************/ insert into test values ( '张曼' ) select * from test |
- 上一篇:MySQL数据库性能优化之五(存储引擎选择)
- 下一篇:Mysql的底层封装