Get the App
SLTechnology News&Howtos  ›  Database  › 

How to use mysql to generate unique serial number

Shulou Source: shulou.com Published: 2022-06-01 21:35:35 09月18日 Update

How to use mysql to generate unique serial numbers? In cases where the database is divided into tables or the program itself needs a unique id, we need a scheme to generate a unique id.

You can write a program that combines time and some features to generate a unique id, or you can consider using the self-adding id feature in the database to achieve this requirement. Here is an example of mysql.

First, create a table that specifically generates id, where the id field is the primary key and the replace_key field is the unique key.

CREATE TABLE `ticket` (`id` bigint (20) unsigned NOT NULL auto_increment, `replace_ key` char (1) NOT NULL default'', PRIMARY KEY (`id`), UNIQUE KEY `replace_ key` (`replace_ key`) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=10001

Each time you need to generate an id, use the replace into statement to generate a new record to replace the old record, and then return to this id.

REPLACE INTO `ticket` (`ticket`) VALUES ('a'); SELECT LAST_INSERT_ID ()

The above is a brief introduction to how to use mysql to generate a unique serial number, of course, the detailed use of the above differences have to be used by everyone to understand. If you want to know more, welcome to follow the industry information channel!

Tags: Generation serial number field data database program different abbreviated example sub-table that is situation scheme time more features features industries statements information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Microsoft OPPO Reno Huawei NVidia