当前位置:网站首页>Interview shock 59: can there be multiple auto increment columns in a table?
Interview shock 59: can there be multiple auto increment columns in a table?
2022-06-22 08:41:00 【Wang Lei】
Auto incrementing columns can use auto_increment To achieve , When a column is identified as auto_increment after , If you do not set any values for this column when adding , Or set... For this column NULL When the value of , Then it will fill this column with self incrementing rules .
PS: The following content of this article is based on MySQL InnoDB Database engine .
By default, the value of auto increment column is 1, Each increment 1, For example, the following tables are created SQL:
create table tab_incre(
id int primary key auto_increment,
name varchar(250) not null
);
When we add , No auto increment id Set any value , Its execution results are as follows :
From the above results, we can see that the default value of auto increment column is 1, Each increment 1.
1. Manually specify self increment
You can manually specify self increment when creating a table , If you do not specify self increment , Then it defaults to 1 As a self increment , Manually specify self increment SQL The order is as follows :
create table tab_incre(
id int primary key auto_increment,
name varchar(250) not null
) auto_increment=50;
Use “show create table table_name” You can view the auto increment column value of the auto increment column in the table , As shown in the figure below :
The self increment of this table is 50, We can also create a piece of data to verify whether the self increment is 50, As shown in the figure below : 
2. Manually modify self increment
After the table is created , We can also pass alter Command to modify the value of the auto increment column , Its modification commands are as follows :
alter table table_name auto_increment=n;
If you want to tab_incre The self increment in the table is modified as 100, The following can be used: SQL To achieve : 
matters needing attention
When we try to set the self increment to a value smaller than the maximum value in the self increment column , The self increment will automatically become the maximum value of the self increment column +1 Value , As shown in the figure below : 
3. Can a table have multiple auto increment Columns ?
There can only be one self incrementing column in a table , This is similar to the rule that a table can only have one primary key , When we try to add a self incrementing column to a table , It can be added successfully , As shown in the figure below :
When we try to add multiple auto incrementing columns to a table , You will be prompted that there can only be one self incrementing error message , As shown in the figure below :
4. Other matters needing attention
Except that only one auto increment column can be added to a table , The following two issues need to be paid attention to in auto incrementing .
4.1 Auto incrementing column can only be of integer type
The field type of auto increment column can only be integer type (TINYINT、SMALLINT、INT、BIGINT etc. ), As shown in the figure below :
When we use other types as data types for auto incrementing Columns , The following error will be prompted : 
4.2 Must cooperate key Use it together
auto_increment Must cooperate key Use it together , This key It can be primary key or foreign key, without key You're going to report a mistake , As shown below : 
PS:auto_increment You can also work with unique constraints unique Use it together .
summary
The default value of auto increment column is 1, Each increment 1, However, you can also manually specify self increment when creating tables , Of course, in special cases, after the table is created , It can also be done through alter Modify self increment . There can only be one self incrementing column in a table , Just like a table can only have one primary key , If you set multiple auto increment Columns , that SQL Execution will report an error . In addition, note that the auto increment column should be of integer type , And auto_increment Need to cooperate with key Use it together , This key It can be primary key or foreign key.
It's up to you to judge right and wrong , Disdain is to listen to people , Gain or loss is more important than number .
official account :Java Analysis of the real interview questions
Interview collection :https://gitee.com/mydb/interview
边栏推荐
- 邮件巨头曝严重漏洞,用户数据被窃取
- Epidemic situation of novel coronavirus
- How to troubleshoot OOM
- Basic operation knowledge of DML and DQL
- Summary of sub database and sub table 1
- 17 iterator mode
- Web knowledge 3 (cookie+session)
- 18 中介者模式
- The challenge of image based voice processing in real-time audio and video -- RTC dev Meetup
- 08 桥接模式
猜你喜欢

Thoroughly understand my SQL index knowledge points

Top ten of the year! Saining network security was once again shortlisted in the top 100 report on China's digital security

16 解释器模式

深度学习——(1)ResNet实现

Installation and use of Jupiter notebook

19 memo mode

Flask blog practice - realize the classified management of blogs

Interpreting the technology group in maker Education

How to troubleshoot OOM

steam教育文化传承的必要性
随机推荐
Flask博客实战 - 实现用户管理
我的第一个Go程序
Flask blog practice - realize the classified management of blogs
Basic operation knowledge of DML and DQL
FastCorrect:语音识别快速纠错模型丨RTC Dev Meetup
Flask blog practice - create background management application
MySQL sub database and sub table
【自适应控制】最小二乘法离线辨识
17 iterator mode
10.file/io stream -bite
Thread.start()方法源码分析
面试突击59:一个表中可以有多个自增列吗?
读取jar包里面文件夹下的所有文件
Flask博客实战 - 创建后台管理应用
15 命令模式
Golang 开发 常用的第三方库 没有最全只有更全
培养以科学技能为本的Steam教育
Introduction to MySQL database Basics
开展有效的创客教育课程与活动
邮件巨头曝严重漏洞,用户数据被窃取