Home > Database > Mysql Tutorial > body text

mysql-merge合并表

WBOY
Release: 2016-06-07 16:38:35
Original
1419 people have browsed it

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

merge表


注意:
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 |
+----+------+------+
Copy after login


作者:maildocgaojingru 发表于2013-8-26 17:32:27 原文链接

阅读:53 评论:0 查看评论

mysql-merge合并表

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!