Database-1
create table sample (
id INT,
nm VARCHAR(10)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
UNION=(for tables from another databases);
So, when we do union what actually it meance? Please explain, I am getting confusing for this type of UNION
.
é£ä¹,å½æ们èåèµ·æ¥å®é ä¸æ¯ä»ä¹æ¶åå¢?请解éä¸ä¸,æ对è¿ç§ç±»åçUNIONæå°å°æã
1 个解å³æ¹æ¡
#1
That looks close to the syntax for creating a merge table
, but it has the engine type wrong. Your statement will ignore the union clause and simply create a new, empty table. In order to create merge table
you need to specify ENGINE=MERGE
.
è¿çèµ·æ¥æ¥è¿äºå建å并表çè¯æ³,ä½å®çå¼æç±»åé误ãæ¨çè¯å¥å°å¿½ç¥unionåå¥,åªéå建ä¸ä¸ªæ°ç空表ãè¦å建å并表,æ¨éè¦æå®ENGINE = MERGEã
14.3 MERGEåå¨å¼æ
The MERGE storage engine, also known as the MRG_MyISAM engine, is a collection of identical MyISAM tables that can be used as one.
MERGEåå¨å¼æ,ä¹ç§°ä¸ºMRG_MyISAMå¼æ,æ¯å¯ä»¥ä½ä¸ºä¸ä¸ªä½¿ç¨çç¸åMyISAM表çéåã
The tables you specify in the UNION
clause there, must all be identical - ie, having the same index and column specification, and they must all be in the same order in each table.
æ¨å¨é£éçUNIONåå¥ä¸æå®çè¡¨å¿ é¡»å ¨é¨ç¸å - å³å ·æç¸åçç´¢å¼ååè§è,并ä¸å®ä»¬å¿ é¡»å¨æ¯ä¸ªè¡¨ä¸ç顺åºç¸åã
After that, can you query your merge table and access the data from all of the tables that form it.
ä¹å,æ¨å¯ä»¥æ¥è¯¢å并表并访é®ææå®çææ表ä¸çæ°æ®ã
You can also insert into your merge table, which is something you cannot do with a view:
æ¨ä¹å¯ä»¥æå ¥å°å并表ä¸,è¿æ¯æ¨æ æ³å¯¹è§å¾æ§è¡çæä½:
You can optionally specify an INSERT_METHOD option to control how inserts into the MERGE table take place. Use a value of FIRST or LAST to cause inserts to be made in the first or last underlying table, respectively. If you specify no INSERT_METHOD option or if you specify it with a value of NO, inserts into the MERGE table are not permitted and attempts to do so result in an error.
æ¨å¯ä»¥éæ©æå®INSERT_METHODé项æ¥æ§å¶å¦ä½è¿å ¥MERGE表çæå ¥ã使ç¨FIRSTæLASTå¼åå«å¯¼è´å¨ç¬¬ä¸ä¸ªææåä¸ä¸ªåºç¡è¡¨ä¸è¿è¡æå ¥ãå¦ææªæå®INSERT_METHODé项æè 使ç¨å¼NOæå®å®,åä¸å 许æå ¥MERGE表,å°è¯è¿æ ·åä¼å¯¼è´é误ã
Anyway, the doco has the rest of the information if you want to peruse more - I've never felt the need to use this type of table.
æ 论å¦ä½,å¦æä½ æ³è¦æ´å¤å°é 读,docoè¿æå ¶ä»çä¿¡æ¯ - æä»æ¥æ²¡æè§å¾éè¦ä½¿ç¨è¿ç§ç±»åç表ã
Example:
mysql>
mysql> create table t2 (
-> id integer primary key auto_increment,
-> val char(20)
-> ) engine=myisam;
Query OK, 0 rows affected (0.05 sec)
mysql>
mysql> insert into t1(val) values ('table1 a'), ('table1 b');
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> insert into t2(val) values ('table2 a'), ('table2 b');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql>
mysql>
mysql> create table mt (
-> id integer primary key auto_increment,
-> val char(20)
-> ) engine=merge union=(t1,t2) insert_method=last;
Query OK, 0 rows affected (0.04 sec)
mysql>
mysql> select * from mt;
+----+----------+
| id | val |
+----+----------+
| 1 | table1 a |
| 2 | table1 b |
| 1 | table2 a |
| 2 | table2 b |
+----+----------+
4 rows in set (0.00 sec)
mysql> insert into mt(val) values ('12345');
Query OK, 1 row affected (0.00 sec)
mysql> select * from mt;
+----+----------+
| id | val |
+----+----------+
| 1 | table1 a |
| 2 | table1 b |
| 1 | table2 a |
| 2 | table2 b |
| 3 | 12345 |
+----+----------+
5 rows in set (0.01 sec)
mysql> select * from t2;
+----+----------+
| id | val |
+----+----------+
| 1 | table2 a |
| 2 | table2 b |
| 3 | 12345 |
+----+----------+
3 rows in set (0.00 sec)
#1
That looks close to the syntax for creating a merge table
, but it has the engine type wrong. Your statement will ignore the union clause and simply create a new, empty table. In order to create merge table
you need to specify ENGINE=MERGE
.
è¿çèµ·æ¥æ¥è¿äºå建å并表çè¯æ³,ä½å®çå¼æç±»åé误ãæ¨çè¯å¥å°å¿½ç¥unionåå¥,åªéå建ä¸ä¸ªæ°ç空表ãè¦å建å并表,æ¨éè¦æå®ENGINE = MERGEã
14.3 MERGEåå¨å¼æ
The MERGE storage engine, also known as the MRG_MyISAM engine, is a collection of identical MyISAM tables that can be used as one.
MERGEåå¨å¼æ,ä¹ç§°ä¸ºMRG_MyISAMå¼æ,æ¯å¯ä»¥ä½ä¸ºä¸ä¸ªä½¿ç¨çç¸åMyISAM表çéåã
The tables you specify in the UNION
clause there, must all be identical - ie, having the same index and column specification, and they must all be in the same order in each table.
æ¨å¨é£éçUNIONåå¥ä¸æå®çè¡¨å¿ é¡»å ¨é¨ç¸å - å³å ·æç¸åçç´¢å¼ååè§è,并ä¸å®ä»¬å¿ é¡»å¨æ¯ä¸ªè¡¨ä¸ç顺åºç¸åã
After that, can you query your merge table and access the data from all of the tables that form it.
ä¹å,æ¨å¯ä»¥æ¥è¯¢å并表并访é®ææå®çææ表ä¸çæ°æ®ã
You can also insert into your merge table, which is something you cannot do with a view:
æ¨ä¹å¯ä»¥æå ¥å°å并表ä¸,è¿æ¯æ¨æ æ³å¯¹è§å¾æ§è¡çæä½:
You can optionally specify an INSERT_METHOD option to control how inserts into the MERGE table take place. Use a value of FIRST or LAST to cause inserts to be made in the first or last underlying table, respectively. If you specify no INSERT_METHOD option or if you specify it with a value of NO, inserts into the MERGE table are not permitted and attempts to do so result in an error.
æ¨å¯ä»¥éæ©æå®INSERT_METHODé项æ¥æ§å¶å¦ä½è¿å ¥MERGE表çæå ¥ã使ç¨FIRSTæLASTå¼åå«å¯¼è´å¨ç¬¬ä¸ä¸ªææåä¸ä¸ªåºç¡è¡¨ä¸è¿è¡æå ¥ãå¦ææªæå®INSERT_METHODé项æè 使ç¨å¼NOæå®å®,åä¸å 许æå ¥MERGE表,å°è¯è¿æ ·åä¼å¯¼è´é误ã
Anyway, the doco has the rest of the information if you want to peruse more - I've never felt the need to use this type of table.
æ 论å¦ä½,å¦æä½ æ³è¦æ´å¤å°é 读,docoè¿æå ¶ä»çä¿¡æ¯ - æä»æ¥æ²¡æè§å¾éè¦ä½¿ç¨è¿ç§ç±»åç表ã
Example:
mysql>
mysql> create table t2 (
-> id integer primary key auto_increment,
-> val char(20)
-> ) engine=myisam;
Query OK, 0 rows affected (0.05 sec)
mysql>
mysql> insert into t1(val) values ('table1 a'), ('table1 b');
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> insert into t2(val) values ('table2 a'), ('table2 b');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql>
mysql>
mysql> create table mt (
-> id integer primary key auto_increment,
-> val char(20)
-> ) engine=merge union=(t1,t2) insert_method=last;
Query OK, 0 rows affected (0.04 sec)
mysql>
mysql> select * from mt;
+----+----------+
| id | val |
+----+----------+
| 1 | table1 a |
| 2 | table1 b |
| 1 | table2 a |
| 2 | table2 b |
+----+----------+
4 rows in set (0.00 sec)
mysql> insert into mt(val) values ('12345');
Query OK, 1 row affected (0.00 sec)
mysql> select * from mt;
+----+----------+
| id | val |
+----+----------+
| 1 | table1 a |
| 2 | table1 b |
| 1 | table2 a |
| 2 | table2 b |
| 3 | 12345 |
+----+----------+
5 rows in set (0.01 sec)
mysql> select * from t2;
+----+----------+
| id | val |
+----+----------+
| 1 | table2 a |
| 2 | table2 b |
| 3 | 12345 |
+----+----------+
3 rows in set (0.00 sec)