Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

MySQL Merge engine implements sub-table

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

The Merge engine is a combination of a group of MyISAM tables, and the combined sub-table structure must be exactly the same. The Merge table itself has no data, and the operation on the Merge table is actually an operation on the child table, but it is transparent to APP. When inserting, it is inserted into the last table by default, or you can specify that it is inserted into the first table. Merger table is actually just a shell of multiple child tables and deletes it without affecting the actual stored data.

Create a child table user1

CREATE TABLE `user1` (`id` int (11) NOT NULL AUTO_INCREMENT, `name` varchar (100th) CHARACTER SET latin1 DEFAULT NULL, PRIMARY KEY (`id`) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8

Create a child table user2

CREATE TABLE `user2` (`id` int (11) NOT NULL AUTO_INCREMENT, `name` varchar (100th) CHARACTER SET latin1 DEFAULT NULL, PRIMARY KEY (`id`) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8

Create Merge table alluser

CREATE TABLE `alluser` (`id` int (11) NOT NULL, `name` varchar (100) DEFAULT NULL, PRIMARY KEY (`id`) ENGINE=MRG_MyISAM DEFAULT CHARSET=latin1 INSERT_METHOD=LAST UNION= (`user1`, `user2`)

Now insert data into alluser

Then look at the data of the user2 table and find that it has been inserted.

The next step is to insert the subtable, and then look at the alluser table

It is found that the insert operation of the child table will eventually be mapped to the alluser table, which realizes the sub-table in a certain sense, but it also has limitations, that is, the data distribution is uneven, because the rules cannot be customized during insertion, only the child tables specified at the time of creation can be inserted, and there are checks without constraints. For example, if id is set as the primary key in all three tables, we insert a piece of data that already exists in user1 in user2. However, the alluser table does not report an error and is inserted successfully

For these reasons, there are not many sub-tables implemented by Merge engine in practical application.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report