批量更改mysql庫中100張表的字段位數

輸入查看當前庫中表的字段位數: mediumint(6) 

[root@db-master ~]# sh alter_table.sh 

alter table user_campaign_0 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
0 finished.
alter table user_campaign_1 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
1 finished.
alter table user_campaign_2 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
2 finished.
alter table user_campaign_3 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
3 finished.
alter table user_campaign_4 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
4 finished.
alter table user_campaign_5 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
5 finished.
alter table user_campaign_6 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
6 finished.
alter table user_campaign_7 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
7 finished.
alter table user_campaign_8 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
8 finished.
alter table user_campaign_9 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
9 finished.
alter table user_campaign_10 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
10 finished.
alter table user_campaign_11 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
11 finished.
alter table user_campaign_12 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
12 finished.
alter table user_campaign_13 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
13 finished.
alter table user_campaign_14 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
14 finished.
alter table user_campaign_15 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
15 finished.
alter table user_campaign_16 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
16 finished.
alter table user_campaign_17 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
17 finished.
alter table user_campaign_18 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
18 finished.
alter table user_campaign_19 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
19 finished.
alter table user_campaign_20 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
20 finished.
alter table user_campaign_21 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
21 finished.
alter table user_campaign_22 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
22 finished.
alter table user_campaign_23 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
23 finished.
alter table user_campaign_24 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
24 finished.
alter table user_campaign_25 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
25 finished.
alter table user_campaign_26 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
26 finished.
alter table user_campaign_27 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
27 finished.
alter table user_campaign_28 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
28 finished.
alter table user_campaign_29 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
29 finished.
alter table user_campaign_30 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
30 finished.
alter table user_campaign_31 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
31 finished.
alter table user_campaign_32 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
32 finished.
alter table user_campaign_33 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
33 finished.
alter table user_campaign_34 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
34 finished.
alter table user_campaign_35 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
35 finished.
alter table user_campaign_36 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
36 finished.
alter table user_campaign_37 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
37 finished.
alter table user_campaign_38 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
38 finished.
alter table user_campaign_39 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
39 finished.
alter table user_campaign_40 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
40 finished.
alter table user_campaign_41 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
41 finished.
alter table user_campaign_42 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
42 finished.
alter table user_campaign_43 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
43 finished.
alter table user_campaign_44 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
44 finished.
alter table user_campaign_45 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
45 finished.
alter table user_campaign_46 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
46 finished.
alter table user_campaign_47 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
47 finished.
alter table user_campaign_48 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
48 finished.
alter table user_campaign_49 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
49 finished.
alter table user_campaign_50 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
50 finished.
alter table user_campaign_51 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
51 finished.
alter table user_campaign_52 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
52 finished.
alter table user_campaign_53 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
53 finished.
alter table user_campaign_54 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
54 finished.
alter table user_campaign_55 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
55 finished.
alter table user_campaign_56 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
56 finished.
alter table user_campaign_57 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
57 finished.
alter table user_campaign_58 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
58 finished.
alter table user_campaign_59 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
59 finished.
alter table user_campaign_60 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
60 finished.
alter table user_campaign_61 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
61 finished.
alter table user_campaign_62 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
62 finished.
alter table user_campaign_63 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
63 finished.
alter table user_campaign_64 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
64 finished.
alter table user_campaign_65 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
65 finished.
alter table user_campaign_66 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
66 finished.
alter table user_campaign_67 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
67 finished.
alter table user_campaign_68 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
68 finished.
alter table user_campaign_69 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
69 finished.
alter table user_campaign_70 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
70 finished.
alter table user_campaign_71 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
71 finished.
alter table user_campaign_72 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
72 finished.
alter table user_campaign_73 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
73 finished.
alter table user_campaign_74 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
74 finished.
alter table user_campaign_75 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
75 finished.
alter table user_campaign_76 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
76 finished.
alter table user_campaign_77 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
77 finished.
alter table user_campaign_78 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
78 finished.
alter table user_campaign_79 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
79 finished.
alter table user_campaign_80 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
80 finished.
alter table user_campaign_81 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
81 finished.
alter table user_campaign_82 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
82 finished.
alter table user_campaign_83 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
83 finished.
alter table user_campaign_84 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
84 finished.
alter table user_campaign_85 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
85 finished.
alter table user_campaign_86 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
86 finished.
alter table user_campaign_87 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
87 finished.
alter table user_campaign_88 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
88 finished.
alter table user_campaign_89 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
89 finished.
alter table user_campaign_90 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
90 finished.
alter table user_campaign_91 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
91 finished.
alter table user_campaign_92 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
92 finished.
alter table user_campaign_93 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
93 finished.
alter table user_campaign_94 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
94 finished.
alter table user_campaign_95 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
95 finished.
alter table user_campaign_96 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
96 finished.
alter table user_campaign_97 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
97 finished.
alter table user_campaign_98 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
98 finished.
alter table user_campaign_99 modify current_stage mediumint(6) unsigned not null default 0 comment '當前關卡';
99 finished.
 
