Mysql inserts data into sub-tables, and mysql inserts data

Source: Internet
Author: User

Mysql inserts data into sub-tables, and mysql inserts data

Use mysql;
/* Create the original data table */
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;

/* Create a stored procedure to add some data to the original data table */
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 ('database basics ', I), concat ('basic knowledge of hard learn', I % 4), DATE_FORMAT (NOW (), '% Y-% m-% d % H: % I: % s '));

Set I = I + 1;
End while;
End;
$
Delimiter;

/* Call the Stored Procedure */
Call proc_articleinfo ();


/* Create a sub-table */
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 ';


/* Create a master table */
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 data to the sub-table */
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;

/* Query data */
Select * from articleinfo;
Select * from tb_articleinfo_0;
Select * from tb_articleinfo_1;
Select * from tb_articleinfo_2;
Select * from tb_articleinfo_3;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.