当前位置:网站首页>已解决:一个表中可以有多个自增列吗
已解决:一个表中可以有多个自增列吗
2022-06-22 19:25:00 【菜鸟是大神】
自增列可使用 auto_increment 来实现,当一个列被标识为 auto_increment 之后,在添加时如果不给此列设置任何值,或给此列设置 NULL 值时,那么它会使用自增的规则来填充此列。
PS:本文以下内容基于 MySQL InnoDB 数据库引擎。
默认情况下自增列的值为 1,每次递增 1,比如以下建表 SQL:
create table tab_incre(
id int primary key auto_increment,
name varchar(250) not null
);

我们在添加时,不给自增列 id 设置任何值,它的执行结果如下:
从上述结果可以看出自增列默认值为 1,每次递增 1。
1.手动指定自增值
在创建表的时候可以手动指定自增值,如果不指定自增值,那么它默认会使用 1 作为自增值,手动指定自增值的 SQL 命令如下:
create table tab_incre(
id int primary key auto_increment,
name varchar(250) not null
) auto_increment=50;

使用“show create table table_name”可以查看表中自增列的自增列值,如下图所示:此表的自增值为 50,我们也可以创建一条数据来验证一下自增值是否为 50,如下图所示:![]()
2.手动修改自增值
当表创建之后,我们也可以通过 alter 命令来修改自增列的值,它的修改命令如下:
alter table table_name auto_increment=n;
如果要将 tab_incre 表中的自增值修改为 100,可使用以下 SQL 来实现:

注意事项
当我们试图将自增值设置为比自增列中的最大值还要小的值的时候,自增值会自动变为自增列的最大值 +1 的值,如下图所示:

3.一个表可以有多个自增列吗?
当我们尝试给一个表添加多个自增列时,会提示只能有一个自增列的报错信息,如下图所示:![]()
4.其他注意事项
除了一个表只能添加一个自增列之外,自增列还需要注意以下两个问题。
4.1 自增列只能为整数类型
自增列的字段类型只能为整数类型(TINYINT、SMALLINT、INT、BIGINT 等),如下图所示:
当我们使用其他类型来作为自增列的数据类型时,会提示如下错误:

4.2 必须配合 key 一起使用
auto_increment 必须配合 key 一起使用,这个 key 可以是 primary key 或 foreign key,如果没有 key 就会报错,如下所示:

PS:auto_increment 也可以配合唯一约束 unique 一起使用。
总结
自增列的值默认是 1,每次递增 1,但也可以在创建表的时候手动指定自增值,当然在特殊情况下我们在表被创建之后,也可以通过 alter 修改自增值。一个表中只能有一个自增列,就像一个表中只能有一个主键一样,如果设置多个自增列,那么 SQL 执行就会报错。除此之外还要注意自增列应该为整数类型,且 auto_increment 需要配合 key 一起使用,这个 key 可以是 primary key 或 foreign key。
边栏推荐
- AAAI 2022 | 传统GAN修改后可解释,并保证卷积核可解释性和生成图像真实性
- [proteus simulation] 74LS138 decoder water lamp
- mysql8.0忘记密码的详细解决方法
- Scheduling with Testing
- 【观察】软件行业创新进入“新周期”,如何在变局中开新局?
- Software testing - Test Case Design & detailed explanation of test classification
- R 语言USArrests 数据集可视化
- 88-被广为流传的参数优化, 是蜜糖还是毒药?
- 什么?你居然不会微信分身
- 79-不要看到有order by xxx desc就创建desc降序索引-文末有赠书福利
猜你喜欢

Classic interview question: a page from entering URL to rendering process

How to realize @ person function in IM instant messaging

一个支持IPFS的电子邮件——SKIFF

Oh, my God, it's a counter attack by eight part essay

leetcode.11 --- 盛最多水的容器
mysql8.0忘记密码的详细解决方法

Alibaba cloud video on demand playback error, console access code:4400

Possible security vulnerabilities in NFT

How to consider the arrangement of complete knapsack

什么?你居然不会微信分身
随机推荐
75-当left join遇到子查询
Ribbon负载均衡
[in depth understanding of tcapulusdb technology] tcapulusdb operation and maintenance
Introduction to JWT
Introduction of neural network (BP) in Intelligent Computing
Dynamicdatabasesource, which supports the master-slave database on the application side
软件上线前为什么要做性能测试?软件性能测试机构怎么找
天,靠八股文逆袭了啊
怎样实现网页端im即时通讯中的@人功能
真正的缓存之王Caffine Cache
[in depth understanding of tcapulusdb technology] business guide for creating doc acceptance
Cloud computing in the metauniverse to enhance your digital experience
他98年的,我玩不过他...
模拟串口UART的实现
6月第3周B站榜单丨飞瓜数据UP主成长排行榜(哔哩哔哩平台)发布!
【Proteus仿真】三极管组成的H桥驱动直流电机+按键正反转控制
AAAI 2022 | traditional Gan can be interpreted after modification, and the interpretability of convolution kernel and the authenticity of generated image are guaranteed
[proteus simulation] 74LS138 decoder water lamp
Shell编程基础(第七篇:分支语句-if)
Introduction of neural networks for Intelligent Computing (Hopfield network DHNN, CHNN)