mysql用merge合并表


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 |
+----+------+-----+

优质内容筛选与推荐>>
1、spring
2、MongoDB 安装和配置
3、OpenCasCade经典问答之几何造型(1)
4、打印函数调用堆栈
5、C#中刷新页面的方式


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号