How to manually obtain the auto-increment primary key in MySql

coldplay.xixi
Release: 2020-10-13 15:07:37
Original
2154 people have browsed it

Mysql method to manually obtain the auto-increment primary key: Maintain a sequences table to obtain data by creating a stored procedure and adding a function. The code is [last_number = last_number increment_by].

How to manually obtain the auto-increment primary key in MySql

Mysql method to manually obtain the auto-increment primary key:

  • By creating a stored procedure plus Maintain a sequence table to obtain data in a functional way (solve it once and use it for life).

  • You can specify the size of each increment and the initial number.

select nextval('TESTDATA') AS batchId CREATE DEFINER=`admin`@`%` FUNCTION `nextval`(seq_name VARCHAR (50)) RETURNS bigint(20) BEGIN UPDATE SEQUENCES SET last_number = last_number + increment_by WHERE sequence_name = seq_name; RETURN currval (seq_name); END;
Copy after login
CREATE DEFINER=`admin`@`%` FUNCTION `currval`(seq_name VARCHAR (50)) RETURNS bigint(20) NO SQL BEGIN SELECT last_number INTO @VALUE FROM SEQUENCES WHERE sequence_name = seq_name; RETURN @VALUE; END;
Copy after login
DROP TABLE IF EXISTS `sequences`; CREATE TABLE `sequences` ( `sequence_owner` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL, `sequence_name` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL, `min_value` bigint(20) NULL DEFAULT 1, `max_value` bigint(20) NULL DEFAULT NULL, `increment_by` bigint(20) NOT NULL DEFAULT 1, `cycle_flag` varchar(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, `order_flag` varchar(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL, `cache_size` bigint(20) NOT NULL, `last_number` bigint(20) NOT NULL, PRIMARY KEY (`sequence_name`) USING BTREE, UNIQUE INDEX `sel`(`sequence_owner`, `sequence_name`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = Compact; -- ---------------------------- -- Records of sequences -- ---------------------------- INSERT INTO `sequences` VALUES ('SCM', 'TESTDATA', 1, 9999999999, 1, 'N', 'Y', 20, 0); INSERT INTO `sequences` VALUES ('SCM', 'SEQ', 1, 9999999999, 1, 'N', 'N', 20, 0); SET FOREIGN_KEY_CHECKS = 1;
Copy after login

More related free learning recommendations:mysql tutorial(Video)

The above is the detailed content of How to manually obtain the auto-increment primary key in MySql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!