Database primary key generation method and device, electronic equipment and storage medium
By generating a primary key generation matrix and a position matrix, and combining a pseudo-random number generator and a shuffling algorithm, the target primary key element is obtained cyclically. This solves the limitations of existing database primary key generation methods in terms of global uniqueness, security, and efficiency, and achieves efficient and secure database primary key generation.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- CHINA TELECOM CORP LTD
- Filing Date
- 2023-09-28
- Publication Date
- 2026-05-19
AI Technical Summary
Existing database primary key generation methods have limitations in terms of global uniqueness, security, and generation efficiency, and are prone to uniqueness issues and security risks, especially in distributed environments.
By generating a primary key generation matrix and a position matrix, and combining a pseudo-random number generator and a shuffling algorithm, the target primary key elements are obtained cyclically. The database primary key is generated using the ASCII character set and the target number set, ensuring the diversity and randomness of the primary key elements.
It improves the global uniqueness and security of database primary keys, reduces duplication, has high generation efficiency and reasonable primary key length, is not easy to store and index, and is suitable for distributed environments.
Smart Images

Figure CN117390018B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and in particular to a method, apparatus, electronic device, and storage medium for generating database primary keys. Background Technology
[0002] With the rapid development of computer technology in the information age, computers have become an indispensable part of people's lives. However, with the rapid growth of the user base, the problem of primary key generation in database system applications has become more severe.
[0003] A database primary key is a unique identifier for each record in a database table. Traditional primary key generation methods include UUID (Universally Unique Identifier), the snowflake algorithm, and auto-incrementing primary keys. UUID is a universally unique identifier, but it is long (a string of 32 hexadecimal digits (128 bits), making it difficult to store and index, and its generation efficiency is low due to its length. The snowflake algorithm can generate globally unique and incrementing IDs, but it may have synchronization problems in multi-system, distributed environments. Auto-incrementing primary keys are easy to implement, but they have uniqueness issues in distributed environments, and primary key values are easily predictable, posing security risks. Currently, these primary key generation methods all have their own limitations, such as lack of global uniqueness, low security, and low generation efficiency. Summary of the Invention
[0004] This application provides a method, apparatus, electronic device, and storage medium for generating database primary keys, which helps to improve the efficiency of primary key generation and enhance global uniqueness and security.
[0005] To address the aforementioned problems, in a first aspect, embodiments of this application provide a method for generating a database primary key, comprising:
[0006] A primary key generation matrix is generated based on the target character set and the target number set. The target character set includes characters from the ASCII character set, and the primary key elements in the primary key generation matrix are unique.
[0007] A position matrix is generated based on the number of primary key elements in the primary key generation matrix, and the position elements in the position matrix represent the positions of the primary key elements in the primary key generation matrix.
[0008] Based on the random seed information, a pseudo-random number generator generates random integers within a target integer range, wherein the number of integers within the target integer range is the same as the number of position elements in the position matrix;
[0009] Based on the random integer, obtain the position element in the position matrix corresponding to the random integer, and use it as the target position element;
[0010] Determine the target position represented by the target position element, and obtain the target primary key element corresponding to the target position from the primary key generation matrix;
[0011] The operation of generating the position matrix and obtaining the target primary key element is performed repeatedly until a character sequence that meets the target condition is obtained. The character sequence is then used as the database primary key, and the character sequence includes the target primary key element obtained in each iteration.
[0012] Secondly, embodiments of this application provide a database primary key generation apparatus, comprising:
[0013] The primary key matrix generation module is used to generate a primary key generation matrix based on a target character set and a target number set. The target character set includes characters from the ASCII character set, and the primary key elements in the primary key generation matrix are mutually exclusive.
[0014] The position matrix generation module is used to generate a position matrix based on the number of primary key elements in the primary key generation matrix, wherein the position elements in the position matrix represent the positions of the primary key elements in the primary key generation matrix.
[0015] The random integer generation module is used to generate random integers within a target integer range using a pseudo-random number generator based on random seed information. The number of integers within the target integer range is the same as the number of position elements in the position matrix.
[0016] The target position element acquisition module is used to acquire the position element in the position matrix corresponding to the random integer based on the random integer, and use it as the target position element;
[0017] The target primary key element acquisition module is used to determine the target position represented by the target position element and to obtain the target primary key element corresponding to the target position from the primary key generation matrix;
[0018] The loop control module is used to repeatedly execute the above operations from generating the position matrix to obtaining the target primary key element until a character sequence that meets the target condition is obtained, and to use the character sequence as the database primary key, wherein the character sequence includes the target primary key element obtained in each loop.
[0019] Thirdly, embodiments of this application also provide an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the database primary key generation method described in embodiments of this application.
[0020] Fourthly, embodiments of this application provide a computer-readable storage medium storing a computer program thereon, which, when executed by a processor, discloses a method for generating database primary keys.
[0021] The database primary key generation method, apparatus, electronic device, and storage medium provided in this application embodiment generate a primary key generation matrix based on a target character set and a target number set. A position matrix is generated based on the number of primary key elements in the primary key generation matrix. A random integer within the target integer range is generated using a pseudo-random number generator based on random seed information. The target position element corresponding to the random integer in the position matrix is obtained based on the random integer. The target position represented by the target position element is determined, and the target primary key element corresponding to the target position is obtained from the primary key generation matrix. The target character set is composed of characters from the ASCII character set, and combined with the target number set to generate a primary key generation matrix containing a sufficient number of primary key elements. Each time a primary key element is retrieved from the primary key generation matrix, a random integer is generated, and the target position element in the position matrix is obtained based on the random integer. Then, the target primary key element corresponding to the target position element is retrieved from the primary key generation matrix. This process is repeated multiple times to retrieve the target primary key element, which is then used as the database primary key. By combining the diversity of primary key elements, the randomness of random integers, and the multiple iterations of retrieving primary key elements to form the database primary key, the complexity and randomness of the database primary key are improved. This avoids duplicate database primary keys, improves global uniqueness and security, and the length of the generated database primary key (e.g., 16 characters) does not need to be too long, thus improving generation efficiency. Attached Figure Description
[0022] To more clearly illustrate the technical solutions of the embodiments of this application, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0023] Figure 1 This is a flowchart of a database primary key generation method provided in an embodiment of this application;
[0024] Figure 2 This is an example diagram of the primary key generation matrix in an embodiment of this application;
[0025] Figure 3 This is a flowchart of a database primary key generation method provided in an embodiment of this application;
[0026] Figure 4This is an example diagram of the initial position matrix in an embodiment of this application;
[0027] Figure 5 This is an example diagram showing the rearrangement of the initial position matrix using a shuffling algorithm, as described in this application embodiment.
[0028] Figure 6 This is a schematic diagram of the structure of a database primary key generation device provided in an embodiment of this application;
[0029] Figure 7 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Detailed Implementation
[0030] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0031] Figure 1 This is a flowchart illustrating a method for generating a database primary key according to an embodiment of this application. This method can be executed by an electronic device such as a computer. Figure 1 As shown, the method includes steps 110 to 170.
[0032] Step 110: Generate a primary key generation matrix based on the target character set and the target number set. The target character set includes characters from the ASCII character set, and the primary key elements in the primary key generation matrix are unique.
[0033] The target character set contains characters selected from the ASCII character set. This target character set can include uppercase letters, lowercase letters, and the numbers 1 through 9, as well as the number 0, and other symbol characters. The target number set contains integers, and the numbers in the target number set are unique to the characters in the target character set; that is, the target number set does not include the numbers included in the target character set. This ensures that the primary key elements in the generated primary key matrix are all unique.
[0034] To generate database primary keys with high complexity and randomness, this embodiment selects the ASCII character set as the base character set for the primary keys. The ASCII character set contains 62 printable characters, including letters (uppercase and lowercase) and numbers (0-9). These characters offer good readability and compatibility, and their diversity provides sufficient complexity for primary key generation. Printable characters are selected from the ASCII character set to form the target character set. To generate a primary key generation matrix of a preset size, additional numbers can be added, forming the target number set.
[0035] Each primary key is generated by using either a character from the target character set or a number from the target number set as a primary key element in the primary key generation matrix. These primary key elements are then filled into blank matrices of a preset size. The number of primary key elements in the primary key generation matrix is the sum of the number of characters in the target character set and the number of numbers in the target number set.
[0036] For example, 61 printable characters from the ASCII character set, including letters (uppercase and lowercase) and numbers (1-9), can be used as the target character set. The default size of the primary key generation matrix is 10×10. In order to generate a 10×10 matrix, 39 additional numbers (e.g., 10-48) can be added. These numbers form the target number set. The 61 characters from the ASCII character set and the 39 additional numbers are used to construct a 10×10 two-dimensional character matrix, which serves as the primary key generation matrix. Figure 2 This is an example diagram of the primary key generation matrix in the embodiments of this application, such as... Figure 2 As shown, each character (including numbers) has a corresponding position (row number and column number) in the primary key generation matrix.
[0037] Step 120: Generate a position matrix based on the number of primary key elements in the primary key generation matrix, wherein the position elements in the position matrix represent the positions of the primary key elements in the primary key generation matrix.
[0038] The number of position elements in the position matrix is the same as the number of primary key elements in the primary key generation matrix, and each position element in the position matrix represents the position of a primary key element in the primary key generation matrix.
[0039] Different symbols can be used to represent the position of each primary key element in the primary key generation matrix. These symbols are then arranged and filled into a matrix of the same size as the primary key generation matrix to obtain a position matrix. For example, the position elements in the position matrix can be consecutive integers. For instance, when the primary key generation matrix is a 10×10 matrix, the numbers 1 to 100 can be used to represent the position of each primary key element in the primary key generation matrix.
[0040] In one embodiment of this application, each position element in the position matrix is a consecutive integer;
[0041] The step of generating a position matrix based on the number of primary key elements in the primary key generation matrix includes: rearranging the consecutive integers using a shuffling algorithm, and generating a position matrix including the consecutive integers according to the rearranged order.
[0042] The shuffling algorithm may include the Fisher-Yates shuffling algorithm. Of course, in addition to the Fisher-Yates shuffling algorithm, the shuffling algorithm may also employ any other algorithm that can rearrange the cards, such as the Knuth-Durstenfeld shuffling algorithm, the Inside-Out Algorithm, etc.
[0043] In the position matrix, each element is a consecutive integer. For example, in a 10×10 primary key generation matrix, the consecutive integers can be from 1 to 100. To improve the global uniqueness of the generated primary key, each time a primary key is generated, characters can be taken from the host generation matrix multiple times to form the primary key. Each time a character is taken, a random integer is mapped to the primary key generation matrix through the position matrix to obtain the primary key element (character). In this way, each time a character is taken, the consecutive integers can be arranged differently to generate different position matrices.
[0044] A shuffling algorithm can be used to shuffle and rearrange consecutive integers, and then fill the empty matrix with the integers in the rearranged order to obtain a position matrix containing consecutive integers. The shuffling algorithm can include the Fisher-Yates shuffling algorithm, which is implemented by drawing cards. The process is as follows: Initialize an original array and a new array. The original array contains consecutive integers in the order of consecutive integers. Continuously and randomly draw an integer from the unprocessed array (the array of integers remaining after removing integers from the original array) and place the drawn integer into the new array, until all integers in the unprocessed array have been drawn. The Fisher-Yates shuffling algorithm shuffles a set of integers online with equal probability, ensuring that each integer has an equal probability of appearing in each position. The algorithm is efficient, with a time complexity of O(n), where n is the number of integers.
[0045] By employing a shuffling algorithm to rearrange consecutive integers and generating a position matrix according to the rearranged order, the position matrix generated each time is different, thereby enhancing the randomness of the characters extracted each time and improving the global uniqueness of the generated database primary key.
[0046] Step 130: Based on the random seed information, generate random integers within the target integer range using a pseudo-random number generator. The number of integers within the target integer range is the same as the number of position elements in the position matrix.
[0047] Here, the random integer represents the position of a position element in the position matrix, that is, it represents the row and column index in the position matrix. The random seed information can be the current device status information, or it can be other status information outside the device, such as the current weather information.
[0048] A pseudo-random number seed value is generated based on the random seed information. Then, a pseudo-random number generator generates random integers within the target integer range based on the pseudo-random number seed value. Since the random seed information is different at different times, the pseudo-random number seed value generated at different times will also be different. This results in different random integers generated by the pseudo-random number generator at different times, thus increasing the randomness.
[0049] In one embodiment of this application, the random seed information includes at least two of the following: current time, current device status, and current weather information;
[0050] The step of generating a random integer within a target integer range using a pseudo-random number generator based on random seed information includes: generating a pseudo-random number seed value for the pseudo-random number generator based on at least two of the current time, current device status, and current weather information; and generating a random integer within a target integer range using the pseudo-random number generator based on the pseudo-random number seed value.
[0051] The random seed information is quantized by converting each piece of information in the random seed information into specific data values, namely, the current time, the current device status, and the current weather information are quantized into specific data values. At least two of the data values of the current time, the current device status, and the current weather information are concatenated, and a pseudo-random number seed value is generated based on the concatenated data value. Then, a pseudo-random number generator is called based on the pseudo-random number seed value to generate a random integer within the target integer range.
[0052] By determining the pseudo-random number seed value based on at least two of the current time, current device status, and current weather information, it is possible to ensure that the pseudo-random number seed values generated at different times are different, thereby improving the randomness of the generated random integers.
[0053] In one embodiment of this application, generating a pseudo-random number seed value based on at least two of the current time, current device status, and current weather information includes: determining a first data value corresponding to the current time, determining a second data value corresponding to the current device status, and determining a third data value corresponding to the current weather information, wherein the current device status includes at least one of the current CPU status, current memory status, and current disk space status; concatenating at least two of the first data value, the second data value, and the third data value into a target data value; and performing a hash mapping on the target data value using a hash function to obtain the pseudo-random number seed value.
[0054] The current time is quantified into a first data value. Specifically, the current year, current month, and current time point can be concatenated to obtain the first data value. For example, if the current time is 13:53:212 on July 18, 2023, the first data value is 202307181353212 by concatenating the current year 2023, the current month 7, and the current time point 13:53:212.
[0055] The current device state is quantized into a second data value. The current CPU state refers to the current CPU utilization rate. For example, if the current CPU utilization rate is 35%, the quantized value is 35. The current memory state refers to the current memory utilization rate. For example, if the memory utilization rate is 51%, the quantized value is 51. The current disk space state refers to the current disk space utilization rate. For example, if the current disk space utilization rate is 72%, the quantized value is 72. The quantized values of at least one of the current CPU state, current memory state, and current disk space state are concatenated to obtain the second data value corresponding to the current device state. For example, when the current device state includes the current CPU state, current memory state, and current disk space state, the second data value obtained after concatenating the above quantized values can be 355172.
[0056] Each type of weather information can be pre-coded into a specific integer. For example, sunny days can be coded as 1, cloudy days as 2, rainy days as 3, and so on. When the current weather information is obtained, the corresponding code can be directly retrieved from the pre-coded data to obtain the third data value corresponding to the current weather information.
[0057] At least two of the first, second, and third data values are concatenated to obtain the target data value. Since the target data value is large and has many bits, it is not suitable as a seed value for pseudo-random numbers. A hash function can be used to perform a hash mapping on the target data value, and the value obtained after mapping can be used as the seed value for pseudo-random numbers.
[0058] The hash function is used to map input data of arbitrary length to output data of fixed length. The hash function can be the MurmurHash function, a highly efficient hash function that can map input data of arbitrary length to a 32-bit or 64-bit hash value. Besides MurmurHash, other hash functions can also be used, such as FarmHash and XXhash. Using MurmurHash to hash the target data value improves the efficiency of generating pseudo-random number seed values, and the generated pseudo-random number seed values are uniformly distributed. MurmurHash provides a consistent and efficient seed generation method for pseudo-random number generators, improving their randomness and supporting the generation of the same random number sequence from the same input, making it suitable for scenarios requiring repeatable randomness.
[0059] By concatenating at least two of the first data value corresponding to the current time, the second data value corresponding to the current device status, and the third data value corresponding to the current weather information into a target data value, and mapping the target data value to a pseudo-random number seed value through a hash function, the randomness of the pseudo-random number seed value can be enhanced since the probability of at least two of the current time, current device status, and current weather information being repeated is relatively low, thereby improving the randomness of the generated random integer.
[0060] In one embodiment of this application, the pseudo-random number generator is a Mason rot pseudo-random number generator;
[0061] The step of generating a random integer within the target integer range using the pseudo-random number generator based on the pseudo-random number seed value includes: initializing a Mason slew pseudo-random number generator based on the pseudo-random number seed value; and generating a random integer within the target integer range using the Mason slew pseudo-random number generator.
[0062] The Mersenne rotation pseudorandom number generator is initialized using a pseudorandom number seed value. Then, it generates random integers within a target integer range. The `randrange()` function of the Mersenne rotation generator is used to generate random integers within the target integer range. When called, `random.randrange(start, stop[, step])`, where `start` represents the starting value of the target integer range (included within the range), `stop` represents the ending value of the target integer range (excluding the range), and `step` is the step size. After initialization, the Mersenne rotation pseudorandom number generator can be used to generate pseudorandom numbers. For example, when generating a 10×10 matrix for the primary key, the value of `start` can be 1, the value of `stop` can be 101, and the value of `step` can be 1.
[0063] Random integers are generated by employing a Mason slew pseudorandom number generator. This is because the Mason slew pseudorandom number generator has a long period (typically 2^36). 19937-1 The characteristics of high speed and uniform distribution over a large range can avoid the repetition of generated random integers, and the speed of generating random integers is relatively fast. Of course, in addition to the Mason rot pseudo-random number generator, other pseudo-random number generators can also be used, such as the Linear Congruential Generator (LCG) or the Linear Feedback Shift Register (LFSR).
[0064] Step 140: Based on the random integer, obtain the position element in the position matrix corresponding to the random integer, and use it as the target position element.
[0065] The position matrix is used to map random integers to specific primary key elements in the primary key generation matrix. It is a matrix that connects the random integers and the primary key generation matrix. The random integer represents the position of the element to be selected in the position matrix. The position of the element represented by the random integer is determined, and the element at that position is obtained from the position matrix as the target element. For example, when the random integer is 13, the position of the element it represents is in the 2nd row and 3rd column.
[0066] Step 150: Determine the target position represented by the target position element, and obtain the target primary key element corresponding to the target position from the primary key generation matrix.
[0067] The target position element represents the target position in the primary key generation matrix. Based on the mapping relationship between the position element and the position it represents in the corner generation matrix, the target position represented by the target position element is determined, and the target primary key element corresponding to the target position is obtained from the primary key generation matrix. For example, when the target position element is 46, the target position it represents is in the 5th row and 6th column.
[0068] Step 160: Determine whether a character sequence that satisfies the target condition has been obtained. If yes, proceed to step 170; otherwise, proceed to step 120.
[0069] The target primary key element obtained in the current iteration is concatenated with the target primary key element obtained in the previous iteration to obtain a character sequence. After this iteration, it is determined whether a character sequence satisfying the target condition is obtained. The target condition may include either reaching a target number of iterations or the character sequence length reaching a target length. Reaching the target number of iterations (e.g., 16 iterations) is relatively easy to control, and controlling the number of iterations can prevent the generated character sequence length from becoming too large, improving the efficiency of database primary key generation. Reaching the target length of the character sequence (e.g., 16 characters) makes it easier to control the length of the generated database primary key. However, since the lengths of primary key elements in the primary key generation matrix may vary, it is not easy to control the number of iterations. For example, if only one character remains in the last iteration, but the primary key element determined by the previous iteration has two characters, it does not meet the requirements, and the iteration needs to be repeated.
[0070] Step 170: Use the character sequence as the database primary key, whereby the character sequence includes the target primary key element obtained in each loop.
[0071] Use the final character sequence as the database primary key.
[0072] The database primary key generation method provided in this application embodiment generates a primary key generation matrix based on a target character set and a target number set. It then generates a position matrix based on the number of primary key elements in the primary key generation matrix. A random integer within the target integer range is generated using a pseudo-random number generator based on random seed information. The target position element corresponding to the random integer in the position matrix is obtained based on the random integer. The target position represented by the target position element is determined, and the target primary key element corresponding to the target position is obtained from the primary key generation matrix. The target character set is composed of characters from the ASCII character set, and combined with the target number set to generate a primary key generation matrix containing a sufficient number of primary key elements. Furthermore, each generation process involves... When retrieving the primary key element from the matrix, a random integer is generated and used to obtain the target position element in the position matrix. Then, the target primary key element corresponding to the target position element is obtained from the primary key generation matrix. This process is repeated multiple times to obtain the target primary key element, which is then used as the database primary key. By combining the diversity of primary key elements, the randomness of random integers, and the multiple iterations of obtaining primary key elements, the complexity and randomness of the database primary key are improved. This avoids duplicate primary keys, enhances global uniqueness and security, and the length of the generated database primary key (e.g., 16 characters) does not need to be too long, thus improving generation efficiency.
[0073] Figure 3 This is a flowchart illustrating a method for generating a database primary key according to an embodiment of this application. This method can be executed by an electronic device such as a computer. This embodiment uses a 10×10 matrix as an example for illustration. Figure 3 As shown, the method includes steps 310 to 370.
[0074] Step 310: Generate a primary key generation matrix based on the target character set and the target number set. The target character set includes uppercase letters, lowercase letters, and numbers 1 to 9. The target number set includes numbers 10 to 48.
[0075] The 61 characters in the ASCII character set and the 39 supplementary numbers are used to construct a 10×10 two-dimensional character matrix, which is then used as the primary key generation matrix. Figure 2 This is an example diagram of the primary key generation matrix in the embodiments of this application, such as... Figure 2 As shown, each character (including numbers) has a corresponding position (row number and column number) in the primary key generation matrix.
[0076] Step 320: Generate a position matrix based on the number of primary key elements in the primary key generation matrix. Each position element in the position matrix is a number from 1 to 100.
[0077] Since a Mason swirl pseudorandom number generator is subsequently used to generate random integers from 1 to 100, a 10×10 position matrix can be created to map these random integers to each character in the primary key generation matrix. In this position matrix, each cell contains a character corresponding to its row and column index (i.e., a number from 1 to 100). Therefore, when the random number generator generates a number, the corresponding character can be directly found in this position matrix.
[0078] Figure 4 This is an example diagram of the initial position matrix in an embodiment of this application, such as... Figure 4 As shown, the position matrix is a 10×10 matrix, and the number of position elements is the same as the number of primary key elements in the primary key generation matrix. Each position element in the position matrix is a number from 1 to 100. In the initial position matrix, each position element is arranged in the order of the numbers from 1 to 100.
[0079] Figure 5 This is an example diagram showing the rearrangement of the initial position matrix using a shuffling algorithm, as described in this application embodiment. The Fisher-Yates shuffling algorithm can be used to rearrange the original... Figure 4 The values from 1 to 100 are shuffled and rearranged to generate a new 10×10 position matrix, as shown below. Figure 5 As shown.
[0080] Step 330: Based on the random seed information, generate a random integer within the target integer range using a pseudo-random number generator.
[0081] A pseudo-random number generator is a crucial part of the database primary key generation process. First, the random seed information—current time, current CPU state, current memory state, current disk space state, and current weather information—needs to be converted into quantifiable data values. This quantization can be achieved using the following methods: Obtain the current time, for example, July 18, 2023, 13:53:212, and quantize it to obtain an initial integer of 202307181353212; obtain the current CPU state (current CPU utilization), assuming a utilization rate of 35%, the quantized value will be 35; obtain the current memory state (current memory utilization), assuming a utilization rate of 51%, the quantized value will be 51; obtain the current weather information, and based on the numerical encoding corresponding to the weather information, obtain the quantized value. For example, each type of weather information can be pre-converted into a specific integer, such as sunny day being encoded as 1, cloudy day as 2, rainy day as 3, and so on.
[0082] After further quantizing the current time, current CPU state, current memory state, current disk space state, and current weather information, the quantized values are concatenated to obtain the target data value. The MurmurHash function can be used to map the target data value to a pseudo-random number seed value. Then, the pseudo-random number seed value is used to initialize the Mason swirl pseudo-random number generator. The randrange() function of the Mason swirl pseudo-random number generator is used to generate random integers within the target integer range (1-100), random.randrange(start,stop[,step]), where start is the starting value of the random integer (1 here), stop is the ending value of the random number (not included in the range, 101 here), and step is the step size (default is 1). After the Mason swirl pseudo-random number generator is initialized, it can be used to generate random integers.
[0083] Step 340: Based on the random integer, obtain the position element in the position matrix corresponding to the random integer, and use it as the target position element.
[0084] Retrieve the target position element corresponding to the random integer position in the position matrix. For example, if the random integer is 13, then the position corresponding to 13 in the position matrix is the second row and third column. Retrieve the target position element 46 corresponding to the second row and third column in the position matrix.
[0085] Step 350: Determine the target position represented by the target position element, and obtain the target primary key element corresponding to the target position from the primary key generation matrix.
[0086] If the target position element in the position matrix is 46, then the target position in the primary key generation matrix represented by this target position element is the 5th row and 6th column. Obtain the target primary key element 7 corresponding to the 5th row and 6th column from the primary key generation matrix.
[0087] Step 360: Determine whether a character sequence that meets the target condition has been obtained. If yes, proceed to step 370; otherwise, proceed to step 320.
[0088] Step 370: Use the character sequence as the database primary key, wherein the character sequence includes the target primary key element obtained in each loop.
[0089] This application uses the ASCII character set as the basis for primary key generation, and combines position matrix, MurmurHash function, Mason rotation algorithm, and Fisher-Yates shuffle algorithm to generate primary keys. This method not only improves the security of database primary keys, but also expands the availability of primary keys and reduces the risk of primary key exhaustion in big data environments.
[0090] Figure 6 This is a schematic diagram of the structure of a database primary key generation device provided in an embodiment of this application, as shown below. Figure 6 As shown, the device includes:
[0091] The primary key matrix generation module 610 is used to generate a primary key generation matrix based on a target character set and a target number set. The target character set includes characters from the ASCII character set, and the primary key elements in the primary key generation matrix are mutually exclusive.
[0092] The position matrix generation module 620 is used to generate a position matrix based on the number of primary key elements in the primary key generation matrix, wherein the position elements in the position matrix represent the positions of the primary key elements in the primary key generation matrix.
[0093] The random integer generation module 630 is used to generate random integers within a target integer range using a pseudo-random number generator based on random seed information, wherein the number of integers within the target integer range is the same as the number of position elements in the position matrix;
[0094] The target position element acquisition module 640 is used to acquire the position element in the position matrix corresponding to the random integer according to the random integer, and use it as the target position element;
[0095] The target primary key element acquisition module 650 is used to determine the target position represented by the target position element and to obtain the target primary key element corresponding to the target position from the primary key generation matrix;
[0096] The loop control module 660 is used to repeatedly execute the above-mentioned operations of generating the position matrix to obtaining the target primary key element until a character sequence that meets the target condition is obtained, and to use the character sequence as the database primary key, wherein the character sequence includes the target primary key element obtained in each loop.
[0097] Optionally, the random seed information includes at least two of the following: current time, current device status, and current weather information;
[0098] The random integer generation module includes:
[0099] The seed value generation unit is used to generate a pseudo-random number seed value based on at least two of the following: current time, current device status, and current weather information.
[0100] A random integer generation unit is used to generate random integers within a target integer range using the pseudo-random number generator based on the pseudo-random number seed value.
[0101] Optionally, the seed value generation unit is specifically used for:
[0102] A first data value corresponding to the current time is determined, a second data value corresponding to the current device status is determined, and a third data value corresponding to the current weather information is determined. The current device status includes at least one of the current CPU status, current memory status, and current disk space status.
[0103] At least two of the first data value, the second data value, and the third data value are concatenated to form the target data value;
[0104] The target data value is hashed using a hash function to obtain the pseudo-random number seed value.
[0105] Optionally, the hash function is the MurmurHash function.
[0106] Optionally, the pseudo-random number generator is a Mason swirl pseudo-random number generator;
[0107] The random integer generation unit is specifically used for:
[0108] Initialize the Mason rotation pseudo-random number generator based on the pseudo-random number seed value;
[0109] The Mason rot pseudo-random number generator generates random integers within the target integer range.
[0110] Optionally, each position element in the position matrix is a consecutive integer;
[0111] The position matrix generation module is specifically used for:
[0112] The consecutive integers are rearranged using a shuffling algorithm, and a position matrix including the consecutive integers is generated according to the rearranged order.
[0113] Optionally, the shuffling algorithm includes the Fisher-Yates shuffling algorithm.
[0114] Optionally, the target character set includes uppercase letters, lowercase letters, and the numbers 1 to 9.
[0115] Optionally, the target condition includes either reaching a target number of iterations or reaching a target length for the character sequence.
[0116] The database primary key generation apparatus provided in this application embodiment is used to implement the steps of the database primary key generation method described in this application embodiment. The specific implementation of each module of the apparatus is described in the corresponding steps, and will not be repeated here.
[0117] The database primary key generation apparatus provided in this application generates a primary key generation matrix based on a target character set and a target number set. It then generates a position matrix based on the number of primary key elements in the primary key generation matrix. A random integer within the target integer range is generated using a pseudo-random number generator based on random seed information. The target position element corresponding to the random integer in the position matrix is obtained based on the random integer. The target position represented by the target position element is determined, and the target primary key element corresponding to the target position is obtained from the primary key generation matrix. The target character set is composed of characters from the ASCII character set, and combined with the target number set to generate a primary key generation matrix containing a sufficient number of primary key elements. Furthermore, each time the primary key is generated... When retrieving the primary key element from the matrix, a random integer is generated and used to obtain the target position element in the position matrix. Then, the target primary key element corresponding to the target position element is obtained from the primary key generation matrix. This process is repeated multiple times to obtain the target primary key element, which is then used as the database primary key. By combining the diversity of primary key elements, the randomness of random integers, and the multiple iterations of obtaining primary key elements, the complexity and randomness of the database primary key are improved. This avoids duplicate primary keys, enhances global uniqueness and security, and the length of the generated database primary key (e.g., 16 characters) does not need to be too long, thus improving generation efficiency.
[0118] Figure 7 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application, such as... Figure 7 As shown, the electronic device 700 may include one or more processors 710 and one or more memories 720 connected to the processors 710. The electronic device 700 may also include an input interface 730 and an output interface 740 for communicating with another device or system. Program code executed by the processor 710 may be stored in the memory 720.
[0119] The processor 710 in the electronic device 700 calls the program code stored in the memory 720 to execute the database primary key generation method in the above embodiment.
[0120] This application also provides a computer-readable storage medium storing a computer program thereon, which, when executed by a processor, implements the database primary key generation method as described in this application.
[0121] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on its differences from other embodiments. Similar or identical parts between embodiments can be referred to interchangeably. For the apparatus embodiments, since they are fundamentally similar to the method embodiments, the description is relatively simple; relevant parts can be referred to the descriptions in the method embodiments.
[0122] The foregoing has provided a detailed description of a database primary key generation method, apparatus, electronic device, and storage medium provided in the embodiments of this application. Specific examples have been used to illustrate the principles and implementation methods of this application. The descriptions of the above embodiments are only for the purpose of helping to understand the method and core ideas of this application. At the same time, for those skilled in the art, there will be changes in the specific implementation methods and application scope based on the ideas of this application. Therefore, the content of this specification should not be construed as a limitation of this application.
[0123] Through the above description of the embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus necessary general-purpose hardware platforms, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solutions, in essence or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute the methods described in the various embodiments or some parts of the embodiments.
Claims
1. A method for generating a database primary key, characterized in that, include: A primary key generation matrix is generated based on the target character set and the target number set. The target character set includes characters from the ASCII character set, and the primary key elements in the primary key generation matrix are unique. A position matrix is generated based on the number of primary key elements in the primary key generation matrix, and the position elements in the position matrix represent the positions of the primary key elements in the primary key generation matrix. Based on the random seed information, a pseudo-random number generator generates random integers within a target integer range, wherein the number of integers within the target integer range is the same as the number of position elements in the position matrix; Based on the random integer, obtain the position element in the position matrix corresponding to the random integer, and use it as the target position element; Determine the target position represented by the target position element, and obtain the target primary key element corresponding to the target position from the primary key generation matrix; The operation of generating the position matrix and obtaining the target primary key element is performed repeatedly until a character sequence that meets the target condition is obtained. The character sequence is then used as the database primary key, and the character sequence includes the target primary key element obtained in each iteration.
2. The method according to claim 1, characterized in that, The random seed information includes at least two of the following: current time, current device status, and current weather information; The step of generating a random integer within the target integer range using a pseudo-random number generator based on random seed information includes: Generate a pseudo-random number seed value based on at least two of the following: current time, current device status, and current weather information; Based on the pseudo-random number seed value, a random integer within the target integer range is generated by the pseudo-random number generator.
3. The method according to claim 2, characterized in that, The step of generating a pseudo-random number seed value based on at least two of the following: current time, current device status, and current weather information, includes: A first data value corresponding to the current time is determined, a second data value corresponding to the current device status is determined, and a third data value corresponding to the current weather information is determined. The current device status includes at least one of the current CPU status, current memory status, and current disk space status. At least two of the first data value, the second data value, and the third data value are concatenated to form the target data value; The target data value is hashed using a hash function to obtain the pseudo-random number seed value.
4. The method according to claim 3, characterized in that, The hash function is the MurmurHash function.
5. The method according to claim 2, characterized in that, The pseudo-random number generator is a Mason rotation pseudo-random number generator. The step of generating a random integer within a target integer range using the pseudo-random number generator based on the pseudo-random number seed value includes: Initialize the Mason rotation pseudo-random number generator based on the pseudo-random number seed value; The Mason rot pseudo-random number generator generates random integers within the target integer range.
6. The method according to any one of claims 1-5, characterized in that, Each element in the position matrix is a consecutive integer; The step of generating a position matrix based on the number of primary key elements in the primary key generation matrix includes: The consecutive integers are rearranged using a shuffling algorithm, and a position matrix including the consecutive integers is generated according to the rearranged order.
7. The method according to claim 6, characterized in that, The shuffling algorithm includes the Fisher-Yates shuffling algorithm.
8. The method according to any one of claims 1-5, characterized in that, The target character set includes uppercase letters, lowercase letters, and the numbers 1 to 9.
9. The method according to any one of claims 1-5, characterized in that, The target conditions include either reaching the target number of iterations or reaching the target length of the character sequence.
10. A database primary key generation apparatus, characterized in that, include: The primary key matrix generation module is used to generate a primary key generation matrix based on a target character set and a target number set. The target character set includes characters from the ASCII character set, and the primary key elements in the primary key generation matrix are mutually exclusive. The position matrix generation module is used to generate a position matrix based on the number of primary key elements in the primary key generation matrix, wherein the position elements in the position matrix represent the positions of the primary key elements in the primary key generation matrix. The random integer generation module is used to generate random integers within a target integer range using a pseudo-random number generator based on random seed information. The number of integers within the target integer range is the same as the number of position elements in the position matrix. The target position element acquisition module is used to acquire the position element in the position matrix corresponding to the random integer based on the random integer, and use it as the target position element; The target primary key element acquisition module is used to determine the target position represented by the target position element and to obtain the target primary key element corresponding to the target position from the primary key generation matrix; The loop control module is used to repeatedly execute the above operations from generating the position matrix to obtaining the target primary key element until a character sequence that meets the target condition is obtained, and to use the character sequence as the database primary key, wherein the character sequence includes the target primary key element obtained in each loop.
11. An electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the database primary key generation method according to any one of claims 1 to 9.
12. A computer-readable storage medium having a computer program stored thereon, characterized in that, When executed by a processor, the program implements the database primary key generation method according to any one of claims 1 to 9.