从MySQL中删除指定类型索引的脚本
本文内容遵从CC版权协议, 可以随意转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址: http://www.penglixun.com/database/delete_a_type_index.html
好久没写Perl了,今天遇到一个很机械化的问题,又写了一个脚本。
需求是这样的:因为要把一个库转成InnoDB,所以需要把InnoDB不支持的FULLTEXT索引删除,写了个脚本清掉。
#!/usr/bin/perl -w
# Delete A Type Of Index From DB
use DBI;
my $db = 'abc';
my $host = '127.0.0.1';
my $user = 'root';
my $passwd = '';
my $type = 'FULLTEXT';
my $dbh = DBI->connect("DBI:mysql:database=$db;host=$host", $user, $passwd, {'RaiseError' => 1});
my $query1 = $dbh->prepare("SHOW TABLES");
$query1->execute ();
while(my $t_name = $query1->fetchrow()) {
my $query2 = $dbh->prepare("SHOW INDEX FROM $t_name");
$query2->execute();
while(my $i_name = $query2->fetchrow_hashref()) {
#print("$t_name,$i_name->{'Key_name'},$i_name->{'Index_type'}\n");
if ($i_name->{'Index_type'} eq $type) {
#print("$t_name,$i_name->{'Key_name'}\n");
drop_index($t_name,$i_name->{'Key_name'});
}
}
}
$dbh->disconnect;
sub drop_index {
local($t);
local($i);
$t = $_[0];
$i = $_[1];
my $q = $dbh->prepare("ALTER TABLE $t DROP INDEX $i");
$q->execute();
print("DROP $t\t$i Success\n\n");
}