经常设计数据库,有时候分表数量比较大,单个创建实在浪费时间,于是写了一个存储模板,可以快速创建多张分表,比如:
-- 程序存储器
DROP PROCEDURE IF EXISTS `gameLog_month`;
create procedure gameLog_month(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 IF NOT EXISTS hunan_db_log_', i,
"(
`id` int(11) NOT NULL AUTO_INCREMENT,
`gameType` varchar(20) DEFAULT '' COMMENT '游戏类别',
`userId` int(11) DEFAULT 0 COMMENT '玩家id',
`deskId` int(11) DEFAULT 0 COMMENT '玩家桌子ID',
`deskNum` tinyint(2) DEFAULT 0 COMMENT '座位标号1;2;3;4;5;6,1号位代表创建者',
`tableMode` tinyint(2) DEFAULT 0 COMMENT '桌子类型:5棋牌馆模式',
`score` int(11) DEFAULT 0 COMMENT '分数',
`scoreBase` int(11) DEFAULT 0 COMMENT '分数基数',
`playSign` int(11) DEFAULT 0 COMMENT '棋牌馆创建标识',
`agentId` int(11) DEFAULT 0 COMMENT '代理ID',
`logTime` int(11) DEFAULT 0 COMMENT '日志时间',
`winlevel` tinyint(2) DEFAULT 0 COMMENT '胜利级别0-10,默认0不区别,10大赢家',
`createTime` datetime DEFAULT NULL COMMENT '创建时间',
PRIMARY KEY (`id`),
INDEX `gameType` (`gameType`) USING BTREE,
INDEX `userId` (`userId`) USING BTREE,
INDEX `logTime` (`logTime`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='棋牌馆玩家日志信息,按月分表处理'
");
PREPARE sql_create_table FROM @sql_create_table;
EXECUTE sql_create_table;
set i=i+1;
end while;
end;