Home  >  Article  >  Database  >  mysql-merge合并表_MySQL

mysql-merge合并表_MySQL

WBOY
WBOYOriginal
2016-06-01 13:30:361203browse

bitsCN.com

mysql-merge合并表

 

[sql] 

注意:  

1  每个子表的结构必须一致,主表和子表的结构需要一致,  

  

2  每个子表的索引在merge表中都会存在,所以在merge表中不能根据该索引进行唯一性检索。  

  

3  子表需要是MyISAM引擎  

4  AUTO_INCREMENT 不会按照你所期望的方式工作。  

  

建表语句  

  

create table tablename(正常的字段)engine=merge insert_method=last  

 

  

insert_method:  

  

有两个值如下:  

LAST  如果你执行insert 指令来操作merge表时,插入操作会把数据添加到最后一个子表中。  

  

FIRST  同理,执行插入数据时会把数据添加到第一个子表中。  

  

例子:  

  

create table user1(  id int(10) not null auto_increment,    name varchar(50),  sex int(1),    primary key(id)  )engine=myisam charset=utf8;    create table user2(    id int(10) not null auto_increment,    name varchar(50),  sex int(10)    ,primary key(id)  )engine=myisam charset=utf8;    insert into user1 (name,sex) values('张三',0);    insert into user2 (name,sex) values('lisi',1);    mysql> select * from user1;  +----+------+------+  | id | name | sex  |  +----+------+------+  |  1 | 张三 |    0 |  +----+------+------+    mysql> select * from user2;  +----+------+------+  | id | name | sex  |  +----+------+------+  |  1 | lisi |    1 |  +----+------+------+    create table alluser(    id int(10) not null auto_increment,  name varchar(50),    sex int(10),  index(id)    )type=merge union=(user1,user2) insert_method=last;    mysql> select * from alluser;  +----+------+------+  | id | name | sex  |  +----+------+------+  |  1 | 张三 |    0 |  |  1 | lisi |    1 |  +----+------+------+    mysql> insert into alluser(name,sex) values('嘿嘿',0);    mysql> select * from user1;  +----+------+------+  | id | name | sex  |  +----+------+------+  |  1 | 张三 |    0 |  +----+------+------+  1 row in set (0.00 sec)    mysql> select * from user2;  +----+------+------+  | id | name | sex  |  +----+------+------+  |  1 | lisi |    1 |  |  2 | 嘿嘿 |    0 |  +----+------+------+  

 

  

2 rows in set (0.00 sec)  

//他把这条数据存入了user2表里是因为我们的insert_method的参数填写的是last  

  mysql> update alluser set sex=replace(sex,0,1) where id=2;  +----+------+------+  | id | name | sex  |  +----+------+------+  |  1 | 张三 |    0 |  |  1 | lisi |    1 |  |  2 | 嘿嘿 |    1 |  +----+------+------+  

 


bitsCN.com
Statement:
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