In the process of introducing MySQL's multi-version concurrency control In this article, we will focus on the row identifier
To put it simply, if there is a primary key or a non-empty unique index in the table and it consists of only one integer column, then you can use the Pay special attention to the keywords mentioned in the document: primary key, unique index, not empty, single column, and numeric type. Next, we will start from these perspectives and explore the mysterious hidden field 1. Primary key existsFirst, let's look at the case where the primary key is set and is of numeric type. Use the following statement to create a table: CREATE TABLE `table1` ( `id` bigint(20) NOT NULL PRIMARY KEY , `name` varchar(32) DEFAULT NULL )ENGINE=InnoDB; After inserting three test data, execute the following query statement and directly query select *,_rowid from table1 Check the execution results, It can be seen that when the primary key is set and the primary key field is of numeric type, Let’s review several keywords in the previously mentioned document and analyze them separately. Since the primary key must be a non-null field, let's take a look at the case where the primary key is a non-numeric field. The table is created as follows: CREATE TABLE `table2` ( `id` varchar(20) NOT NULL PRIMARY KEY , `name` varchar(32) DEFAULT NULL )ENGINE=InnoDB; Execute the same query above on 2. No primary key, but a unique index existsAfter testing the two types of primary keys above, let's look at the situation when there is no primary key in the table but a unique index exists. First, test the case where a non-empty unique index is added to a numeric field and create a table as follows: CREATE TABLE `table3` ( `id` bigint(20) NOT NULL UNIQUE KEY, `name` varchar(32) )ENGINE=InnoDB; The query executes fine, and The difference between a unique index and a primary key is that the field where the unique index is located can be CREATE TABLE `table4` ( `id` bigint(20) UNIQUE KEY, `name` varchar(32) )ENGINE=InnoDB; Execute the query statement. In this case, Similar to the primary key, we test the case where the unique index is added to a non-numeric field. Next, when creating a table, a unique index is added to the character type field and a not null constraint is added: CREATE TABLE `table5` ( `id` bigint(20), `name` varchar(32) NOT NULL UNIQUE KEY )ENGINE=InnoDB; The same query cannot display Based on the test results of the above three situations, we can conclude that when there is no primary key but a unique index exists, 3. There is a joint primary key or joint unique indexIn the above tests, we applied the primary key or unique index to a single column. What will happen if a composite primary key or composite unique index is used? Let’s take a look at the instructions in the official documentation first:
In simple terms, if a primary key exists and consists of only one column of numeric type, then the value of Based on this description, let's test the situation of the joint primary key. The following table is created with two columns of numeric type fields as the joint primary key: CREATE TABLE `table6` ( `id` bigint(20) NOT NULL, `no` bigint(20) NOT NULL, `name` varchar(32), PRIMARY KEY(`id`,`no`) )ENGINE=InnoDB; Execute the query whose results cannot be displayed to Similarly, this theory can also be applied to unique indexes. If a non-empty unique index is not composed of a single column, then 4. There are multiple unique indexes In MySQL, each table can have only one primary key, but can have multiple unique indexes. So if there are multiple unique indexes that meet the rules at the same time, which one will be referenced as the value of
To put it simply, if the first non-empty unique index in the table consists of only one integer type field, then In the following table, create two unique indexes that both meet the rules: CREATE TABLE `table8_2` ( `id` bigint(20) NOT NULL, `no` bigint(20) NOT NULL, `name` varchar(32), UNIQUE KEY(no), UNIQUE KEY(id) )ENGINE=InnoDB; Take a look at the results of executing the query statement: You can see that the value of So, if the first unique index created in the table does not meet the reference rules of CREATE TABLE `table9` ( `id` bigint(20) NOT NULL, `no` bigint(20) NOT NULL, `name` varchar(32), UNIQUE KEY `index1`(`id`,`no`), UNIQUE KEY `index2`(`id`) )ENGINE=InnoDB; Querying the table shows that although there is a single-column non-empty unique index, the first column selected sequentially does not meet the requirements, so If the order of the statements for creating the unique index above is reversed, 5. Primary key and unique index exist at the same time From the above example, we can see that the definition order of the unique index will determine which index will be applied Create two tables using the following statements. The only difference is the order in which the primary key and unique index are created: CREATE TABLE `table11` ( `id` bigint(20) NOT NULL, `no` bigint(20) NOT NULL, PRIMARY KEY(id), UNIQUE KEY (no) )ENGINE=InnoDB; CREATE TABLE `table12` ( `id` bigint(20) NOT NULL, `no` bigint(20) NOT NULL, UNIQUE KEY(id), PRIMARY KEY (no) )ENGINE=InnoDB; View the running results: It can be concluded that when both a qualified primary key and a unique index exist, 6. No primary key or unique index that meets the requirements In the above, we call In fact, CREATE TABLE `table10` ( `id` bigint(20), `name` varchar(32) )ENGINE=InnoDB; First, we need to find the mysql process ps -ef | grep mysqld As you can see, the mysql process Before we get started, we need to do some preparation. In Next we need to use gdb -p 2068 -ex 'p dict_sys->row_id=1' -batch Command execution results: Insert 3 rows of data into an empty table: INSERT INTO table10 VALUES (100000001, 'Hydra'); INSERT INTO table10 VALUES (100000002, 'Trunks'); INSERT INTO table10 VALUES (100000003, 'Susan'); View the data in the table. The corresponding Then use the gdb -p 2068 -ex 'p dict_sys->row_id=281474976710656' -batch Command execution results: Insert three more records into the table: INSERT INTO table10 VALUES (100000004, 'King'); INSERT INTO table10 VALUES (100000005, 'Queen'); INSERT INTO table10 VALUES (100000006, 'Jack'); Viewing all the data in the table, we can see that two of the three data inserted for the first time have been overwritten: Why does data coverage occur? Let's analyze this result. First, before the first data is inserted, When When the same Therefore, when the primary key or unique index in the table does not meet the requirements mentioned above, the implicit
This is the end of this article about the specific use of hidden columns in MySQL. For more relevant MySQL hidden columns, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Web page image optimization tools and usage tips sharing
>>: Example code for evenly distributing elements using css3 flex layout
1. Introduction The EXPLAIN statement provides in...
1. CSS file naming conventions Suggestion: Use le...
Overview Nginx load balancing provides upstream s...
There is such a requirement: an import button, cl...
Relationship between MySQL and MariaDB MariaDB da...
In web design, we often use arrows as decoration ...
Six steps to install MySQL (only the installation...
1. Property List Copy code The code is as follows:...
1. Create a sequence table CREATE TABLE `sequence...
Table of contents 1. Implementation process 2. Di...
In higher versions of Tomcat, the default mode is...
Table of contents Lock Overview Lock classificati...
This article introduces the effect of website pro...
Table of contents 1. How to obtain different view...
1: Tag selector The tag selector is used for all ...