mysql 中id字段自增

alter table album change ALBUM_ID ALBUM_ID bigint not null auto_increment;

alter table album auto_increment=1;

创建:
mysql>create table cc(id int auto_increment,name varchar(20),primary key(id));
修改:
mysql> alter table cc change id id int primary key auto_increment;

注:只有int类型且为primary key 才可以使用auto_increment.

如果用hibernate作为持久层,那么主键ID处用identity类型.


基本命令格式如下:
ALTER TABLE tbl_name MODIFY col_name column_definition

其中column_definition字段需要包括以下内容:
1、该列的数据类型及AUTO_INCREMENT字段;
2、如该列必须为NOT NULL及PRIMARY KEY(UNIQUE)属性。如果不是,需添加相应定义。
alter table test MODIFY id INT UNSIGNED AUTO_INCREMENT;

desc test;



ALTER TABLE causes auto_increment resequencing, resulting in duplicate entry ’1′ for key ‘PRIMARY’

解决方法:

第1步:将主键字段值为0的那条记录值改为其他大于0且不重复的任意数

第2步:修改主键字段为auto_increment

第3步:把刚才修改过的那条记录的值还原


猜你喜欢

转载自blog.csdn.net/xiaofanren1111/article/details/79770647