使用shell腳本批量更改:
[root@jxq-st-ms-01 ~]# sh alter_table.sh
0 finished.
1 finished.
2 finished.
3 finished.
4 finished.
5 finished.
6 finished.
7 finished.
8 finished.
9 finished.
10 finished.
11 finished.
12 finished.
13 finished.
14 finished.
15 finished.
16 finished.
17 finished.
18 finished.
19 finished.
20 finished.
21 finished.
22 finished.
23 finished.
24 finished.
25 finished.
26 finished.
27 finished.
28 finished.
29 finished.
30 finished.
31 finished.
32 finished.
33 finished.
34 finished.
35 finished.
36 finished.
37 finished.
38 finished.
39 finished.
40 finished.
41 finished.
42 finished.
43 finished.
44 finished.
45 finished.
46 finished.
47 finished.
48 finished.
49 finished.
50 finished.
51 finished.
52 finished.
53 finished.
54 finished.
55 finished.
56 finished.
57 finished.
58 finished.
59 finished.
60 finished.
61 finished.
62 finished.
63 finished.
64 finished.
65 finished.
66 finished.
67 finished.
68 finished.
69 finished.
70 finished.
71 finished.
72 finished.
73 finished.
74 finished.
75 finished.
76 finished.
77 finished.
78 finished.
79 finished.
80 finished.
81 finished.
82 finished.
83 finished.
84 finished.
85 finished.
86 finished.
87 finished.
88 finished.
89 finished.
90 finished.
91 finished.
92 finished.
93 finished.
94 finished.
95 finished.
96 finished.
97 finished.
98 finished.
99 finished.
 
進入數據庫查看:
[root@db-master ~]# mysql -uroot -p123456 -A youxidb
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4162345
Server version: 5.1.62-log Source distribution
 
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql> show create table user_campaign_0;
+-----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table           | Create Table                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
+-----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| user_campaign_0 | CREATE TABLE `user_campaign_0` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '玩家用戶中心id',
  `current_stage` mediumint(5) unsigned NOT NULL DEFAULT '0' COMMENT '當前關卡',
  `stage_detail` varchar(1000) DEFAULT NULL COMMENT '歷史關卡詳情',
  `all_star` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT '玩家獲得的所有星星',
  `create_at` datetime NOT NULL COMMENT '創建時間',
  `update_at` datetime NOT NULL COMMENT '更新時間',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uiq_uid` (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='玩家PVE戰役數據表'                                         |
+-----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
 
現在已經把youxidb庫的100張表的mediumint(6) 更改爲:mediumint(5) 
 
腳本內容:[root@db-master ~]# cat alter_table.sh
#!/bin/bash
 
i=0
while [ $i -ne 100 ] 
do
  echo "alter table user_campaign_$i modify current_stage mediumint(5) unsigned not null default 0 comment '當前關卡';"|mysql -uroot -p123456 -A youxidb
  echo "$i finished."
  i=$(($i+1))
done
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章