mysql實現插入資料到分表

來源:互聯網
上載者:User

標籤:format   where   mit   style   預存程序   資料表   title   roc   nod   

use mysql;
/*建立未經處理資料表*/
DROP TABLE IF EXISTS `articleinfo`;
CREATE TABLE `articleinfo`(
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`title` VARCHAR(20) DEFAULT NULL,
`content` VARCHAR(20) DEFAULT NULL,
`comment_time` VARCHAR(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

/*建立預存程序來新增部分資料到未經處理資料表*/
delimiter $$
drop procedure if exists proc_articleinfo;
create procedure proc_articleinfo()
begin
declare i int;
set i=1;
while(i<=32) do
insert into articleinfo(title,content,comment_time)
values (concat(‘資料庫基礎‘,i),concat(‘努力學習基礎知識‘,i%4),DATE_FORMAT(NOW(),‘%Y-%m-%d %H:%i:%S‘));

set i=i+1;
end while;
end;
$$
delimiter ;

/*調用預存程序*/
call proc_articleinfo();


/*建立分表 */
DROP TABLE IF EXISTS `tb_articleinfo_0`;
CREATE TABLE `tb_articleinfo_0`(
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`title` VARCHAR(20) DEFAULT NULL,
`content` VARCHAR(20) DEFAULT NULL,
`comment_time` VARCHAR(20) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
CREATE TABLE `tb_articleinfo_1` LIKE `tb_articleinfo_0`;
CREATE TABLE `tb_articleinfo_2` LIKE `tb_articleinfo_0`;
CREATE TABLE `tb_articleinfo_3` LIKE `tb_articleinfo_0`;


/*建立主表*/
DROP TABLE IF EXISTS `tb_articleinfo`;
CREATE TABLE `tb_articleinfo`(
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`title` VARCHAR(20) DEFAULT NULL,
`content` VARCHAR(20) DEFAULT NULL,
`comment_time` VARCHAR(20) DEFAULT NULL,
index(`id`)
) ENGINE=MRG_MYISAM UNION=(`tb_articleinfo_0`,`tb_articleinfo_1`,`tb_articleinfo_2`,`tb_articleinfo_3`) INSERT_METHOD=LAST DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;


/*插入資料到分表*/
insert into tb_articleinfo_0(title,content,comment_time)
select title,content,comment_time from articleinfo where id%4=0;

insert into tb_articleinfo_1(title,content,comment_time)
select title,content,comment_time from articleinfo where id%4=1;

insert into tb_articleinfo_2(title,content,comment_time)
select title,content,comment_time from articleinfo where id%4=2;

insert into tb_articleinfo_3(title,content,comment_time)
select title,content,comment_time from articleinfo where id%4=3;

commit;

/*查詢資料*/
select * from articleinfo;
select * from tb_articleinfo_0;
select * from tb_articleinfo_1;
select * from tb_articleinfo_2;
select * from tb_articleinfo_3;

mysql實現插入資料到分表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.