当前位置:网站首页>已解决:一个表中可以有多个自增列吗
已解决:一个表中可以有多个自增列吗
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。
边栏推荐
- 关于放大器失真的原因你了解多少呢?
- Ribbon load balancing
- Introduction to JWT
- [in depth understanding of tcapulusdb technology] tcapulusdb operation and maintenance
- A Dynamic Near-Optimal Algorithm for Online Linear Programming
- 智能计算之神经网络(BP)介绍
- 84-我对网传<52 条 SQL 语句性能优化策略>的一些看法
- [graduation season] step by step? Thinking about four years of University by an automation er
- ROS从入门到精通(八) 常用传感器与消息数据
- Multi transactions in redis
猜你喜欢

智能計算之神經網絡(BP)介紹

Three months of self-taught automatic test, salary from 4.5K to 15K, who knows what I have experienced?

MySQL高级(二)

完全背包如何考慮排列問題

R语言AirPassengers数据集可视化
![Web technology sharing | [Gaode map] to realize customized track playback](/img/80/7daba6716b85276de8d09b9d016313.png)
Web technology sharing | [Gaode map] to realize customized track playback

The road to systematic construction of geek planet business monitoring and alarm system

web技术分享| 【高德地图】实现自定义的轨迹回放

科技云报道:东数西算不止于“算”,更需“新存储”

Emotion analysis with RNN & CNN pytorch
随机推荐
Comment le sac à dos complet considère - t - il la disposition?
Overview of common loss functions for in-depth learning: basic forms, principles and characteristics
uniapp小程序商城开发thinkphp6积分商城、团购、秒杀 封装APP
Nestjs integrates config module and Nacos to realize configuration unification
支持在 Kubernetes 运行,添加多种连接器,SeaTunnel 2.1.2 版本正式发布!
A Dynamic Near-Optimal Algorithm for Online Linear Programming
数字化转型的失败原因及成功之道
Three months of self-taught automatic test, salary from 4.5K to 15K, who knows what I have experienced?
Kotlin1.6.20 new features context receivers tips
完全背包如何考慮排列問題
MySQL advanced (II)
R语言midwest数据集可视化
71-对2010年阿里一道Oracle DBA面试题目的分析
用RNN & CNN进行情感分析 - PyTorch
Unityeditor editor script execution menu
[resolved] -go_ out: protoc-gen-go: Plugin failed with status code 1.
Classic interview question: a page from entering URL to rendering process
Be careful with MySQL filesort
真正的缓存之王Caffine Cache
MySQL基础——约束