当前位置:网站首页>面试突击59:一个表中可以有多个自增列吗?
面试突击59:一个表中可以有多个自增列吗?
2022-06-22 08:31: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。
是非审之于己,毁誉听之于人,得失安之于数。
公众号:Java面试真题解析
边栏推荐
- 11 appearance mode
- 并发三大特性2-有序性
- Bit group sort
- Yolov5 reports an error: attributeerror: 'upsample' object has no attribute 'recommend_ scale_ Solution of 'factor'
- Fastcorrect: speech recognition fast error correction model RTC dev Meetup
- Questions 1 to 100 of the national information security grade examination nisp level 1 question bank (1)
- Top ten of the year! Saining network security was once again shortlisted in the top 100 report on China's digital security
- Application of complex science in Maker Teaching Research
- 13 代理模式
- Five skills to be an outstanding cloud architect
猜你喜欢

Record once · fluent file buffer

学科融合对steam教育的作用

14 responsibility chain mode

PostgreSQL source code (56) extensible type analysis expandedobject/expandedrecord

同态加密的基本概念

One hot and embedding

The necessity of steam education culture inheritance

12 享元模式

I spring and autumn web Penetration Test Engineer (elementary) learning notes (Chapter 2)

Synchronized
随机推荐
Win10 add group policy command
Basic concepts of homomorphic encryption
Object to string pit
Basic operation knowledge of DML and DQL
Prompt installremove of the Service denied when installing MySQL service
07 adapter mode
One hot and embedding
17 iterator mode
安装 MySQL 服务时提示 InstallRemove of the Service Denied
08 bridging mode
Several methods to prevent repeated form submission + actual measurement
Basic knowledge and practical application of redis
Some problems encountered in using swagger in golang
Web knowledge 4 (filter+listener)
A simple - timed task component (quartz) -demo
Any to Any 实时变声的实现与落地丨RTC Dev Meetup
Web Knowledge 1 (server +servlet)
解读创客教育中的技术一族
开展有效的创客教育课程与活动
16 interpreter mode