How is the primary key id of mysql database generated
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!