MYSQL批量建立表的儲存過程

2022-08-29 01:48:07 字數 3501 閱讀 3850

因為業務需要,建立了100個表,但是這些表的結構都是一樣的,作為程式設計師,就是要解決這種重複勞動。然而這種事情還要單獨寫個php指令碼的話太麻煩了吧,所以就乾脆學了一下直接用mysql儲存過程怎麼實現:

首先是建立表的:( lpad(`@i`, 2, '0')的作用 是將1,2,3這些數字轉化為'01','02','03')

drop

procedure

ifexists

`create_tables`;

create

procedure

create_tables ()

begin

declare `@i` int (11

);declare `@createsql` varchar (2560

);set `@i` =0;

while `@i` <

100do

--建立表

set@createsql

=concat(

"create

table

ifnot

exists

guess_record",

lpad(`

@i`, 2, '0'

), "(

`id`

int(10) unsigned not

null

auto_increment,

`userid`

int(12) unsigned not

null comment '

使用者id',

`issue`

int(10) unsigned not

null comment '期號'

, `options_id`

int(10) unsigned not

null comment '

選項id',

`guess_time`

int(10) unsigned not

null comment '

答題時間',

`is_stat`

tinyint(4) not

null comment '

是否統計過',

primary

key(`id`),

key`issue` (`issue`) using btree,

key`userid` (`userid`) using btree

) engine

=innodb default charset=

utf8;");

prepare stmt from

@createsql

;execute

stmt;

set `@i` = `@i` +1;

endwhile

;end

然後是批量刪除表的(嘿嘿,搞事情)

drop

procedure

ifexists

`drop_tables`;

create

procedure

drop_tables ()

begin

declare `@i` int (11

);declare `@dropsql` varchar (2560

);set `@i` =0;

while `@i` <

100do

--建立表

set@dropsql

= concat("drop

table

ifexists guess_record",lpad(`@i`, 2, '0'

));prepare stmt from

@dropsql

;execute

stmt;

set `@i` = `@i` +1;

endwhile

;end

我用這個儲存過程其實就是一次性的,所以完整的過程是 1、建立儲存過程 2、執行儲存過程(批量建立表)3、刪除儲存過程

#1

:建立儲存過程

drop

procedure

ifexists

`create_tables`;

delimiter $$

//定義結束符

create

procedure

create_tables ()

begin

declare `@i` int (11

);declare `@createsql` varchar (2560

);set `@i` =0;

while `@i` <

100do

--建立表

set@createsql

=concat(

"create

table

ifnot

exists

guess_record",

lpad(`

@i`, 2, '0'

), "(

`id`

int(10) unsigned not

null

auto_increment,

`userid`

int(12) unsigned not

null comment '

使用者id',

`issue`

int(10) unsigned not

null comment '期號'

, `options_id`

int(10) unsigned not

null comment '

選項id',

`guess_time`

int(10) unsigned not

null comment '

答題時間',

`is_stat`

tinyint(4) not

null comment '

是否統計過',

primary

key(`id`),

key`issue` (`issue`) using btree,

key`userid` (`userid`) using btree

) engine

=innodb default charset=

utf8;");

prepare stmt from

@createsql

;execute

stmt;

set `@i` = `@i` +1;

endwhile

;end

$$delimiter ;

//結束符改為; #2

:執行儲存過程

call `create_tables`();#3

:刪除儲存過程

drop

procedure

ifexists `create_tables`;

MYSQL批量建表儲存過程

分表比較多的情況,如何批量建立,可通過儲存過程實現 建立乙個儲存過程 紅色表結構,藍色為表名及表數量 delimiter create procedure sp create tab begin set str id int 11 not null auto increment comment 自增...

mysql儲存過程批量建表

asif 用mysql的儲存過程建立100張表 mysql delimiter create procedure createtables begin declareiint declaretable namevarchar 20 declaretable prevarchar 20 declare...

mysql批量查詢 修改表, 批量刪除儲存過程

場景 有的時候需要批量更新部分有規律的表或者修改其屬性。處理方案 使用 函式concat 來生成批量執行sql語句,再執行批量sql語句。如 批量刪除所有表 select concat drop table table name,from information schema.tables wher...