Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How is the primary key id of mysql database generated

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains how the mysql database primary key id is generated. Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "mysql database primary key id is how to generate it!"

Catalogue

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

2. Automatic growth of id

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

Sql:

CREATE TABLE `test` (`id` varchar) COLLATE utf8_estonian_ci NOT NULL COMMENT 'only do not repeat', `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 (255i) COLLATE utf8_estonian_ci DEFAULT NULL, `username` varchar (255i) COLLATE utf8_estonian_ci DEFAULT NULL, `password` varchar (255i) COLLATE utf8_estonian_ci DEFAULT NULL, `classes` varchar (255i) 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

Table:

Insert statement:

INSERT INTO test (id,sex,name,username,password,classes,major,QQ,introducemyself) VALUE (replace (uuid (),'-','), 1, "Xiaomi", "xck", "001", "Class 8", 265, 953190259, "I am the best")

It was executed twice and two different id were generated:

2. Automatic growth of id

Change the type to integer, and select automatic growth below.

Look at DDL:

CREATE TABLE `test` (`id` bigint) NOT NULL AUTO_INCREMENT COMMENT 'only do not repeat', `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 (255th) 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, "Xiaomi", "xck", "001", "Class 8", 265, 953, 190259, "I am the best")

Corresponding database changes:

At this point, I believe that everyone on the "mysql database primary key id is how to generate" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report