MySQL 自增序列

-- 5.7.23
select version();

-- 非主键形式的自增字段
create table test3
(
 id int auto_increment not null,
 str varchar(2),
 key(id)
);

-- 自增默认从1开始
insert into test3(str) values('ab');
insert into test3(str) values('cd');

-- truncat后,自增序列重新开始
truncate test3;
insert into test3(str) values('ef');

-- 设置自增开始值
alter table test3 AUTO_INCREMENT=100;

insert into test3(str) values('gh');

扫描二维码关注公众号,回复: 4242884 查看本文章

-- 同时 创建自增序列字段,与主键

create table test4
(
 id int auto_increment not null,
 str varchar(2),
 num int,
 key(id),
 PRIMARY KEY(str)
);

-- duplicate 同样生效

insert into test4(str, num) values('ab',12) on duplicate key update num = num + values(num);

insert into test4(str, num) values('ab',12) on duplicate key update num = num + values(num);

select * from test4;

-- 查询下一个,自增序列的值

SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'test4';

注意:使用duplicate 会使,自增序列,出现跳跃式增长。遇到不要感到意外,哈哈~

END

猜你喜欢

转载自blog.csdn.net/Cky079/article/details/84108728