In the development process, we usually use auto-increment numbers as the id primary key for data tables, and id is a numeric type, which is not easy to understand. After converting IDs into numbers in a certain format, it is easy to know what the IDs represent.
In the development process, we usually use auto-increment numbers as the id primary key for data tables, and id is a numeric type, which is not easy to understand. After converting IDs into numbers in a certain format, it is easy to know what the IDs represent.
For example, order table id = 20160111197681234, only look at the id we do not know this id is the order table id, and converted to the number O-20160111197681234, it is easy to see that is the order table records, then, you can search the order table by id.
Rule created by ID
1. unique
Use auto-incremental id generation to ensure uniqueness
2. as short as possible
You can use numbers to obtain the remainder of the corresponding letter to create a shorter number.
Algorithm principle
1. add a custom prefix for identification
2. the format is composed of a prefix, letters, and numbers. only N digits are retained. if the number exceeds the limit, a letter is used to calculate the remainder.
For example:
Id = 1
Prefix = F
Number reserved 3 bits
The created number is: F-A-001
The code is as follows:
IDCode. class. php
Demo. php
';}?>
Output:
1 = F-A-0019 = F-A-00910 = F-A-01099 = F-A-099100 = F-A-100999 = F-A-9991000 = F-B-0001009 = F-B-0092099 = F-C-0993999 = F-D-9999999 = F-J-99914999 = F-O-99999999 = F-VD-999
Source code: Click to view
In the development process, we usually use auto-increment numbers as the id primary key for data tables, and id is a numeric type, which is not easy to understand. After converting IDs into numbers in a certain format, it is easy to know what the IDs represent.
For example, order table id = 20160111197681234, only look at the id we do not know this id is the order table id, and converted to the number O-20160111197681234, it is easy to see that is the order table records, then, you can search the order table by id.
Rule created by ID
1. unique
Use auto-incremental id generation to ensure uniqueness
2. as short as possible
You can use numbers to obtain the remainder of the corresponding letter to create a shorter number.
Algorithm principle
1. add a custom prefix for identification
2. the format is composed of a prefix, letters, and numbers. only N digits are retained. if the number exceeds the limit, a letter is used to calculate the remainder.
For example:
Id = 1
Prefix = F
Number reserved 3 bits
The created number is: F-A-001
The code is as follows:
IDCode. class. php
Demo. php
';}?>
Output:
1 = F-A-0019 = F-A-00910 = F-A-01099 = F-A-099100 = F-A-100999 = F-A-9991000 = F-B-0001009 = F-B-0092099 = F-C-0993999 = F-D-9999999 = F-J-99914999 = F-O-99999999 = F-VD-999
The above is the content of the unique id class created by php based on the auto-increment id. For more information, see PHP Chinese network (www.php1.cn )!