#在工作中,经常会有以时间或者数字命名,创建n张表的需求。 #下面给出4种常用的批量创建100张表的小脚本。 #结果 mysql> show tables; +-----------------+ | Tables_in_tsdb | +-----------------+ | Total_Winner_01 | | Total_Winner_02 | | Total_Winner_03 | | Total_Winner_04 | | Total_Winner_05 | | Total_Winner_06 | | Total_Winner_07 | | Total_Winner_08 | | Total_Winner_09 | | Total_Winner_10 | | Total_Winner_11 | | Total_Winner_12 | | Total_Winner_13 | | Total_Winner_14 | | Total_Winner_15 | +-----------------+ 15 rows in set (0.00 sec) #第一种: ##create 100 tables #!/bin/sh # db_name=‘USE RenmaiInfluenceDB‘ for i in {0 99}; do len=`expr length $i` if [ $len -eq 2 ];then num=$i else num="0${i}" fi echo ‘‘‘ CREATE TABLE `Total_Winner_‘‘‘$num‘‘‘` ( `Id` INT (10), `UserId` INT (10), PRIMARY KEY (`Id`) ) ENGINE = INNODB DEFAULT CHARSET = utf8 ; ‘‘‘ echo done ======================================================================================================== #第二种: #!/bin/sh #creat tables for i in `seq 1 15` do var=`printf "%02d\n" $i` echo ‘‘‘ CREATE TABLE `Total_Winner_‘‘‘$var‘‘‘` ( `Id` INT (10), `UserId` INT (10), PRIMARY KEY (`Id`) ) ENGINE = INNODB DEFAULT CHARSET = utf8 ; ‘‘‘ echo done ============================================================================ #第三种: #!/bin/sh # 需要将 SQL 中的" ` " 符号删除 for i in `seq 0 1 99` do STEP=`printf %02d ${i}` create_tab_sql=" CREATE TABLE Total_Winner_${STEP} ( Id INT (10), UserId INT (10), PRIMARY KEY (Id) ) ENGINE = INNODB DEFAULT CHARSET = utf8 ;" echo -e ${create_tab_sql} echo done =================================================================================== #第四种:Python脚本 #!/usr/bin/python import string for i in range(7,13): sql = "CREATE TABLE `Content_2015%02d` LIKE `Content_201503`;" % i print(sql) for i in range(15,20): sql = "CREATE TABLE `Content_2016%02d` LIKE `Content_201503`;" % i print(sql) ========================================
时间: 2024-11-05 13:37:05