您好,登錄后才能下訂單哦!
#在工作中,經常會有以時間或者數字命名,創建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) ========================================
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。