Data record method and device of database table, storage medium and electronic equipment
Patent Information
- Application Number
- CN202410270757.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-03-08
- Publication Date
- 2026-09-15
- Estimated Expiration
- 2044-03-08
AI Technical Summary
[0009] This application addresses the issue of data insertion into a database table containing existing data records at a non-last position. By specifying the sorting field value for the data to be written, and defining a first target value within this sorting field value based on the sequential relationship between the data record corresponding to the data to be written and a reference data record, the writing position of the data can be determined. The data records in the database table are then sorted according to the sorting field value without modifying the sorting field values of existing data records in the database table. Therefore, this solution resolves the problem of sorting disorder in related database table data recording methods, effectively preventing such disorder.
Smart Images

Figure CN118093591B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of databases, and more specifically, to a data recording method and apparatus, storage medium and electronic device for database tables. Background Technology
[0002] Currently, when a database table already stores data records sorted according to a sorting field, inserting a new record in the middle requires shifting the sequence number of all subsequent records one position to the right. Each modification to the database table involves a significant number of records, and if multiple people submit code, there's a high probability of sorting field conflicts, leading to complete sorting disorder. Therefore, it's evident that the data recording methods used in related technologies for database tables are prone to sorting problems. Summary of the Invention
[0003] This application provides a data recording method, apparatus, storage medium, and electronic device for database tables, to at least solve the problem that data recording methods for database tables in related technologies are prone to sorting disorder.
[0004] According to one embodiment of this application, a data recording method for a database table is provided, comprising: obtaining first data to be written, wherein the first data to be written is data to be inserted between adjacent first data records and second data records in the database table; determining a first sorting field value corresponding to the first data to be written based on a sorting field value of a reference data record, wherein the reference data record is one of the first data record and the second data record, the first sorting field value is obtained by adding a first target value to the sorting field value of the reference data record, the first target value is determined according to the sequential relationship between the data record corresponding to the first data to be written and the reference data record, and the sorting field value of the data record in the database table is used to sort the data records in the database table; and inserting the first data to be written into the database table based on the first sorting field value to obtain a target data record corresponding to the first data to be written.
[0005] According to another embodiment of this application, a data recording device for a database table is provided, comprising: a first acquisition unit, configured to acquire first data to be written, wherein the first data to be written is data to be inserted between adjacent first data records and second data records in the database table; a first determination unit, configured to determine a first sorting field value corresponding to the first data to be written based on a sorting field value of a reference data record, wherein the reference data record is one of the first data record and the second data record, the first sorting field value is obtained by adding a first target value to the sorting field value of the reference data record, the first target value is determined according to the sequential relationship between the data record corresponding to the first data to be written and the reference data record, and the sorting field value of the data record in the database table is used to sort the data records in the database table; and a writing unit, configured to insert the first data to be written into the database table based on the first sorting field value to obtain a target data record corresponding to the first data to be written.
[0006] According to yet another embodiment of this application, a computer-readable storage medium is also provided, wherein a computer program is stored therein, and the computer program is configured to perform the steps in any of the above method embodiments when it is run.
[0007] According to yet another embodiment of this application, an electronic device is also provided, including a memory and a processor, wherein the memory stores a computer program and the processor is configured to run the computer program to perform the steps in any of the above method embodiments.
[0008] According to yet another embodiment of this application, a computer program product is also provided, including a computer program that, when executed by a processor, implements the steps in any of the above method embodiments.
[0009] This application addresses the issue of data insertion into a database table containing existing data records at a non-last position. By specifying the sorting field value for the data to be written, and defining a first target value within this sorting field value based on the sequential relationship between the data record corresponding to the data to be written and a reference data record, the writing position of the data can be determined. The data records in the database table are then sorted according to the sorting field value without modifying the sorting field values of existing data records in the database table. Therefore, this solution resolves the problem of sorting disorder in related database table data recording methods, effectively preventing such disorder. Attached Figure Description
[0010] Figure 1This is a hardware structure block diagram of a server device for a database table data recording method according to an embodiment of this application;
[0011] Figure 2 This is a flowchart illustrating a data recording method for a database table according to an embodiment of this application;
[0012] Figure 3 This is a flowchart illustrating another method for recording data in a database table according to an embodiment of this application;
[0013] Figure 4 This is a flowchart illustrating another method for recording data in a database table according to an embodiment of this application;
[0014] Figure 5 This is a schematic diagram of a data recording method for a database table according to an embodiment of this application;
[0015] Figure 6 This is a structural block diagram of an optional database table data recording device provided in an embodiment of this application. Detailed Implementation
[0016] The embodiments of this application will be described in detail below with reference to the accompanying drawings and examples.
[0017] It should be noted that the terms "first," "second," etc., in the specification, claims, and drawings of this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence.
[0018] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this application belongs. The terminology used herein is for the purpose of describing embodiments of this application only and is not intended to limit this application.
[0019] The methods and embodiments provided in this application can be executed on a server device or a similar computing device. Taking running on a server device as an example, Figure 1 This is a hardware structure block diagram of a server device for a database table data recording method according to an embodiment of this application. For example... Figure 1 As shown, the server device may include one or more ( Figure 1 Only one is shown in the diagram. A processor 102 (which may include, but is not limited to, a microprocessor MCU or a programmable logic device FPGA, etc.) and a memory 104 for storing data are also shown. The server device may further include a transmission device 106 for communication functions and an input / output device 108. Those skilled in the art will understand that... Figure 1The structure shown is for illustrative purposes only and does not limit the structure of the server equipment described above. For example, the server equipment may also include components that are more... Figure 1 The more or fewer components shown, or having the same Figure 1 The different configurations shown.
[0020] The memory 104 can be used to store computer programs, such as application software programs and modules, like the computer program corresponding to the data recording method of the database table in this embodiment. The processor 102 executes various functional applications and data processing by running the computer program stored in the memory 104, thus implementing the above-described method. The memory 104 may include high-speed random access memory and non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory 104 may further include memory remotely located relative to the processor 102, and these remote memories can be connected to server devices via a network. Examples of such networks include, but are not limited to, the Internet, corporate intranets, local area networks, mobile communication networks, and combinations thereof.
[0021] The transmission device 106 is used to receive or send data via a network. Specific examples of the network described above may include a wireless network provided by a communication provider for the server device. In one example, the transmission device 106 includes a NIC (Network Interface Controller), which can connect to other network devices via a base station to communicate with the Internet. In another example, the transmission device 106 may be an RF (Radio Frequency) module used for wireless communication with the Internet.
[0022] This embodiment provides a method for recording data in a database table. Figure 2 This is a flowchart illustrating a data recording method for a database table according to an embodiment of this application, as shown below. Figure 2 As shown, the process includes the following steps:
[0023] Step S202: Obtain the first data to be written, wherein the first data to be written is the data to be inserted into the database table between the first data record and the second data record.
[0024] The data recording method for database tables in this embodiment can be applied to scenarios where data is inserted into a database with pre-sorted data records. A database is a collection of organized and stored data. It is a structured collection of information that can be easily accessed, managed, and updated by a computer system. Data in a database is organized in the form of tables, which contain different fields and records, enabling data to be stored and retrieved quickly and efficiently. Databases can exist in different forms, the most common being relational databases (such as MySQL, Oracle, and SQL Server) and non-relational databases (such as MongoDB and Redis). Relational databases use tables, rows, and columns to organize data, while non-relational databases organize and store data in other ways. At least some embodiments in this application use database tables that record data in the form of tables as an example.
[0025] Currently, when inserting data into a database table that already contains data records sorted by a sorting field, it is generally necessary to use an insert statement and multiple update statements to update the data records. The update statements are used to update the sorting field of the existing data records at the position where the insert statement is to be inserted, as well as the data records that follow, in order to avoid sorting confusion.
[0026] For example, the software system already has menus for user management, permission management, recycle bin, overview, etc., and the database maintains menu records as shown in Table 1:
[0027] Table 1
[0028] User Management 1 Access Management 2 Recycling Station 3 Overview 4
[0029] The backend sorts based on the size of the sorting field. If we want to insert a new menu "Role Management" between User Management and Permission Management, in addition to adding an insert statement, we also need to add three update statements to change the values of the Permission Management, Recycle Bin, and Overview sorting fields. The modified results are shown in Table 2:
[0030] Table 2
[0031] User Management 1 Role Management 2 Access Management 3 Recycling Station 4
[0032] Table 2 (continued)
[0033] Overview 5
[0034] Taking the modification of records using SQL (Structured Query Language) statements as an example, modifying a record requires adding four SQL statements. If there are many menus, more records will be affected. In this case, the database already has sorted data records. If you want to insert a new data record in the middle, you need to shift all subsequent data records one position to the right. Each time you modify a table record, there will be a lot of records. If multiple people make code submissions on the backend, it is highly likely to cause conflicts in the sorting fields, and the entire sorting will become chaotic.
[0035] Here, SQL refers to a standardized language specifically designed for managing relational database systems. SQL is used to perform various tasks, including retrieving data, inserting new records, updating existing records, and deleting data. It is also used to create and modify database tables, define relationships and constraints, and so on.
[0036] To at least partially solve the above problems, in this embodiment, the sorting field of the data record is recorded in a new way. When the data to be written is inserted between the first and second adjacent data records in the database table, the sorting field value of the newly inserted data record is set between the sorting field values of the first and second data records. That is, only the insert statement is used to insert data in the middle of the database table, keeping the sorting field values of the original data records in the database table unchanged. This facilitates development by developers and avoids the sorting chaos caused by multiple users modifying database table records.
[0037] As needed, researchers can write the first data to be written into the database table in the backend using a computer language (such as SQL). Here, the first data to be written can be one or more, and this embodiment does not limit this.
[0038] In this embodiment, the database table may already contain data records arranged in the order of the sorting field values. When the sorting field value is not specified, the data to be written can be written to the end of the database table according to the order rules of the existing sorting field. In this embodiment, the sorting field value of the data to be written is specified as an example to explain how to place the data to be written in the middle or initial position of the existing data records in the database table.
[0039] Step S204: Based on the sorting field value of the reference data record, determine the first sorting field value corresponding to the first data to be written. The reference data record is one of the first data record and the second data record. The first sorting field value is obtained by adding a first target value to the sorting field value of the reference data record. The first target value is determined according to the relationship between the data record corresponding to the first data to be written and the reference data record. The sorting field value of the data record in the database table is used to sort the data records in the database table.
[0040] To insert the data to be written into the middle position of an existing, adjacent first and second data record in a database table, the sorting field value of the data to be written can be set to the sorting field value corresponding to the middle position of the first and second data records. In this embodiment, the first sorting field value corresponding to the first data to be written can be determined based on the sorting field value of a reference data record. The reference data record can be one of the first and second data records. When inserting the data to be written into the beginning of an existing data record in a database table, the sorting field value of the reference data record can be preset to a preset value. For example, the preset value can be 0, which is less than the sorting field value of the beginning of the existing data record in the database table.
[0041] The first sorting field value can be obtained by adding a first target value to the sorting field value of the reference data record. Here, the length of the first sorting field value is longer than the length of the sorting field value of the reference data record. The first target value is determined based on the sequential relationship between the data record corresponding to the first data to be written and the reference data record. Correspondingly, the sequential relationship between the data record corresponding to the first data to be written and the reference data record can be determined based on the first target value.
[0042] It should be noted that the sort field value of data records in a database table is used to sort the data records in the database table.
[0043] Step S206: Insert the first data to be written into the database table based on the value of the first sorting field to obtain the target data record corresponding to the first data to be written.
[0044] If the database table allows data to be inserted directly in the middle position, the first data to be written and the first sorting field value can be directly inserted into the position corresponding to the first sorting field value. That is, the target data record corresponding to the first data to be written is inserted between the adjacent first data record and the second data record.
[0045] When the insert statement is placed at the end of the database table, the first data to be written and the first sorting field value are inserted at the end of the database table. When the backend needs to sort the data records in the database table, it can update the position of the target data record between the adjacent first and second data records based on the first sorting field value, ensuring that the data records are arranged in the correct order in the database table.
[0046] Here, inserting the first piece of data to be written into the database table can be achieved using an SQL insert statement.
[0047] Through the above steps, the first data to be written is obtained, which is the data to be inserted into the database table between adjacent first and second data records. Based on the sorting field value of the reference data record, the first sorting field value corresponding to the first data to be written is determined, where the reference data record is one of the first and second data records. The first sorting field value is obtained by adding a first target value to the sorting field value of the reference data record. The first target value is determined according to the sequential relationship between the data record corresponding to the first data to be written and the reference data record. The sorting field value of the data record in the database table is used to sort the data records in the database table. Based on the first sorting field value, the first data to be written is inserted into the database table to obtain the target data record corresponding to the first data to be written. This solves the problem of easy sorting disorder in the sequence record method of the database table in related technologies and reduces the risk of sorting disorder in the database table.
[0048] In one exemplary embodiment, before determining the first sorting field value corresponding to the first data to be written based on the sorting field value of the reference data record, in conjunction with Figure 3 The above methods also include:
[0049] S11, if the length of the sorting field value of the first data record is different from the length of the sorting field value of the second data record, the data record with the longest sorting field value between the first data record and the second data record shall be determined as the reference data record;
[0050] S12, if the length of the sorting field value of the first data record is the same as the length of the sorting field value of the second data record, the data record that appears earlier in the first data record and the second data record is determined as the reference data record.
[0051] When inserting the first data to be written between two adjacent data records with different lengths of sorting field values, the data record with the longest sorting field value is used as the reference data record. The first target value is added to the sorting field value of the reference data record to obtain the first sorting field value corresponding to the first data to be written. The length of the first sorting field value is greater than the length of the sorting field value of the reference data record.
[0052] When inserting the first piece of data to be written between two adjacent data records with the same length of sort field values, the data record that appears earlier is used as the reference data record, that is, the data record with the smaller sort field value is used as the reference data record.
[0053] Specifically, taking the sorting field value of the first data record in the existing data records in the database table as 1 and the default value as 0, when inserting the data to be written into the first data record in the existing data records in the database table, the first target value can be added to the default value of 0 to obtain the first sorting field value.
[0054] In this embodiment, using a sorting field value with a longer length (different sorting field values) or a smaller length (same sorting field values) as a benchmark facilitates subsequent comparison of the size of the sorting field values.
[0055] In an exemplary embodiment, determining the first sorting field value corresponding to the first data to be written, based on the sorting field value of a reference data record, includes:
[0056] S21, append the first target value after the sorting field value of the reference data record, and separate it by a specified symbol to obtain the first sorting field value.
[0057] The length of the sorting field value can be determined by a specified symbol. For example, if the specified symbol is "|", and the sorting field value of the reference data record is 0 and the first target value is 1, the first sorting field value is 0|1, where the length of 0|1 is greater than the length of 0. If the specified symbol is "|", and the sorting field value of the reference data record is 0|1 and the first target value is 1, the first sorting field value is 0|1|1, where the length of 0|1|1 is greater than the length of 0|1.
[0058] In an exemplary embodiment, the last data record inserted into the database table is the base data record, and the sorting field value of the base data record in the database table increases sequentially starting from a first specified value;
[0059] Before determining the first sorting field value corresponding to the first piece of data to be written, based on the sorting field values of the reference data records, in conjunction with... Figure 4 The above methods also include:
[0060] S31, if the first data to be written is to be inserted before the reference data record, the sorting field value of the first benchmark data record before the reference data record is determined as the first target value;
[0061] S32, when the first data to be written is to be inserted after the reference data record, the sorting field value of the first benchmark data record after the reference data record is determined as the first target value.
[0062] When adding data records to the end of a database table according to the sorting rules of the existing records' sorting fields, the sorting field values of the newly inserted data records can be sequentially incremented according to the sorting rules of the existing records' sorting fields. The data record written according to the sorting rules of the existing records' sorting fields is called the base data record. The sorting field values of the base data record can start from a first specified value and increment sequentially. For example, if the first field value is 1, when inserting data records to the end of the database table sequentially, the sorting field values of the newly inserted data records can be 2, 3, 4, 5...
[0063] In this embodiment, taking the writing of the data to be written to the end of a non-database table as an example, as shown in Table 3, when the first data to be written is to be inserted before the data record (reference data record) corresponding to role management, the sorting field value 1 of the first baseline data record before the reference data record is determined as the first target value; when the first data to be written is to be inserted after the data record (reference data record) corresponding to role management, the sorting field value 2 of the first baseline data record after the reference data record is determined as the first target value.
[0064] Table 3
[0065] front page 0 Overview 1 User Management 1|2 Role Management 1|2|2 Access Management 1|2|2|2 log 2 Recycling Station 3
[0066] Using the sorting field value of the reference data record as the base value, when inserting data before the base value, the sorting field value of the first base data record before the reference data record is added after the base value; when inserting data after the base value, the sorting field value of the first base data record after the reference data record is added after the base value. That is, the first target value used to generate the first sorting field value when inserting the first data to be written before the reference data record is less than the first target value used to generate the first sorting field value when inserting the first data to be written after the reference data record.
[0067] This embodiment establishes a rule for generating the sorting field value of newly inserted data, which facilitates the orderly management of data records in the database table.
[0068] In an exemplary embodiment, each data record inserted at the end of the database table is a baseline data record, and the sorting field value of the baseline data record in the database table is sequentially increased starting from a first specified value;
[0069] After inserting the first data to be written into the database table based on the value of the first sorting field, and obtaining the target data record corresponding to the first data to be written, the above method further includes:
[0070] Obtain the second data to be written, where the second data to be written is the last data to be written to the database table;
[0071] Add a second specified value to the sort field value of the last data record in the database table, and set it as the second sort field value;
[0072] The second data to be written is inserted into the last part of the database table based on the value of the second sorting field, resulting in a data record corresponding to the second data to be written.
[0073] Similar to the aforementioned embodiments, when the sorting field value of the baseline data record in the database table increments sequentially starting from a first specified value, and the second data to be written is the data to be written to the end of the database table, the sorting field value of the second data to be written can increment sequentially according to the sorting rules of the existing baseline data record's sorting field value in the database table. In this embodiment, the increment can be set to the second specified value each time, that is, the sorting field value of the baseline data record in the database table is sequentially the first specified value, the first specified value plus the second specified value, the first specified value plus two second specified values, ...
[0074] For example, in this embodiment, taking a first specified value of 0 and a second specified value of 1 as an example, the baseline data record can be as follows: Figure 5 As shown.
[0075] The second data to be written is inserted into the end of the database table based on the second sorting field value, resulting in a data record corresponding to the second data to be written. Here, the sorting field value of the second data to be written can be specified according to the sorting rules of the sorting field value in the database table, or the second data to be written can be directly written to the end of the database table without specifying the second sorting field value, and the database table can automatically generate the sorting field value of the second data to be written.
[0076] In an exemplary embodiment, each data record inserted at the end of the database table is a baseline data record, and the sorting field value of the baseline data record in the database table is sequentially increased starting from a first specified value;
[0077] When inserting data between two data records of the same length with the same length of sort field value, the sort field value of the inserted data is obtained by concatenating the sort field value of the first data record of the same length with the sort field value of the second data record of the same length, and then splitting it by a specified symbol.
[0078] When inserting data between two data records of different lengths with different sort field values, the sort field value of the inserted data is obtained by concatenating the second target value after the sort field value of the target data record with the longest sort field value and splitting it using a specified symbol. Specifically, when the inserted data is to be inserted before the reference data record, the second target value is the sort field value of the first reference data record before the target data record. When the inserted data is to be inserted after the reference data record, the second target value is the sort field value of the first reference data record after the target data record.
[0079] After inserting the first data to be written into the database table based on the value of the first sorting field, and obtaining the target data record corresponding to the first data to be written, the above method further includes:
[0080] Sort the data records in the database table in ascending order of the sorting field value to obtain the sorted database table.
[0081] In this embodiment, the first data to be written is the data to be inserted into the database table between adjacent first and second data records. The sorting field value of the first data record is 0, and the sorting field value of the second data record is 1. Both the first and second data records are baseline data records. At this time, the initial order has been arranged as shown in Table 4.
[0082] Table 4
[0083] 0 1
[0084] At this point, the lower number (the smaller number in the initial number) is 0, and the higher number (the larger number in the initial number) is 1. When added for the first time, the two are combined to form a new sort value 0|1. The sorting is as shown in Table 5.
[0085] Table 5
[0086] 0 0|1 1
[0087] Insert records between 0 and 0|1, and between 0|1 and 1. When inserting between 0 and 0|1, since 0|1 is relatively long, use it as the standard for comparison (i.e., the sorting field value of the reference data record). To insert data before 0|1, add "|lower number" (i.e., the sorting field value of the first baseline data record before the reference data record), which is equivalent to adding "|0". The result is 0|1|0. Similarly, insert data between 0|1 and 1. Since 0|1 is relatively long, use it as the standard for comparison. To insert data after 0|1, add "|higher number" (i.e., the sorting field value of the first baseline data record after the reference data record), which is equivalent to adding "|1". The result is 0|1|1. The results are shown in Table 6.
[0088] Table 6
[0089] 0 0|1|0 0|1 0|1|1 1
[0090] In this embodiment, by setting a rule for the sorting field value of newly inserted data records that are not at the last position in an existing database table, and specifying the sorting field value of the data to be written based on this rule, the data records in the database table can be sorted in ascending order of the sorting field value after the data to be written and the corresponding sorting field value are written to the database table, resulting in a sorted database table, thus avoiding the need to modify the sorting field value of existing data records in the database table.
[0091] In one exemplary embodiment, data records in a database table are sorted in ascending order of sorting field values to obtain a sorted database table, including:
[0092] The database table is sorted by sequentially traversing each data record and sorting the traversed data record into the sorted data record, before the first sorted data record whose sort field value is greater than the sort field value of the traversed data record.
[0093] In determining the relative size of two sorting field values, the field values at the same position in both sorting field values are compared sequentially from left to right until the comparison termination condition is met. Alternatively, if the lengths of the two sorting field values are different, and the field values at the same position in the shorter sorting field value are all the same as those in the longer sorting field value, the last field value of the shorter sorting field value is compared sequentially with the uncompared field values in the longer sorting field value until the comparison termination condition is met. The comparison termination condition includes: if the compared field values are different, the relative size of the first different field value determines the relative size of the two sorting field values; or, if all field values have been compared and are the same, the longer sorting field value is larger.
[0094] For example, taking the comparison of a set of sorting field values in Table 6 as an example, the sorting field values are first divided according to the specified symbol "|" (a[0] represents the element with index 0 in array a, that is, the first sorting field value from left to right, a=[0] means a is 0, c=[0,1] means c is 0|1).
[0095] a = [0], b = [0, 1, 0], c = [0, 1], d = [0, 1, 1], e = [1]. At this time, a and c are compared first. The first digit a[0] = c[0], because a has only 1 digit while c has 2 digits. Now, a[0] and c[1] are compared. If a[0] < c[1], then c is placed after a (a < c). If a[0] > c[1] or a[0] = c[1], then a is placed after c (c < a). At this time, we can compare and find that a < c < e.
[0096] Comparing a and b, the first two digits are both 0, and the length of a is only 1. Comparing the last digit with the next digit of b, a[0] < b[1], so a < b. Comparing b and c, the length of c is less than b, and the first and second digits of b and c are equal. At this time, comparing b[3] and c[2], c[2] = 1 > b[3] = 0, so c > b. Similarly, comparing c and d, the first and second digits of d and c are equal, and c[2] = d[3]. According to the rule, if they are equal, the one with the larger length is greater, so d > c. Comparing b and d, the first two digits are equal, and the third digit d[3] > b[3], so d > b. Comparing d and a, the first two digits of d and a are equal, and the length of a is only 1. Comparing the last digit with the next digit of d, d[1] > a[0], so d > a.
[0097] In this embodiment, adding a new sorting field will not affect the existing sorting field. Data can be inserted in the middle of a record without shifting the sorting of subsequent records. This requires minimal modification to the database script and is more secure.
[0098] As an optional exemplary embodiment of this application, the data recording method of the database table in this application embodiment is explained by taking the continuous insertion of data into two existing baseline data records in the database table as an example.
[0099] This application proposes a new database table record sorting scheme, specifically including a database table sorting field maintenance rule and a sorting rule, which aims to ensure that the insertion of a record does not affect other existing records. Here, the sorting field value of the baseline data record is the initial value.
[0100] If a database table already has sort fields A and B (where A and B are the initial values of the base data record), and A < B, then record A is the lower number (the smaller value in A and B) and B is the higher number (the larger value in A and B). If a record is to be inserted between A and B, then the sort field value of this record will be A|B, and the existing sort will be [A, A|B, B]. If a new record is to be inserted between A and A|B, then the sort field value of the new record will be A|B|A, and the existing sort will be [A, A|B|A, A|B, B]. If a record is to be inserted between A|B and B, then the sort field value of the new record will be A|B|B, and the existing sort will be [A, A|B|A, A|B]. The rule is that A is the lower number and B is the higher number. The initial sorting field value between A and B is denoted as A|B. If two numbers are inserted later, the one with the larger length after splitting by "|" is taken as the standard number. If the inserted number is greater than the standard number, "|B" is added to the standard number. If the inserted number is less than the one with the larger length, "|A" is added to the standard number. The comparison rule is to split by "|" and compare the same position. If A|B and A|B|B are compared, the first two subscripts are compared and they are equal. If A|B|B has a third number, the last number B after splitting A|B is compared with the third number of A|B|B. If they are equal or less, A|B|B is greater than A|B. If it is greater, A|B is greater than A|B|B.
[0101] That is, in this embodiment, the insertion principle is to take the longer segment based on "|" as the standard number. If you want to insert to the left of the standard number, you add the standard number plus "|lower number" as the sorting value of the new record. If you want to insert to the right of the standard number, you add the standard number plus "|higher number" as the sorting value of the new record.
[0102] Sorting field value comparison rules: For comparison of the same length, start comparing the first digit (counting from left to right). Stop comparing when a digit can be compared. Determine the size based on the value at that digit. The digit with the larger value is larger.
[0103] As shown in the figure, when comparing [A, B, B, A] and [A, B, A, A], the first and second digits are the same. When comparing the third digit, B is greater than A, so the comparison ends. Then we can determine that [A, B, B, A] is greater than [A, B, A, A].
[0104] When comparing numbers of different lengths, first compare the values of the same digits, starting from the first digit. Stop comparing when you encounter a digit where the size can be determined. The larger digit is the larger number. For example, [A, B, B, A] and [A, B, A, B, B, B], if the first and second digits are equal, then when you reach the third digit, B is greater than A. Therefore, [A, B, B, A] is greater than [A, B, A, B, B, B].
[0105] If the digits in the same place value cannot be compared, compare the last digit of the shorter digit with the next digit of the longer digit. If the last digit of the shorter digit is greater than the next digit of the longer digit, that is, the shorter digit is after the longer digit (shorter is greater than longer), for example, a = [A, B, B] and b = [A, B, B, A, B], the first three digits are equal, but a[2] > b[3], so a is greater than b. If the last digit of the shorter digit is less than the next digit of the longer digit, that is, the shorter digit is before the longer digit (shorter is less than longer), for example, a = [A, B, A] and b = [A, B, A, B], the first three digits are equal, but a[2] < b[3], so a is less than b. If the last digit of the shorter digit is equal to the next digit of the longer digit, that is, the longer digit is after the shorter digit, then the shorter digit is less than the longer digit.
[0106] This application allows for a new way of recording table records in the sorting field. The sorting rule is as follows: first, determine the lower and higher numbers, that is, the larger number in the initial values is the higher number and the smaller number is the lower number. The initial values are A and B, which are inserted for the first time. The first insertion value is A|B. Subsequent insertions take the longer number as the standard number based on the length of the | split. If you want to insert to the left of the standard number, add the standard number + "|lower number" as the sorting value of the new record. If you want to insert to the right of the standard number, add the standard number + "|higher number" as the sorting value of the new record. The size comparison rules are as follows: For numbers of the same length, the comparison starts from the first index and stops when a digit can be compared. The larger digit is the larger number. For numbers of different lengths, the comparison starts from the first index and stops when a digit can be compared. The larger digit is the larger number. If digits of the same length cannot be compared, the last digit of the shorter number is compared with the next digit of the longer number. If the last digit of the shorter number is greater than the next digit of the longer number (i.e., the shorter number is after the longer number, and the longer number is less than the shorter number), the comparison is more convenient for developers and avoids sorting problems caused by multiple users modifying database records.
[0107] Through the above description of the embodiments, those skilled in the art can clearly understand that the methods according to the above embodiments can be implemented by means of software plus necessary general-purpose hardware platforms. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of this application, 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 is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk) and includes several instructions to cause a terminal device (which may be a mobile phone, computer, server, or network device, etc.) to execute the methods of the various embodiments of this application.
[0108] According to another aspect of the embodiments of this application, a data recording device for a database table is also provided. This device is used to implement the data recording method for a database table provided in the above embodiments, and will not be repeated hereafter. As used below, the term "module" can be a combination of software and / or hardware that implements a predetermined function. Although the device described in the following embodiments is preferably implemented in software, hardware implementation, or a combination of software and hardware, is also possible and contemplated.
[0109] Figure 6 This is a structural block diagram of an optional database table data recording device provided in an embodiment of this application, such as... Figure 6 As shown, the device includes:
[0110] The first acquisition unit 602 is used to acquire the first data to be written, wherein the first data to be written is the data to be inserted into the database table between the adjacent first data record and the second data record;
[0111] The first determining unit 604 is used to determine a first sorting field value corresponding to the first data to be written based on the sorting field value of the reference data record. The reference data record is one of the first data record and the second data record. The first sorting field value is obtained by adding a first target value to the sorting field value of the reference data record. The first target value is determined according to the sequential relationship between the data record corresponding to the first data to be written and the reference data record. The sorting field value of the data record in the database table is used to sort the data records in the database table.
[0112] The writing unit 606 is used to insert the first data to be written into the database table based on the value of the first sorting field, so as to obtain the target data record corresponding to the first data to be written.
[0113] It should be noted that the first acquisition unit 602 in this embodiment can be used to execute the above step S202, the first determination unit 604 in this embodiment can be used to execute the above step S204, and the writing unit 606 in this embodiment can be used to execute the above step S206.
[0114] Through the embodiments of this application, a first data to be written is obtained, wherein the first data to be written is data to be inserted into the database table between adjacent first and second data records; based on the sorting field value of a reference data record, a first sorting field value corresponding to the first data to be written is determined, wherein the reference data record is one of the first and second data records, and the first sorting field value is obtained by adding a first target value to the sorting field value of the reference data record, the first target value being determined according to the sequential relationship between the data record corresponding to the first data to be written and the reference data record, and the sorting field value of the data record in the database table is used to sort the data records in the database table; based on the first sorting field value, the first data to be written is inserted into the database table to obtain the target data record corresponding to the first data to be written, thereby solving the problem that the data recording method of the database table in the related art is prone to sorting disorder.
[0115] In one exemplary embodiment, the above-described apparatus further includes:
[0116] The second determining unit is used to determine the data record with the longest sorting field value between the first data record and the second data record as the reference data record before determining the first sorting field value corresponding to the first data to be written based on the sorting field value of the reference data record.
[0117] The third determining unit is used to determine the data record that appears earlier in the first data record and the second data record as the reference data record when the length of the sorting field value of the first data record is the same as the length of the sorting field value of the second data record.
[0118] In one exemplary embodiment, the first determining unit includes:
[0119] The first execution module is used to concatenate the first target value after the sorting field value of the reference data record and split it by a specified symbol to obtain the first sorting field value.
[0120] In an exemplary embodiment, the last data record inserted into the database table is the base data record, and the sorting field value of the base data record in the database table increases sequentially starting from a first specified value;
[0121] The above-mentioned device also includes:
[0122] The fourth determining unit is used to determine the sorting field value of the first reference data record before the reference data record as the first target value before the first data to be written is inserted into the reference data record, in the case that before the first data to be written is determined based on the sorting field value of the reference data record and before the first sorting field value corresponding to the first data to be written is determined;
[0123] The fifth determining unit is used to determine the sorting field value of the first benchmark data record after the reference data record as the first target value when the first data to be written is to be inserted after the reference data record.
[0124] In an exemplary embodiment, each data record inserted at the end of the database table is a baseline data record, and the sorting field value of the baseline data record in the database table is sequentially increased starting from a first specified value;
[0125] The above-mentioned device also includes:
[0126] The second acquisition unit is used to acquire the second data to be written after inserting the first data to be written into the database table based on the value of the first sorting field and obtaining the target data record corresponding to the first data to be written, wherein the second data to be written is the last data to be written into the database table.
[0127] An execution unit is used to add a second specified value to the sorting field value of the last data record in the database table to obtain the second sorting field value;
[0128] The insertion unit is used to insert the second data to be written into the end of the database table based on the value of the second sorting field, so as to obtain the data record corresponding to the second data to be written.
[0129] In an exemplary embodiment, each data record inserted at the end of the database table is a baseline data record, and the sorting field value of the baseline data record in the database table is sequentially increased starting from a first specified value;
[0130] When inserting data between two data records of the same length with the same length of sort field value, the sort field value of the inserted data is obtained by concatenating the sort field value of the first data record of the same length with the sort field value of the second data record of the same length, and then splitting it by a specified symbol.
[0131] When inserting data between two data records of different lengths with different sort field values, the sort field value of the inserted data is obtained by concatenating the second target value after the sort field value of the target data record with the longest sort field value and splitting it using a specified symbol. Specifically, when the inserted data is to be inserted before the reference data record, the second target value is the sort field value of the first reference data record before the target data record. When the inserted data is to be inserted after the reference data record, the second target value is the sort field value of the first reference data record after the target data record.
[0132] After inserting the first data to be written into the database table based on the value of the first sorting field to obtain the target data record corresponding to the first data to be written, the above-mentioned device further includes:
[0133] A sorting unit is used to sort data records in a database table in ascending order of the sorting field value, resulting in a sorted database table.
[0134] In one exemplary embodiment, the sorting unit includes:
[0135] The second execution module is used to sequentially traverse each data record in the database table and sort the traversed data records into the sorted data records, before the first sorted data record whose sort field value is greater than the sort field value of the traversed data record, thus obtaining the sorted database table;
[0136] In determining the relative size of two sorting field values, the field values at the same position in both sorting field values are compared sequentially from left to right until the comparison termination condition is met. Alternatively, if the lengths of the two sorting field values are different, and the field values at the same position in the shorter sorting field value and the longer sorting field value are all the same, the last field value of the shorter sorting field value is compared sequentially with the uncompared field values in the longer sorting field value until the comparison termination condition is met. The comparison termination condition includes: if the compared field values are different, the relative size of the first different field value is used to determine the relative size of the two sorting field values; or if all field values have been compared and are the same, the longer sorting field value is considered larger.
[0137] It should be noted that the above modules can be implemented by software or hardware. For the latter, they can be implemented in the following ways, but are not limited to: all the above modules are located in the same processor; or, the above modules are located in different processors in any combination.
[0138] Embodiments of this application also provide a computer-readable storage medium storing a computer program, wherein the computer program is configured to execute the steps in any of the above method embodiments when run.
[0139] In one exemplary embodiment, the aforementioned computer-readable storage medium may include, but is not limited to, various media capable of storing computer programs, such as USB flash drives, ROM (Read-Only Memory), RAM (Random Access Memory), portable hard drives, magnetic disks, or optical disks.
[0140] Embodiments of this application also provide an electronic device, including a memory and a processor, wherein the memory stores a computer program and the processor is configured to run the computer program to perform the steps in any of the above method embodiments.
[0141] In one exemplary embodiment, the electronic device may further include a transmission device and an input / output device, wherein the transmission device is connected to the processor and the input / output device is connected to the processor.
[0142] Specific examples in this embodiment can be found in the examples described in the above embodiments and exemplary implementations, and will not be repeated here.
[0143] Embodiments of this application also provide a computer program product, which includes a computer program that, when executed by a processor, implements the steps in any of the above method embodiments.
[0144] Embodiments of this application also provide another computer program product, including a non-volatile computer-readable storage medium storing a computer program that, when executed by a processor, implements the steps in any of the above method embodiments.
[0145] The embodiments described herein also provide a computer program that includes computer instructions stored in a computer-readable storage medium; a processor of a computer device reads the computer instructions from the computer-readable storage medium and executes the computer instructions, causing the computer device to perform the steps in any of the above method embodiments.
[0146] Obviously, those skilled in the art should understand that the modules or steps of this application described above can be implemented using general-purpose computing devices. They can be centralized on a single computing device or distributed across a network of multiple computing devices. They can be implemented using computer-executable program code, and thus can be stored in a storage device for execution by a computing device. In some cases, the steps shown or described can be performed in a different order than those presented here, or they can be fabricated as separate integrated circuit modules, or multiple modules or steps can be fabricated as a single integrated circuit module. Thus, this application is not limited to any particular combination of hardware and software.
[0147] The above description is merely a preferred embodiment of this application and is not intended to limit this application. Various modifications and variations can be made to this application by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the principles of this application should be included within the protection scope of this application.
Claims
1. A method for recording data in a database table, characterized in that, include: Obtain the first data to be written, wherein the first data to be written is the data to be inserted into the database table between the first data record and the second data record that are adjacent to each other; If the length of the sorting field value of the first data record is different from the length of the sorting field value of the second data record, the data record with the longest sorting field value between the first data record and the second data record shall be determined as the reference data record. If the length of the sorting field value of the first data record is the same as the length of the sorting field value of the second data record, the data record that appears earlier in the first data record and the second data record shall be determined as the reference data record. When the first data to be written is to be inserted before the reference data record, the sorting field value of the first benchmark data record before the reference data record is determined as the first target value, wherein the last data record inserted in the database table is the benchmark data record, and the sorting field value of the benchmark data record in the database table increases sequentially starting from the first specified value; When the first data to be written is to be inserted after the reference data record, the sorting field value of the first benchmark data record after the reference data record is determined as the first target value; Determining a first sorting field value corresponding to the first data to be written based on the sorting field value of the reference data record includes: concatenating the first target value after the sorting field value of the reference data record and splitting it by a specified symbol to obtain the first sorting field value, wherein the reference data record is one of the first data record and the second data record, the first sorting field value is obtained by adding the first target value to the sorting field value of the reference data record, the first target value is determined according to the sequential relationship between the data record corresponding to the first data to be written and the reference data record, and the sorting field value of the data record in the database table is used to sort the data records in the database table; The first data to be written is inserted into the database table based on the first sorting field value, thereby obtaining the target data record corresponding to the first data to be written.
2. The method according to claim 1, characterized in that, Each time a data record is inserted at the end of the database table, it is a base data record. The sorting field value of the base data record in the database table increases sequentially starting from a first specified value. After inserting the first data to be written into the database table based on the first sorting field value to obtain the target data record corresponding to the first data to be written, the method further includes: Obtain the second data to be written, wherein the second data to be written is the data to be written to the last table of the database; A second specified value is added to the sorting field value of the last data record in the database table, and this value is determined as the second sorting field value. Based on the value of the second sorting field, the second data to be written is inserted into the end of the database table to obtain a data record corresponding to the second data to be written.
3. The method according to claim 1, characterized in that, Each time a data record is inserted at the end of the database table, it is a base data record. The sorting field value of the base data record in the database table increases sequentially starting from a first specified value. When inserting data between two data records of the same length with the same length of sort field value, the sort field value of the inserted data is obtained by concatenating the sort field value of the first data record of the same length with the sort field value of the second data record of the same length, and then splitting it by a specified symbol. When inserting data between two data records of different lengths with different sort field values, the sort field value of the inserted data is obtained by concatenating a second target value after the sort field value of the target data record with the longest sort field value among the two data records, and then splitting it by a specified symbol. Specifically, when the inserted data is to be inserted before the reference data record, the second target value is the sort field value of the first reference data record before the target data record. When the inserted data is to be inserted after the reference data record, the second target value is the sort field value of the first reference data record after the target data record. After inserting the first data to be written into the database table based on the first sorting field value to obtain the target data record corresponding to the first data to be written, the method further includes: The data records in the database table are sorted in ascending order of the sorting field value to obtain the sorted database table.
4. The method according to claim 3, characterized in that, The step of sorting the data records in the database table according to the sorting field value in ascending order to obtain the sorted database table includes: The database table is sorted by sequentially traversing each data record in the database table and sorting the traversed data records into the sorted data records, before the first sorted data record whose sort field value is greater than the sort field value of the traversed data record, thus obtaining the sorted database table; In determining the size relationship between two sorting field values, the field values at the same position in the two sorting field values are compared sequentially from left to right until the comparison termination condition is met. Alternatively, if the lengths of the two sorting field values are different, and the field values at the same position in the shorter sorting field value and the longer sorting field value are all the same, the last field value of the shorter sorting field value is compared sequentially with the uncompared field values in the longer sorting field value until the comparison termination condition is met. The comparison termination condition includes: if the compared field values are different, the size relationship of the two sorting field values is determined by the size relationship of the first compared field value that is different; if all field values have been compared and the compared field values are the same, the longer sorting field value is larger.
5. A data recording device for a database table, characterized in that, include: The first acquisition unit is used to acquire the first data to be written, wherein the first data to be written is the data to be inserted into the database table between the adjacent first data record and the second data record; The second determining unit is used to determine the data record with the longest sorting field value among the first data record and the second data record as the reference data record when the length of the sorting field value of the first data record is different from the length of the sorting field value of the second data record. The third determining unit is used to determine the data record that appears earlier in the first data record and the second data record as the reference data record when the length of the sorting field value of the first data record is the same as the length of the sorting field value of the second data record. The fourth determining unit is used to determine the sorting field value of the first benchmark data record before the reference data record as a first target value when the first data to be written is to be inserted before the reference data record. The last data record inserted in the database table is the benchmark data record, and the sorting field value of the benchmark data record in the database table increases sequentially starting from the first specified value. The fifth determining unit is used to determine the sorting field value of the first benchmark data record after the reference data record as the first target value when the first data to be written is to be inserted after the reference data record. The first determining unit is configured to determine a first sorting field value corresponding to the first data to be written based on the sorting field value of the reference data record, comprising: a first execution module, configured to concatenate a first target value after the sorting field value of the reference data record and divide it by a specified symbol to obtain the first sorting field value, wherein the reference data record is one of the first data record and the second data record, the first sorting field value is obtained by adding the first target value to the sorting field value of the reference data record, the first target value is determined according to the sequential relationship between the data record corresponding to the first data to be written and the reference data record, and the sorting field value of the data record in the database table is used to sort the data records in the database table; The writing unit is used to insert the first data to be written into the database table based on the value of the first sorting field, so as to obtain the target data record corresponding to the first data to be written.
6. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program, wherein the computer program, when executed by a processor, implements the method of any one of claims 1 to 4.
7. 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 method of any one of claims 1 to 4.
Citation Information
Patent Citations
Data processing method and device of data table, equipment and storage medium
CN112181995A
Data sorting method and device, electronic equipment and storage medium
CN116150144A