Example analysis of mysql non-primary key self-increment usage

Example analysis of mysql non-primary key self-increment usage

This article uses an example to illustrate the usage of MySQL non-primary key self-increment. Share with you for your reference, the details are as follows:

In MySQL, not only the primary key can be set to auto-increment, but the column set as the key can be set to auto-increment. as follows:

CREATE TABLE t1 (
  id INT,
  col1 INT auto_increment NOT NULL
);

The results are as follows:

If you set the col1 column as the key, you can create an auto-increment column.

CREATE TABLE t1 (
  id INT,
  col1 INT auto_increment NOT NULL,
  key(col1)
);

The results are as follows:

If we set id as the primary key, we can still create it successfully.

CREATE TABLE t2 (
  id INT PRIMARY KEY,
  col1 INT auto_increment NOT NULL,
  key(col1)
);

The results are as follows:

Therefore, the auto-increment column must be a key, but it does not necessarily have to be a primary key. But can a table have multiple auto-increment columns?

Answer: A table can only have one auto-increment column.

CREATE TABLE t3 (
  id INT PRIMARY KEY auto_increment,
  col1 INT auto_increment NOT NULL,
  key(col1)
);

The results are as follows:

Readers who are interested in more MySQL-related content can check out the following topics on this site: "Summary of MySQL Index Operation Skills", "Summary of MySQL Common Functions", "Summary of MySQL Log Operation Skills", "Summary of MySQL Transaction Operation Skills", "Summary of MySQL Stored Procedure Skills" and "Summary of MySQL Database Lock-Related Skills".

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • Tutorial on primary key in MySQL and setting its auto-increment
  • Example of changing the auto-increment primary key type from int to char in mysql
  • Solution to running out of MySQL's auto-increment ID (primary key)
  • What to do if the auto-increment primary key in MySQL is used up
  • MySQL 8 new features: detailed explanation of persistence of auto-increment primary key
  • Mysql auto-increment primary key id is not processed in this way
  • Detailed explanation of the implementation of MySQL auto-increment primary key
  • Why is the MySQL auto-increment primary key not continuous?

<<:  echars 3D map solution for custom colors of regions

>>:  VMware pro15 installation macOS10.13 detailed installation diagram (picture and text)

Recommend

The use of v-model in vue3 components and in-depth explanation

Table of contents Use two-way binding data in v-m...

Native JS to achieve image marquee effects

Today I will share with you a picture marquee eff...

HTML+CSS to achieve layered pyramid example

This article mainly introduces the example of imp...

Analysis of MySQL crash recovery based on Redo Log and Undo Log

Table of contents MySQL crash recovery process 1....

js realizes horizontal and vertical sliders

Recently, when I was doing a practice project, I ...

JavaScript to achieve stair rolling special effects (jQuery implementation)

I believe everyone has used JD. There is a very c...

How to use Vue cache function

Table of contents Cache function in vue2 Transfor...

Various problems encountered by novices when installing mysql into docker

Preface Recently, my computer often takes a long ...

Detailed usage of Linux text search command find

The find command is mainly used to find directori...

Html long text automatically cuts off when it exceeds the tag width

When we display long text, we often need to interc...

How to use axios to filter multiple repeated requests in a project

Table of contents 1. Introduction: In this case, ...

Example code showing common graphic effects in CSS styles

Let me briefly describe some common basic graphic...

How to use Dayjs to calculate common dates in Vue

When using vue to develop projects, the front end...