mysql分表操作
---创建源数据表,用来往分表插入测试数据
create table member(
id bigint auto_increment primary key,name varchar(20),
sex tinyint not null default '0'
)engine=myisam default charset=utf8 auto_increment=1;
---编造数据
insert into member(id,name,sex) values (1,'jacson','0');
insert into member(name,sex) select name,sex from member;
insert into member(name,sex) select name,sex from member;
insert into member(name,sex) select name,sex from member;
-----/*分表*/----------
----创建两个分表tb_member1,tb_member2
DROP table IF EXISTS tb_member1;
create table tb_member1(
id bigint primary key auto_increment ,
name varchar(20),
sex tinyint not null default '0'
)ENGINE=MyISAM/*不支持事物处理*/ DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
DROP table IF EXISTS tb_member2;
create table tb_member2(
id bigint primary key auto_increment ,
name varchar(20),
sex tinyint not null default '0'
)ENGINE=MyISAM/*不支持事物处理*/ DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
---创建主表tb_member
DROP table IF EXISTS tb_member;
create table tb_member(
id bigint primary key auto_increment ,
name varchar(20),
sex tinyint not null default '0'
)ENGINE=MERGE UNION=(tb_member1,tb_member2) INSERT_METHOD=LAST CHARSET=utf8 AUTO_INCREMENT=1 ;
----查看一下tb_member表的结构:desc tb_member;
----把数据分到两个分表中去:
insert into tb_member1(id,name,sex) select id,name,sex from member where id%2=0;
insert into tb_member2(id,name,sex) select id,name,sex from member where id%2=1;
-----查看一下主表的数据:select * from tb_member;