[go: up one dir, main page]

CN117390018A - Database primary key generation method and device, electronic equipment and storage medium - Google Patents

Database primary key generation method and device, electronic equipment and storage medium Download PDF

Info

Publication number
CN117390018A
CN117390018A CN202311278557.7A CN202311278557A CN117390018A CN 117390018 A CN117390018 A CN 117390018A CN 202311278557 A CN202311278557 A CN 202311278557A CN 117390018 A CN117390018 A CN 117390018A
Authority
CN
China
Prior art keywords
target
primary key
matrix
random
generating
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202311278557.7A
Other languages
Chinese (zh)
Inventor
刘维
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
China Telecom Corp Ltd
Original Assignee
China Telecom Corp Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by China Telecom Corp Ltd filed Critical China Telecom Corp Ltd
Priority to CN202311278557.7A priority Critical patent/CN117390018A/en
Publication of CN117390018A publication Critical patent/CN117390018A/en
Pending legal-status Critical Current

Links

Classifications

    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/22Indexing; Data structures therefor; Storage structures
    • G06F16/2282Tablespace storage structures; Management thereof
    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/22Indexing; Data structures therefor; Storage structures
    • G06F16/2228Indexing structures
    • G06F16/2272Management thereof

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Software Systems (AREA)
  • Data Mining & Analysis (AREA)
  • Databases & Information Systems (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Input From Keyboards Or The Like (AREA)

Abstract

The embodiment of the application discloses a method and a device for generating a database primary key, electronic equipment and a storage medium, wherein the method comprises the following steps: generating a primary key generation matrix according to a target character set and a target number set, wherein the target character set comprises characters in an ASCII character set; generating a position matrix according to the number of the primary key elements in the primary key generation matrix; generating a random integer in a target integer range through a pseudo-random number generator according to the random seed information; acquiring a target position element corresponding to a random integer in a position matrix; determining a target position represented by a target position element, and acquiring a target main key element corresponding to the target position from a main key generation matrix; and circularly executing the operation from generating the position matrix to acquiring the target main key element until a character sequence meeting the target condition is obtained, taking the character sequence as a database main key, wherein the character sequence comprises the target main key element. According to the method and the device for generating the primary key, the primary key generation efficiency and the security can be improved.

Description

Database primary key generation method and device, electronic equipment and storage medium
Technical Field
The embodiment of the application relates to the technical field of computers, in particular to a method and a device for generating a database primary key, electronic equipment and a storage medium.
Background
With the rapid development of computer technology in the information age, computers have become an integral part of people's lives. However, with the rapid growth of user populations, the problem of primary key generation in database system applications has also become more severe.
The primary key of the database is a unique identification of each record in the database table. The traditional primary key generation method comprises UUID (Universally Unique Identifier, universal unique identification code), snowflake algorithm, database primary key self-increment and the like. UUID is a universal unique identification code, but has a longer length (a character string consisting of a group of 32 hexadecimal digits (128 bits)), is not easy to store and index, and has lower generation efficiency due to the longer length; snowflake algorithms can generate globally unique and incremental IDs, but synchronization problems can exist in multi-system, distributed environments; the main key of the database is automatically enhanced, but the uniqueness problem exists in the distributed environment, the main key value is easily predicted, and the potential safety hazard exists. At present, the primary key generation methods have respective limitations, such as lack of global uniqueness, low security, low generation efficiency and the like.
Disclosure of Invention
The embodiment of the application provides a method, a device, electronic equipment and a storage medium for generating a main key of a database, which are beneficial to improving the generation efficiency of the main key and improving the global uniqueness and safety.
In order to solve the above problem, in a first aspect, an embodiment of the present application provides a method for generating a database primary key, including:
generating a primary key generation matrix according to a target character set and a target number set, wherein the target character set comprises characters in an ASCII character set, and primary key elements in the primary key generation matrix are not repeated;
generating a position matrix according to the number of the main key elements in the main key generation matrix, wherein the position elements in the position matrix represent the positions of the main key elements in the main key generation matrix;
generating random integers in a target integer range through a pseudo-random number generator according to random seed information, wherein the number of the integers in the target integer range is the same as the number of the position elements in the position matrix;
acquiring a position element corresponding to the random integer in the position matrix according to the random integer, and taking the position element as a target position element;
determining a target position represented by the target position element, and acquiring a target primary key element corresponding to the target position from the primary key generation matrix;
And circularly executing the operations from generating the position matrix to acquiring the target main key element until a character sequence meeting target conditions is obtained, and taking the character sequence as a database main key, wherein the character sequence comprises the target main key element acquired in each cycle.
In a second aspect, an embodiment of the present application provides a device for generating a primary key of a database, including:
the primary key matrix generation module is used for generating a primary key generation matrix according to a target character set and a target number set, wherein the target character set comprises characters in an ASCII character set, and primary key elements in the primary key generation matrix are not repeated;
the position matrix generation module is used for generating a position matrix according to the number of the main key elements in the main key generation matrix, and the position elements in the position matrix represent the positions of the main key elements in the main key generation matrix;
the random integer generation module is used for generating random integers in a target integer range through a pseudo-random number generator according to random seed information, wherein the number of the integers in the target integer range is the same as the number of the position elements in the position matrix;
the target position element acquisition module is used for acquiring position elements corresponding to the random integers in the position matrix according to the random integers, and taking the position elements as target position elements;
The target primary key element acquisition module is used for determining a target position represented by the target position element and acquiring a target primary key element corresponding to the target position from the primary key generation matrix;
and the circulation control module is used for circularly executing the operations from generating the position matrix to acquiring the target main key element until a character sequence meeting the target condition is obtained, and taking the character sequence as a database main key, wherein the character sequence comprises the target main key element acquired in each circulation.
In a third aspect, an embodiment of the present application further provides an electronic device, including a memory, a processor, and a computer program stored in the memory and capable of running on the processor, where the processor implements the method for generating the database primary key according to the embodiment of the present application when executing the computer program.
In a fourth aspect, embodiments of the present application provide a computer-readable storage medium having stored thereon a computer program which, when executed by a processor, performs a method of generating a database primary key as disclosed in embodiments of the present application.
The method, the device, the electronic equipment and the storage medium for generating the database primary key provided by the embodiment of the application generate a primary key generation matrix according to a target character set and a target number set, generate a position matrix according to the number of primary key elements in the primary key generation matrix, generate random integers in a target integer range according to random seed information through a pseudo-random number generator, acquire target position elements corresponding to the random integers in the position matrix according to the random integers, determine the target positions represented by the target position elements, acquire the target primary key elements corresponding to the target positions from the primary key generation matrix, adopt characters in ASCII character sets to form a target character set, combine the target number set to generate the primary key generation matrix comprising enough primary key elements, and each time the primary key element is taken from the primary key generation matrix, the target position element in the position matrix is obtained by generating a random integer and based on the random integer, further the target primary key element corresponding to the target position element is obtained from the primary key generation matrix, the target primary key element is circularly and repeatedly obtained, the repeatedly obtained target primary key element is taken as the database primary key, the database primary key is formed by combining the diversity of the primary key element, the randomness of the random integer and the repeatedly obtained primary key element, the complexity and the randomness of the database primary key are improved, the repetition of the database primary key can be avoided, the global uniqueness and the safety are improved, and the length (for example, 16 bits) of the generated database primary key can be not needed to be too long, so that the generation efficiency can be improved.
Drawings
In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings that are required for the embodiments or the description of the prior art will be briefly described below, it being obvious that the drawings in the following description are only some embodiments of the present application, and that other drawings may be obtained according to these drawings without inventive effort for a person skilled in the art.
FIG. 1 is a flowchart of a method for generating a database primary key according to an embodiment of the present application;
FIG. 2 is an exemplary diagram of a primary key generation matrix in an embodiment of the present application;
FIG. 3 is a flowchart of a method for generating a database primary key according to an embodiment of the present application;
FIG. 4 is an exemplary diagram of an initial position matrix in an embodiment of the present application;
FIG. 5 is an exemplary diagram of an embodiment of the present application after rearranging an initial position matrix using a shuffling algorithm;
fig. 6 is a schematic structural diagram of a device for generating a database primary key according to an embodiment of the present application;
fig. 7 is a schematic structural diagram of an electronic device according to an embodiment of the present application.
Detailed Description
The following description of the embodiments of the present application will be made clearly and fully with reference to the accompanying drawings, in which it is evident that the embodiments described are some, but not all, of the embodiments of the present application. All other embodiments, which can be made by one of ordinary skill in the art based on the embodiments herein without making any inventive effort, are intended to be within the scope of the present application.
Fig. 1 is a flowchart of a method for generating a database primary key according to an embodiment of the present application, where the method for generating a database primary key may be executed by an electronic device such as a computer, as shown in fig. 1, and the method includes: steps 110 to 170.
Step 110, generating a primary key generation matrix according to the target character set and the target number set, wherein the target character set comprises characters in an ASCII character set, and primary key elements in the primary key generation matrix are not repeated.
Wherein the characters in the target character set are characters selected from an ASCII character set. The target character set may include uppercase letters, lowercase letters, and numbers 1 through 9, and may include the number 0, although other symbol characters may be included. The digits in the target digit set are integers, and the digits in the target digit set are not repeated with the characters in the target character set, namely the digits in the target digit set are not included in the target character set, and all the primary key elements in the primary key generation matrix generated in this way are not repeated.
To generate a database primary key with high complexity and randomness, embodiments of the present application select an ASCII character set as the base character set for the primary key. The ASCII character set contains 62 printable characters, including letters (uppercase and lowercase), digits (0-9), which have good readability and compatibility, while the diversity of numbers can provide sufficient complexity for the generation of primary keys. Printable characters are selected from the ASCII character set to form a target character set. To generate a primary key generating matrix of a preset size, numbers may be additionally supplemented, which constitute a target number set.
And respectively taking one character in the target character set or one number in the target number set as one main key element in the main key generation matrix, respectively filling each main key element into a blank matrix with a preset size, and generating the main key generation matrix. 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 digits in the target digit set.
By way of example, 61 printable characters in an ASCII character set, including letters (uppercase and lowercase), digits (1-9), may be provided as a target character set, with a primary key generating matrix of 10 x 10 preset size, such that 39 digits (e.g., 10-48) may be additionally appended to generate a 10 x 10 matrix, which digits constitute the target digit set, with 61 characters in the ASCII character set and 39 digits appended to construct a 10 x 10 two-dimensional character matrix as the primary key generating matrix. FIG. 2 is an exemplary diagram of a primary key generator matrix in an embodiment of the present application, as shown in FIG. 2, with each character (including a number) having a corresponding position (row number and column number) in the primary key generator matrix.
And 120, generating a position matrix according to the number of the 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.
Wherein 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 characterizes the position of one primary key element in the primary key generation matrix.
The position matrix can be obtained by using different symbol marks to represent the positions of each primary key element in the primary key generation matrix, arranging the symbol marks, and filling the symbol marks into the matrix with the same size as the primary key generation matrix. By way of example, each positional element in the positional matrix may be a continuous integer, for example, where the primary key generation matrix is a 10 x 10 matrix, the digits 1 through 100 may be used to characterize the position of each primary key element in the primary key generation matrix.
In one embodiment of the present application, each position element in the position matrix is a continuous integer;
generating a position matrix according to the number of the primary key elements in the primary key generation matrix, including: and rearranging the continuous integers by adopting a shuffling algorithm, and generating a position matrix comprising the continuous integers according to the rearranged arrangement sequence.
The shuffling Algorithm may include a Fisher-Yates shuffling Algorithm, although any other shuffling Algorithm may be used in addition to the Fisher-Yates shuffling Algorithm, such as a Knuth-Durstenfeld shuffling Algorithm, an Inside-Out Algorithm Algorithm, and the like.
Each positional element in the positional matrix is a continuous integer, for example, when the primary key generation matrix is a 10×10 matrix, the continuous integer may be 1 to 100. When the main key of the database is generated each time, in order to improve the global uniqueness of the generated main key of the database, the main key of the database can be formed by taking characters from a host generating matrix for a plurality of times, and the main key elements (characters) of the main key generating matrix are obtained by mapping random integers into the main key generating matrix through the position matrix each time when the characters are taken, so that continuous integers can be arranged differently to generate different position matrices each time when the characters are taken.
The shuffling algorithm can be adopted to scatter and rearrange the continuous integers, and each integer is sequentially filled into the blank matrix according to the rearranged arrangement sequence to obtain a position matrix comprising the continuous integers. The shuffling algorithm can comprise a Fisher-Yates shuffling algorithm, wherein the shuffling algorithm is shuffling realized by card drawing, and the processing process is as follows: initializing an original array and a new array, wherein the original array comprises continuous integers, the sequence of each integer is the sequence of the continuous integers, randomly extracting an integer from an untreated array (an array formed by other integers after the original array extracts the integers), and placing the extracted integer into the new array until the whole integer in the untreated array is extracted. The Fisher-Yates shuffling algorithm is used for scrambling a group of integers on line with equal probability, so that the probability of each integer appearing at each position is equal, the algorithm efficiency is high, the time complexity is O (n), and n is the number of integers.
The shuffling algorithm is adopted to rearrange the continuous integers, and the position matrix is generated according to the rearranged arrangement sequence, so that the position matrix generated each time is different, the randomness of the characters extracted each time is enhanced, and the global uniqueness of the generated database primary key is improved.
Step 130, generating random integers in a target integer range by a pseudo-random number generator according to the random seed information, wherein the number of the integers in the target integer range is the same as the number of the position elements in the position matrix.
Wherein the random integer characterizes the position of one position element in the position matrix, i.e. characterizes the row-column index in the position matrix. The random seed information may be current device state information or may be other state information outside the device, such as current weather information, etc.
And generating a pseudo-random number seed value according to the random seed information, and generating a random integer in a target integer range through a pseudo-random number generator based on the pseudo-random number seed value. The random seed information is different at different times, so that the pseudo random number seed values generated at different times are different, and the random integers generated by the pseudo random number generator at different times are different, so that the randomness is higher.
In one embodiment of the present application, the random seed information includes at least two of a current time, a current device state, and current weather information;
the generating random integers in the target integer range by a pseudo-random number generator according to the random seed information comprises the following steps: generating a pseudo-random number seed value of the pseudo-random number generator according to at least two of the current time, the current equipment state and the current weather information; and generating random integers in a target integer range through the pseudo-random number generator according to the pseudo-random number seed value.
The method comprises the steps of quantizing random seed information, respectively quantizing various information in the random seed information into specific data values, namely respectively quantizing current time, current equipment state and current weather information into specific data values, splicing data values of at least two of the current time, the current equipment state and the current weather information, generating a pseudo-random number seed value based on the spliced data values, and calling a pseudo-random number generator to generate a random integer in a target integer range based on the pseudo-random number seed value.
By determining the pseudo-random number seed value according to at least two of the current time, the current equipment state and the current weather information, the difference of the pseudo-random number seed values generated at different times can be ensured, and therefore the randomness of the generated random integer is improved.
In one embodiment of the present application, the generating a pseudorandom number seed value according to at least two of a current time, a current device state, and current weather information includes: determining a first data value corresponding to the current time, determining a second data value corresponding to the current equipment state, and determining a third data value corresponding to the current weather information, wherein the current equipment state comprises at least one of a current CPU state, a current memory state and a current disk space state; splicing at least two of the first data value, the second data value and the third data value into a target data value; and carrying out hash mapping on the target data value through a hash function to obtain the pseudo-random number seed value.
The current time is quantized into a first data value, specifically, the current year, the current month and the current time point in the current time are spliced to obtain the first data value, for example, 53 minutes 212 milliseconds at the current time of 2023, 7 months, 18 days, 13 hours, and 53 minutes 212 milliseconds at the current year 2023, 7 months and the current time point 13 are spliced to obtain the first data value 202307181353212.
Quantizing the current device state to a second data value, the current CPU state referring to a current CPU utilization, e.g., if the current CPU utilization is 35%, the quantized value is 35; the current memory state refers to the current memory usage, for example, if the memory usage is 51%, the quantized value is 51; the current disk space state refers to the usage of the current disk space, for example, if the usage of the current disk space is 72%, the quantized value is 72; and splicing the quantized values of at least one of the current CPU state, the current memory state and the current disk space state to obtain a second data value corresponding to the current equipment state, wherein when the current equipment state comprises the current CPU state, the current memory state and the current disk space state, the second data value obtained by splicing the quantized values can be 355172.
Each weather information may be converted into a specific integer in advance. For example, a sunny day may be encoded as 1, a cloudy day as 2, a rainy day as 3, and so on. When the current weather information is acquired, the code corresponding to the current weather information can be directly acquired from the pre-code, and a third data value corresponding to the current weather information is obtained.
At least two of the first data value, the second data value and the third data value are spliced to obtain a target data value, and the target data value is larger and has more digits, so that the target data value is unfavorable to be used as a pseudorandom number seed value, hash mapping can be carried out on the target data value through a hash function, and the value obtained after mapping is used as the pseudorandom number seed value.
The hash function is used for mapping input data with any length into output data with fixed length. The hash function may be a murmur hash function, which is an efficient hash function that can map any length of input data to a 32-bit or 64-bit hash value. The hash function may use other hash functions, such as a farm hash function, an XXhash function, etc., in addition to the murmur hash function. The Murmur Hash function is adopted to carry out Hash mapping on the target data value, so that the generation efficiency of the pseudo-random number seed value can be improved, and the generated pseudo-random number seed value is uniformly distributed; the Murmur Hash function can provide a consistent and efficient seed generation mode for the pseudo-random number generator, improves the randomness of the pseudo-random number generator, supports the same input to generate the same random number sequence, and is suitable for scenes requiring repeatable randomness.
At least two of a first data value corresponding to the current time, a second data value corresponding to the current equipment state and a third data value corresponding to the current weather information are spliced to be target data values, and the target data values are mapped to pseudo-random number seed values through a hash function.
In one embodiment of the present application, the pseudo-random number generator is a meisen rotation pseudo-random generator;
the generating, by the pseudo-random number generator, a random integer within a target integer range according to the pseudo-random number seed value includes: initializing a Meissen rotation pseudo-random generator according to the pseudo-random number seed value; random integers within the target integer range are generated by the meisen rotation pseudorandom number generator.
The pseudorandom number seed value is used to initialize a meisen rotation pseudorandom number generator, after which random integers within a target integer range are generated by the meisen rotation pseudorandom number generator. Random integers within the target integer range may be generated by using a randrange () function of the mersen rotation random number generator, which is expressed as random. For example, when the primary key generation matrix is a matrix of 10×10, the value of start may be 1, the value of stop may be 101, and the value of step may be 1.
By employing a meisen rotation pseudorandom number generator to generate random integers, since the meisen rotation pseudorandom number generator has a long period (period is typically 2) 19937-1 And the random integer generation device has the characteristics of high speed and uniform distribution in a large range, can avoid the repetition of the generated random integer, and has high speed of generating the random integer. Of course, other pseudo-random number generators may be employed in addition to the meisen rotation pseudo-random number generator, such as a linear congruence generator (Linear Congruential Generator, LCG) or a linear feedback shift register (Linear Feedback Shift Register, LFSR) or the like.
And 140, according to the random integer, acquiring a position element corresponding to the random integer in the position matrix as a target position element.
The position matrix is used for mapping the random integer to the specific primary key element position of the primary key generation matrix, and is a matrix for connecting the random integer and the primary key generation matrix. The random integer representation needs to be selected from the position matrix, the position of the position element represented by the random integer is determined, and the position element of the position is obtained from the position matrix and is used as a target position element. For example, when the random integer is 13, the position of the characterized position element is row 2 and column 3.
And 150, determining the target position represented by the target position element, and acquiring a target primary key element corresponding to the target position from the primary key generation matrix.
The target position element characterizes the target position in the primary key generation matrix, the target position represented by the target position element is determined based on the mapping relation between the position element and the position in the represented corner generation matrix, 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 characterized target position is row 5 and column 6.
Step 160, it is determined whether a character sequence satisfying the target condition is obtained, if yes, step 170 is executed, and if no, step 120 is executed.
And splicing the target main key element obtained in the current cycle with the target main key element obtained in the previous cycle to obtain a character sequence, and judging whether the character sequence meeting the target condition is obtained after the current cycle. The target condition may include that the number of loops reaches a target number or that the length of the character sequence reaches a target length. The number of loops reaching the target number of times (for example, 16 times can be achieved), the length of the generated character sequence can be controlled not to be too large by controlling the number of loops, and the generation efficiency of the database primary key is improved. The length of the character sequence reaches the target length (for example, may be 16 bits, etc.), so that the length of the generated database primary key can be controlled more easily, but since the lengths of primary key elements in the primary key generation matrix may be different, the number of loops is not easy to control, and if one character remains in the last loop, and the primary key elements determined by the loop are two characters, the loop is not satisfactory, and loop acquisition needs to be performed again.
And step 170, taking the character sequence as a database main key, wherein the character sequence comprises the target main key element acquired in each cycle.
And taking the final character sequence as a database primary key.
According to the method for generating the database primary key, a primary key generation matrix is generated according to a target character set and a target number set, a position matrix is generated according to the number of primary key elements in the primary key generation matrix, random integers in a target integer range are generated through a pseudo-random number generator according to random seed information, target position represented by the target position elements is determined according to target position elements corresponding to the random integers in the random integer acquisition position matrix, target primary key elements corresponding to the target position are acquired from the primary key generation matrix, characters in ASCII character sets are adopted to form a target character set, the primary key generation matrix comprising enough primary key elements is generated by combining the target number set, and each time the primary key elements are acquired from the primary key generation matrix, the target position elements in the position matrix are obtained by generating random integers and based on the random integers, further target main key elements corresponding to the target position elements are obtained from the main key generation matrix, the target main key elements are circularly obtained for many times, the repeatedly obtained target main key elements are used as database main keys, the database main keys are formed by combining the diversity of the main key elements, the randomness of the random integers and the repeatedly obtained main key elements, the complexity and the randomness of the database main keys are improved, the repetition of the database main keys can be avoided, the global uniqueness and the safety are improved, and the length (for example, 16 bits) of the generated database main keys can be omitted, so that the generation efficiency can be improved.
Fig. 3 is a flowchart of a method for generating a database primary key according to an embodiment of the present application, where the method for generating a database primary key may be executed by an electronic device such as a computer, and this embodiment is illustrated by taking a matrix with a primary angle generation matrix of 10×10 as an example, as shown in fig. 3, and includes: steps 310 to 370.
Step 310, generating a primary key generation matrix according to a target character set and a target digit set, wherein the target character set comprises capital letters, lowercase letters and digits 1 to 9, and the target digit set comprises digits 10 to 48.
61 characters in the ASCII character set and 39 numbers in addition are constructed into a 10 x 10 two-dimensional character matrix as a primary key generation matrix. FIG. 2 is an exemplary diagram of a primary key generator matrix in an embodiment of the present application, as shown in FIG. 2, with each character (including a number) having a corresponding position (row number and column number) in the primary key generator matrix.
Step 320, generating a position matrix according to the number of the primary key elements in the primary key generation matrix, wherein each position element in the position matrix is a number from the number 1 to the number 100.
Since a meisen rotation pseudorandom number generator is subsequently used to generate random integers of 1 to 100. To map these random integers to each character in the primary key generation matrix, a 10 x 10 position matrix may be created. In the position matrix, each cell contains a character corresponding to the column and row index of the cell (i.e., a number from 1 to 100). Thus, when the random number generator generates a number, the corresponding character can be found directly in this position matrix.
Fig. 4 is an exemplary diagram of an initial position matrix in the embodiment of the present application, as shown in fig. 4, the position matrix is a 10×10 matrix, 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 a number 1 to a number 100, and in the initial position matrix, each position element is arranged in a numerical order from a number 1 to a number 100.
Fig. 5 is an exemplary diagram of an embodiment of the present application after the initial position matrix has been rearranged using a shuffling algorithm. The Fisher-Yates shuffling algorithm may be used to scatter and rearrange the original values of 1-100 in FIG. 4 to generate a new 10X 10 position matrix, as shown in FIG. 5.
Step 330, generating random integers in the target integer range by the pseudo-random number generator according to the random seed information.
The pseudo-random number generator is an important part of the database key generation process, and first, the random seed information, i.e., the current time, the current CPU state, the current memory state, the current disk space state, and the current weather information, needs to be converted into a quantifiable data value. Quantization can be performed using the following method: obtaining the current time, for example, 53 minutes 212 milliseconds at 2023, 7, 18 and 13, and quantifying the current time to obtain an initial integer of 202307181353212; acquiring a current CPU state (current CPU utilization rate), and assuming that the current CPU utilization rate is 35%, obtaining a quantized value of 35; acquiring a current memory state (current memory utilization), and assuming that the current memory utilization is 51%, obtaining a quantized value of 51; the current weather information is obtained, and a quantized value corresponding to the current weather information is obtained based on a numerical code corresponding to the current weather information, for example, each weather information can be converted into a specific integer in advance, for example, a sunny day can be coded into 1, a cloudy day can be coded into 2, a rainy day can be coded into 3, and the like.
And after the current time, the current CPU state, the current memory state, the current disk space state and the current weather information are continuously quantized, splicing the quantized values to obtain a target data value, and mapping the target data value into a pseudo-random number seed value by adopting a Murmur Hash function. The pseudorandom number seed value is then used to initialize the mersen rotation pseudorandom number generator, which may be used to generate random integers after initialization of the mersen rotation pseudorandom number generator by generating random integers within a target integer range (1-100) using the randrange () function of the mersen rotation pseudorandom number generator, where start is the start value of the random integer (here 1), stop is the end value of the random number (not included in the range, here 101), step is the step size (default 1).
Step 340, according to the random integer, obtaining a position element corresponding to the random integer in the position matrix as a target position element.
And obtaining a target position element of a position corresponding to the random integer from the position matrix. For example, if the random integer is 13, the position in the position matrix corresponding to 13 is the second row and the third column, and the target position element 46 corresponding to the second row and the third column is obtained from the position matrix.
Step 350, determining the target position represented by the target position element, and obtaining the target primary key element corresponding to the target position from the primary key generation matrix
And if the target position element in the position matrix is 46, the target position in the primary key generation matrix represented by the target position element is 5 th row and 6 th column, and the target primary key element 7 corresponding to the 5 th row and 6 th column is acquired from the primary key generation matrix.
Step 360, it is determined whether a character sequence satisfying the target condition is obtained, if yes, step 370 is executed, and if no, step 320 is executed.
And 370, taking the character sequence as a database main key, wherein the character sequence comprises the target main key element acquired in each cycle.
According to the method, the ASCII character set is used as a basis for generating the primary key, and the primary key is generated by combining a position matrix, a Murmur Hash function, a Mersen rotation algorithm and a Fisher-Yates shuffling algorithm.
Fig. 6 is a schematic structural diagram of a device for generating a database primary key according to an embodiment of the present application, as shown in fig. 6, where the device includes:
A primary key matrix generating module 610, configured to generate a primary key generating matrix according to a target character set and a target number set, where the target character set includes characters in an ASCII character set, and primary key elements in the primary key generating matrix are not repeated;
a position matrix generating module 620, configured to generate a position matrix according to the number of primary key elements in the primary key generating matrix, where the position elements in the position matrix represent positions of the primary key elements in the primary key generating matrix;
a random integer generation module 630, configured to generate random integers in a target integer range by using a pseudo-random number generator according to random seed information, where the number of integers in the target integer range is the same as the number of position elements in the position matrix;
a target position element obtaining module 640, configured to obtain, according to the random integer, a position element corresponding to the random integer in the position matrix, as a target position element;
a target primary key element obtaining module 650, configured to determine a target position represented by the target position element, and obtain a target primary key element corresponding to the target position from the primary key generating matrix;
and the circulation control module 660 is configured to perform the above operations from generating the position matrix to obtaining the target primary key element in a circulation manner until a character sequence meeting a target condition is obtained, and take the character sequence as a database primary key, where the character sequence includes the target primary key element obtained in each circulation.
Optionally, the random seed information includes at least two of a current time, a current device state, and current weather information;
the random integer generation module includes:
the seed value generation unit is used for generating pseudo-random number seed values according to at least two of the current time, the current equipment state and the current weather information;
and the random integer generation unit is used for generating random integers in the target integer range through the pseudo-random number generator according to the pseudo-random number seed value.
Optionally, the seed value generating unit is specifically configured to:
determining a first data value corresponding to the current time, determining a second data value corresponding to the current equipment state, and determining a third data value corresponding to the current weather information, wherein the current equipment state comprises at least one of a current CPU state, a current memory state and a current disk space state;
splicing at least two of the first data value, the second data value and the third data value into a target data value;
and carrying out hash mapping on the target data value through a hash function to obtain the pseudo-random number seed value.
Optionally, the hash function is a murmur hash function.
Optionally, the pseudo-random number generator is a meisen rotation pseudo-random number generator;
the random integer generation unit is specifically configured to:
initializing a Meissen rotation pseudo-random number generator according to the pseudo-random number seed value;
random integers within the target integer range are generated by the meisen rotation pseudorandom number generator.
Optionally, each position element in the position matrix is a continuous integer;
the position matrix generation module is specifically configured to:
and rearranging the continuous integers by adopting a shuffling algorithm, and generating a position matrix comprising the continuous integers according to the rearranged arrangement sequence.
Optionally, the shuffling algorithm comprises a Fisher-yartes shuffling algorithm.
Optionally, the set of target characters includes uppercase letters, lowercase letters, and numbers 1 through 9.
Optionally, the target condition includes that the number of loops reaches a target number or that the length of the character sequence reaches a target length.
The generating device for the database primary key provided in the embodiment of the present application is used to implement each step of the generating method for the database primary key described in the embodiment of the present application, and specific embodiments of each module of the device refer to corresponding steps, which are not repeated herein.
According to the generation device of the database primary key, the primary key generation matrix is generated according to the target character set and the target number set, the position matrix is generated according to the number of primary key elements in the primary key generation matrix, random integers in the range of target integers are generated according to random seed information through the pseudo-random number generator, target positions represented by the target position elements are determined according to the target position elements corresponding to the random integers in the random integers, target primary key elements corresponding to the target positions are obtained from the primary key generation matrix, characters in the ASCII character set are adopted to form the target character set, the primary key generation matrix comprising enough primary key elements is generated by combining the target number set, and the target position elements in the position matrix are obtained by generating the random integers and based on the random integers each time the primary key elements are obtained from the primary key generation matrix, the target primary key elements corresponding to the target position elements are obtained from the primary key generation matrix in a circulating mode, the multiple times of obtaining the target primary key elements serving as the primary keys of the database, the multiple times of the primary key elements, the random integers and the circulating primary key are obtained in the primary key can be combined with the multiple times of the primary key of the database, the data can be generated in a large number of the database, the database can be avoided, the reliability is improved, and the reliability is 16 is high, and the reliability is can be realized.
Fig. 7 is a schematic structural diagram of an electronic device according to an embodiment of the present application, and as shown in fig. 7, the electronic device 700 may include one or more processors 710 and one or more memories 720 connected to the processors 710. Electronic device 700 may also include an input interface 730 and an output interface 740 for communicating with another apparatus or system. Program code executed by processor 710 may be stored in memory 720.
The processor 710 in the electronic device 700 invokes the program code stored in the memory 720 to perform the database primary key generation method in the above-described embodiment.
The embodiment of the application also provides a computer readable storage medium, on which a computer program is stored, which when executed by a processor, implements the method for generating the database primary key according to the embodiment of the application.
In this specification, each embodiment is described in a progressive manner, and each embodiment is mainly described by differences from other embodiments, and identical and similar parts between the embodiments are all enough to be referred to each other. For the device embodiments, since they are substantially similar to the method embodiments, the description is relatively simple, and reference is made to the description of the method embodiments for relevant points.
The foregoing describes in detail a method, an apparatus, an electronic device, and a storage medium for generating a database primary key according to embodiments of the present application, where specific examples are applied to illustrate principles and implementations of the present application, and the descriptions of the foregoing embodiments are only used to help understand the method and core ideas of the present application; meanwhile, as those skilled in the art will have modifications in the specific embodiments and application scope in accordance with the ideas of the present application, the present description should not be construed as limiting the present application in view of the above.
From the above description of the embodiments, it will be apparent to those skilled in the art that the embodiments may be implemented by means of software plus necessary general hardware platforms, or may be implemented by hardware. Based on this understanding, the foregoing technical solution may be embodied essentially or in a part contributing to the prior art in the form of a software product, which may be stored in a computer readable storage medium, such as ROM/RAM, a magnetic disk, an optical disk, etc., including several instructions for causing a computer device (which may be a personal computer, a server, or a network device, etc.) to execute the method described in the respective embodiments or some parts of the embodiments.

Claims (12)

1. A method for generating a primary key of a database, comprising:
generating a primary key generation matrix according to a target character set and a target number set, wherein the target character set comprises characters in an ASCII character set, and primary key elements in the primary key generation matrix are not repeated;
generating a position matrix according to the number of the main key elements in the main key generation matrix, wherein the position elements in the position matrix represent the positions of the main key elements in the main key generation matrix;
generating random integers in a target integer range through a pseudo-random number generator according to random seed information, wherein the number of the integers in the target integer range is the same as the number of the position elements in the position matrix;
acquiring a position element corresponding to the random integer in the position matrix according to the random integer, and taking the position element as a target position element;
determining a target position represented by the target position element, and acquiring a target primary key element corresponding to the target position from the primary key generation matrix;
and circularly executing the operations from generating the position matrix to acquiring the target main key element until a character sequence meeting target conditions is obtained, and taking the character sequence as a database main key, wherein the character sequence comprises the target main key element acquired in each cycle.
2. The method of claim 1, wherein the random seed information comprises at least two of a current time, a current device state, and current weather information;
the generating random integers in the target integer range by a pseudo-random number generator according to the random seed information comprises the following steps:
generating a pseudo-random number seed value according to at least two of the current time, the current equipment state and the current weather information;
and generating random integers in a target integer range through the pseudo-random number generator according to the pseudo-random number seed value.
3. The method of claim 2, wherein generating the pseudorandom number seed value based on at least two of a current time, a current device state, and current weather information comprises:
determining a first data value corresponding to the current time, determining a second data value corresponding to the current equipment state, and determining a third data value corresponding to the current weather information, wherein the current equipment state comprises at least one of a current CPU state, a current memory state and a current disk space state;
splicing at least two of the first data value, the second data value and the third data value into a target data value;
And carrying out hash mapping on the target data value through a hash function to obtain the pseudo-random number seed value.
4. A method according to claim 3, wherein the hash function is a murmur hash function.
5. The method of claim 2, wherein the pseudorandom number generator is a meisen rotation pseudorandom number generator;
the generating, by the pseudo-random number generator, a random integer within a target integer range according to the pseudo-random number seed value includes:
initializing a Meissen rotation pseudo-random number generator according to the pseudo-random number seed value;
random integers within the target integer range are generated by the meisen rotation pseudorandom number generator.
6. The method of any one of claims 1-5, wherein each positional element in the positional matrix is a contiguous integer;
generating a position matrix according to the number of the primary key elements in the primary key generation matrix, including:
and rearranging the continuous integers by adopting a shuffling algorithm, and generating a position matrix comprising the continuous integers according to the rearranged arrangement sequence.
7. The method of claim 6, wherein the shuffling algorithm comprises a Fisher-yartes shuffling algorithm.
8. The method of any of claims 1-5, wherein the set of target characters comprises uppercase letters, lowercase letters, and numbers 1 through 9.
9. The method of any of claims 1-5, wherein the target condition comprises a number of loops reaching a target number or a length of a character sequence reaching a target length.
10. A database primary key generation apparatus, comprising:
the primary key matrix generation module is used for generating a primary key generation matrix according to a target character set and a target number set, wherein the target character set comprises characters in an ASCII character set, and primary key elements in the primary key generation matrix are not repeated;
the position matrix generation module is used for generating a position matrix according to the number of the main key elements in the main key generation matrix, and the position elements in the position matrix represent the positions of the main key elements in the main key generation matrix;
the random integer generation module is used for generating random integers in a target integer range through a pseudo-random number generator according to random seed information, wherein the number of the integers in the target integer range is the same as the number of the position elements in the position matrix;
The target position element acquisition module is used for acquiring position elements corresponding to the random integers in the position matrix according to the random integers, and taking the position elements as target position elements;
the target primary key element acquisition module is used for determining a target position represented by the target position element and acquiring a target primary key element corresponding to the target position from the primary key generation matrix;
and the circulation control module is used for circularly executing the operations from generating the position matrix to acquiring the target main key element until a character sequence meeting the target condition is obtained, and taking the character sequence as a database main key, wherein the character sequence comprises the target main key element acquired in each circulation.
11. An electronic device comprising a memory, a processor and a computer program stored on the memory and executable on the processor, characterized in that the processor implements the method of generating a database primary key according to any one of claims 1 to 9 when executing the computer program.
12. A computer-readable storage medium, on which a computer program is stored, characterized in that the program, when executed by a processor, implements the method of generating a database primary key according to any one of claims 1 to 9.
CN202311278557.7A 2023-09-28 2023-09-28 Database primary key generation method and device, electronic equipment and storage medium Pending CN117390018A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202311278557.7A CN117390018A (en) 2023-09-28 2023-09-28 Database primary key generation method and device, electronic equipment and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202311278557.7A CN117390018A (en) 2023-09-28 2023-09-28 Database primary key generation method and device, electronic equipment and storage medium

Publications (1)

Publication Number Publication Date
CN117390018A true CN117390018A (en) 2024-01-12

Family

ID=89467545

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202311278557.7A Pending CN117390018A (en) 2023-09-28 2023-09-28 Database primary key generation method and device, electronic equipment and storage medium

Country Status (1)

Country Link
CN (1) CN117390018A (en)

Similar Documents

Publication Publication Date Title
Wang et al. An image encryption algorithm based on hyperchaotic system and DNA coding
US12211116B2 (en) System and method for encoding and authenticating a digital image
CN112487253B (en) User invitation code generation method, verification method, device, equipment and storage medium
EP2729869A1 (en) Method of providing a portable true random number generator based on the microstructure and noise found in digital images
CN110489466B (en) Method and device for generating invitation code, terminal equipment and storage medium
CN110879895A (en) Color image block encryption algorithm based on hyperchaotic system and DNA sequence
CN106651735A (en) Chaos theory-based digital image parallel encryption method
EP4156598A1 (en) Challenge-response pair generation apparatus and method based on puf
CN115941189A (en) Encryption method for document data
CN116484442A (en) Intelligent electricity selling data storage method based on data encryption
CN111324908A (en) Batch data desensitization method
CN117390018A (en) Database primary key generation method and device, electronic equipment and storage medium
Zangana Watermarking System Using LSB
CN116305294B (en) Data leakage tracing method and device, electronic equipment and storage medium
CN109245884B (en) Data communication method and device for wind turbine
TWI664849B (en) Method, computer program product and processing system for generating secure alternative representation
Al-Muhammed et al. Dynamic text encryption
CN116010984A (en) Multiple encryption storage method, device and equipment for relational database data
CA2953091C (en) System and method for encoding and authenticating a digital image
Zhang [Retracted] Multimedia Image Encryption Analysis Based on High‐Dimensional Chaos Algorithm
CN112473129B (en) Incremental data acquisition method, system, electronic device and storage medium
CN106815238B (en) Serialization and deserialization method and device for structured data
CN113824807B (en) Invitation code generation method, device, electronic device and storage medium
US12107974B1 (en) Encryption method and apparatus
CN108460442B (en) Color two-dimensional code generation method and device

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination