merge合並表的要求
1.合並的表使用的必須是MyISAM引擎
2.表的結構必須一致,包括索引、字段類型、引擎和字符集
實例:
create table if not exists user1(
id int(11) not null auto_increment,
name varchar(50) default null,
sex int(1) not null default 0,
primary key (id)
)engine = MyISAM default charset = utf8 auto_increment=1;
create table if not exists user2(
id int(11) not null auto_increment,
name varchar(50) default null,
sex int(1) not null default 0,
primary key (id)
)engine = MyISAM default charset = utf8 auto_increment=1;
create table if not exists alluser(
id int(11) not null auto_increment,
name varchar(50) default null,
sex int(1) not null default 0,
primary key (id)
)engine = merge union=(user1,user2) insert_method = last auto_increment=1;
執行insert into alluser (name,sex) values ('tian',1);報錯如下:
ERROR 1168 (HY000): Unable to open underlying table which is differently defined or of non-MyISAM type or doesn't exist
百度了一下原來是默認字符集沒寫,修改如下
create table if not exists alluser1(
id int(11) not null auto_increment,
name varchar(50) default null,
sex int(1) not null default 0,
primary key (id)
)engine = merge union=(user1,user2) insert_method = last auto_increment=1 default charset=utf8;
執行insert into alluser1 (name,sex) values ('tian',1);成功
執行select * from alluser1;顯示如下:
+----+------+-----+
| id | name | sex |
+----+------+-----+
| 1 | tian | 1 |
+----+------+-----+
執行select * from user2;顯示如下:
+----+------+-----+
| id | name | sex |
+----+------+-----+
| 1 | tian | 1 |
+----+------+-----+