Home Database Mysql Tutorial mysqlmerge表介绍_MySQL

mysqlmerge表介绍_MySQL

May 31, 2016 am 08:46 AM

在Mysql数据库中,Mysql Merge表有点类似于视图。下面就让我们来一起了解一下Mysql Merge表都有哪些优点,希望对您能有所帮助。

Mysql Merge表的优点:
A: 分离静态的和动态的数据
B:利用结构接近的的数据来优化查询
C: 查询时可以访问更少的数据
D:更容易维护大数据集
E: 可以通过修改.mrg文件来修改Merge表,当然也可以用alter进行修改,修改后要通过FLUSH TABLES刷新表缓存,此法可以动态增加减少子表
创建方法,例:
mysql>CREATE TABLE t1(a INT NOT NULL PRIMARY KEY)ENGINE=MyISAM;
mysql>CREATE TABLE t2(a INT NOT NULL PRIMARY KEY)ENGINE=MyISAM;
mysql>CREATE TABLE mrg(a INT NOT NULL PRIMARY KEY)ENGINE=MERGE UNION=(t1,t2) INSERT_METHOD=LAST;
下面添加测试数据
mysql>INSERT INTO t1(a) VALUES(1),(2);
mysql>INSERT INTO t2(a)VALUES(1),(2);
查询一下看看结果
mysql>SELECT a FROM mrg;
结果会显示出t1,t2两个表中的数据
+------+
| a |
+------+
| 1|
| 2|
| 1|
| 2|
+------+

对于merge表,需要注意的是
1。每个子表的结构必须一致,主表和子表的结构需要一致,
2。每个子表的索引在merge表中都会存在,所以在merge表中不能根据该索引进行唯一性检索。
3 子表需要是MyISAM引擎
4 REPLACE在merge表中不会工作
5 AUTO_INCREMENT 不会按照你所期望的方式工作。

创建Mysql Merge表的参数 INSERT_METHOD有几个参数 。
LAST 如果你执行insert 指令来操作merge表时,插入操作会把数据添加到最后一个子表中。FIRST 同理,执行插入数据时会把数据添加到第一个子表中。
比如本例,对merge表执行插入操作
mysql>INSERT INTO mrg(a)VALUES(18);
查询一下
mysql>SELECT a FROM t2;
结果你会发现18出现在t2表中。
---------------------------------------------------------------
如果你对mrg表或者子表进行了DROP操作,那将有可能会产生些不可预知的情况。
如果删除mrg表,那么各个子表间将不会有联系。但是如果删除其中的任一子表,对于GNU/LINUX来说,merge表结构及数据仍然存在。
mysql>DROP TABLE t1,t2;
mysql>SELECT a FROM mrg;
结果你会发现mrg表的查询结果不变。 

他将多个表在逻辑上当作一个表来查询。他建立后有两个文件,
.frm 表结构定义
.mrg union表的名字清单
--
-- merger表的结构 `test_merge`
--

CREATE TABLE IF NOT EXISTS `test_merge` (
`id` int(5) NOT NULL auto_increment,
`names` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MRG_MyISAM DEFAULT CHARSET=gb2312 INSERT_METHOD=LAST UNION=(`test_merge_1`,`test_merge_2`,`test_merge_3`);

--
-- 导出表中的数据 `test_merge`
--

INSERT INTO `test_merge` (`id`, `names`) VALUES
(1, 'aa'),
(1, 'bb'),
(1, 'cc');

-- --------------------------------------------------------

--
-- 基本表的结构 `test_merge_1`
--

CREATE TABLE IF NOT EXISTS `test_merge_1` (
`id` int(5) NOT NULL auto_increment,
`names` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=gb2312 AUTO_INCREMENT=3 ;

--
-- 导出表中的数据 `test_merge_1`
--

INSERT INTO `test_merge_1` (`id`, `names`) VALUES
(1, 'aa');

-- --------------------------------------------------------

--
--基本 表的结构 `test_merge_2`
--

CREATE TABLE IF NOT EXISTS `test_merge_2` (
`id` int(5) NOT NULL auto_increment,
`names` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=gb2312 AUTO_INCREMENT=2 ;

--
-- 导出表中的数据 `test_merge_2`
--

INSERT INTO `test_merge_2` (`id`, `names`) VALUES
(1, 'bb');

-- --------------------------------------------------------

--
-- 基本表的结构 `test_merge_3`
--

CREATE TABLE IF NOT EXISTS `test_merge_3` (
`id` int(5) NOT NULL auto_increment,
`names` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=gb2312 AUTO_INCREMENT=2 ;

--
-- 导出表中的数据 `test_merge_3`
--

INSERT INTO `test_merge_3` (`id`, `names`) VALUES
(1, 'cc');
1. 此表类似于SQL中的union机制。
2. 此表结构必须与基本表完全一致,包括列名、顺序。UNION表必须同属一个DATABASE。
3. 基本表类型必须是MyISAM。
4. 可以通过修改.mrg文件来修改MERGE表,每个基本表的名字占一行。注意:修改后要通过FLUSH TABLES刷新表缓存。
5. 对基本表的更改可以直接反映在此表上。
6. INSERT_METHOD的取值可以是: 0 不允许插入 FIRST 插入到UNION中的第一个表 LAST 插入到UNION中的最后一个表。(4.0之后可用)
7. 定义在它上面的约束没有任何作用,约束是由基本表控制的,例如两个基本表中存在着同样的一个Key值,那么在MERGE表中会有两个一样的Key值。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

How do I configure SSL/TLS encryption for MySQL connections? How do I configure SSL/TLS encryption for MySQL connections? Mar 18, 2025 pm 12:01 PM

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

How do you handle large datasets in MySQL? How do you handle large datasets in MySQL? Mar 21, 2025 pm 12:15 PM

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

How do you drop a table in MySQL using the DROP TABLE statement? How do you drop a table in MySQL using the DROP TABLE statement? Mar 19, 2025 pm 03:52 PM

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

How do you create indexes on JSON columns? How do you create indexes on JSON columns? Mar 21, 2025 pm 12:13 PM

The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.

How do you represent relationships using foreign keys? How do you represent relationships using foreign keys? Mar 19, 2025 pm 03:48 PM

Article discusses using foreign keys to represent relationships in databases, focusing on best practices, data integrity, and common pitfalls to avoid.

Explain InnoDB Full-Text Search capabilities. Explain InnoDB Full-Text Search capabilities. Apr 02, 2025 pm 06:09 PM

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

See all articles