批量创建100张表
短信预约 -IT技能 免费直播动态提醒
#在工作中,经常会有以时间或者数字命名,创建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)
========================================
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341