设计数据库时,有时候需要分表,当分表数量比较大,单个创建比较浪费时间,于是写了一个存储过程模板,可以快速创建多张分表,比如:
- 创建存储器
create procedure app_game_interface_event_statistics_uid_mod_16(in val_s int, in val_e int)
begin
declare i int;
set i=val_s;
while i<val_e+1 do
set @sql_create_table = concat(
'CREATE TABLE app_game_interface_event_statistics_', i,
" (
`id` int(11) NOT NULL AUTO_INCREMENT,
`uid` int(11) DEFAULT '0' COMMENT '用户uid',
`game_id` bigint(20) DEFAULT '0' COMMENT '游戏id',
`click` int(11) DEFAULT '0' COMMENT '点击次数',
`view` int(11) DEFAULT '0' COMMENT '访问次数',
`update_time` int(11) DEFAULT '0' COMMENT '更新时间',
PRIMARY KEY (`id`),
UNIQUE KEY `uid` (`uid`,`game_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin comment '游戏主界面广告游戏事件表(uid%16)'
");
PREPARE sql_create_table FROM @sql_create_table;
EXECUTE sql_create_table;
set i=i+1;
end while;
end;
- 执行
call app_game_interface_event_statistics_uid_mod_16(0, 15);
- 删除
DROP PROCEDURE IF EXISTS `app_game_interface_event_statistics_uid_mod_16`;
- 检查存储过程
- 为显示用来创建一个存储过程的CREATE语句
show create PROCEDURE app_game_interface_event_statistics_uid_mod_16;
- 为了获得包括何时、由谁创建等详细信息的存储过程列表
show procedure status;
-- 表比较多,用like过滤
show procedure status like 'app_game_interface_event_statistics_uid_mod_16';