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

Delegating Privileges in Linux Using Sudo

Introduction to sudo authority delegation su swit...

Summary of 7 types of logs in MySQL

There are the following log files in MySQL: 1: re...

Play with the connect function with timeout in Linux

In the previous article, we played with timeouts ...

Summary of the characteristics of SQL mode in MySQL

Preface The SQL mode affects the SQL syntax that ...

Implementation of HTML command line interface

HTML Part Copy code The code is as follows: <!D...

A brief analysis of the usage of USING and HAVING in MySQL

This article uses examples to illustrate the usag...

CentOS 7 switching boot kernel and switching boot mode explanation

centos7 switch boot kernel Note: If necessary, it...

Ubuntu installation cuda10.1 driver implementation steps

1. Download cuda10.1: NVIDIA official website lin...

Summary of Common Mistakes in Web Design

In the process of designing a web page, designers...

How to detect whether a file is damaged using Apache Tika

Apache Tika is a library for file type detection ...

Use PHP's mail() function to send emails

Sending emails using PHP's mail function The ...

Detailed explanation of prototypes and prototype chains in JavaScript

Table of contents Prototype chain diagram Essenti...

Example of how to configure multiple virtual hosts in nginx

It is very convenient to configure virtual host v...

13 JavaScript one-liners that will make you look like an expert

Table of contents 1. Get a random Boolean value (...

The rel attribute of the HTML link tag

The <link> tag defines the relationship bet...