How to generate mysql primary key id (self-increment, unique and irregular)

How to generate mysql primary key id (self-increment, unique and irregular)

1. Use the uuid function to generate a unique and irregular primary key id

sql:

CREATE TABLE `test` (
  `id` varchar(100) COLLATE utf8_estonian_ci NOT NULL COMMENT 'Unique and non-duplicate',
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `sex` int(11) DEFAULT NULL,
  `name` varchar(255) COLLATE utf8_estonian_ci DEFAULT NULL,
  `username` varchar(255) COLLATE utf8_estonian_ci DEFAULT NULL,
  `password` varchar(255) COLLATE utf8_estonian_ci DEFAULT NULL,
  `classes` varchar(255) COLLATE utf8_estonian_ci DEFAULT NULL,
  `major` int(255) DEFAULT NULL,
  `QQ` int(20) DEFAULT NULL,
  `introducemyself` varchar(255) COLLATE utf8_estonian_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_estonian_ci ROW_FORMAT=DYNAMIC;

surface:

Insert statement:

INSERT INTO test(id,sex,name,username,password,classes,major,QQ,introducemyself) VALUE(replace(uuid(), '-', ''),1,"小米","xck","001","班八",265,953190259,"我最牛");

Executed twice, generating two different ids:

2. Automatic growth of id

Change the type to integer and select auto-growth below

See DDL:

CREATE TABLE `test` (
  `id` bigint(100) NOT NULL AUTO_INCREMENT COMMENT 'unique and non-duplicate',
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `sex` int(11) DEFAULT NULL,
  `name` varchar(255) COLLATE utf8_estonian_ci DEFAULT NULL,
  `username` varchar(255) COLLATE utf8_estonian_ci DEFAULT NULL,
  `password` varchar(255) COLLATE utf8_estonian_ci DEFAULT NULL,
  `classes` varchar(255) COLLATE utf8_estonian_ci DEFAULT NULL,
  `major` int(255) DEFAULT NULL,
  `QQ` int(20) DEFAULT NULL,
  `introducemyself` varchar(255) COLLATE utf8_estonian_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_estonian_ci ROW_FORMAT=DYNAMIC;

Insert a piece of data, sql:

INSERT INTO test(sex,name,username,password,classes,major,QQ,introducemyself) VALUE(1,"小米","xck","001","班八",265,953190259,"我最牛");

Corresponding database changes:

This is the end of this article about how to generate MySQL primary key ID (self-increment, unique and irregular). For more relevant MySQL primary key ID generation content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Summary of how to generate the primary key of sqlserver database (sqlserver,mysql)

<<:  The iframe frame sets the white background to transparent in IE browser

>>:  How to inherit CSS line-height

Recommend

The most complete package.json analysis

Table of contents 1. Overview 2. Name field 3. Ve...

How to create scheduled tasks using crond tool in Linux

Preface Crond is a scheduled execution tool under...

How to change the color of the entire row (tr) when the mouse stops in HTML

Use pure CSS to change the background color of a ...

What does mysql database do?

MySQL is a relational database management system....

Detailed explanation of the use of umask under Linux

I recently started learning Linux. After reading ...

Simple example of using Docker container

Table of contents 1. Pull the image 2. Run the im...

Attributes in vue v-for loop object

Table of contents 1. Values ​​within loop objects...

Upgrade MySQL 5.1 to 5.5.36 in CentOS

This article records the process of upgrading MyS...

Four categories of CSS selectors: basic, combination, attribute, pseudo-class

What is a selector? The role of the selector is t...

MySQL 8.0.13 installation and configuration method graphic tutorial under win10

I would like to share the installation and config...

10 Deadly Semantic Mistakes in Web Typography

<br />This is from the content of Web front-...

Detailed use of Echarts in vue2 vue3

Table of contents 1. Installation 2. Use Echarts ...

MySQL trigger definition and usage simple example

This article describes the definition and usage o...

How to implement the observer pattern in JavaScript

Table of contents Overview Application scenarios ...